1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2021 Intel Corporation 3 4 #include <linux/acpi.h> 5 #include <linux/delay.h> 6 #include <linux/i2c.h> 7 #include <linux/module.h> 8 #include <linux/pm_runtime.h> 9 #include <media/v4l2-ctrls.h> 10 #include <media/v4l2-device.h> 11 #include <asm/unaligned.h> 12 13 #define IMX208_REG_MODE_SELECT 0x0100 14 #define IMX208_MODE_STANDBY 0x00 15 #define IMX208_MODE_STREAMING 0x01 16 17 /* Chip ID */ 18 #define IMX208_REG_CHIP_ID 0x0000 19 #define IMX208_CHIP_ID 0x0208 20 21 /* V_TIMING internal */ 22 #define IMX208_REG_VTS 0x0340 23 #define IMX208_VTS_60FPS 0x0472 24 #define IMX208_VTS_BINNING 0x0239 25 #define IMX208_VTS_60FPS_MIN 0x0458 26 #define IMX208_VTS_BINNING_MIN 0x0230 27 #define IMX208_VTS_MAX 0xffff 28 29 /* HBLANK control - read only */ 30 #define IMX208_PPL_384MHZ 2248 31 #define IMX208_PPL_96MHZ 2248 32 33 /* Exposure control */ 34 #define IMX208_REG_EXPOSURE 0x0202 35 #define IMX208_EXPOSURE_MIN 4 36 #define IMX208_EXPOSURE_STEP 1 37 #define IMX208_EXPOSURE_DEFAULT 0x190 38 #define IMX208_EXPOSURE_MAX 65535 39 40 /* Analog gain control */ 41 #define IMX208_REG_ANALOG_GAIN 0x0204 42 #define IMX208_ANA_GAIN_MIN 0 43 #define IMX208_ANA_GAIN_MAX 0x00e0 44 #define IMX208_ANA_GAIN_STEP 1 45 #define IMX208_ANA_GAIN_DEFAULT 0x0 46 47 /* Digital gain control */ 48 #define IMX208_REG_GR_DIGITAL_GAIN 0x020e 49 #define IMX208_REG_R_DIGITAL_GAIN 0x0210 50 #define IMX208_REG_B_DIGITAL_GAIN 0x0212 51 #define IMX208_REG_GB_DIGITAL_GAIN 0x0214 52 #define IMX208_DIGITAL_GAIN_SHIFT 8 53 54 /* Orientation */ 55 #define IMX208_REG_ORIENTATION_CONTROL 0x0101 56 57 /* Test Pattern Control */ 58 #define IMX208_REG_TEST_PATTERN_MODE 0x0600 59 #define IMX208_TEST_PATTERN_DISABLE 0x0 60 #define IMX208_TEST_PATTERN_SOLID_COLOR 0x1 61 #define IMX208_TEST_PATTERN_COLOR_BARS 0x2 62 #define IMX208_TEST_PATTERN_GREY_COLOR 0x3 63 #define IMX208_TEST_PATTERN_PN9 0x4 64 #define IMX208_TEST_PATTERN_FIX_1 0x100 65 #define IMX208_TEST_PATTERN_FIX_2 0x101 66 #define IMX208_TEST_PATTERN_FIX_3 0x102 67 #define IMX208_TEST_PATTERN_FIX_4 0x103 68 #define IMX208_TEST_PATTERN_FIX_5 0x104 69 #define IMX208_TEST_PATTERN_FIX_6 0x105 70 71 /* OTP Access */ 72 #define IMX208_OTP_BASE 0x3500 73 #define IMX208_OTP_SIZE 40 74 75 struct imx208_reg { 76 u16 address; 77 u8 val; 78 }; 79 80 struct imx208_reg_list { 81 u32 num_of_regs; 82 const struct imx208_reg *regs; 83 }; 84 85 /* Link frequency config */ 86 struct imx208_link_freq_config { 87 u32 pixels_per_line; 88 89 /* PLL registers for this link frequency */ 90 struct imx208_reg_list reg_list; 91 }; 92 93 /* Mode : resolution and related config&values */ 94 struct imx208_mode { 95 /* Frame width */ 96 u32 width; 97 /* Frame height */ 98 u32 height; 99 100 /* V-timing */ 101 u32 vts_def; 102 u32 vts_min; 103 104 /* Index of Link frequency config to be used */ 105 u32 link_freq_index; 106 /* Default register values */ 107 struct imx208_reg_list reg_list; 108 }; 109 110 static const struct imx208_reg pll_ctrl_reg[] = { 111 {0x0305, 0x02}, 112 {0x0307, 0x50}, 113 {0x303C, 0x3C}, 114 }; 115 116 static const struct imx208_reg mode_1936x1096_60fps_regs[] = { 117 {0x0340, 0x04}, 118 {0x0341, 0x72}, 119 {0x0342, 0x04}, 120 {0x0343, 0x64}, 121 {0x034C, 0x07}, 122 {0x034D, 0x90}, 123 {0x034E, 0x04}, 124 {0x034F, 0x48}, 125 {0x0381, 0x01}, 126 {0x0383, 0x01}, 127 {0x0385, 0x01}, 128 {0x0387, 0x01}, 129 {0x3048, 0x00}, 130 {0x3050, 0x01}, 131 {0x30D5, 0x00}, 132 {0x3301, 0x00}, 133 {0x3318, 0x62}, 134 {0x0202, 0x01}, 135 {0x0203, 0x90}, 136 {0x0205, 0x00}, 137 }; 138 139 static const struct imx208_reg mode_968_548_60fps_regs[] = { 140 {0x0340, 0x02}, 141 {0x0341, 0x39}, 142 {0x0342, 0x08}, 143 {0x0343, 0xC8}, 144 {0x034C, 0x03}, 145 {0x034D, 0xC8}, 146 {0x034E, 0x02}, 147 {0x034F, 0x24}, 148 {0x0381, 0x01}, 149 {0x0383, 0x03}, 150 {0x0385, 0x01}, 151 {0x0387, 0x03}, 152 {0x3048, 0x01}, 153 {0x3050, 0x02}, 154 {0x30D5, 0x03}, 155 {0x3301, 0x10}, 156 {0x3318, 0x75}, 157 {0x0202, 0x01}, 158 {0x0203, 0x90}, 159 {0x0205, 0x00}, 160 }; 161 162 static const s64 imx208_discrete_digital_gain[] = { 163 1, 2, 4, 8, 16, 164 }; 165 166 static const char * const imx208_test_pattern_menu[] = { 167 "Disabled", 168 "Solid Color", 169 "100% Color Bar", 170 "Fade to Grey Color Bar", 171 "PN9", 172 "Fixed Pattern1", 173 "Fixed Pattern2", 174 "Fixed Pattern3", 175 "Fixed Pattern4", 176 "Fixed Pattern5", 177 "Fixed Pattern6" 178 }; 179 180 static const int imx208_test_pattern_val[] = { 181 IMX208_TEST_PATTERN_DISABLE, 182 IMX208_TEST_PATTERN_SOLID_COLOR, 183 IMX208_TEST_PATTERN_COLOR_BARS, 184 IMX208_TEST_PATTERN_GREY_COLOR, 185 IMX208_TEST_PATTERN_PN9, 186 IMX208_TEST_PATTERN_FIX_1, 187 IMX208_TEST_PATTERN_FIX_2, 188 IMX208_TEST_PATTERN_FIX_3, 189 IMX208_TEST_PATTERN_FIX_4, 190 IMX208_TEST_PATTERN_FIX_5, 191 IMX208_TEST_PATTERN_FIX_6, 192 }; 193 194 /* Configurations for supported link frequencies */ 195 #define IMX208_MHZ (1000 * 1000ULL) 196 #define IMX208_LINK_FREQ_384MHZ (384ULL * IMX208_MHZ) 197 #define IMX208_LINK_FREQ_96MHZ (96ULL * IMX208_MHZ) 198 199 #define IMX208_DATA_RATE_DOUBLE 2 200 #define IMX208_NUM_OF_LANES 2 201 #define IMX208_PIXEL_BITS 10 202 203 enum { 204 IMX208_LINK_FREQ_384MHZ_INDEX, 205 IMX208_LINK_FREQ_96MHZ_INDEX, 206 }; 207 208 /* 209 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample 210 * data rate => double data rate; number of lanes => 2; bits per pixel => 10 211 */ 212 static u64 link_freq_to_pixel_rate(u64 f) 213 { 214 f *= IMX208_DATA_RATE_DOUBLE * IMX208_NUM_OF_LANES; 215 do_div(f, IMX208_PIXEL_BITS); 216 217 return f; 218 } 219 220 /* Menu items for LINK_FREQ V4L2 control */ 221 static const s64 link_freq_menu_items[] = { 222 [IMX208_LINK_FREQ_384MHZ_INDEX] = IMX208_LINK_FREQ_384MHZ, 223 [IMX208_LINK_FREQ_96MHZ_INDEX] = IMX208_LINK_FREQ_96MHZ, 224 }; 225 226 /* Link frequency configs */ 227 static const struct imx208_link_freq_config link_freq_configs[] = { 228 [IMX208_LINK_FREQ_384MHZ_INDEX] = { 229 .pixels_per_line = IMX208_PPL_384MHZ, 230 .reg_list = { 231 .num_of_regs = ARRAY_SIZE(pll_ctrl_reg), 232 .regs = pll_ctrl_reg, 233 } 234 }, 235 [IMX208_LINK_FREQ_96MHZ_INDEX] = { 236 .pixels_per_line = IMX208_PPL_96MHZ, 237 .reg_list = { 238 .num_of_regs = ARRAY_SIZE(pll_ctrl_reg), 239 .regs = pll_ctrl_reg, 240 } 241 }, 242 }; 243 244 /* Mode configs */ 245 static const struct imx208_mode supported_modes[] = { 246 { 247 .width = 1936, 248 .height = 1096, 249 .vts_def = IMX208_VTS_60FPS, 250 .vts_min = IMX208_VTS_60FPS_MIN, 251 .reg_list = { 252 .num_of_regs = ARRAY_SIZE(mode_1936x1096_60fps_regs), 253 .regs = mode_1936x1096_60fps_regs, 254 }, 255 .link_freq_index = IMX208_LINK_FREQ_384MHZ_INDEX, 256 }, 257 { 258 .width = 968, 259 .height = 548, 260 .vts_def = IMX208_VTS_BINNING, 261 .vts_min = IMX208_VTS_BINNING_MIN, 262 .reg_list = { 263 .num_of_regs = ARRAY_SIZE(mode_968_548_60fps_regs), 264 .regs = mode_968_548_60fps_regs, 265 }, 266 .link_freq_index = IMX208_LINK_FREQ_96MHZ_INDEX, 267 }, 268 }; 269 270 struct imx208 { 271 struct v4l2_subdev sd; 272 struct media_pad pad; 273 274 struct v4l2_ctrl_handler ctrl_handler; 275 /* V4L2 Controls */ 276 struct v4l2_ctrl *link_freq; 277 struct v4l2_ctrl *pixel_rate; 278 struct v4l2_ctrl *vblank; 279 struct v4l2_ctrl *hblank; 280 struct v4l2_ctrl *vflip; 281 struct v4l2_ctrl *hflip; 282 283 /* Current mode */ 284 const struct imx208_mode *cur_mode; 285 286 /* 287 * Mutex for serialized access: 288 * Protect sensor set pad format and start/stop streaming safely. 289 * Protect access to sensor v4l2 controls. 290 */ 291 struct mutex imx208_mx; 292 293 /* Streaming on/off */ 294 bool streaming; 295 296 /* OTP data */ 297 bool otp_read; 298 char otp_data[IMX208_OTP_SIZE]; 299 300 /* True if the device has been identified */ 301 bool identified; 302 }; 303 304 static inline struct imx208 *to_imx208(struct v4l2_subdev *_sd) 305 { 306 return container_of(_sd, struct imx208, sd); 307 } 308 309 /* Get bayer order based on flip setting. */ 310 static u32 imx208_get_format_code(struct imx208 *imx208) 311 { 312 /* 313 * Only one bayer order is supported. 314 * It depends on the flip settings. 315 */ 316 static const u32 codes[2][2] = { 317 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, }, 318 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, }, 319 }; 320 321 return codes[imx208->vflip->val][imx208->hflip->val]; 322 } 323 324 /* Read registers up to 4 at a time */ 325 static int imx208_read_reg(struct imx208 *imx208, u16 reg, u32 len, u32 *val) 326 { 327 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 328 struct i2c_msg msgs[2]; 329 u8 addr_buf[2] = { reg >> 8, reg & 0xff }; 330 u8 data_buf[4] = { 0, }; 331 int ret; 332 333 if (len > 4) 334 return -EINVAL; 335 336 /* Write register address */ 337 msgs[0].addr = client->addr; 338 msgs[0].flags = 0; 339 msgs[0].len = ARRAY_SIZE(addr_buf); 340 msgs[0].buf = addr_buf; 341 342 /* Read data from register */ 343 msgs[1].addr = client->addr; 344 msgs[1].flags = I2C_M_RD; 345 msgs[1].len = len; 346 msgs[1].buf = &data_buf[4 - len]; 347 348 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 349 if (ret != ARRAY_SIZE(msgs)) 350 return -EIO; 351 352 *val = get_unaligned_be32(data_buf); 353 354 return 0; 355 } 356 357 /* Write registers up to 4 at a time */ 358 static int imx208_write_reg(struct imx208 *imx208, u16 reg, u32 len, u32 val) 359 { 360 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 361 u8 buf[6]; 362 363 if (len > 4) 364 return -EINVAL; 365 366 put_unaligned_be16(reg, buf); 367 put_unaligned_be32(val << (8 * (4 - len)), buf + 2); 368 if (i2c_master_send(client, buf, len + 2) != len + 2) 369 return -EIO; 370 371 return 0; 372 } 373 374 /* Write a list of registers */ 375 static int imx208_write_regs(struct imx208 *imx208, 376 const struct imx208_reg *regs, u32 len) 377 { 378 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 379 unsigned int i; 380 int ret; 381 382 for (i = 0; i < len; i++) { 383 ret = imx208_write_reg(imx208, regs[i].address, 1, 384 regs[i].val); 385 if (ret) { 386 dev_err_ratelimited(&client->dev, 387 "Failed to write reg 0x%4.4x. error = %d\n", 388 regs[i].address, ret); 389 390 return ret; 391 } 392 } 393 394 return 0; 395 } 396 397 /* Open sub-device */ 398 static int imx208_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 399 { 400 struct v4l2_mbus_framefmt *try_fmt = 401 v4l2_subdev_get_try_format(sd, fh->state, 0); 402 403 /* Initialize try_fmt */ 404 try_fmt->width = supported_modes[0].width; 405 try_fmt->height = supported_modes[0].height; 406 try_fmt->code = MEDIA_BUS_FMT_SRGGB10_1X10; 407 try_fmt->field = V4L2_FIELD_NONE; 408 409 return 0; 410 } 411 412 static int imx208_update_digital_gain(struct imx208 *imx208, u32 len, u32 val) 413 { 414 int ret; 415 416 val = imx208_discrete_digital_gain[val] << IMX208_DIGITAL_GAIN_SHIFT; 417 418 ret = imx208_write_reg(imx208, IMX208_REG_GR_DIGITAL_GAIN, 2, val); 419 if (ret) 420 return ret; 421 422 ret = imx208_write_reg(imx208, IMX208_REG_GB_DIGITAL_GAIN, 2, val); 423 if (ret) 424 return ret; 425 426 ret = imx208_write_reg(imx208, IMX208_REG_R_DIGITAL_GAIN, 2, val); 427 if (ret) 428 return ret; 429 430 return imx208_write_reg(imx208, IMX208_REG_B_DIGITAL_GAIN, 2, val); 431 } 432 433 static int imx208_set_ctrl(struct v4l2_ctrl *ctrl) 434 { 435 struct imx208 *imx208 = 436 container_of(ctrl->handler, struct imx208, ctrl_handler); 437 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 438 int ret; 439 440 /* 441 * Applying V4L2 control value only happens 442 * when power is up for streaming 443 */ 444 if (!pm_runtime_get_if_in_use(&client->dev)) 445 return 0; 446 447 switch (ctrl->id) { 448 case V4L2_CID_ANALOGUE_GAIN: 449 ret = imx208_write_reg(imx208, IMX208_REG_ANALOG_GAIN, 450 2, ctrl->val); 451 break; 452 case V4L2_CID_EXPOSURE: 453 ret = imx208_write_reg(imx208, IMX208_REG_EXPOSURE, 454 2, ctrl->val); 455 break; 456 case V4L2_CID_DIGITAL_GAIN: 457 ret = imx208_update_digital_gain(imx208, 2, ctrl->val); 458 break; 459 case V4L2_CID_VBLANK: 460 /* Update VTS that meets expected vertical blanking */ 461 ret = imx208_write_reg(imx208, IMX208_REG_VTS, 2, 462 imx208->cur_mode->height + ctrl->val); 463 break; 464 case V4L2_CID_TEST_PATTERN: 465 ret = imx208_write_reg(imx208, IMX208_REG_TEST_PATTERN_MODE, 466 2, imx208_test_pattern_val[ctrl->val]); 467 break; 468 case V4L2_CID_HFLIP: 469 case V4L2_CID_VFLIP: 470 ret = imx208_write_reg(imx208, IMX208_REG_ORIENTATION_CONTROL, 471 1, 472 imx208->hflip->val | 473 imx208->vflip->val << 1); 474 break; 475 default: 476 ret = -EINVAL; 477 dev_err(&client->dev, 478 "ctrl(id:0x%x,val:0x%x) is not handled\n", 479 ctrl->id, ctrl->val); 480 break; 481 } 482 483 pm_runtime_put(&client->dev); 484 485 return ret; 486 } 487 488 static const struct v4l2_ctrl_ops imx208_ctrl_ops = { 489 .s_ctrl = imx208_set_ctrl, 490 }; 491 492 static const struct v4l2_ctrl_config imx208_digital_gain_control = { 493 .ops = &imx208_ctrl_ops, 494 .id = V4L2_CID_DIGITAL_GAIN, 495 .name = "Digital Gain", 496 .type = V4L2_CTRL_TYPE_INTEGER_MENU, 497 .min = 0, 498 .max = ARRAY_SIZE(imx208_discrete_digital_gain) - 1, 499 .step = 0, 500 .def = 0, 501 .menu_skip_mask = 0, 502 .qmenu_int = imx208_discrete_digital_gain, 503 }; 504 505 static int imx208_enum_mbus_code(struct v4l2_subdev *sd, 506 struct v4l2_subdev_state *sd_state, 507 struct v4l2_subdev_mbus_code_enum *code) 508 { 509 struct imx208 *imx208 = to_imx208(sd); 510 511 if (code->index > 0) 512 return -EINVAL; 513 514 code->code = imx208_get_format_code(imx208); 515 516 return 0; 517 } 518 519 static int imx208_enum_frame_size(struct v4l2_subdev *sd, 520 struct v4l2_subdev_state *sd_state, 521 struct v4l2_subdev_frame_size_enum *fse) 522 { 523 struct imx208 *imx208 = to_imx208(sd); 524 525 if (fse->index >= ARRAY_SIZE(supported_modes)) 526 return -EINVAL; 527 528 if (fse->code != imx208_get_format_code(imx208)) 529 return -EINVAL; 530 531 fse->min_width = supported_modes[fse->index].width; 532 fse->max_width = fse->min_width; 533 fse->min_height = supported_modes[fse->index].height; 534 fse->max_height = fse->min_height; 535 536 return 0; 537 } 538 539 static void imx208_mode_to_pad_format(struct imx208 *imx208, 540 const struct imx208_mode *mode, 541 struct v4l2_subdev_format *fmt) 542 { 543 fmt->format.width = mode->width; 544 fmt->format.height = mode->height; 545 fmt->format.code = imx208_get_format_code(imx208); 546 fmt->format.field = V4L2_FIELD_NONE; 547 } 548 549 static int __imx208_get_pad_format(struct imx208 *imx208, 550 struct v4l2_subdev_state *sd_state, 551 struct v4l2_subdev_format *fmt) 552 { 553 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) 554 fmt->format = *v4l2_subdev_get_try_format(&imx208->sd, 555 sd_state, 556 fmt->pad); 557 else 558 imx208_mode_to_pad_format(imx208, imx208->cur_mode, fmt); 559 560 return 0; 561 } 562 563 static int imx208_get_pad_format(struct v4l2_subdev *sd, 564 struct v4l2_subdev_state *sd_state, 565 struct v4l2_subdev_format *fmt) 566 { 567 struct imx208 *imx208 = to_imx208(sd); 568 int ret; 569 570 mutex_lock(&imx208->imx208_mx); 571 ret = __imx208_get_pad_format(imx208, sd_state, fmt); 572 mutex_unlock(&imx208->imx208_mx); 573 574 return ret; 575 } 576 577 static int imx208_set_pad_format(struct v4l2_subdev *sd, 578 struct v4l2_subdev_state *sd_state, 579 struct v4l2_subdev_format *fmt) 580 { 581 struct imx208 *imx208 = to_imx208(sd); 582 const struct imx208_mode *mode; 583 s32 vblank_def; 584 s32 vblank_min; 585 s64 h_blank; 586 s64 pixel_rate; 587 s64 link_freq; 588 589 mutex_lock(&imx208->imx208_mx); 590 591 fmt->format.code = imx208_get_format_code(imx208); 592 mode = v4l2_find_nearest_size(supported_modes, 593 ARRAY_SIZE(supported_modes), width, height, 594 fmt->format.width, fmt->format.height); 595 imx208_mode_to_pad_format(imx208, mode, fmt); 596 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 597 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format; 598 } else { 599 imx208->cur_mode = mode; 600 __v4l2_ctrl_s_ctrl(imx208->link_freq, mode->link_freq_index); 601 link_freq = link_freq_menu_items[mode->link_freq_index]; 602 pixel_rate = link_freq_to_pixel_rate(link_freq); 603 __v4l2_ctrl_s_ctrl_int64(imx208->pixel_rate, pixel_rate); 604 /* Update limits and set FPS to default */ 605 vblank_def = imx208->cur_mode->vts_def - 606 imx208->cur_mode->height; 607 vblank_min = imx208->cur_mode->vts_min - 608 imx208->cur_mode->height; 609 __v4l2_ctrl_modify_range(imx208->vblank, vblank_min, 610 IMX208_VTS_MAX - imx208->cur_mode->height, 611 1, vblank_def); 612 __v4l2_ctrl_s_ctrl(imx208->vblank, vblank_def); 613 h_blank = 614 link_freq_configs[mode->link_freq_index].pixels_per_line 615 - imx208->cur_mode->width; 616 __v4l2_ctrl_modify_range(imx208->hblank, h_blank, 617 h_blank, 1, h_blank); 618 } 619 620 mutex_unlock(&imx208->imx208_mx); 621 622 return 0; 623 } 624 625 static int imx208_identify_module(struct imx208 *imx208) 626 { 627 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 628 int ret; 629 u32 val; 630 631 if (imx208->identified) 632 return 0; 633 634 ret = imx208_read_reg(imx208, IMX208_REG_CHIP_ID, 635 2, &val); 636 if (ret) { 637 dev_err(&client->dev, "failed to read chip id %x\n", 638 IMX208_CHIP_ID); 639 return ret; 640 } 641 642 if (val != IMX208_CHIP_ID) { 643 dev_err(&client->dev, "chip id mismatch: %x!=%x\n", 644 IMX208_CHIP_ID, val); 645 return -EIO; 646 } 647 648 imx208->identified = true; 649 650 return 0; 651 } 652 653 /* Start streaming */ 654 static int imx208_start_streaming(struct imx208 *imx208) 655 { 656 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 657 const struct imx208_reg_list *reg_list; 658 int ret, link_freq_index; 659 660 ret = imx208_identify_module(imx208); 661 if (ret) 662 return ret; 663 664 /* Setup PLL */ 665 link_freq_index = imx208->cur_mode->link_freq_index; 666 reg_list = &link_freq_configs[link_freq_index].reg_list; 667 ret = imx208_write_regs(imx208, reg_list->regs, reg_list->num_of_regs); 668 if (ret) { 669 dev_err(&client->dev, "%s failed to set plls\n", __func__); 670 return ret; 671 } 672 673 /* Apply default values of current mode */ 674 reg_list = &imx208->cur_mode->reg_list; 675 ret = imx208_write_regs(imx208, reg_list->regs, reg_list->num_of_regs); 676 if (ret) { 677 dev_err(&client->dev, "%s failed to set mode\n", __func__); 678 return ret; 679 } 680 681 /* Apply customized values from user */ 682 ret = __v4l2_ctrl_handler_setup(imx208->sd.ctrl_handler); 683 if (ret) 684 return ret; 685 686 /* set stream on register */ 687 return imx208_write_reg(imx208, IMX208_REG_MODE_SELECT, 688 1, IMX208_MODE_STREAMING); 689 } 690 691 /* Stop streaming */ 692 static int imx208_stop_streaming(struct imx208 *imx208) 693 { 694 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 695 int ret; 696 697 /* set stream off register */ 698 ret = imx208_write_reg(imx208, IMX208_REG_MODE_SELECT, 699 1, IMX208_MODE_STANDBY); 700 if (ret) 701 dev_err(&client->dev, "%s failed to set stream\n", __func__); 702 703 /* 704 * Return success even if it was an error, as there is nothing the 705 * caller can do about it. 706 */ 707 return 0; 708 } 709 710 static int imx208_set_stream(struct v4l2_subdev *sd, int enable) 711 { 712 struct imx208 *imx208 = to_imx208(sd); 713 struct i2c_client *client = v4l2_get_subdevdata(sd); 714 int ret = 0; 715 716 mutex_lock(&imx208->imx208_mx); 717 if (imx208->streaming == enable) { 718 mutex_unlock(&imx208->imx208_mx); 719 return 0; 720 } 721 722 if (enable) { 723 ret = pm_runtime_get_sync(&client->dev); 724 if (ret < 0) 725 goto err_rpm_put; 726 727 /* 728 * Apply default & customized values 729 * and then start streaming. 730 */ 731 ret = imx208_start_streaming(imx208); 732 if (ret) 733 goto err_rpm_put; 734 } else { 735 imx208_stop_streaming(imx208); 736 pm_runtime_put(&client->dev); 737 } 738 739 imx208->streaming = enable; 740 mutex_unlock(&imx208->imx208_mx); 741 742 /* vflip and hflip cannot change during streaming */ 743 v4l2_ctrl_grab(imx208->vflip, enable); 744 v4l2_ctrl_grab(imx208->hflip, enable); 745 746 return ret; 747 748 err_rpm_put: 749 pm_runtime_put(&client->dev); 750 mutex_unlock(&imx208->imx208_mx); 751 752 return ret; 753 } 754 755 static int __maybe_unused imx208_suspend(struct device *dev) 756 { 757 struct i2c_client *client = to_i2c_client(dev); 758 struct v4l2_subdev *sd = i2c_get_clientdata(client); 759 struct imx208 *imx208 = to_imx208(sd); 760 761 if (imx208->streaming) 762 imx208_stop_streaming(imx208); 763 764 return 0; 765 } 766 767 static int __maybe_unused imx208_resume(struct device *dev) 768 { 769 struct i2c_client *client = to_i2c_client(dev); 770 struct v4l2_subdev *sd = i2c_get_clientdata(client); 771 struct imx208 *imx208 = to_imx208(sd); 772 int ret; 773 774 if (imx208->streaming) { 775 ret = imx208_start_streaming(imx208); 776 if (ret) 777 goto error; 778 } 779 780 return 0; 781 782 error: 783 imx208_stop_streaming(imx208); 784 imx208->streaming = 0; 785 786 return ret; 787 } 788 789 /* Verify chip ID */ 790 static const struct v4l2_subdev_video_ops imx208_video_ops = { 791 .s_stream = imx208_set_stream, 792 }; 793 794 static const struct v4l2_subdev_pad_ops imx208_pad_ops = { 795 .enum_mbus_code = imx208_enum_mbus_code, 796 .get_fmt = imx208_get_pad_format, 797 .set_fmt = imx208_set_pad_format, 798 .enum_frame_size = imx208_enum_frame_size, 799 }; 800 801 static const struct v4l2_subdev_ops imx208_subdev_ops = { 802 .video = &imx208_video_ops, 803 .pad = &imx208_pad_ops, 804 }; 805 806 static const struct v4l2_subdev_internal_ops imx208_internal_ops = { 807 .open = imx208_open, 808 }; 809 810 static int imx208_read_otp(struct imx208 *imx208) 811 { 812 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 813 struct i2c_msg msgs[2]; 814 u8 addr_buf[2] = { IMX208_OTP_BASE >> 8, IMX208_OTP_BASE & 0xff }; 815 int ret = 0; 816 817 mutex_lock(&imx208->imx208_mx); 818 819 if (imx208->otp_read) 820 goto out_unlock; 821 822 ret = pm_runtime_get_sync(&client->dev); 823 if (ret < 0) { 824 pm_runtime_put_noidle(&client->dev); 825 goto out_unlock; 826 } 827 828 ret = imx208_identify_module(imx208); 829 if (ret) 830 goto out_pm_put; 831 832 /* Write register address */ 833 msgs[0].addr = client->addr; 834 msgs[0].flags = 0; 835 msgs[0].len = ARRAY_SIZE(addr_buf); 836 msgs[0].buf = addr_buf; 837 838 /* Read data from registers */ 839 msgs[1].addr = client->addr; 840 msgs[1].flags = I2C_M_RD; 841 msgs[1].len = sizeof(imx208->otp_data); 842 msgs[1].buf = imx208->otp_data; 843 844 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 845 if (ret == ARRAY_SIZE(msgs)) { 846 imx208->otp_read = true; 847 ret = 0; 848 } 849 850 out_pm_put: 851 pm_runtime_put(&client->dev); 852 853 out_unlock: 854 mutex_unlock(&imx208->imx208_mx); 855 856 return ret; 857 } 858 859 static ssize_t otp_read(struct file *filp, struct kobject *kobj, 860 struct bin_attribute *bin_attr, 861 char *buf, loff_t off, size_t count) 862 { 863 struct i2c_client *client = to_i2c_client(kobj_to_dev(kobj)); 864 struct v4l2_subdev *sd = i2c_get_clientdata(client); 865 struct imx208 *imx208 = to_imx208(sd); 866 int ret; 867 868 ret = imx208_read_otp(imx208); 869 if (ret) 870 return ret; 871 872 memcpy(buf, &imx208->otp_data[off], count); 873 return count; 874 } 875 876 static const BIN_ATTR_RO(otp, IMX208_OTP_SIZE); 877 878 /* Initialize control handlers */ 879 static int imx208_init_controls(struct imx208 *imx208) 880 { 881 struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); 882 struct v4l2_ctrl_handler *ctrl_hdlr = &imx208->ctrl_handler; 883 s64 exposure_max; 884 s64 vblank_def; 885 s64 vblank_min; 886 s64 pixel_rate_min; 887 s64 pixel_rate_max; 888 int ret; 889 890 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8); 891 if (ret) 892 return ret; 893 894 mutex_init(&imx208->imx208_mx); 895 ctrl_hdlr->lock = &imx208->imx208_mx; 896 imx208->link_freq = 897 v4l2_ctrl_new_int_menu(ctrl_hdlr, 898 &imx208_ctrl_ops, 899 V4L2_CID_LINK_FREQ, 900 ARRAY_SIZE(link_freq_menu_items) - 1, 901 0, link_freq_menu_items); 902 903 if (imx208->link_freq) 904 imx208->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 905 906 pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]); 907 pixel_rate_min = 908 link_freq_to_pixel_rate(link_freq_menu_items[ARRAY_SIZE(link_freq_menu_items) - 1]); 909 /* By default, PIXEL_RATE is read only */ 910 imx208->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx208_ctrl_ops, 911 V4L2_CID_PIXEL_RATE, 912 pixel_rate_min, pixel_rate_max, 913 1, pixel_rate_max); 914 915 vblank_def = imx208->cur_mode->vts_def - imx208->cur_mode->height; 916 vblank_min = imx208->cur_mode->vts_min - imx208->cur_mode->height; 917 imx208->vblank = 918 v4l2_ctrl_new_std(ctrl_hdlr, &imx208_ctrl_ops, V4L2_CID_VBLANK, 919 vblank_min, 920 IMX208_VTS_MAX - imx208->cur_mode->height, 1, 921 vblank_def); 922 923 imx208->hblank = 924 v4l2_ctrl_new_std(ctrl_hdlr, &imx208_ctrl_ops, V4L2_CID_HBLANK, 925 IMX208_PPL_384MHZ - imx208->cur_mode->width, 926 IMX208_PPL_384MHZ - imx208->cur_mode->width, 927 1, 928 IMX208_PPL_384MHZ - imx208->cur_mode->width); 929 930 if (imx208->hblank) 931 imx208->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; 932 933 exposure_max = imx208->cur_mode->vts_def - 8; 934 v4l2_ctrl_new_std(ctrl_hdlr, &imx208_ctrl_ops, V4L2_CID_EXPOSURE, 935 IMX208_EXPOSURE_MIN, exposure_max, 936 IMX208_EXPOSURE_STEP, IMX208_EXPOSURE_DEFAULT); 937 938 imx208->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx208_ctrl_ops, 939 V4L2_CID_HFLIP, 0, 1, 1, 0); 940 if (imx208->hflip) 941 imx208->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 942 imx208->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx208_ctrl_ops, 943 V4L2_CID_VFLIP, 0, 1, 1, 0); 944 if (imx208->vflip) 945 imx208->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 946 947 v4l2_ctrl_new_std(ctrl_hdlr, &imx208_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, 948 IMX208_ANA_GAIN_MIN, IMX208_ANA_GAIN_MAX, 949 IMX208_ANA_GAIN_STEP, IMX208_ANA_GAIN_DEFAULT); 950 951 v4l2_ctrl_new_custom(ctrl_hdlr, &imx208_digital_gain_control, NULL); 952 953 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx208_ctrl_ops, 954 V4L2_CID_TEST_PATTERN, 955 ARRAY_SIZE(imx208_test_pattern_menu) - 1, 956 0, 0, imx208_test_pattern_menu); 957 958 if (ctrl_hdlr->error) { 959 ret = ctrl_hdlr->error; 960 dev_err(&client->dev, "%s control init failed (%d)\n", 961 __func__, ret); 962 goto error; 963 } 964 965 imx208->sd.ctrl_handler = ctrl_hdlr; 966 967 return 0; 968 969 error: 970 v4l2_ctrl_handler_free(ctrl_hdlr); 971 mutex_destroy(&imx208->imx208_mx); 972 973 return ret; 974 } 975 976 static void imx208_free_controls(struct imx208 *imx208) 977 { 978 v4l2_ctrl_handler_free(imx208->sd.ctrl_handler); 979 } 980 981 static int imx208_probe(struct i2c_client *client) 982 { 983 struct imx208 *imx208; 984 int ret; 985 bool full_power; 986 u32 val = 0; 987 988 device_property_read_u32(&client->dev, "clock-frequency", &val); 989 if (val != 19200000) { 990 dev_err(&client->dev, 991 "Unsupported clock-frequency %u. Expected 19200000.\n", 992 val); 993 return -EINVAL; 994 } 995 996 imx208 = devm_kzalloc(&client->dev, sizeof(*imx208), GFP_KERNEL); 997 if (!imx208) 998 return -ENOMEM; 999 1000 /* Initialize subdev */ 1001 v4l2_i2c_subdev_init(&imx208->sd, client, &imx208_subdev_ops); 1002 1003 full_power = acpi_dev_state_d0(&client->dev); 1004 if (full_power) { 1005 /* Check module identity */ 1006 ret = imx208_identify_module(imx208); 1007 if (ret) { 1008 dev_err(&client->dev, "failed to find sensor: %d", ret); 1009 goto error_probe; 1010 } 1011 } 1012 1013 /* Set default mode to max resolution */ 1014 imx208->cur_mode = &supported_modes[0]; 1015 1016 ret = imx208_init_controls(imx208); 1017 if (ret) { 1018 dev_err(&client->dev, "failed to init controls: %d", ret); 1019 goto error_probe; 1020 } 1021 1022 /* Initialize subdev */ 1023 imx208->sd.internal_ops = &imx208_internal_ops; 1024 imx208->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1025 imx208->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1026 1027 /* Initialize source pad */ 1028 imx208->pad.flags = MEDIA_PAD_FL_SOURCE; 1029 ret = media_entity_pads_init(&imx208->sd.entity, 1, &imx208->pad); 1030 if (ret) { 1031 dev_err(&client->dev, "%s failed:%d\n", __func__, ret); 1032 goto error_handler_free; 1033 } 1034 1035 ret = v4l2_async_register_subdev_sensor(&imx208->sd); 1036 if (ret < 0) 1037 goto error_media_entity; 1038 1039 ret = device_create_bin_file(&client->dev, &bin_attr_otp); 1040 if (ret) { 1041 dev_err(&client->dev, "sysfs otp creation failed\n"); 1042 goto error_async_subdev; 1043 } 1044 1045 /* Set the device's state to active if it's in D0 state. */ 1046 if (full_power) 1047 pm_runtime_set_active(&client->dev); 1048 pm_runtime_enable(&client->dev); 1049 pm_runtime_idle(&client->dev); 1050 1051 return 0; 1052 1053 error_async_subdev: 1054 v4l2_async_unregister_subdev(&imx208->sd); 1055 1056 error_media_entity: 1057 media_entity_cleanup(&imx208->sd.entity); 1058 1059 error_handler_free: 1060 imx208_free_controls(imx208); 1061 1062 error_probe: 1063 mutex_destroy(&imx208->imx208_mx); 1064 1065 return ret; 1066 } 1067 1068 static void imx208_remove(struct i2c_client *client) 1069 { 1070 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1071 struct imx208 *imx208 = to_imx208(sd); 1072 1073 device_remove_bin_file(&client->dev, &bin_attr_otp); 1074 v4l2_async_unregister_subdev(sd); 1075 media_entity_cleanup(&sd->entity); 1076 imx208_free_controls(imx208); 1077 1078 pm_runtime_disable(&client->dev); 1079 pm_runtime_set_suspended(&client->dev); 1080 1081 mutex_destroy(&imx208->imx208_mx); 1082 } 1083 1084 static const struct dev_pm_ops imx208_pm_ops = { 1085 SET_SYSTEM_SLEEP_PM_OPS(imx208_suspend, imx208_resume) 1086 }; 1087 1088 #ifdef CONFIG_ACPI 1089 static const struct acpi_device_id imx208_acpi_ids[] = { 1090 { "INT3478" }, 1091 { /* sentinel */ } 1092 }; 1093 1094 MODULE_DEVICE_TABLE(acpi, imx208_acpi_ids); 1095 #endif 1096 1097 static struct i2c_driver imx208_i2c_driver = { 1098 .driver = { 1099 .name = "imx208", 1100 .pm = &imx208_pm_ops, 1101 .acpi_match_table = ACPI_PTR(imx208_acpi_ids), 1102 }, 1103 .probe = imx208_probe, 1104 .remove = imx208_remove, 1105 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE, 1106 }; 1107 1108 module_i2c_driver(imx208_i2c_driver); 1109 1110 MODULE_AUTHOR("Yeh, Andy <andy.yeh@intel.com>"); 1111 MODULE_AUTHOR("Chen, Ping-chung <ping-chung.chen@intel.com>"); 1112 MODULE_AUTHOR("Shawn Tu <shawnx.tu@intel.com>"); 1113 MODULE_DESCRIPTION("Sony IMX208 sensor driver"); 1114 MODULE_LICENSE("GPL v2"); 1115