1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Sony imx335 Camera Sensor Driver 4 * 5 * Copyright (C) 2021 Intel Corporation 6 */ 7 #include <asm/unaligned.h> 8 9 #include <linux/clk.h> 10 #include <linux/delay.h> 11 #include <linux/i2c.h> 12 #include <linux/module.h> 13 #include <linux/pm_runtime.h> 14 15 #include <media/v4l2-ctrls.h> 16 #include <media/v4l2-fwnode.h> 17 #include <media/v4l2-subdev.h> 18 19 /* Streaming Mode */ 20 #define IMX335_REG_MODE_SELECT 0x3000 21 #define IMX335_MODE_STANDBY 0x01 22 #define IMX335_MODE_STREAMING 0x00 23 24 /* Lines per frame */ 25 #define IMX335_REG_LPFR 0x3030 26 27 /* Chip ID */ 28 #define IMX335_REG_ID 0x3912 29 #define IMX335_ID 0x00 30 31 /* Exposure control */ 32 #define IMX335_REG_SHUTTER 0x3058 33 #define IMX335_EXPOSURE_MIN 1 34 #define IMX335_EXPOSURE_OFFSET 9 35 #define IMX335_EXPOSURE_STEP 1 36 #define IMX335_EXPOSURE_DEFAULT 0x0648 37 38 /* Analog gain control */ 39 #define IMX335_REG_AGAIN 0x30e8 40 #define IMX335_AGAIN_MIN 0 41 #define IMX335_AGAIN_MAX 240 42 #define IMX335_AGAIN_STEP 1 43 #define IMX335_AGAIN_DEFAULT 0 44 45 /* Group hold register */ 46 #define IMX335_REG_HOLD 0x3001 47 48 /* Input clock rate */ 49 #define IMX335_INCLK_RATE 24000000 50 51 /* CSI2 HW configuration */ 52 #define IMX335_LINK_FREQ 594000000 53 #define IMX335_NUM_DATA_LANES 4 54 55 #define IMX335_REG_MIN 0x00 56 #define IMX335_REG_MAX 0xfffff 57 58 /** 59 * struct imx335_reg - imx335 sensor register 60 * @address: Register address 61 * @val: Register value 62 */ 63 struct imx335_reg { 64 u16 address; 65 u8 val; 66 }; 67 68 /** 69 * struct imx335_reg_list - imx335 sensor register list 70 * @num_of_regs: Number of registers in the list 71 * @regs: Pointer to register list 72 */ 73 struct imx335_reg_list { 74 u32 num_of_regs; 75 const struct imx335_reg *regs; 76 }; 77 78 /** 79 * struct imx335_mode - imx335 sensor mode structure 80 * @width: Frame width 81 * @height: Frame height 82 * @code: Format code 83 * @hblank: Horizontal blanking in lines 84 * @vblank: Vertical blanking in lines 85 * @vblank_min: Minimum vertical blanking in lines 86 * @vblank_max: Maximum vertical blanking in lines 87 * @pclk: Sensor pixel clock 88 * @link_freq_idx: Link frequency index 89 * @reg_list: Register list for sensor mode 90 */ 91 struct imx335_mode { 92 u32 width; 93 u32 height; 94 u32 code; 95 u32 hblank; 96 u32 vblank; 97 u32 vblank_min; 98 u32 vblank_max; 99 u64 pclk; 100 u32 link_freq_idx; 101 struct imx335_reg_list reg_list; 102 }; 103 104 /** 105 * struct imx335 - imx335 sensor device structure 106 * @dev: Pointer to generic device 107 * @client: Pointer to i2c client 108 * @sd: V4L2 sub-device 109 * @pad: Media pad. Only one pad supported 110 * @reset_gpio: Sensor reset gpio 111 * @inclk: Sensor input clock 112 * @ctrl_handler: V4L2 control handler 113 * @link_freq_ctrl: Pointer to link frequency control 114 * @pclk_ctrl: Pointer to pixel clock control 115 * @hblank_ctrl: Pointer to horizontal blanking control 116 * @vblank_ctrl: Pointer to vertical blanking control 117 * @exp_ctrl: Pointer to exposure control 118 * @again_ctrl: Pointer to analog gain control 119 * @vblank: Vertical blanking in lines 120 * @cur_mode: Pointer to current selected sensor mode 121 * @mutex: Mutex for serializing sensor controls 122 */ 123 struct imx335 { 124 struct device *dev; 125 struct i2c_client *client; 126 struct v4l2_subdev sd; 127 struct media_pad pad; 128 struct gpio_desc *reset_gpio; 129 struct clk *inclk; 130 struct v4l2_ctrl_handler ctrl_handler; 131 struct v4l2_ctrl *link_freq_ctrl; 132 struct v4l2_ctrl *pclk_ctrl; 133 struct v4l2_ctrl *hblank_ctrl; 134 struct v4l2_ctrl *vblank_ctrl; 135 struct { 136 struct v4l2_ctrl *exp_ctrl; 137 struct v4l2_ctrl *again_ctrl; 138 }; 139 u32 vblank; 140 const struct imx335_mode *cur_mode; 141 struct mutex mutex; 142 }; 143 144 static const s64 link_freq[] = { 145 IMX335_LINK_FREQ, 146 }; 147 148 /* Sensor mode registers */ 149 static const struct imx335_reg mode_2592x1940_regs[] = { 150 {0x3000, 0x01}, 151 {0x3002, 0x00}, 152 {0x300c, 0x3b}, 153 {0x300d, 0x2a}, 154 {0x3018, 0x04}, 155 {0x302c, 0x3c}, 156 {0x302e, 0x20}, 157 {0x3056, 0x94}, 158 {0x3074, 0xc8}, 159 {0x3076, 0x28}, 160 {0x304c, 0x00}, 161 {0x314c, 0xc6}, 162 {0x315a, 0x02}, 163 {0x3168, 0xa0}, 164 {0x316a, 0x7e}, 165 {0x31a1, 0x00}, 166 {0x3288, 0x21}, 167 {0x328a, 0x02}, 168 {0x3414, 0x05}, 169 {0x3416, 0x18}, 170 {0x3648, 0x01}, 171 {0x364a, 0x04}, 172 {0x364c, 0x04}, 173 {0x3678, 0x01}, 174 {0x367c, 0x31}, 175 {0x367e, 0x31}, 176 {0x3706, 0x10}, 177 {0x3708, 0x03}, 178 {0x3714, 0x02}, 179 {0x3715, 0x02}, 180 {0x3716, 0x01}, 181 {0x3717, 0x03}, 182 {0x371c, 0x3d}, 183 {0x371d, 0x3f}, 184 {0x372c, 0x00}, 185 {0x372d, 0x00}, 186 {0x372e, 0x46}, 187 {0x372f, 0x00}, 188 {0x3730, 0x89}, 189 {0x3731, 0x00}, 190 {0x3732, 0x08}, 191 {0x3733, 0x01}, 192 {0x3734, 0xfe}, 193 {0x3735, 0x05}, 194 {0x3740, 0x02}, 195 {0x375d, 0x00}, 196 {0x375e, 0x00}, 197 {0x375f, 0x11}, 198 {0x3760, 0x01}, 199 {0x3768, 0x1b}, 200 {0x3769, 0x1b}, 201 {0x376a, 0x1b}, 202 {0x376b, 0x1b}, 203 {0x376c, 0x1a}, 204 {0x376d, 0x17}, 205 {0x376e, 0x0f}, 206 {0x3776, 0x00}, 207 {0x3777, 0x00}, 208 {0x3778, 0x46}, 209 {0x3779, 0x00}, 210 {0x377a, 0x89}, 211 {0x377b, 0x00}, 212 {0x377c, 0x08}, 213 {0x377d, 0x01}, 214 {0x377e, 0x23}, 215 {0x377f, 0x02}, 216 {0x3780, 0xd9}, 217 {0x3781, 0x03}, 218 {0x3782, 0xf5}, 219 {0x3783, 0x06}, 220 {0x3784, 0xa5}, 221 {0x3788, 0x0f}, 222 {0x378a, 0xd9}, 223 {0x378b, 0x03}, 224 {0x378c, 0xeb}, 225 {0x378d, 0x05}, 226 {0x378e, 0x87}, 227 {0x378f, 0x06}, 228 {0x3790, 0xf5}, 229 {0x3792, 0x43}, 230 {0x3794, 0x7a}, 231 {0x3796, 0xa1}, 232 {0x37b0, 0x36}, 233 {0x3a00, 0x01}, 234 }; 235 236 /* Supported sensor mode configurations */ 237 static const struct imx335_mode supported_mode = { 238 .width = 2592, 239 .height = 1940, 240 .hblank = 342, 241 .vblank = 2560, 242 .vblank_min = 2560, 243 .vblank_max = 133060, 244 .pclk = 396000000, 245 .link_freq_idx = 0, 246 .code = MEDIA_BUS_FMT_SRGGB12_1X12, 247 .reg_list = { 248 .num_of_regs = ARRAY_SIZE(mode_2592x1940_regs), 249 .regs = mode_2592x1940_regs, 250 }, 251 }; 252 253 /** 254 * to_imx335() - imx335 V4L2 sub-device to imx335 device. 255 * @subdev: pointer to imx335 V4L2 sub-device 256 * 257 * Return: pointer to imx335 device 258 */ 259 static inline struct imx335 *to_imx335(struct v4l2_subdev *subdev) 260 { 261 return container_of(subdev, struct imx335, sd); 262 } 263 264 /** 265 * imx335_read_reg() - Read registers. 266 * @imx335: pointer to imx335 device 267 * @reg: register address 268 * @len: length of bytes to read. Max supported bytes is 4 269 * @val: pointer to register value to be filled. 270 * 271 * Big endian register addresses with little endian values. 272 * 273 * Return: 0 if successful, error code otherwise. 274 */ 275 static int imx335_read_reg(struct imx335 *imx335, u16 reg, u32 len, u32 *val) 276 { 277 struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd); 278 struct i2c_msg msgs[2] = {0}; 279 u8 addr_buf[2] = {0}; 280 u8 data_buf[4] = {0}; 281 int ret; 282 283 if (WARN_ON(len > 4)) 284 return -EINVAL; 285 286 put_unaligned_be16(reg, addr_buf); 287 288 /* Write register address */ 289 msgs[0].addr = client->addr; 290 msgs[0].flags = 0; 291 msgs[0].len = ARRAY_SIZE(addr_buf); 292 msgs[0].buf = addr_buf; 293 294 /* Read data from register */ 295 msgs[1].addr = client->addr; 296 msgs[1].flags = I2C_M_RD; 297 msgs[1].len = len; 298 msgs[1].buf = data_buf; 299 300 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 301 if (ret != ARRAY_SIZE(msgs)) 302 return -EIO; 303 304 *val = get_unaligned_le32(data_buf); 305 306 return 0; 307 } 308 309 /** 310 * imx335_write_reg() - Write register 311 * @imx335: pointer to imx335 device 312 * @reg: register address 313 * @len: length of bytes. Max supported bytes is 4 314 * @val: register value 315 * 316 * Big endian register addresses with little endian values. 317 * 318 * Return: 0 if successful, error code otherwise. 319 */ 320 static int imx335_write_reg(struct imx335 *imx335, u16 reg, u32 len, u32 val) 321 { 322 struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd); 323 u8 buf[6] = {0}; 324 325 if (WARN_ON(len > 4)) 326 return -EINVAL; 327 328 put_unaligned_be16(reg, buf); 329 put_unaligned_le32(val, buf + 2); 330 if (i2c_master_send(client, buf, len + 2) != len + 2) 331 return -EIO; 332 333 return 0; 334 } 335 336 /** 337 * imx335_write_regs() - Write a list of registers 338 * @imx335: pointer to imx335 device 339 * @regs: list of registers to be written 340 * @len: length of registers array 341 * 342 * Return: 0 if successful. error code otherwise. 343 */ 344 static int imx335_write_regs(struct imx335 *imx335, 345 const struct imx335_reg *regs, u32 len) 346 { 347 unsigned int i; 348 int ret; 349 350 for (i = 0; i < len; i++) { 351 ret = imx335_write_reg(imx335, regs[i].address, 1, regs[i].val); 352 if (ret) 353 return ret; 354 } 355 356 return 0; 357 } 358 359 /** 360 * imx335_update_controls() - Update control ranges based on streaming mode 361 * @imx335: pointer to imx335 device 362 * @mode: pointer to imx335_mode sensor mode 363 * 364 * Return: 0 if successful, error code otherwise. 365 */ 366 static int imx335_update_controls(struct imx335 *imx335, 367 const struct imx335_mode *mode) 368 { 369 int ret; 370 371 ret = __v4l2_ctrl_s_ctrl(imx335->link_freq_ctrl, mode->link_freq_idx); 372 if (ret) 373 return ret; 374 375 ret = __v4l2_ctrl_s_ctrl(imx335->hblank_ctrl, mode->hblank); 376 if (ret) 377 return ret; 378 379 return __v4l2_ctrl_modify_range(imx335->vblank_ctrl, mode->vblank_min, 380 mode->vblank_max, 1, mode->vblank); 381 } 382 383 /** 384 * imx335_update_exp_gain() - Set updated exposure and gain 385 * @imx335: pointer to imx335 device 386 * @exposure: updated exposure value 387 * @gain: updated analog gain value 388 * 389 * Return: 0 if successful, error code otherwise. 390 */ 391 static int imx335_update_exp_gain(struct imx335 *imx335, u32 exposure, u32 gain) 392 { 393 u32 lpfr, shutter; 394 int ret; 395 396 lpfr = imx335->vblank + imx335->cur_mode->height; 397 shutter = lpfr - exposure; 398 399 dev_dbg(imx335->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u", 400 exposure, gain, shutter, lpfr); 401 402 ret = imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 1); 403 if (ret) 404 return ret; 405 406 ret = imx335_write_reg(imx335, IMX335_REG_LPFR, 3, lpfr); 407 if (ret) 408 goto error_release_group_hold; 409 410 ret = imx335_write_reg(imx335, IMX335_REG_SHUTTER, 3, shutter); 411 if (ret) 412 goto error_release_group_hold; 413 414 ret = imx335_write_reg(imx335, IMX335_REG_AGAIN, 2, gain); 415 416 error_release_group_hold: 417 imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 0); 418 419 return ret; 420 } 421 422 /** 423 * imx335_set_ctrl() - Set subdevice control 424 * @ctrl: pointer to v4l2_ctrl structure 425 * 426 * Supported controls: 427 * - V4L2_CID_VBLANK 428 * - cluster controls: 429 * - V4L2_CID_ANALOGUE_GAIN 430 * - V4L2_CID_EXPOSURE 431 * 432 * Return: 0 if successful, error code otherwise. 433 */ 434 static int imx335_set_ctrl(struct v4l2_ctrl *ctrl) 435 { 436 struct imx335 *imx335 = 437 container_of(ctrl->handler, struct imx335, ctrl_handler); 438 u32 analog_gain; 439 u32 exposure; 440 int ret; 441 442 switch (ctrl->id) { 443 case V4L2_CID_VBLANK: 444 imx335->vblank = imx335->vblank_ctrl->val; 445 446 dev_dbg(imx335->dev, "Received vblank %u, new lpfr %u", 447 imx335->vblank, 448 imx335->vblank + imx335->cur_mode->height); 449 450 ret = __v4l2_ctrl_modify_range(imx335->exp_ctrl, 451 IMX335_EXPOSURE_MIN, 452 imx335->vblank + 453 imx335->cur_mode->height - 454 IMX335_EXPOSURE_OFFSET, 455 1, IMX335_EXPOSURE_DEFAULT); 456 break; 457 case V4L2_CID_EXPOSURE: 458 /* Set controls only if sensor is in power on state */ 459 if (!pm_runtime_get_if_in_use(imx335->dev)) 460 return 0; 461 462 exposure = ctrl->val; 463 analog_gain = imx335->again_ctrl->val; 464 465 dev_dbg(imx335->dev, "Received exp %u, analog gain %u", 466 exposure, analog_gain); 467 468 ret = imx335_update_exp_gain(imx335, exposure, analog_gain); 469 470 pm_runtime_put(imx335->dev); 471 472 break; 473 default: 474 dev_err(imx335->dev, "Invalid control %d", ctrl->id); 475 ret = -EINVAL; 476 } 477 478 return ret; 479 } 480 481 /* V4l2 subdevice control ops*/ 482 static const struct v4l2_ctrl_ops imx335_ctrl_ops = { 483 .s_ctrl = imx335_set_ctrl, 484 }; 485 486 /** 487 * imx335_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes 488 * @sd: pointer to imx335 V4L2 sub-device structure 489 * @sd_state: V4L2 sub-device configuration 490 * @code: V4L2 sub-device code enumeration need to be filled 491 * 492 * Return: 0 if successful, error code otherwise. 493 */ 494 static int imx335_enum_mbus_code(struct v4l2_subdev *sd, 495 struct v4l2_subdev_state *sd_state, 496 struct v4l2_subdev_mbus_code_enum *code) 497 { 498 if (code->index > 0) 499 return -EINVAL; 500 501 code->code = supported_mode.code; 502 503 return 0; 504 } 505 506 /** 507 * imx335_enum_frame_size() - Enumerate V4L2 sub-device frame sizes 508 * @sd: pointer to imx335 V4L2 sub-device structure 509 * @sd_state: V4L2 sub-device configuration 510 * @fsize: V4L2 sub-device size enumeration need to be filled 511 * 512 * Return: 0 if successful, error code otherwise. 513 */ 514 static int imx335_enum_frame_size(struct v4l2_subdev *sd, 515 struct v4l2_subdev_state *sd_state, 516 struct v4l2_subdev_frame_size_enum *fsize) 517 { 518 if (fsize->index > 0) 519 return -EINVAL; 520 521 if (fsize->code != supported_mode.code) 522 return -EINVAL; 523 524 fsize->min_width = supported_mode.width; 525 fsize->max_width = fsize->min_width; 526 fsize->min_height = supported_mode.height; 527 fsize->max_height = fsize->min_height; 528 529 return 0; 530 } 531 532 /** 533 * imx335_fill_pad_format() - Fill subdevice pad format 534 * from selected sensor mode 535 * @imx335: pointer to imx335 device 536 * @mode: pointer to imx335_mode sensor mode 537 * @fmt: V4L2 sub-device format need to be filled 538 */ 539 static void imx335_fill_pad_format(struct imx335 *imx335, 540 const struct imx335_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 = mode->code; 546 fmt->format.field = V4L2_FIELD_NONE; 547 fmt->format.colorspace = V4L2_COLORSPACE_RAW; 548 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 549 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; 550 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; 551 } 552 553 /** 554 * imx335_get_pad_format() - Get subdevice pad format 555 * @sd: pointer to imx335 V4L2 sub-device structure 556 * @sd_state: V4L2 sub-device configuration 557 * @fmt: V4L2 sub-device format need to be set 558 * 559 * Return: 0 if successful, error code otherwise. 560 */ 561 static int imx335_get_pad_format(struct v4l2_subdev *sd, 562 struct v4l2_subdev_state *sd_state, 563 struct v4l2_subdev_format *fmt) 564 { 565 struct imx335 *imx335 = to_imx335(sd); 566 567 mutex_lock(&imx335->mutex); 568 569 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 570 struct v4l2_mbus_framefmt *framefmt; 571 572 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); 573 fmt->format = *framefmt; 574 } else { 575 imx335_fill_pad_format(imx335, imx335->cur_mode, fmt); 576 } 577 578 mutex_unlock(&imx335->mutex); 579 580 return 0; 581 } 582 583 /** 584 * imx335_set_pad_format() - Set subdevice pad format 585 * @sd: pointer to imx335 V4L2 sub-device structure 586 * @sd_state: V4L2 sub-device configuration 587 * @fmt: V4L2 sub-device format need to be set 588 * 589 * Return: 0 if successful, error code otherwise. 590 */ 591 static int imx335_set_pad_format(struct v4l2_subdev *sd, 592 struct v4l2_subdev_state *sd_state, 593 struct v4l2_subdev_format *fmt) 594 { 595 struct imx335 *imx335 = to_imx335(sd); 596 const struct imx335_mode *mode; 597 int ret = 0; 598 599 mutex_lock(&imx335->mutex); 600 601 mode = &supported_mode; 602 imx335_fill_pad_format(imx335, mode, fmt); 603 604 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 605 struct v4l2_mbus_framefmt *framefmt; 606 607 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); 608 *framefmt = fmt->format; 609 } else { 610 ret = imx335_update_controls(imx335, mode); 611 if (!ret) 612 imx335->cur_mode = mode; 613 } 614 615 mutex_unlock(&imx335->mutex); 616 617 return ret; 618 } 619 620 /** 621 * imx335_init_pad_cfg() - Initialize sub-device pad configuration 622 * @sd: pointer to imx335 V4L2 sub-device structure 623 * @sd_state: V4L2 sub-device configuration 624 * 625 * Return: 0 if successful, error code otherwise. 626 */ 627 static int imx335_init_pad_cfg(struct v4l2_subdev *sd, 628 struct v4l2_subdev_state *sd_state) 629 { 630 struct imx335 *imx335 = to_imx335(sd); 631 struct v4l2_subdev_format fmt = { 0 }; 632 633 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE; 634 imx335_fill_pad_format(imx335, &supported_mode, &fmt); 635 636 return imx335_set_pad_format(sd, sd_state, &fmt); 637 } 638 639 /** 640 * imx335_start_streaming() - Start sensor stream 641 * @imx335: pointer to imx335 device 642 * 643 * Return: 0 if successful, error code otherwise. 644 */ 645 static int imx335_start_streaming(struct imx335 *imx335) 646 { 647 const struct imx335_reg_list *reg_list; 648 int ret; 649 650 /* Write sensor mode registers */ 651 reg_list = &imx335->cur_mode->reg_list; 652 ret = imx335_write_regs(imx335, reg_list->regs, 653 reg_list->num_of_regs); 654 if (ret) { 655 dev_err(imx335->dev, "fail to write initial registers"); 656 return ret; 657 } 658 659 /* Setup handler will write actual exposure and gain */ 660 ret = __v4l2_ctrl_handler_setup(imx335->sd.ctrl_handler); 661 if (ret) { 662 dev_err(imx335->dev, "fail to setup handler"); 663 return ret; 664 } 665 666 /* Start streaming */ 667 ret = imx335_write_reg(imx335, IMX335_REG_MODE_SELECT, 668 1, IMX335_MODE_STREAMING); 669 if (ret) { 670 dev_err(imx335->dev, "fail to start streaming"); 671 return ret; 672 } 673 674 /* Initial regulator stabilization period */ 675 usleep_range(18000, 20000); 676 677 return 0; 678 } 679 680 /** 681 * imx335_stop_streaming() - Stop sensor stream 682 * @imx335: pointer to imx335 device 683 * 684 * Return: 0 if successful, error code otherwise. 685 */ 686 static int imx335_stop_streaming(struct imx335 *imx335) 687 { 688 return imx335_write_reg(imx335, IMX335_REG_MODE_SELECT, 689 1, IMX335_MODE_STANDBY); 690 } 691 692 /** 693 * imx335_set_stream() - Enable sensor streaming 694 * @sd: pointer to imx335 subdevice 695 * @enable: set to enable sensor streaming 696 * 697 * Return: 0 if successful, error code otherwise. 698 */ 699 static int imx335_set_stream(struct v4l2_subdev *sd, int enable) 700 { 701 struct imx335 *imx335 = to_imx335(sd); 702 int ret; 703 704 mutex_lock(&imx335->mutex); 705 706 if (enable) { 707 ret = pm_runtime_resume_and_get(imx335->dev); 708 if (ret) 709 goto error_unlock; 710 711 ret = imx335_start_streaming(imx335); 712 if (ret) 713 goto error_power_off; 714 } else { 715 imx335_stop_streaming(imx335); 716 pm_runtime_put(imx335->dev); 717 } 718 719 mutex_unlock(&imx335->mutex); 720 721 return 0; 722 723 error_power_off: 724 pm_runtime_put(imx335->dev); 725 error_unlock: 726 mutex_unlock(&imx335->mutex); 727 728 return ret; 729 } 730 731 /** 732 * imx335_detect() - Detect imx335 sensor 733 * @imx335: pointer to imx335 device 734 * 735 * Return: 0 if successful, -EIO if sensor id does not match 736 */ 737 static int imx335_detect(struct imx335 *imx335) 738 { 739 int ret; 740 u32 val; 741 742 ret = imx335_read_reg(imx335, IMX335_REG_ID, 2, &val); 743 if (ret) 744 return ret; 745 746 if (val != IMX335_ID) { 747 dev_err(imx335->dev, "chip id mismatch: %x!=%x", 748 IMX335_ID, val); 749 return -ENXIO; 750 } 751 752 return 0; 753 } 754 755 /** 756 * imx335_parse_hw_config() - Parse HW configuration and check if supported 757 * @imx335: pointer to imx335 device 758 * 759 * Return: 0 if successful, error code otherwise. 760 */ 761 static int imx335_parse_hw_config(struct imx335 *imx335) 762 { 763 struct fwnode_handle *fwnode = dev_fwnode(imx335->dev); 764 struct v4l2_fwnode_endpoint bus_cfg = { 765 .bus_type = V4L2_MBUS_CSI2_DPHY 766 }; 767 struct fwnode_handle *ep; 768 unsigned long rate; 769 unsigned int i; 770 int ret; 771 772 if (!fwnode) 773 return -ENXIO; 774 775 /* Request optional reset pin */ 776 imx335->reset_gpio = devm_gpiod_get_optional(imx335->dev, "reset", 777 GPIOD_OUT_LOW); 778 if (IS_ERR(imx335->reset_gpio)) { 779 dev_err(imx335->dev, "failed to get reset gpio %ld", 780 PTR_ERR(imx335->reset_gpio)); 781 return PTR_ERR(imx335->reset_gpio); 782 } 783 784 /* Get sensor input clock */ 785 imx335->inclk = devm_clk_get(imx335->dev, NULL); 786 if (IS_ERR(imx335->inclk)) { 787 dev_err(imx335->dev, "could not get inclk"); 788 return PTR_ERR(imx335->inclk); 789 } 790 791 rate = clk_get_rate(imx335->inclk); 792 if (rate != IMX335_INCLK_RATE) { 793 dev_err(imx335->dev, "inclk frequency mismatch"); 794 return -EINVAL; 795 } 796 797 ep = fwnode_graph_get_next_endpoint(fwnode, NULL); 798 if (!ep) 799 return -ENXIO; 800 801 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); 802 fwnode_handle_put(ep); 803 if (ret) 804 return ret; 805 806 if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX335_NUM_DATA_LANES) { 807 dev_err(imx335->dev, 808 "number of CSI2 data lanes %d is not supported", 809 bus_cfg.bus.mipi_csi2.num_data_lanes); 810 ret = -EINVAL; 811 goto done_endpoint_free; 812 } 813 814 if (!bus_cfg.nr_of_link_frequencies) { 815 dev_err(imx335->dev, "no link frequencies defined"); 816 ret = -EINVAL; 817 goto done_endpoint_free; 818 } 819 820 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) 821 if (bus_cfg.link_frequencies[i] == IMX335_LINK_FREQ) 822 goto done_endpoint_free; 823 824 ret = -EINVAL; 825 826 done_endpoint_free: 827 v4l2_fwnode_endpoint_free(&bus_cfg); 828 829 return ret; 830 } 831 832 /* V4l2 subdevice ops */ 833 static const struct v4l2_subdev_video_ops imx335_video_ops = { 834 .s_stream = imx335_set_stream, 835 }; 836 837 static const struct v4l2_subdev_pad_ops imx335_pad_ops = { 838 .init_cfg = imx335_init_pad_cfg, 839 .enum_mbus_code = imx335_enum_mbus_code, 840 .enum_frame_size = imx335_enum_frame_size, 841 .get_fmt = imx335_get_pad_format, 842 .set_fmt = imx335_set_pad_format, 843 }; 844 845 static const struct v4l2_subdev_ops imx335_subdev_ops = { 846 .video = &imx335_video_ops, 847 .pad = &imx335_pad_ops, 848 }; 849 850 /** 851 * imx335_power_on() - Sensor power on sequence 852 * @dev: pointer to i2c device 853 * 854 * Return: 0 if successful, error code otherwise. 855 */ 856 static int imx335_power_on(struct device *dev) 857 { 858 struct v4l2_subdev *sd = dev_get_drvdata(dev); 859 struct imx335 *imx335 = to_imx335(sd); 860 int ret; 861 862 gpiod_set_value_cansleep(imx335->reset_gpio, 1); 863 864 ret = clk_prepare_enable(imx335->inclk); 865 if (ret) { 866 dev_err(imx335->dev, "fail to enable inclk"); 867 goto error_reset; 868 } 869 870 usleep_range(20, 22); 871 872 return 0; 873 874 error_reset: 875 gpiod_set_value_cansleep(imx335->reset_gpio, 0); 876 877 return ret; 878 } 879 880 /** 881 * imx335_power_off() - Sensor power off sequence 882 * @dev: pointer to i2c device 883 * 884 * Return: 0 if successful, error code otherwise. 885 */ 886 static int imx335_power_off(struct device *dev) 887 { 888 struct v4l2_subdev *sd = dev_get_drvdata(dev); 889 struct imx335 *imx335 = to_imx335(sd); 890 891 gpiod_set_value_cansleep(imx335->reset_gpio, 0); 892 893 clk_disable_unprepare(imx335->inclk); 894 895 return 0; 896 } 897 898 /** 899 * imx335_init_controls() - Initialize sensor subdevice controls 900 * @imx335: pointer to imx335 device 901 * 902 * Return: 0 if successful, error code otherwise. 903 */ 904 static int imx335_init_controls(struct imx335 *imx335) 905 { 906 struct v4l2_ctrl_handler *ctrl_hdlr = &imx335->ctrl_handler; 907 const struct imx335_mode *mode = imx335->cur_mode; 908 u32 lpfr; 909 int ret; 910 911 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6); 912 if (ret) 913 return ret; 914 915 /* Serialize controls with sensor device */ 916 ctrl_hdlr->lock = &imx335->mutex; 917 918 /* Initialize exposure and gain */ 919 lpfr = mode->vblank + mode->height; 920 imx335->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 921 &imx335_ctrl_ops, 922 V4L2_CID_EXPOSURE, 923 IMX335_EXPOSURE_MIN, 924 lpfr - IMX335_EXPOSURE_OFFSET, 925 IMX335_EXPOSURE_STEP, 926 IMX335_EXPOSURE_DEFAULT); 927 928 imx335->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 929 &imx335_ctrl_ops, 930 V4L2_CID_ANALOGUE_GAIN, 931 IMX335_AGAIN_MIN, 932 IMX335_AGAIN_MAX, 933 IMX335_AGAIN_STEP, 934 IMX335_AGAIN_DEFAULT); 935 936 v4l2_ctrl_cluster(2, &imx335->exp_ctrl); 937 938 imx335->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 939 &imx335_ctrl_ops, 940 V4L2_CID_VBLANK, 941 mode->vblank_min, 942 mode->vblank_max, 943 1, mode->vblank); 944 945 /* Read only controls */ 946 imx335->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 947 &imx335_ctrl_ops, 948 V4L2_CID_PIXEL_RATE, 949 mode->pclk, mode->pclk, 950 1, mode->pclk); 951 952 imx335->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, 953 &imx335_ctrl_ops, 954 V4L2_CID_LINK_FREQ, 955 ARRAY_SIZE(link_freq) - 956 1, 957 mode->link_freq_idx, 958 link_freq); 959 if (imx335->link_freq_ctrl) 960 imx335->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 961 962 imx335->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 963 &imx335_ctrl_ops, 964 V4L2_CID_HBLANK, 965 IMX335_REG_MIN, 966 IMX335_REG_MAX, 967 1, mode->hblank); 968 if (imx335->hblank_ctrl) 969 imx335->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 970 971 if (ctrl_hdlr->error) { 972 dev_err(imx335->dev, "control init failed: %d", 973 ctrl_hdlr->error); 974 v4l2_ctrl_handler_free(ctrl_hdlr); 975 return ctrl_hdlr->error; 976 } 977 978 imx335->sd.ctrl_handler = ctrl_hdlr; 979 980 return 0; 981 } 982 983 /** 984 * imx335_probe() - I2C client device binding 985 * @client: pointer to i2c client device 986 * 987 * Return: 0 if successful, error code otherwise. 988 */ 989 static int imx335_probe(struct i2c_client *client) 990 { 991 struct imx335 *imx335; 992 int ret; 993 994 imx335 = devm_kzalloc(&client->dev, sizeof(*imx335), GFP_KERNEL); 995 if (!imx335) 996 return -ENOMEM; 997 998 imx335->dev = &client->dev; 999 1000 /* Initialize subdev */ 1001 v4l2_i2c_subdev_init(&imx335->sd, client, &imx335_subdev_ops); 1002 1003 ret = imx335_parse_hw_config(imx335); 1004 if (ret) { 1005 dev_err(imx335->dev, "HW configuration is not supported"); 1006 return ret; 1007 } 1008 1009 mutex_init(&imx335->mutex); 1010 1011 ret = imx335_power_on(imx335->dev); 1012 if (ret) { 1013 dev_err(imx335->dev, "failed to power-on the sensor"); 1014 goto error_mutex_destroy; 1015 } 1016 1017 /* Check module identity */ 1018 ret = imx335_detect(imx335); 1019 if (ret) { 1020 dev_err(imx335->dev, "failed to find sensor: %d", ret); 1021 goto error_power_off; 1022 } 1023 1024 /* Set default mode to max resolution */ 1025 imx335->cur_mode = &supported_mode; 1026 imx335->vblank = imx335->cur_mode->vblank; 1027 1028 ret = imx335_init_controls(imx335); 1029 if (ret) { 1030 dev_err(imx335->dev, "failed to init controls: %d", ret); 1031 goto error_power_off; 1032 } 1033 1034 /* Initialize subdev */ 1035 imx335->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1036 imx335->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1037 1038 /* Initialize source pad */ 1039 imx335->pad.flags = MEDIA_PAD_FL_SOURCE; 1040 ret = media_entity_pads_init(&imx335->sd.entity, 1, &imx335->pad); 1041 if (ret) { 1042 dev_err(imx335->dev, "failed to init entity pads: %d", ret); 1043 goto error_handler_free; 1044 } 1045 1046 ret = v4l2_async_register_subdev_sensor(&imx335->sd); 1047 if (ret < 0) { 1048 dev_err(imx335->dev, 1049 "failed to register async subdev: %d", ret); 1050 goto error_media_entity; 1051 } 1052 1053 pm_runtime_set_active(imx335->dev); 1054 pm_runtime_enable(imx335->dev); 1055 pm_runtime_idle(imx335->dev); 1056 1057 return 0; 1058 1059 error_media_entity: 1060 media_entity_cleanup(&imx335->sd.entity); 1061 error_handler_free: 1062 v4l2_ctrl_handler_free(imx335->sd.ctrl_handler); 1063 error_power_off: 1064 imx335_power_off(imx335->dev); 1065 error_mutex_destroy: 1066 mutex_destroy(&imx335->mutex); 1067 1068 return ret; 1069 } 1070 1071 /** 1072 * imx335_remove() - I2C client device unbinding 1073 * @client: pointer to I2C client device 1074 * 1075 * Return: 0 if successful, error code otherwise. 1076 */ 1077 static void imx335_remove(struct i2c_client *client) 1078 { 1079 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1080 struct imx335 *imx335 = to_imx335(sd); 1081 1082 v4l2_async_unregister_subdev(sd); 1083 media_entity_cleanup(&sd->entity); 1084 v4l2_ctrl_handler_free(sd->ctrl_handler); 1085 1086 pm_runtime_disable(&client->dev); 1087 if (!pm_runtime_status_suspended(&client->dev)) 1088 imx335_power_off(&client->dev); 1089 pm_runtime_set_suspended(&client->dev); 1090 1091 mutex_destroy(&imx335->mutex); 1092 } 1093 1094 static const struct dev_pm_ops imx335_pm_ops = { 1095 SET_RUNTIME_PM_OPS(imx335_power_off, imx335_power_on, NULL) 1096 }; 1097 1098 static const struct of_device_id imx335_of_match[] = { 1099 { .compatible = "sony,imx335" }, 1100 { } 1101 }; 1102 1103 MODULE_DEVICE_TABLE(of, imx335_of_match); 1104 1105 static struct i2c_driver imx335_driver = { 1106 .probe = imx335_probe, 1107 .remove = imx335_remove, 1108 .driver = { 1109 .name = "imx335", 1110 .pm = &imx335_pm_ops, 1111 .of_match_table = imx335_of_match, 1112 }, 1113 }; 1114 1115 module_i2c_driver(imx335_driver); 1116 1117 MODULE_DESCRIPTION("Sony imx335 sensor driver"); 1118 MODULE_LICENSE("GPL"); 1119