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/clk-provider.h> 19 #include <linux/clkdev.h> 20 #include <linux/delay.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/i2c.h> 23 #include <linux/module.h> 24 #include <linux/pm_runtime.h> 25 #include <linux/regulator/consumer.h> 26 #include <media/v4l2-ctrls.h> 27 #include <media/v4l2-device.h> 28 #include <media/v4l2-event.h> 29 #include <media/v4l2-fwnode.h> 30 #include <media/v4l2-mediabus.h> 31 #include <asm/unaligned.h> 32 33 #define IMX219_REG_VALUE_08BIT 1 34 #define IMX219_REG_VALUE_16BIT 2 35 36 #define IMX219_REG_MODE_SELECT 0x0100 37 #define IMX219_MODE_STANDBY 0x00 38 #define IMX219_MODE_STREAMING 0x01 39 40 /* Chip ID */ 41 #define IMX219_REG_CHIP_ID 0x0000 42 #define IMX219_CHIP_ID 0x0219 43 44 /* External clock frequency is 24.0M */ 45 #define IMX219_XCLK_FREQ 24000000 46 47 /* Pixel rate is fixed at 182.4M for all the modes */ 48 #define IMX219_PIXEL_RATE 182400000 49 50 #define IMX219_DEFAULT_LINK_FREQ 456000000 51 52 /* V_TIMING internal */ 53 #define IMX219_REG_VTS 0x0160 54 #define IMX219_VTS_15FPS 0x0dc6 55 #define IMX219_VTS_30FPS_1080P 0x06e3 56 #define IMX219_VTS_30FPS_BINNED 0x06e3 57 #define IMX219_VTS_MAX 0xffff 58 59 #define IMX219_VBLANK_MIN 4 60 61 /*Frame Length Line*/ 62 #define IMX219_FLL_MIN 0x08a6 63 #define IMX219_FLL_MAX 0xffff 64 #define IMX219_FLL_STEP 1 65 #define IMX219_FLL_DEFAULT 0x0c98 66 67 /* HBLANK control - read only */ 68 #define IMX219_PPL_DEFAULT 3448 69 70 /* Exposure control */ 71 #define IMX219_REG_EXPOSURE 0x015a 72 #define IMX219_EXPOSURE_MIN 4 73 #define IMX219_EXPOSURE_STEP 1 74 #define IMX219_EXPOSURE_DEFAULT 0x640 75 #define IMX219_EXPOSURE_MAX 65535 76 77 /* Analog gain control */ 78 #define IMX219_REG_ANALOG_GAIN 0x0157 79 #define IMX219_ANA_GAIN_MIN 0 80 #define IMX219_ANA_GAIN_MAX 232 81 #define IMX219_ANA_GAIN_STEP 1 82 #define IMX219_ANA_GAIN_DEFAULT 0x0 83 84 /* Digital gain control */ 85 #define IMX219_REG_DIGITAL_GAIN 0x0158 86 #define IMX219_DGTL_GAIN_MIN 0x0100 87 #define IMX219_DGTL_GAIN_MAX 0x0fff 88 #define IMX219_DGTL_GAIN_DEFAULT 0x0100 89 #define IMX219_DGTL_GAIN_STEP 1 90 91 #define IMX219_REG_ORIENTATION 0x0172 92 93 /* Test Pattern Control */ 94 #define IMX219_REG_TEST_PATTERN 0x0600 95 #define IMX219_TEST_PATTERN_DISABLE 0 96 #define IMX219_TEST_PATTERN_SOLID_COLOR 1 97 #define IMX219_TEST_PATTERN_COLOR_BARS 2 98 #define IMX219_TEST_PATTERN_GREY_COLOR 3 99 #define IMX219_TEST_PATTERN_PN9 4 100 101 /* Test pattern colour components */ 102 #define IMX219_REG_TESTP_RED 0x0602 103 #define IMX219_REG_TESTP_GREENR 0x0604 104 #define IMX219_REG_TESTP_BLUE 0x0606 105 #define IMX219_REG_TESTP_GREENB 0x0608 106 #define IMX219_TESTP_COLOUR_MIN 0 107 #define IMX219_TESTP_COLOUR_MAX 0x03ff 108 #define IMX219_TESTP_COLOUR_STEP 1 109 #define IMX219_TESTP_RED_DEFAULT IMX219_TESTP_COLOUR_MAX 110 #define IMX219_TESTP_GREENR_DEFAULT 0 111 #define IMX219_TESTP_BLUE_DEFAULT 0 112 #define IMX219_TESTP_GREENB_DEFAULT 0 113 114 struct imx219_reg { 115 u16 address; 116 u8 val; 117 }; 118 119 struct imx219_reg_list { 120 unsigned int num_of_regs; 121 const struct imx219_reg *regs; 122 }; 123 124 /* Mode : resolution and related config&values */ 125 struct imx219_mode { 126 /* Frame width */ 127 unsigned int width; 128 /* Frame height */ 129 unsigned int height; 130 131 /* V-timing */ 132 unsigned int vts_def; 133 134 /* Default register values */ 135 struct imx219_reg_list reg_list; 136 }; 137 138 /* 139 * Register sets lifted off the i2C interface from the Raspberry Pi firmware 140 * driver. 141 * 3280x2464 = mode 2, 1920x1080 = mode 1, and 1640x1232 = mode 4. 142 */ 143 static const struct imx219_reg mode_3280x2464_regs[] = { 144 {0x0100, 0x00}, 145 {0x30eb, 0x0c}, 146 {0x30eb, 0x05}, 147 {0x300a, 0xff}, 148 {0x300b, 0xff}, 149 {0x30eb, 0x05}, 150 {0x30eb, 0x09}, 151 {0x0114, 0x01}, 152 {0x0128, 0x00}, 153 {0x012a, 0x18}, 154 {0x012b, 0x00}, 155 {0x0164, 0x00}, 156 {0x0165, 0x00}, 157 {0x0166, 0x0c}, 158 {0x0167, 0xcf}, 159 {0x0168, 0x00}, 160 {0x0169, 0x00}, 161 {0x016a, 0x09}, 162 {0x016b, 0x9f}, 163 {0x016c, 0x0c}, 164 {0x016d, 0xd0}, 165 {0x016e, 0x09}, 166 {0x016f, 0xa0}, 167 {0x0170, 0x01}, 168 {0x0171, 0x01}, 169 {0x0174, 0x00}, 170 {0x0175, 0x00}, 171 {0x018c, 0x0a}, 172 {0x018d, 0x0a}, 173 {0x0301, 0x05}, 174 {0x0303, 0x01}, 175 {0x0304, 0x03}, 176 {0x0305, 0x03}, 177 {0x0306, 0x00}, 178 {0x0307, 0x39}, 179 {0x0309, 0x0a}, 180 {0x030b, 0x01}, 181 {0x030c, 0x00}, 182 {0x030d, 0x72}, 183 {0x0624, 0x0c}, 184 {0x0625, 0xd0}, 185 {0x0626, 0x09}, 186 {0x0627, 0xa0}, 187 {0x455e, 0x00}, 188 {0x471e, 0x4b}, 189 {0x4767, 0x0f}, 190 {0x4750, 0x14}, 191 {0x4540, 0x00}, 192 {0x47b4, 0x14}, 193 {0x4713, 0x30}, 194 {0x478b, 0x10}, 195 {0x478f, 0x10}, 196 {0x4793, 0x10}, 197 {0x4797, 0x0e}, 198 {0x479b, 0x0e}, 199 {0x0162, 0x0d}, 200 {0x0163, 0x78}, 201 }; 202 203 static const struct imx219_reg mode_1920_1080_regs[] = { 204 {0x0100, 0x00}, 205 {0x30eb, 0x05}, 206 {0x30eb, 0x0c}, 207 {0x300a, 0xff}, 208 {0x300b, 0xff}, 209 {0x30eb, 0x05}, 210 {0x30eb, 0x09}, 211 {0x0114, 0x01}, 212 {0x0128, 0x00}, 213 {0x012a, 0x18}, 214 {0x012b, 0x00}, 215 {0x0162, 0x0d}, 216 {0x0163, 0x78}, 217 {0x0164, 0x02}, 218 {0x0165, 0xa8}, 219 {0x0166, 0x0a}, 220 {0x0167, 0x27}, 221 {0x0168, 0x02}, 222 {0x0169, 0xb4}, 223 {0x016a, 0x06}, 224 {0x016b, 0xeb}, 225 {0x016c, 0x07}, 226 {0x016d, 0x80}, 227 {0x016e, 0x04}, 228 {0x016f, 0x38}, 229 {0x0170, 0x01}, 230 {0x0171, 0x01}, 231 {0x0174, 0x00}, 232 {0x0175, 0x00}, 233 {0x018c, 0x0a}, 234 {0x018d, 0x0a}, 235 {0x0301, 0x05}, 236 {0x0303, 0x01}, 237 {0x0304, 0x03}, 238 {0x0305, 0x03}, 239 {0x0306, 0x00}, 240 {0x0307, 0x39}, 241 {0x0309, 0x0a}, 242 {0x030b, 0x01}, 243 {0x030c, 0x00}, 244 {0x030d, 0x72}, 245 {0x0624, 0x07}, 246 {0x0625, 0x80}, 247 {0x0626, 0x04}, 248 {0x0627, 0x38}, 249 {0x455e, 0x00}, 250 {0x471e, 0x4b}, 251 {0x4767, 0x0f}, 252 {0x4750, 0x14}, 253 {0x4540, 0x00}, 254 {0x47b4, 0x14}, 255 {0x4713, 0x30}, 256 {0x478b, 0x10}, 257 {0x478f, 0x10}, 258 {0x4793, 0x10}, 259 {0x4797, 0x0e}, 260 {0x479b, 0x0e}, 261 {0x0162, 0x0d}, 262 {0x0163, 0x78}, 263 }; 264 265 static const struct imx219_reg mode_1640_1232_regs[] = { 266 {0x0100, 0x00}, 267 {0x30eb, 0x0c}, 268 {0x30eb, 0x05}, 269 {0x300a, 0xff}, 270 {0x300b, 0xff}, 271 {0x30eb, 0x05}, 272 {0x30eb, 0x09}, 273 {0x0114, 0x01}, 274 {0x0128, 0x00}, 275 {0x012a, 0x18}, 276 {0x012b, 0x00}, 277 {0x0164, 0x00}, 278 {0x0165, 0x00}, 279 {0x0166, 0x0c}, 280 {0x0167, 0xcf}, 281 {0x0168, 0x00}, 282 {0x0169, 0x00}, 283 {0x016a, 0x09}, 284 {0x016b, 0x9f}, 285 {0x016c, 0x06}, 286 {0x016d, 0x68}, 287 {0x016e, 0x04}, 288 {0x016f, 0xd0}, 289 {0x0170, 0x01}, 290 {0x0171, 0x01}, 291 {0x0174, 0x01}, 292 {0x0175, 0x01}, 293 {0x018c, 0x0a}, 294 {0x018d, 0x0a}, 295 {0x0301, 0x05}, 296 {0x0303, 0x01}, 297 {0x0304, 0x03}, 298 {0x0305, 0x03}, 299 {0x0306, 0x00}, 300 {0x0307, 0x39}, 301 {0x0309, 0x0a}, 302 {0x030b, 0x01}, 303 {0x030c, 0x00}, 304 {0x030d, 0x72}, 305 {0x0624, 0x06}, 306 {0x0625, 0x68}, 307 {0x0626, 0x04}, 308 {0x0627, 0xd0}, 309 {0x455e, 0x00}, 310 {0x471e, 0x4b}, 311 {0x4767, 0x0f}, 312 {0x4750, 0x14}, 313 {0x4540, 0x00}, 314 {0x47b4, 0x14}, 315 {0x4713, 0x30}, 316 {0x478b, 0x10}, 317 {0x478f, 0x10}, 318 {0x4793, 0x10}, 319 {0x4797, 0x0e}, 320 {0x479b, 0x0e}, 321 {0x0162, 0x0d}, 322 {0x0163, 0x78}, 323 }; 324 325 static const char * const imx219_test_pattern_menu[] = { 326 "Disabled", 327 "Color Bars", 328 "Solid Color", 329 "Grey Color Bars", 330 "PN9" 331 }; 332 333 static const int imx219_test_pattern_val[] = { 334 IMX219_TEST_PATTERN_DISABLE, 335 IMX219_TEST_PATTERN_COLOR_BARS, 336 IMX219_TEST_PATTERN_SOLID_COLOR, 337 IMX219_TEST_PATTERN_GREY_COLOR, 338 IMX219_TEST_PATTERN_PN9, 339 }; 340 341 /* regulator supplies */ 342 static const char * const imx219_supply_name[] = { 343 /* Supplies can be enabled in any order */ 344 "VANA", /* Analog (2.8V) supply */ 345 "VDIG", /* Digital Core (1.8V) supply */ 346 "VDDL", /* IF (1.2V) supply */ 347 }; 348 349 #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name) 350 351 /* 352 * Initialisation delay between XCLR low->high and the moment when the sensor 353 * can start capture (i.e. can leave software stanby) must be not less than: 354 * t4 + max(t5, t6 + <time to initialize the sensor register over I2C>) 355 * where 356 * t4 is fixed, and is max 200uS, 357 * t5 is fixed, and is 6000uS, 358 * t6 depends on the sensor external clock, and is max 32000 clock periods. 359 * As per sensor datasheet, the external clock must be from 6MHz to 27MHz. 360 * So for any acceptable external clock t6 is always within the range of 361 * 1185 to 5333 uS, and is always less than t5. 362 * For this reason this is always safe to wait (t4 + t5) = 6200 uS, then 363 * initialize the sensor over I2C, and then exit the software standby. 364 * 365 * This start-up time can be optimized a bit more, if we start the writes 366 * over I2C after (t4+t6), but before (t4+t5) expires. But then sensor 367 * initialization over I2C may complete before (t4+t5) expires, and we must 368 * ensure that capture is not started before (t4+t5). 369 * 370 * This delay doesn't account for the power supply startup time. If needed, 371 * this should be taken care of via the regulator framework. E.g. in the 372 * case of DT for regulator-fixed one should define the startup-delay-us 373 * property. 374 */ 375 #define IMX219_XCLR_MIN_DELAY_US 6200 376 #define IMX219_XCLR_DELAY_RANGE_US 1000 377 378 /* Mode configs */ 379 static const struct imx219_mode supported_modes[] = { 380 { 381 /* 8MPix 15fps mode */ 382 .width = 3280, 383 .height = 2464, 384 .vts_def = IMX219_VTS_15FPS, 385 .reg_list = { 386 .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs), 387 .regs = mode_3280x2464_regs, 388 }, 389 }, 390 { 391 /* 1080P 30fps cropped */ 392 .width = 1920, 393 .height = 1080, 394 .vts_def = IMX219_VTS_30FPS_1080P, 395 .reg_list = { 396 .num_of_regs = ARRAY_SIZE(mode_1920_1080_regs), 397 .regs = mode_1920_1080_regs, 398 }, 399 }, 400 { 401 /* 2x2 binned 30fps mode */ 402 .width = 1640, 403 .height = 1232, 404 .vts_def = IMX219_VTS_30FPS_BINNED, 405 .reg_list = { 406 .num_of_regs = ARRAY_SIZE(mode_1640_1232_regs), 407 .regs = mode_1640_1232_regs, 408 }, 409 }, 410 }; 411 412 struct imx219 { 413 struct v4l2_subdev sd; 414 struct media_pad pad; 415 416 struct clk *xclk; /* system clock to IMX219 */ 417 u32 xclk_freq; 418 419 struct gpio_desc *reset_gpio; 420 struct regulator_bulk_data supplies[IMX219_NUM_SUPPLIES]; 421 422 struct v4l2_ctrl_handler ctrl_handler; 423 /* V4L2 Controls */ 424 struct v4l2_ctrl *pixel_rate; 425 struct v4l2_ctrl *exposure; 426 struct v4l2_ctrl *vflip; 427 struct v4l2_ctrl *hflip; 428 struct v4l2_ctrl *vblank; 429 struct v4l2_ctrl *hblank; 430 431 /* Current mode */ 432 const struct imx219_mode *mode; 433 434 /* 435 * Mutex for serialized access: 436 * Protect sensor module set pad format and start/stop streaming safely. 437 */ 438 struct mutex mutex; 439 440 /* Streaming on/off */ 441 bool streaming; 442 }; 443 444 static inline struct imx219 *to_imx219(struct v4l2_subdev *_sd) 445 { 446 return container_of(_sd, struct imx219, sd); 447 } 448 449 /* Read registers up to 2 at a time */ 450 static int imx219_read_reg(struct imx219 *imx219, u16 reg, u32 len, u32 *val) 451 { 452 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 453 struct i2c_msg msgs[2]; 454 u8 addr_buf[2] = { reg >> 8, reg & 0xff }; 455 u8 data_buf[4] = { 0, }; 456 int ret; 457 458 if (len > 4) 459 return -EINVAL; 460 461 /* Write register address */ 462 msgs[0].addr = client->addr; 463 msgs[0].flags = 0; 464 msgs[0].len = ARRAY_SIZE(addr_buf); 465 msgs[0].buf = addr_buf; 466 467 /* Read data from register */ 468 msgs[1].addr = client->addr; 469 msgs[1].flags = I2C_M_RD; 470 msgs[1].len = len; 471 msgs[1].buf = &data_buf[4 - len]; 472 473 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 474 if (ret != ARRAY_SIZE(msgs)) 475 return -EIO; 476 477 *val = get_unaligned_be32(data_buf); 478 479 return 0; 480 } 481 482 /* Write registers up to 2 at a time */ 483 static int imx219_write_reg(struct imx219 *imx219, u16 reg, u32 len, u32 val) 484 { 485 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 486 u8 buf[6]; 487 488 if (len > 4) 489 return -EINVAL; 490 491 put_unaligned_be16(reg, buf); 492 put_unaligned_be32(val << (8 * (4 - len)), buf + 2); 493 if (i2c_master_send(client, buf, len + 2) != len + 2) 494 return -EIO; 495 496 return 0; 497 } 498 499 /* Write a list of registers */ 500 static int imx219_write_regs(struct imx219 *imx219, 501 const struct imx219_reg *regs, u32 len) 502 { 503 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 504 unsigned int i; 505 int ret; 506 507 for (i = 0; i < len; i++) { 508 ret = imx219_write_reg(imx219, regs[i].address, 1, regs[i].val); 509 if (ret) { 510 dev_err_ratelimited(&client->dev, 511 "Failed to write reg 0x%4.4x. error = %d\n", 512 regs[i].address, ret); 513 514 return ret; 515 } 516 } 517 518 return 0; 519 } 520 521 /* Get bayer order based on flip setting. */ 522 static u32 imx219_get_format_code(struct imx219 *imx219) 523 { 524 /* 525 * Only one bayer order is supported. 526 * It depends on the flip settings. 527 */ 528 static const u32 codes[2][2] = { 529 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, }, 530 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, }, 531 }; 532 533 lockdep_assert_held(&imx219->mutex); 534 return codes[imx219->vflip->val][imx219->hflip->val]; 535 } 536 537 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 538 { 539 struct imx219 *imx219 = to_imx219(sd); 540 struct v4l2_mbus_framefmt *try_fmt = 541 v4l2_subdev_get_try_format(sd, fh->pad, 0); 542 543 mutex_lock(&imx219->mutex); 544 545 /* Initialize try_fmt */ 546 try_fmt->width = supported_modes[0].width; 547 try_fmt->height = supported_modes[0].height; 548 try_fmt->code = imx219_get_format_code(imx219); 549 try_fmt->field = V4L2_FIELD_NONE; 550 551 mutex_unlock(&imx219->mutex); 552 553 return 0; 554 } 555 556 static int imx219_set_ctrl(struct v4l2_ctrl *ctrl) 557 { 558 struct imx219 *imx219 = 559 container_of(ctrl->handler, struct imx219, ctrl_handler); 560 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 561 int ret; 562 563 if (ctrl->id == V4L2_CID_VBLANK) { 564 int exposure_max, exposure_def; 565 566 /* Update max exposure while meeting expected vblanking */ 567 exposure_max = imx219->mode->height + ctrl->val - 4; 568 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ? 569 exposure_max : IMX219_EXPOSURE_DEFAULT; 570 __v4l2_ctrl_modify_range(imx219->exposure, 571 imx219->exposure->minimum, 572 exposure_max, imx219->exposure->step, 573 exposure_def); 574 } 575 576 /* 577 * Applying V4L2 control value only happens 578 * when power is up for streaming 579 */ 580 if (pm_runtime_get_if_in_use(&client->dev) == 0) 581 return 0; 582 583 switch (ctrl->id) { 584 case V4L2_CID_ANALOGUE_GAIN: 585 ret = imx219_write_reg(imx219, IMX219_REG_ANALOG_GAIN, 586 IMX219_REG_VALUE_08BIT, ctrl->val); 587 break; 588 case V4L2_CID_EXPOSURE: 589 ret = imx219_write_reg(imx219, IMX219_REG_EXPOSURE, 590 IMX219_REG_VALUE_16BIT, ctrl->val); 591 break; 592 case V4L2_CID_DIGITAL_GAIN: 593 ret = imx219_write_reg(imx219, IMX219_REG_DIGITAL_GAIN, 594 IMX219_REG_VALUE_16BIT, ctrl->val); 595 break; 596 case V4L2_CID_TEST_PATTERN: 597 ret = imx219_write_reg(imx219, IMX219_REG_TEST_PATTERN, 598 IMX219_REG_VALUE_16BIT, 599 imx219_test_pattern_val[ctrl->val]); 600 break; 601 case V4L2_CID_HFLIP: 602 case V4L2_CID_VFLIP: 603 ret = imx219_write_reg(imx219, IMX219_REG_ORIENTATION, 1, 604 imx219->hflip->val | 605 imx219->vflip->val << 1); 606 break; 607 case V4L2_CID_VBLANK: 608 ret = imx219_write_reg(imx219, IMX219_REG_VTS, 609 IMX219_REG_VALUE_16BIT, 610 imx219->mode->height + ctrl->val); 611 break; 612 case V4L2_CID_TEST_PATTERN_RED: 613 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_RED, 614 IMX219_REG_VALUE_16BIT, ctrl->val); 615 break; 616 case V4L2_CID_TEST_PATTERN_GREENR: 617 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_GREENR, 618 IMX219_REG_VALUE_16BIT, ctrl->val); 619 break; 620 case V4L2_CID_TEST_PATTERN_BLUE: 621 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_BLUE, 622 IMX219_REG_VALUE_16BIT, ctrl->val); 623 break; 624 case V4L2_CID_TEST_PATTERN_GREENB: 625 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_GREENB, 626 IMX219_REG_VALUE_16BIT, ctrl->val); 627 break; 628 default: 629 dev_info(&client->dev, 630 "ctrl(id:0x%x,val:0x%x) is not handled\n", 631 ctrl->id, ctrl->val); 632 ret = -EINVAL; 633 break; 634 } 635 636 pm_runtime_put(&client->dev); 637 638 return ret; 639 } 640 641 static const struct v4l2_ctrl_ops imx219_ctrl_ops = { 642 .s_ctrl = imx219_set_ctrl, 643 }; 644 645 static int imx219_enum_mbus_code(struct v4l2_subdev *sd, 646 struct v4l2_subdev_pad_config *cfg, 647 struct v4l2_subdev_mbus_code_enum *code) 648 { 649 struct imx219 *imx219 = to_imx219(sd); 650 651 /* 652 * Only one bayer order is supported (though it depends on the flip 653 * settings) 654 */ 655 if (code->index > 0) 656 return -EINVAL; 657 658 code->code = imx219_get_format_code(imx219); 659 660 return 0; 661 } 662 663 static int imx219_enum_frame_size(struct v4l2_subdev *sd, 664 struct v4l2_subdev_pad_config *cfg, 665 struct v4l2_subdev_frame_size_enum *fse) 666 { 667 struct imx219 *imx219 = to_imx219(sd); 668 669 if (fse->index >= ARRAY_SIZE(supported_modes)) 670 return -EINVAL; 671 672 if (fse->code != imx219_get_format_code(imx219)) 673 return -EINVAL; 674 675 fse->min_width = supported_modes[fse->index].width; 676 fse->max_width = fse->min_width; 677 fse->min_height = supported_modes[fse->index].height; 678 fse->max_height = fse->min_height; 679 680 return 0; 681 } 682 683 static void imx219_reset_colorspace(struct v4l2_mbus_framefmt *fmt) 684 { 685 fmt->colorspace = V4L2_COLORSPACE_SRGB; 686 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 687 fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, 688 fmt->colorspace, 689 fmt->ycbcr_enc); 690 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 691 } 692 693 static void imx219_update_pad_format(struct imx219 *imx219, 694 const struct imx219_mode *mode, 695 struct v4l2_subdev_format *fmt) 696 { 697 fmt->format.width = mode->width; 698 fmt->format.height = mode->height; 699 fmt->format.code = imx219_get_format_code(imx219); 700 fmt->format.field = V4L2_FIELD_NONE; 701 702 imx219_reset_colorspace(&fmt->format); 703 } 704 705 static int __imx219_get_pad_format(struct imx219 *imx219, 706 struct v4l2_subdev_pad_config *cfg, 707 struct v4l2_subdev_format *fmt) 708 { 709 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 710 struct v4l2_mbus_framefmt *try_fmt = 711 v4l2_subdev_get_try_format(&imx219->sd, cfg, fmt->pad); 712 /* update the code which could change due to vflip or hflip: */ 713 try_fmt->code = imx219_get_format_code(imx219); 714 fmt->format = *try_fmt; 715 } else { 716 imx219_update_pad_format(imx219, imx219->mode, fmt); 717 } 718 719 return 0; 720 } 721 722 static int imx219_get_pad_format(struct v4l2_subdev *sd, 723 struct v4l2_subdev_pad_config *cfg, 724 struct v4l2_subdev_format *fmt) 725 { 726 struct imx219 *imx219 = to_imx219(sd); 727 int ret; 728 729 mutex_lock(&imx219->mutex); 730 ret = __imx219_get_pad_format(imx219, cfg, fmt); 731 mutex_unlock(&imx219->mutex); 732 733 return ret; 734 } 735 736 static int imx219_set_pad_format(struct v4l2_subdev *sd, 737 struct v4l2_subdev_pad_config *cfg, 738 struct v4l2_subdev_format *fmt) 739 { 740 struct imx219 *imx219 = to_imx219(sd); 741 const struct imx219_mode *mode; 742 struct v4l2_mbus_framefmt *framefmt; 743 int exposure_max, exposure_def, hblank; 744 745 mutex_lock(&imx219->mutex); 746 747 /* Bayer order varies with flips */ 748 fmt->format.code = imx219_get_format_code(imx219); 749 750 mode = v4l2_find_nearest_size(supported_modes, 751 ARRAY_SIZE(supported_modes), 752 width, height, 753 fmt->format.width, fmt->format.height); 754 imx219_update_pad_format(imx219, mode, fmt); 755 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 756 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); 757 *framefmt = fmt->format; 758 } else if (imx219->mode != mode) { 759 imx219->mode = mode; 760 /* Update limits and set FPS to default */ 761 __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN, 762 IMX219_VTS_MAX - mode->height, 1, 763 mode->vts_def - mode->height); 764 __v4l2_ctrl_s_ctrl(imx219->vblank, 765 mode->vts_def - mode->height); 766 /* Update max exposure while meeting expected vblanking */ 767 exposure_max = mode->vts_def - 4; 768 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ? 769 exposure_max : IMX219_EXPOSURE_DEFAULT; 770 __v4l2_ctrl_modify_range(imx219->exposure, 771 imx219->exposure->minimum, 772 exposure_max, imx219->exposure->step, 773 exposure_def); 774 /* 775 * Currently PPL is fixed to IMX219_PPL_DEFAULT, so hblank 776 * depends on mode->width only, and is not changeble in any 777 * way other than changing the mode. 778 */ 779 hblank = IMX219_PPL_DEFAULT - mode->width; 780 __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1, 781 hblank); 782 } 783 784 mutex_unlock(&imx219->mutex); 785 786 return 0; 787 } 788 789 static int imx219_start_streaming(struct imx219 *imx219) 790 { 791 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 792 const struct imx219_reg_list *reg_list; 793 int ret; 794 795 /* Apply default values of current mode */ 796 reg_list = &imx219->mode->reg_list; 797 ret = imx219_write_regs(imx219, reg_list->regs, reg_list->num_of_regs); 798 if (ret) { 799 dev_err(&client->dev, "%s failed to set mode\n", __func__); 800 return ret; 801 } 802 803 /* Apply customized values from user */ 804 ret = __v4l2_ctrl_handler_setup(imx219->sd.ctrl_handler); 805 if (ret) 806 return ret; 807 808 /* set stream on register */ 809 return imx219_write_reg(imx219, IMX219_REG_MODE_SELECT, 810 IMX219_REG_VALUE_08BIT, IMX219_MODE_STREAMING); 811 } 812 813 static void imx219_stop_streaming(struct imx219 *imx219) 814 { 815 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 816 int ret; 817 818 /* set stream off register */ 819 ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT, 820 IMX219_REG_VALUE_08BIT, IMX219_MODE_STANDBY); 821 if (ret) 822 dev_err(&client->dev, "%s failed to set stream\n", __func__); 823 } 824 825 static int imx219_set_stream(struct v4l2_subdev *sd, int enable) 826 { 827 struct imx219 *imx219 = to_imx219(sd); 828 struct i2c_client *client = v4l2_get_subdevdata(sd); 829 int ret = 0; 830 831 mutex_lock(&imx219->mutex); 832 if (imx219->streaming == enable) { 833 mutex_unlock(&imx219->mutex); 834 return 0; 835 } 836 837 if (enable) { 838 ret = pm_runtime_get_sync(&client->dev); 839 if (ret < 0) { 840 pm_runtime_put_noidle(&client->dev); 841 goto err_unlock; 842 } 843 844 /* 845 * Apply default & customized values 846 * and then start streaming. 847 */ 848 ret = imx219_start_streaming(imx219); 849 if (ret) 850 goto err_rpm_put; 851 } else { 852 imx219_stop_streaming(imx219); 853 pm_runtime_put(&client->dev); 854 } 855 856 imx219->streaming = enable; 857 858 /* vflip and hflip cannot change during streaming */ 859 __v4l2_ctrl_grab(imx219->vflip, enable); 860 __v4l2_ctrl_grab(imx219->hflip, enable); 861 862 mutex_unlock(&imx219->mutex); 863 864 return ret; 865 866 err_rpm_put: 867 pm_runtime_put(&client->dev); 868 err_unlock: 869 mutex_unlock(&imx219->mutex); 870 871 return ret; 872 } 873 874 /* Power/clock management functions */ 875 static int imx219_power_on(struct device *dev) 876 { 877 struct i2c_client *client = to_i2c_client(dev); 878 struct v4l2_subdev *sd = i2c_get_clientdata(client); 879 struct imx219 *imx219 = to_imx219(sd); 880 int ret; 881 882 ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES, 883 imx219->supplies); 884 if (ret) { 885 dev_err(&client->dev, "%s: failed to enable regulators\n", 886 __func__); 887 return ret; 888 } 889 890 ret = clk_prepare_enable(imx219->xclk); 891 if (ret) { 892 dev_err(&client->dev, "%s: failed to enable clock\n", 893 __func__); 894 goto reg_off; 895 } 896 897 gpiod_set_value_cansleep(imx219->reset_gpio, 1); 898 usleep_range(IMX219_XCLR_MIN_DELAY_US, 899 IMX219_XCLR_MIN_DELAY_US + IMX219_XCLR_DELAY_RANGE_US); 900 901 return 0; 902 903 reg_off: 904 regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies); 905 906 return ret; 907 } 908 909 static int imx219_power_off(struct device *dev) 910 { 911 struct i2c_client *client = to_i2c_client(dev); 912 struct v4l2_subdev *sd = i2c_get_clientdata(client); 913 struct imx219 *imx219 = to_imx219(sd); 914 915 gpiod_set_value_cansleep(imx219->reset_gpio, 0); 916 regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies); 917 clk_disable_unprepare(imx219->xclk); 918 919 return 0; 920 } 921 922 static int __maybe_unused imx219_suspend(struct device *dev) 923 { 924 struct i2c_client *client = to_i2c_client(dev); 925 struct v4l2_subdev *sd = i2c_get_clientdata(client); 926 struct imx219 *imx219 = to_imx219(sd); 927 928 if (imx219->streaming) 929 imx219_stop_streaming(imx219); 930 931 return 0; 932 } 933 934 static int __maybe_unused imx219_resume(struct device *dev) 935 { 936 struct i2c_client *client = to_i2c_client(dev); 937 struct v4l2_subdev *sd = i2c_get_clientdata(client); 938 struct imx219 *imx219 = to_imx219(sd); 939 int ret; 940 941 if (imx219->streaming) { 942 ret = imx219_start_streaming(imx219); 943 if (ret) 944 goto error; 945 } 946 947 return 0; 948 949 error: 950 imx219_stop_streaming(imx219); 951 imx219->streaming = 0; 952 953 return ret; 954 } 955 956 static int imx219_get_regulators(struct imx219 *imx219) 957 { 958 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 959 unsigned int i; 960 961 for (i = 0; i < IMX219_NUM_SUPPLIES; i++) 962 imx219->supplies[i].supply = imx219_supply_name[i]; 963 964 return devm_regulator_bulk_get(&client->dev, 965 IMX219_NUM_SUPPLIES, 966 imx219->supplies); 967 } 968 969 /* Verify chip ID */ 970 static int imx219_identify_module(struct imx219 *imx219) 971 { 972 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 973 int ret; 974 u32 val; 975 976 ret = imx219_read_reg(imx219, IMX219_REG_CHIP_ID, 977 IMX219_REG_VALUE_16BIT, &val); 978 if (ret) { 979 dev_err(&client->dev, "failed to read chip id %x\n", 980 IMX219_CHIP_ID); 981 return ret; 982 } 983 984 if (val != IMX219_CHIP_ID) { 985 dev_err(&client->dev, "chip id mismatch: %x!=%x\n", 986 IMX219_CHIP_ID, val); 987 return -EIO; 988 } 989 990 return 0; 991 } 992 993 static const struct v4l2_subdev_core_ops imx219_core_ops = { 994 .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 995 .unsubscribe_event = v4l2_event_subdev_unsubscribe, 996 }; 997 998 static const struct v4l2_subdev_video_ops imx219_video_ops = { 999 .s_stream = imx219_set_stream, 1000 }; 1001 1002 static const struct v4l2_subdev_pad_ops imx219_pad_ops = { 1003 .enum_mbus_code = imx219_enum_mbus_code, 1004 .get_fmt = imx219_get_pad_format, 1005 .set_fmt = imx219_set_pad_format, 1006 .enum_frame_size = imx219_enum_frame_size, 1007 }; 1008 1009 static const struct v4l2_subdev_ops imx219_subdev_ops = { 1010 .core = &imx219_core_ops, 1011 .video = &imx219_video_ops, 1012 .pad = &imx219_pad_ops, 1013 }; 1014 1015 static const struct v4l2_subdev_internal_ops imx219_internal_ops = { 1016 .open = imx219_open, 1017 }; 1018 1019 /* Initialize control handlers */ 1020 static int imx219_init_controls(struct imx219 *imx219) 1021 { 1022 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 1023 struct v4l2_ctrl_handler *ctrl_hdlr; 1024 unsigned int height = imx219->mode->height; 1025 int exposure_max, exposure_def, hblank; 1026 int i, ret; 1027 1028 ctrl_hdlr = &imx219->ctrl_handler; 1029 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 9); 1030 if (ret) 1031 return ret; 1032 1033 mutex_init(&imx219->mutex); 1034 ctrl_hdlr->lock = &imx219->mutex; 1035 1036 /* By default, PIXEL_RATE is read only */ 1037 imx219->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 1038 V4L2_CID_PIXEL_RATE, 1039 IMX219_PIXEL_RATE, 1040 IMX219_PIXEL_RATE, 1, 1041 IMX219_PIXEL_RATE); 1042 1043 /* Initial vblank/hblank/exposure parameters based on current mode */ 1044 imx219->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 1045 V4L2_CID_VBLANK, IMX219_VBLANK_MIN, 1046 IMX219_VTS_MAX - height, 1, 1047 imx219->mode->vts_def - height); 1048 hblank = IMX219_PPL_DEFAULT - imx219->mode->width; 1049 imx219->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 1050 V4L2_CID_HBLANK, hblank, hblank, 1051 1, hblank); 1052 if (imx219->hblank) 1053 imx219->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1054 exposure_max = imx219->mode->vts_def - 4; 1055 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ? 1056 exposure_max : IMX219_EXPOSURE_DEFAULT; 1057 imx219->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 1058 V4L2_CID_EXPOSURE, 1059 IMX219_EXPOSURE_MIN, exposure_max, 1060 IMX219_EXPOSURE_STEP, 1061 exposure_def); 1062 1063 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, 1064 IMX219_ANA_GAIN_MIN, IMX219_ANA_GAIN_MAX, 1065 IMX219_ANA_GAIN_STEP, IMX219_ANA_GAIN_DEFAULT); 1066 1067 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_DIGITAL_GAIN, 1068 IMX219_DGTL_GAIN_MIN, IMX219_DGTL_GAIN_MAX, 1069 IMX219_DGTL_GAIN_STEP, IMX219_DGTL_GAIN_DEFAULT); 1070 1071 imx219->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 1072 V4L2_CID_HFLIP, 0, 1, 1, 0); 1073 if (imx219->hflip) 1074 imx219->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 1075 1076 imx219->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 1077 V4L2_CID_VFLIP, 0, 1, 1, 0); 1078 if (imx219->vflip) 1079 imx219->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 1080 1081 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx219_ctrl_ops, 1082 V4L2_CID_TEST_PATTERN, 1083 ARRAY_SIZE(imx219_test_pattern_menu) - 1, 1084 0, 0, imx219_test_pattern_menu); 1085 for (i = 0; i < 4; i++) { 1086 /* 1087 * The assumption is that 1088 * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1 1089 * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2 1090 * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3 1091 */ 1092 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 1093 V4L2_CID_TEST_PATTERN_RED + i, 1094 IMX219_TESTP_COLOUR_MIN, 1095 IMX219_TESTP_COLOUR_MAX, 1096 IMX219_TESTP_COLOUR_STEP, 1097 IMX219_TESTP_COLOUR_MAX); 1098 /* The "Solid color" pattern is white by default */ 1099 } 1100 1101 if (ctrl_hdlr->error) { 1102 ret = ctrl_hdlr->error; 1103 dev_err(&client->dev, "%s control init failed (%d)\n", 1104 __func__, ret); 1105 goto error; 1106 } 1107 1108 imx219->sd.ctrl_handler = ctrl_hdlr; 1109 1110 return 0; 1111 1112 error: 1113 v4l2_ctrl_handler_free(ctrl_hdlr); 1114 mutex_destroy(&imx219->mutex); 1115 1116 return ret; 1117 } 1118 1119 static void imx219_free_controls(struct imx219 *imx219) 1120 { 1121 v4l2_ctrl_handler_free(imx219->sd.ctrl_handler); 1122 mutex_destroy(&imx219->mutex); 1123 } 1124 1125 static int imx219_check_hwcfg(struct device *dev) 1126 { 1127 struct fwnode_handle *endpoint; 1128 struct v4l2_fwnode_endpoint ep_cfg = { 1129 .bus_type = V4L2_MBUS_CSI2_DPHY 1130 }; 1131 int ret = -EINVAL; 1132 1133 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); 1134 if (!endpoint) { 1135 dev_err(dev, "endpoint node not found\n"); 1136 return -EINVAL; 1137 } 1138 1139 if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) { 1140 dev_err(dev, "could not parse endpoint\n"); 1141 goto error_out; 1142 } 1143 1144 /* Check the number of MIPI CSI2 data lanes */ 1145 if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) { 1146 dev_err(dev, "only 2 data lanes are currently supported\n"); 1147 goto error_out; 1148 } 1149 1150 /* Check the link frequency set in device tree */ 1151 if (!ep_cfg.nr_of_link_frequencies) { 1152 dev_err(dev, "link-frequency property not found in DT\n"); 1153 goto error_out; 1154 } 1155 1156 if (ep_cfg.nr_of_link_frequencies != 1 || 1157 ep_cfg.link_frequencies[0] != IMX219_DEFAULT_LINK_FREQ) { 1158 dev_err(dev, "Link frequency not supported: %lld\n", 1159 ep_cfg.link_frequencies[0]); 1160 goto error_out; 1161 } 1162 1163 ret = 0; 1164 1165 error_out: 1166 v4l2_fwnode_endpoint_free(&ep_cfg); 1167 fwnode_handle_put(endpoint); 1168 1169 return ret; 1170 } 1171 1172 static int imx219_probe(struct i2c_client *client) 1173 { 1174 struct device *dev = &client->dev; 1175 struct imx219 *imx219; 1176 int ret; 1177 1178 imx219 = devm_kzalloc(&client->dev, sizeof(*imx219), GFP_KERNEL); 1179 if (!imx219) 1180 return -ENOMEM; 1181 1182 v4l2_i2c_subdev_init(&imx219->sd, client, &imx219_subdev_ops); 1183 1184 /* Check the hardware configuration in device tree */ 1185 if (imx219_check_hwcfg(dev)) 1186 return -EINVAL; 1187 1188 /* Get system clock (xclk) */ 1189 imx219->xclk = devm_clk_get(dev, NULL); 1190 if (IS_ERR(imx219->xclk)) { 1191 dev_err(dev, "failed to get xclk\n"); 1192 return PTR_ERR(imx219->xclk); 1193 } 1194 1195 imx219->xclk_freq = clk_get_rate(imx219->xclk); 1196 if (imx219->xclk_freq != IMX219_XCLK_FREQ) { 1197 dev_err(dev, "xclk frequency not supported: %d Hz\n", 1198 imx219->xclk_freq); 1199 return -EINVAL; 1200 } 1201 1202 ret = imx219_get_regulators(imx219); 1203 if (ret) { 1204 dev_err(dev, "failed to get regulators\n"); 1205 return ret; 1206 } 1207 1208 /* Request optional enable pin */ 1209 imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset", 1210 GPIOD_OUT_HIGH); 1211 1212 /* 1213 * The sensor must be powered for imx219_identify_module() 1214 * to be able to read the CHIP_ID register 1215 */ 1216 ret = imx219_power_on(dev); 1217 if (ret) 1218 return ret; 1219 1220 ret = imx219_identify_module(imx219); 1221 if (ret) 1222 goto error_power_off; 1223 1224 /* Set default mode to max resolution */ 1225 imx219->mode = &supported_modes[0]; 1226 1227 ret = imx219_init_controls(imx219); 1228 if (ret) 1229 goto error_power_off; 1230 1231 /* Initialize subdev */ 1232 imx219->sd.internal_ops = &imx219_internal_ops; 1233 imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1234 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1235 1236 /* Initialize source pad */ 1237 imx219->pad.flags = MEDIA_PAD_FL_SOURCE; 1238 1239 ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad); 1240 if (ret) { 1241 dev_err(dev, "failed to init entity pads: %d\n", ret); 1242 goto error_handler_free; 1243 } 1244 1245 ret = v4l2_async_register_subdev_sensor_common(&imx219->sd); 1246 if (ret < 0) { 1247 dev_err(dev, "failed to register sensor sub-device: %d\n", ret); 1248 goto error_media_entity; 1249 } 1250 1251 /* Enable runtime PM and turn off the device */ 1252 pm_runtime_set_active(dev); 1253 pm_runtime_enable(dev); 1254 pm_runtime_idle(dev); 1255 1256 return 0; 1257 1258 error_media_entity: 1259 media_entity_cleanup(&imx219->sd.entity); 1260 1261 error_handler_free: 1262 imx219_free_controls(imx219); 1263 1264 error_power_off: 1265 imx219_power_off(dev); 1266 1267 return ret; 1268 } 1269 1270 static int imx219_remove(struct i2c_client *client) 1271 { 1272 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1273 struct imx219 *imx219 = to_imx219(sd); 1274 1275 v4l2_async_unregister_subdev(sd); 1276 media_entity_cleanup(&sd->entity); 1277 imx219_free_controls(imx219); 1278 1279 pm_runtime_disable(&client->dev); 1280 if (!pm_runtime_status_suspended(&client->dev)) 1281 imx219_power_off(&client->dev); 1282 pm_runtime_set_suspended(&client->dev); 1283 1284 return 0; 1285 } 1286 1287 static const struct of_device_id imx219_dt_ids[] = { 1288 { .compatible = "sony,imx219" }, 1289 { /* sentinel */ } 1290 }; 1291 MODULE_DEVICE_TABLE(of, imx219_dt_ids); 1292 1293 static const struct dev_pm_ops imx219_pm_ops = { 1294 SET_SYSTEM_SLEEP_PM_OPS(imx219_suspend, imx219_resume) 1295 SET_RUNTIME_PM_OPS(imx219_power_off, imx219_power_on, NULL) 1296 }; 1297 1298 static struct i2c_driver imx219_i2c_driver = { 1299 .driver = { 1300 .name = "imx219", 1301 .of_match_table = imx219_dt_ids, 1302 .pm = &imx219_pm_ops, 1303 }, 1304 .probe_new = imx219_probe, 1305 .remove = imx219_remove, 1306 }; 1307 1308 module_i2c_driver(imx219_i2c_driver); 1309 1310 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com"); 1311 MODULE_DESCRIPTION("Sony IMX219 sensor driver"); 1312 MODULE_LICENSE("GPL v2"); 1313