1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * imx214.c - imx214 sensor driver 4 * 5 * Copyright 2018 Qtechnology A/S 6 * 7 * Ricardo Ribalda <ribalda@kernel.org> 8 */ 9 #include <linux/clk.h> 10 #include <linux/delay.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/i2c.h> 13 #include <linux/module.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/regmap.h> 16 #include <linux/regulator/consumer.h> 17 #include <media/media-entity.h> 18 #include <media/v4l2-ctrls.h> 19 #include <media/v4l2-fwnode.h> 20 #include <media/v4l2-subdev.h> 21 22 #define IMX214_DEFAULT_CLK_FREQ 24000000 23 #define IMX214_DEFAULT_LINK_FREQ 480000000 24 #define IMX214_DEFAULT_PIXEL_RATE ((IMX214_DEFAULT_LINK_FREQ * 8LL) / 10) 25 #define IMX214_FPS 30 26 #define IMX214_MBUS_CODE MEDIA_BUS_FMT_SRGGB10_1X10 27 28 static const char * const imx214_supply_name[] = { 29 "vdda", 30 "vddd", 31 "vdddo", 32 }; 33 34 #define IMX214_NUM_SUPPLIES ARRAY_SIZE(imx214_supply_name) 35 36 struct imx214 { 37 struct device *dev; 38 struct clk *xclk; 39 struct regmap *regmap; 40 41 struct v4l2_subdev sd; 42 struct media_pad pad; 43 struct v4l2_mbus_framefmt fmt; 44 struct v4l2_rect crop; 45 46 struct v4l2_ctrl_handler ctrls; 47 struct v4l2_ctrl *pixel_rate; 48 struct v4l2_ctrl *link_freq; 49 struct v4l2_ctrl *exposure; 50 struct v4l2_ctrl *unit_size; 51 52 struct regulator_bulk_data supplies[IMX214_NUM_SUPPLIES]; 53 54 struct gpio_desc *enable_gpio; 55 56 /* 57 * Serialize control access, get/set format, get selection 58 * and start streaming. 59 */ 60 struct mutex mutex; 61 }; 62 63 struct reg_8 { 64 u16 addr; 65 u8 val; 66 }; 67 68 enum { 69 IMX214_TABLE_WAIT_MS = 0, 70 IMX214_TABLE_END, 71 IMX214_MAX_RETRIES, 72 IMX214_WAIT_MS 73 }; 74 75 /*From imx214_mode_tbls.h*/ 76 static const struct reg_8 mode_4096x2304[] = { 77 {0x0114, 0x03}, 78 {0x0220, 0x00}, 79 {0x0221, 0x11}, 80 {0x0222, 0x01}, 81 {0x0340, 0x0C}, 82 {0x0341, 0x7A}, 83 {0x0342, 0x13}, 84 {0x0343, 0x90}, 85 {0x0344, 0x00}, 86 {0x0345, 0x38}, 87 {0x0346, 0x01}, 88 {0x0347, 0x98}, 89 {0x0348, 0x10}, 90 {0x0349, 0x37}, 91 {0x034A, 0x0A}, 92 {0x034B, 0x97}, 93 {0x0381, 0x01}, 94 {0x0383, 0x01}, 95 {0x0385, 0x01}, 96 {0x0387, 0x01}, 97 {0x0900, 0x00}, 98 {0x0901, 0x00}, 99 {0x0902, 0x00}, 100 {0x3000, 0x35}, 101 {0x3054, 0x01}, 102 {0x305C, 0x11}, 103 104 {0x0112, 0x0A}, 105 {0x0113, 0x0A}, 106 {0x034C, 0x10}, 107 {0x034D, 0x00}, 108 {0x034E, 0x09}, 109 {0x034F, 0x00}, 110 {0x0401, 0x00}, 111 {0x0404, 0x00}, 112 {0x0405, 0x10}, 113 {0x0408, 0x00}, 114 {0x0409, 0x00}, 115 {0x040A, 0x00}, 116 {0x040B, 0x00}, 117 {0x040C, 0x10}, 118 {0x040D, 0x00}, 119 {0x040E, 0x09}, 120 {0x040F, 0x00}, 121 122 {0x0301, 0x05}, 123 {0x0303, 0x02}, 124 {0x0305, 0x03}, 125 {0x0306, 0x00}, 126 {0x0307, 0x96}, 127 {0x0309, 0x0A}, 128 {0x030B, 0x01}, 129 {0x0310, 0x00}, 130 131 {0x0820, 0x12}, 132 {0x0821, 0xC0}, 133 {0x0822, 0x00}, 134 {0x0823, 0x00}, 135 136 {0x3A03, 0x09}, 137 {0x3A04, 0x50}, 138 {0x3A05, 0x01}, 139 140 {0x0B06, 0x01}, 141 {0x30A2, 0x00}, 142 143 {0x30B4, 0x00}, 144 145 {0x3A02, 0xFF}, 146 147 {0x3011, 0x00}, 148 {0x3013, 0x01}, 149 150 {0x0202, 0x0C}, 151 {0x0203, 0x70}, 152 {0x0224, 0x01}, 153 {0x0225, 0xF4}, 154 155 {0x0204, 0x00}, 156 {0x0205, 0x00}, 157 {0x020E, 0x01}, 158 {0x020F, 0x00}, 159 {0x0210, 0x01}, 160 {0x0211, 0x00}, 161 {0x0212, 0x01}, 162 {0x0213, 0x00}, 163 {0x0214, 0x01}, 164 {0x0215, 0x00}, 165 {0x0216, 0x00}, 166 {0x0217, 0x00}, 167 168 {0x4170, 0x00}, 169 {0x4171, 0x10}, 170 {0x4176, 0x00}, 171 {0x4177, 0x3C}, 172 {0xAE20, 0x04}, 173 {0xAE21, 0x5C}, 174 175 {IMX214_TABLE_WAIT_MS, 10}, 176 {0x0138, 0x01}, 177 {IMX214_TABLE_END, 0x00} 178 }; 179 180 static const struct reg_8 mode_1920x1080[] = { 181 {0x0114, 0x03}, 182 {0x0220, 0x00}, 183 {0x0221, 0x11}, 184 {0x0222, 0x01}, 185 {0x0340, 0x0C}, 186 {0x0341, 0x7A}, 187 {0x0342, 0x13}, 188 {0x0343, 0x90}, 189 {0x0344, 0x04}, 190 {0x0345, 0x78}, 191 {0x0346, 0x03}, 192 {0x0347, 0xFC}, 193 {0x0348, 0x0B}, 194 {0x0349, 0xF7}, 195 {0x034A, 0x08}, 196 {0x034B, 0x33}, 197 {0x0381, 0x01}, 198 {0x0383, 0x01}, 199 {0x0385, 0x01}, 200 {0x0387, 0x01}, 201 {0x0900, 0x00}, 202 {0x0901, 0x00}, 203 {0x0902, 0x00}, 204 {0x3000, 0x35}, 205 {0x3054, 0x01}, 206 {0x305C, 0x11}, 207 208 {0x0112, 0x0A}, 209 {0x0113, 0x0A}, 210 {0x034C, 0x07}, 211 {0x034D, 0x80}, 212 {0x034E, 0x04}, 213 {0x034F, 0x38}, 214 {0x0401, 0x00}, 215 {0x0404, 0x00}, 216 {0x0405, 0x10}, 217 {0x0408, 0x00}, 218 {0x0409, 0x00}, 219 {0x040A, 0x00}, 220 {0x040B, 0x00}, 221 {0x040C, 0x07}, 222 {0x040D, 0x80}, 223 {0x040E, 0x04}, 224 {0x040F, 0x38}, 225 226 {0x0301, 0x05}, 227 {0x0303, 0x02}, 228 {0x0305, 0x03}, 229 {0x0306, 0x00}, 230 {0x0307, 0x96}, 231 {0x0309, 0x0A}, 232 {0x030B, 0x01}, 233 {0x0310, 0x00}, 234 235 {0x0820, 0x12}, 236 {0x0821, 0xC0}, 237 {0x0822, 0x00}, 238 {0x0823, 0x00}, 239 240 {0x3A03, 0x04}, 241 {0x3A04, 0xF8}, 242 {0x3A05, 0x02}, 243 244 {0x0B06, 0x01}, 245 {0x30A2, 0x00}, 246 247 {0x30B4, 0x00}, 248 249 {0x3A02, 0xFF}, 250 251 {0x3011, 0x00}, 252 {0x3013, 0x01}, 253 254 {0x0202, 0x0C}, 255 {0x0203, 0x70}, 256 {0x0224, 0x01}, 257 {0x0225, 0xF4}, 258 259 {0x0204, 0x00}, 260 {0x0205, 0x00}, 261 {0x020E, 0x01}, 262 {0x020F, 0x00}, 263 {0x0210, 0x01}, 264 {0x0211, 0x00}, 265 {0x0212, 0x01}, 266 {0x0213, 0x00}, 267 {0x0214, 0x01}, 268 {0x0215, 0x00}, 269 {0x0216, 0x00}, 270 {0x0217, 0x00}, 271 272 {0x4170, 0x00}, 273 {0x4171, 0x10}, 274 {0x4176, 0x00}, 275 {0x4177, 0x3C}, 276 {0xAE20, 0x04}, 277 {0xAE21, 0x5C}, 278 279 {IMX214_TABLE_WAIT_MS, 10}, 280 {0x0138, 0x01}, 281 {IMX214_TABLE_END, 0x00} 282 }; 283 284 static const struct reg_8 mode_table_common[] = { 285 /* software reset */ 286 287 /* software standby settings */ 288 {0x0100, 0x00}, 289 290 /* ATR setting */ 291 {0x9300, 0x02}, 292 293 /* external clock setting */ 294 {0x0136, 0x18}, 295 {0x0137, 0x00}, 296 297 /* global setting */ 298 /* basic config */ 299 {0x0101, 0x00}, 300 {0x0105, 0x01}, 301 {0x0106, 0x01}, 302 {0x4550, 0x02}, 303 {0x4601, 0x00}, 304 {0x4642, 0x05}, 305 {0x6227, 0x11}, 306 {0x6276, 0x00}, 307 {0x900E, 0x06}, 308 {0xA802, 0x90}, 309 {0xA803, 0x11}, 310 {0xA804, 0x62}, 311 {0xA805, 0x77}, 312 {0xA806, 0xAE}, 313 {0xA807, 0x34}, 314 {0xA808, 0xAE}, 315 {0xA809, 0x35}, 316 {0xA80A, 0x62}, 317 {0xA80B, 0x83}, 318 {0xAE33, 0x00}, 319 320 /* analog setting */ 321 {0x4174, 0x00}, 322 {0x4175, 0x11}, 323 {0x4612, 0x29}, 324 {0x461B, 0x12}, 325 {0x461F, 0x06}, 326 {0x4635, 0x07}, 327 {0x4637, 0x30}, 328 {0x463F, 0x18}, 329 {0x4641, 0x0D}, 330 {0x465B, 0x12}, 331 {0x465F, 0x11}, 332 {0x4663, 0x11}, 333 {0x4667, 0x0F}, 334 {0x466F, 0x0F}, 335 {0x470E, 0x09}, 336 {0x4909, 0xAB}, 337 {0x490B, 0x95}, 338 {0x4915, 0x5D}, 339 {0x4A5F, 0xFF}, 340 {0x4A61, 0xFF}, 341 {0x4A73, 0x62}, 342 {0x4A85, 0x00}, 343 {0x4A87, 0xFF}, 344 345 /* embedded data */ 346 {0x5041, 0x04}, 347 {0x583C, 0x04}, 348 {0x620E, 0x04}, 349 {0x6EB2, 0x01}, 350 {0x6EB3, 0x00}, 351 {0x9300, 0x02}, 352 353 /* imagequality */ 354 /* HDR setting */ 355 {0x3001, 0x07}, 356 {0x6D12, 0x3F}, 357 {0x6D13, 0xFF}, 358 {0x9344, 0x03}, 359 {0x9706, 0x10}, 360 {0x9707, 0x03}, 361 {0x9708, 0x03}, 362 {0x9E04, 0x01}, 363 {0x9E05, 0x00}, 364 {0x9E0C, 0x01}, 365 {0x9E0D, 0x02}, 366 {0x9E24, 0x00}, 367 {0x9E25, 0x8C}, 368 {0x9E26, 0x00}, 369 {0x9E27, 0x94}, 370 {0x9E28, 0x00}, 371 {0x9E29, 0x96}, 372 373 /* CNR parameter setting */ 374 {0x69DB, 0x01}, 375 376 /* Moire reduction */ 377 {0x6957, 0x01}, 378 379 /* image enhancement */ 380 {0x6987, 0x17}, 381 {0x698A, 0x03}, 382 {0x698B, 0x03}, 383 384 /* white balanace */ 385 {0x0B8E, 0x01}, 386 {0x0B8F, 0x00}, 387 {0x0B90, 0x01}, 388 {0x0B91, 0x00}, 389 {0x0B92, 0x01}, 390 {0x0B93, 0x00}, 391 {0x0B94, 0x01}, 392 {0x0B95, 0x00}, 393 394 /* ATR setting */ 395 {0x6E50, 0x00}, 396 {0x6E51, 0x32}, 397 {0x9340, 0x00}, 398 {0x9341, 0x3C}, 399 {0x9342, 0x03}, 400 {0x9343, 0xFF}, 401 {IMX214_TABLE_END, 0x00} 402 }; 403 404 /* 405 * Declare modes in order, from biggest 406 * to smallest height. 407 */ 408 static const struct imx214_mode { 409 u32 width; 410 u32 height; 411 const struct reg_8 *reg_table; 412 } imx214_modes[] = { 413 { 414 .width = 4096, 415 .height = 2304, 416 .reg_table = mode_4096x2304, 417 }, 418 { 419 .width = 1920, 420 .height = 1080, 421 .reg_table = mode_1920x1080, 422 }, 423 }; 424 425 static inline struct imx214 *to_imx214(struct v4l2_subdev *sd) 426 { 427 return container_of(sd, struct imx214, sd); 428 } 429 430 static int __maybe_unused imx214_power_on(struct device *dev) 431 { 432 struct i2c_client *client = to_i2c_client(dev); 433 struct v4l2_subdev *sd = i2c_get_clientdata(client); 434 struct imx214 *imx214 = to_imx214(sd); 435 int ret; 436 437 ret = regulator_bulk_enable(IMX214_NUM_SUPPLIES, imx214->supplies); 438 if (ret < 0) { 439 dev_err(imx214->dev, "failed to enable regulators: %d\n", ret); 440 return ret; 441 } 442 443 usleep_range(2000, 3000); 444 445 ret = clk_prepare_enable(imx214->xclk); 446 if (ret < 0) { 447 regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies); 448 dev_err(imx214->dev, "clk prepare enable failed\n"); 449 return ret; 450 } 451 452 gpiod_set_value_cansleep(imx214->enable_gpio, 1); 453 usleep_range(12000, 15000); 454 455 return 0; 456 } 457 458 static int __maybe_unused imx214_power_off(struct device *dev) 459 { 460 struct i2c_client *client = to_i2c_client(dev); 461 struct v4l2_subdev *sd = i2c_get_clientdata(client); 462 struct imx214 *imx214 = to_imx214(sd); 463 464 gpiod_set_value_cansleep(imx214->enable_gpio, 0); 465 466 clk_disable_unprepare(imx214->xclk); 467 468 regulator_bulk_disable(IMX214_NUM_SUPPLIES, imx214->supplies); 469 usleep_range(10, 20); 470 471 return 0; 472 } 473 474 static int imx214_enum_mbus_code(struct v4l2_subdev *sd, 475 struct v4l2_subdev_state *sd_state, 476 struct v4l2_subdev_mbus_code_enum *code) 477 { 478 if (code->index > 0) 479 return -EINVAL; 480 481 code->code = IMX214_MBUS_CODE; 482 483 return 0; 484 } 485 486 static int imx214_enum_frame_size(struct v4l2_subdev *subdev, 487 struct v4l2_subdev_state *sd_state, 488 struct v4l2_subdev_frame_size_enum *fse) 489 { 490 if (fse->code != IMX214_MBUS_CODE) 491 return -EINVAL; 492 493 if (fse->index >= ARRAY_SIZE(imx214_modes)) 494 return -EINVAL; 495 496 fse->min_width = fse->max_width = imx214_modes[fse->index].width; 497 fse->min_height = fse->max_height = imx214_modes[fse->index].height; 498 499 return 0; 500 } 501 502 #ifdef CONFIG_VIDEO_ADV_DEBUG 503 static int imx214_s_register(struct v4l2_subdev *subdev, 504 const struct v4l2_dbg_register *reg) 505 { 506 struct imx214 *imx214 = container_of(subdev, struct imx214, sd); 507 508 return regmap_write(imx214->regmap, reg->reg, reg->val); 509 } 510 511 static int imx214_g_register(struct v4l2_subdev *subdev, 512 struct v4l2_dbg_register *reg) 513 { 514 struct imx214 *imx214 = container_of(subdev, struct imx214, sd); 515 unsigned int aux; 516 int ret; 517 518 reg->size = 1; 519 ret = regmap_read(imx214->regmap, reg->reg, &aux); 520 reg->val = aux; 521 522 return ret; 523 } 524 #endif 525 526 static const struct v4l2_subdev_core_ops imx214_core_ops = { 527 #ifdef CONFIG_VIDEO_ADV_DEBUG 528 .g_register = imx214_g_register, 529 .s_register = imx214_s_register, 530 #endif 531 }; 532 533 static struct v4l2_mbus_framefmt * 534 __imx214_get_pad_format(struct imx214 *imx214, 535 struct v4l2_subdev_state *sd_state, 536 unsigned int pad, 537 enum v4l2_subdev_format_whence which) 538 { 539 switch (which) { 540 case V4L2_SUBDEV_FORMAT_TRY: 541 return v4l2_subdev_get_try_format(&imx214->sd, sd_state, pad); 542 case V4L2_SUBDEV_FORMAT_ACTIVE: 543 return &imx214->fmt; 544 default: 545 return NULL; 546 } 547 } 548 549 static int imx214_get_format(struct v4l2_subdev *sd, 550 struct v4l2_subdev_state *sd_state, 551 struct v4l2_subdev_format *format) 552 { 553 struct imx214 *imx214 = to_imx214(sd); 554 555 mutex_lock(&imx214->mutex); 556 format->format = *__imx214_get_pad_format(imx214, sd_state, 557 format->pad, 558 format->which); 559 mutex_unlock(&imx214->mutex); 560 561 return 0; 562 } 563 564 static struct v4l2_rect * 565 __imx214_get_pad_crop(struct imx214 *imx214, 566 struct v4l2_subdev_state *sd_state, 567 unsigned int pad, enum v4l2_subdev_format_whence which) 568 { 569 switch (which) { 570 case V4L2_SUBDEV_FORMAT_TRY: 571 return v4l2_subdev_get_try_crop(&imx214->sd, sd_state, pad); 572 case V4L2_SUBDEV_FORMAT_ACTIVE: 573 return &imx214->crop; 574 default: 575 return NULL; 576 } 577 } 578 579 static int imx214_set_format(struct v4l2_subdev *sd, 580 struct v4l2_subdev_state *sd_state, 581 struct v4l2_subdev_format *format) 582 { 583 struct imx214 *imx214 = to_imx214(sd); 584 struct v4l2_mbus_framefmt *__format; 585 struct v4l2_rect *__crop; 586 const struct imx214_mode *mode; 587 588 mutex_lock(&imx214->mutex); 589 590 __crop = __imx214_get_pad_crop(imx214, sd_state, format->pad, 591 format->which); 592 593 mode = v4l2_find_nearest_size(imx214_modes, 594 ARRAY_SIZE(imx214_modes), width, height, 595 format->format.width, 596 format->format.height); 597 598 __crop->width = mode->width; 599 __crop->height = mode->height; 600 601 __format = __imx214_get_pad_format(imx214, sd_state, format->pad, 602 format->which); 603 __format->width = __crop->width; 604 __format->height = __crop->height; 605 __format->code = IMX214_MBUS_CODE; 606 __format->field = V4L2_FIELD_NONE; 607 __format->colorspace = V4L2_COLORSPACE_SRGB; 608 __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace); 609 __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, 610 __format->colorspace, __format->ycbcr_enc); 611 __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace); 612 613 format->format = *__format; 614 615 mutex_unlock(&imx214->mutex); 616 617 return 0; 618 } 619 620 static int imx214_get_selection(struct v4l2_subdev *sd, 621 struct v4l2_subdev_state *sd_state, 622 struct v4l2_subdev_selection *sel) 623 { 624 struct imx214 *imx214 = to_imx214(sd); 625 626 if (sel->target != V4L2_SEL_TGT_CROP) 627 return -EINVAL; 628 629 mutex_lock(&imx214->mutex); 630 sel->r = *__imx214_get_pad_crop(imx214, sd_state, sel->pad, 631 sel->which); 632 mutex_unlock(&imx214->mutex); 633 return 0; 634 } 635 636 static int imx214_entity_init_cfg(struct v4l2_subdev *subdev, 637 struct v4l2_subdev_state *sd_state) 638 { 639 struct v4l2_subdev_format fmt = { }; 640 641 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE; 642 fmt.format.width = imx214_modes[0].width; 643 fmt.format.height = imx214_modes[0].height; 644 645 imx214_set_format(subdev, sd_state, &fmt); 646 647 return 0; 648 } 649 650 static int imx214_set_ctrl(struct v4l2_ctrl *ctrl) 651 { 652 struct imx214 *imx214 = container_of(ctrl->handler, 653 struct imx214, ctrls); 654 u8 vals[2]; 655 int ret; 656 657 /* 658 * Applying V4L2 control value only happens 659 * when power is up for streaming 660 */ 661 if (!pm_runtime_get_if_in_use(imx214->dev)) 662 return 0; 663 664 switch (ctrl->id) { 665 case V4L2_CID_EXPOSURE: 666 vals[1] = ctrl->val; 667 vals[0] = ctrl->val >> 8; 668 ret = regmap_bulk_write(imx214->regmap, 0x202, vals, 2); 669 if (ret < 0) 670 dev_err(imx214->dev, "Error %d\n", ret); 671 ret = 0; 672 break; 673 674 default: 675 ret = -EINVAL; 676 } 677 678 pm_runtime_put(imx214->dev); 679 680 return ret; 681 } 682 683 static const struct v4l2_ctrl_ops imx214_ctrl_ops = { 684 .s_ctrl = imx214_set_ctrl, 685 }; 686 687 #define MAX_CMD 4 688 static int imx214_write_table(struct imx214 *imx214, 689 const struct reg_8 table[]) 690 { 691 u8 vals[MAX_CMD]; 692 int i; 693 int ret; 694 695 for (; table->addr != IMX214_TABLE_END ; table++) { 696 if (table->addr == IMX214_TABLE_WAIT_MS) { 697 usleep_range(table->val * 1000, 698 table->val * 1000 + 500); 699 continue; 700 } 701 702 for (i = 0; i < MAX_CMD; i++) { 703 if (table[i].addr != (table[0].addr + i)) 704 break; 705 vals[i] = table[i].val; 706 } 707 708 ret = regmap_bulk_write(imx214->regmap, table->addr, vals, i); 709 710 if (ret) { 711 dev_err(imx214->dev, "write_table error: %d\n", ret); 712 return ret; 713 } 714 715 table += i - 1; 716 } 717 718 return 0; 719 } 720 721 static int imx214_start_streaming(struct imx214 *imx214) 722 { 723 const struct imx214_mode *mode; 724 int ret; 725 726 mutex_lock(&imx214->mutex); 727 ret = imx214_write_table(imx214, mode_table_common); 728 if (ret < 0) { 729 dev_err(imx214->dev, "could not sent common table %d\n", ret); 730 goto error; 731 } 732 733 mode = v4l2_find_nearest_size(imx214_modes, 734 ARRAY_SIZE(imx214_modes), width, height, 735 imx214->fmt.width, imx214->fmt.height); 736 ret = imx214_write_table(imx214, mode->reg_table); 737 if (ret < 0) { 738 dev_err(imx214->dev, "could not sent mode table %d\n", ret); 739 goto error; 740 } 741 ret = __v4l2_ctrl_handler_setup(&imx214->ctrls); 742 if (ret < 0) { 743 dev_err(imx214->dev, "could not sync v4l2 controls\n"); 744 goto error; 745 } 746 ret = regmap_write(imx214->regmap, 0x100, 1); 747 if (ret < 0) { 748 dev_err(imx214->dev, "could not sent start table %d\n", ret); 749 goto error; 750 } 751 752 mutex_unlock(&imx214->mutex); 753 return 0; 754 755 error: 756 mutex_unlock(&imx214->mutex); 757 return ret; 758 } 759 760 static int imx214_stop_streaming(struct imx214 *imx214) 761 { 762 int ret; 763 764 ret = regmap_write(imx214->regmap, 0x100, 0); 765 if (ret < 0) 766 dev_err(imx214->dev, "could not sent stop table %d\n", ret); 767 768 return ret; 769 } 770 771 static int imx214_s_stream(struct v4l2_subdev *subdev, int enable) 772 { 773 struct imx214 *imx214 = to_imx214(subdev); 774 int ret; 775 776 if (enable) { 777 ret = pm_runtime_resume_and_get(imx214->dev); 778 if (ret < 0) 779 return ret; 780 781 ret = imx214_start_streaming(imx214); 782 if (ret < 0) 783 goto err_rpm_put; 784 } else { 785 ret = imx214_stop_streaming(imx214); 786 if (ret < 0) 787 goto err_rpm_put; 788 pm_runtime_put(imx214->dev); 789 } 790 791 return 0; 792 793 err_rpm_put: 794 pm_runtime_put(imx214->dev); 795 return ret; 796 } 797 798 static int imx214_g_frame_interval(struct v4l2_subdev *subdev, 799 struct v4l2_subdev_frame_interval *fival) 800 { 801 fival->interval.numerator = 1; 802 fival->interval.denominator = IMX214_FPS; 803 804 return 0; 805 } 806 807 static int imx214_enum_frame_interval(struct v4l2_subdev *subdev, 808 struct v4l2_subdev_state *sd_state, 809 struct v4l2_subdev_frame_interval_enum *fie) 810 { 811 const struct imx214_mode *mode; 812 813 if (fie->index != 0) 814 return -EINVAL; 815 816 mode = v4l2_find_nearest_size(imx214_modes, 817 ARRAY_SIZE(imx214_modes), width, height, 818 fie->width, fie->height); 819 820 fie->code = IMX214_MBUS_CODE; 821 fie->width = mode->width; 822 fie->height = mode->height; 823 fie->interval.numerator = 1; 824 fie->interval.denominator = IMX214_FPS; 825 826 return 0; 827 } 828 829 static const struct v4l2_subdev_video_ops imx214_video_ops = { 830 .s_stream = imx214_s_stream, 831 .g_frame_interval = imx214_g_frame_interval, 832 .s_frame_interval = imx214_g_frame_interval, 833 }; 834 835 static const struct v4l2_subdev_pad_ops imx214_subdev_pad_ops = { 836 .enum_mbus_code = imx214_enum_mbus_code, 837 .enum_frame_size = imx214_enum_frame_size, 838 .enum_frame_interval = imx214_enum_frame_interval, 839 .get_fmt = imx214_get_format, 840 .set_fmt = imx214_set_format, 841 .get_selection = imx214_get_selection, 842 .init_cfg = imx214_entity_init_cfg, 843 }; 844 845 static const struct v4l2_subdev_ops imx214_subdev_ops = { 846 .core = &imx214_core_ops, 847 .video = &imx214_video_ops, 848 .pad = &imx214_subdev_pad_ops, 849 }; 850 851 static const struct regmap_config sensor_regmap_config = { 852 .reg_bits = 16, 853 .val_bits = 8, 854 .cache_type = REGCACHE_RBTREE, 855 }; 856 857 static int imx214_get_regulators(struct device *dev, struct imx214 *imx214) 858 { 859 unsigned int i; 860 861 for (i = 0; i < IMX214_NUM_SUPPLIES; i++) 862 imx214->supplies[i].supply = imx214_supply_name[i]; 863 864 return devm_regulator_bulk_get(dev, IMX214_NUM_SUPPLIES, 865 imx214->supplies); 866 } 867 868 static int imx214_parse_fwnode(struct device *dev) 869 { 870 struct fwnode_handle *endpoint; 871 struct v4l2_fwnode_endpoint bus_cfg = { 872 .bus_type = V4L2_MBUS_CSI2_DPHY, 873 }; 874 unsigned int i; 875 int ret; 876 877 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); 878 if (!endpoint) { 879 dev_err(dev, "endpoint node not found\n"); 880 return -EINVAL; 881 } 882 883 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg); 884 if (ret) { 885 dev_err(dev, "parsing endpoint node failed\n"); 886 goto done; 887 } 888 889 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) 890 if (bus_cfg.link_frequencies[i] == IMX214_DEFAULT_LINK_FREQ) 891 break; 892 893 if (i == bus_cfg.nr_of_link_frequencies) { 894 dev_err(dev, "link-frequencies %d not supported, Please review your DT\n", 895 IMX214_DEFAULT_LINK_FREQ); 896 ret = -EINVAL; 897 goto done; 898 } 899 900 done: 901 v4l2_fwnode_endpoint_free(&bus_cfg); 902 fwnode_handle_put(endpoint); 903 return ret; 904 } 905 906 static int imx214_probe(struct i2c_client *client) 907 { 908 struct device *dev = &client->dev; 909 struct imx214 *imx214; 910 static const s64 link_freq[] = { 911 IMX214_DEFAULT_LINK_FREQ, 912 }; 913 static const struct v4l2_area unit_size = { 914 .width = 1120, 915 .height = 1120, 916 }; 917 int ret; 918 919 ret = imx214_parse_fwnode(dev); 920 if (ret) 921 return ret; 922 923 imx214 = devm_kzalloc(dev, sizeof(*imx214), GFP_KERNEL); 924 if (!imx214) 925 return -ENOMEM; 926 927 imx214->dev = dev; 928 929 imx214->xclk = devm_clk_get(dev, NULL); 930 if (IS_ERR(imx214->xclk)) { 931 dev_err(dev, "could not get xclk"); 932 return PTR_ERR(imx214->xclk); 933 } 934 935 ret = clk_set_rate(imx214->xclk, IMX214_DEFAULT_CLK_FREQ); 936 if (ret) { 937 dev_err(dev, "could not set xclk frequency\n"); 938 return ret; 939 } 940 941 ret = imx214_get_regulators(dev, imx214); 942 if (ret < 0) { 943 dev_err(dev, "cannot get regulators\n"); 944 return ret; 945 } 946 947 imx214->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); 948 if (IS_ERR(imx214->enable_gpio)) { 949 dev_err(dev, "cannot get enable gpio\n"); 950 return PTR_ERR(imx214->enable_gpio); 951 } 952 953 imx214->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config); 954 if (IS_ERR(imx214->regmap)) { 955 dev_err(dev, "regmap init failed\n"); 956 return PTR_ERR(imx214->regmap); 957 } 958 959 v4l2_i2c_subdev_init(&imx214->sd, client, &imx214_subdev_ops); 960 961 /* 962 * Enable power initially, to avoid warnings 963 * from clk_disable on power_off 964 */ 965 imx214_power_on(imx214->dev); 966 967 pm_runtime_set_active(imx214->dev); 968 pm_runtime_enable(imx214->dev); 969 pm_runtime_idle(imx214->dev); 970 971 v4l2_ctrl_handler_init(&imx214->ctrls, 3); 972 973 imx214->pixel_rate = v4l2_ctrl_new_std(&imx214->ctrls, NULL, 974 V4L2_CID_PIXEL_RATE, 0, 975 IMX214_DEFAULT_PIXEL_RATE, 1, 976 IMX214_DEFAULT_PIXEL_RATE); 977 imx214->link_freq = v4l2_ctrl_new_int_menu(&imx214->ctrls, NULL, 978 V4L2_CID_LINK_FREQ, 979 ARRAY_SIZE(link_freq) - 1, 980 0, link_freq); 981 if (imx214->link_freq) 982 imx214->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 983 984 /* 985 * WARNING! 986 * Values obtained reverse engineering blobs and/or devices. 987 * Ranges and functionality might be wrong. 988 * 989 * Sony, please release some register set documentation for the 990 * device. 991 * 992 * Yours sincerely, Ricardo. 993 */ 994 imx214->exposure = v4l2_ctrl_new_std(&imx214->ctrls, &imx214_ctrl_ops, 995 V4L2_CID_EXPOSURE, 996 0, 3184, 1, 0x0c70); 997 998 imx214->unit_size = v4l2_ctrl_new_std_compound(&imx214->ctrls, 999 NULL, 1000 V4L2_CID_UNIT_CELL_SIZE, 1001 v4l2_ctrl_ptr_create((void *)&unit_size)); 1002 ret = imx214->ctrls.error; 1003 if (ret) { 1004 dev_err(&client->dev, "%s control init failed (%d)\n", 1005 __func__, ret); 1006 goto free_ctrl; 1007 } 1008 1009 imx214->sd.ctrl_handler = &imx214->ctrls; 1010 mutex_init(&imx214->mutex); 1011 imx214->ctrls.lock = &imx214->mutex; 1012 1013 imx214->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1014 imx214->pad.flags = MEDIA_PAD_FL_SOURCE; 1015 imx214->sd.dev = &client->dev; 1016 imx214->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1017 1018 ret = media_entity_pads_init(&imx214->sd.entity, 1, &imx214->pad); 1019 if (ret < 0) { 1020 dev_err(dev, "could not register media entity\n"); 1021 goto free_ctrl; 1022 } 1023 1024 imx214_entity_init_cfg(&imx214->sd, NULL); 1025 1026 ret = v4l2_async_register_subdev_sensor(&imx214->sd); 1027 if (ret < 0) { 1028 dev_err(dev, "could not register v4l2 device\n"); 1029 goto free_entity; 1030 } 1031 1032 return 0; 1033 1034 free_entity: 1035 media_entity_cleanup(&imx214->sd.entity); 1036 free_ctrl: 1037 mutex_destroy(&imx214->mutex); 1038 v4l2_ctrl_handler_free(&imx214->ctrls); 1039 pm_runtime_disable(imx214->dev); 1040 1041 return ret; 1042 } 1043 1044 static void imx214_remove(struct i2c_client *client) 1045 { 1046 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1047 struct imx214 *imx214 = to_imx214(sd); 1048 1049 v4l2_async_unregister_subdev(&imx214->sd); 1050 media_entity_cleanup(&imx214->sd.entity); 1051 v4l2_ctrl_handler_free(&imx214->ctrls); 1052 1053 pm_runtime_disable(&client->dev); 1054 pm_runtime_set_suspended(&client->dev); 1055 1056 mutex_destroy(&imx214->mutex); 1057 } 1058 1059 static const struct of_device_id imx214_of_match[] = { 1060 { .compatible = "sony,imx214" }, 1061 { } 1062 }; 1063 MODULE_DEVICE_TABLE(of, imx214_of_match); 1064 1065 static const struct dev_pm_ops imx214_pm_ops = { 1066 SET_RUNTIME_PM_OPS(imx214_power_off, imx214_power_on, NULL) 1067 }; 1068 1069 static struct i2c_driver imx214_i2c_driver = { 1070 .driver = { 1071 .of_match_table = imx214_of_match, 1072 .pm = &imx214_pm_ops, 1073 .name = "imx214", 1074 }, 1075 .probe = imx214_probe, 1076 .remove = imx214_remove, 1077 }; 1078 1079 module_i2c_driver(imx214_i2c_driver); 1080 1081 MODULE_DESCRIPTION("Sony IMX214 Camera driver"); 1082 MODULE_AUTHOR("Ricardo Ribalda <ribalda@kernel.org>"); 1083 MODULE_LICENSE("GPL v2"); 1084