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