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