1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * OmniVision ov9282 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-event.h> 17 #include <media/v4l2-fwnode.h> 18 #include <media/v4l2-subdev.h> 19 20 /* Streaming Mode */ 21 #define OV9282_REG_MODE_SELECT 0x0100 22 #define OV9282_MODE_STANDBY 0x00 23 #define OV9282_MODE_STREAMING 0x01 24 25 #define OV9282_REG_PLL_CTRL_0D 0x030d 26 #define OV9282_PLL_CTRL_0D_RAW8 0x60 27 #define OV9282_PLL_CTRL_0D_RAW10 0x50 28 29 #define OV9282_REG_TIMING_HTS 0x380c 30 #define OV9282_TIMING_HTS_MAX 0x7fff 31 32 /* Lines per frame */ 33 #define OV9282_REG_LPFR 0x380e 34 35 /* Chip ID */ 36 #define OV9282_REG_ID 0x300a 37 #define OV9282_ID 0x9281 38 39 /* Exposure control */ 40 #define OV9282_REG_EXPOSURE 0x3500 41 #define OV9282_EXPOSURE_MIN 1 42 #define OV9282_EXPOSURE_OFFSET 12 43 #define OV9282_EXPOSURE_STEP 1 44 #define OV9282_EXPOSURE_DEFAULT 0x0282 45 46 /* Analog gain control */ 47 #define OV9282_REG_AGAIN 0x3509 48 #define OV9282_AGAIN_MIN 0x10 49 #define OV9282_AGAIN_MAX 0xff 50 #define OV9282_AGAIN_STEP 1 51 #define OV9282_AGAIN_DEFAULT 0x10 52 53 /* Group hold register */ 54 #define OV9282_REG_HOLD 0x3308 55 56 #define OV9282_REG_ANA_CORE_2 0x3662 57 #define OV9282_ANA_CORE2_RAW8 0x07 58 #define OV9282_ANA_CORE2_RAW10 0x05 59 60 #define OV9282_REG_TIMING_FORMAT_1 0x3820 61 #define OV9282_REG_TIMING_FORMAT_2 0x3821 62 #define OV9282_FLIP_BIT BIT(2) 63 64 #define OV9282_REG_MIPI_CTRL00 0x4800 65 #define OV9282_GATED_CLOCK BIT(5) 66 67 /* Input clock rate */ 68 #define OV9282_INCLK_RATE 24000000 69 70 /* CSI2 HW configuration */ 71 #define OV9282_LINK_FREQ 400000000 72 #define OV9282_NUM_DATA_LANES 2 73 74 /* Pixel rate */ 75 #define OV9282_PIXEL_RATE_10BIT (OV9282_LINK_FREQ * 2 * \ 76 OV9282_NUM_DATA_LANES / 10) 77 #define OV9282_PIXEL_RATE_8BIT (OV9282_LINK_FREQ * 2 * \ 78 OV9282_NUM_DATA_LANES / 8) 79 80 /* 81 * OV9282 native and active pixel array size. 82 * 8 dummy rows/columns on each edge of a 1280x800 active array 83 */ 84 #define OV9282_NATIVE_WIDTH 1296U 85 #define OV9282_NATIVE_HEIGHT 816U 86 #define OV9282_PIXEL_ARRAY_LEFT 8U 87 #define OV9282_PIXEL_ARRAY_TOP 8U 88 #define OV9282_PIXEL_ARRAY_WIDTH 1280U 89 #define OV9282_PIXEL_ARRAY_HEIGHT 800U 90 91 #define OV9282_REG_MIN 0x00 92 #define OV9282_REG_MAX 0xfffff 93 94 /** 95 * struct ov9282_reg - ov9282 sensor register 96 * @address: Register address 97 * @val: Register value 98 */ 99 struct ov9282_reg { 100 u16 address; 101 u8 val; 102 }; 103 104 /** 105 * struct ov9282_reg_list - ov9282 sensor register list 106 * @num_of_regs: Number of registers in the list 107 * @regs: Pointer to register list 108 */ 109 struct ov9282_reg_list { 110 u32 num_of_regs; 111 const struct ov9282_reg *regs; 112 }; 113 114 /** 115 * struct ov9282_mode - ov9282 sensor mode structure 116 * @width: Frame width 117 * @height: Frame height 118 * @hblank_min: Minimum horizontal blanking in lines for non-continuous[0] and 119 * continuous[1] clock modes 120 * @vblank: Vertical blanking in lines 121 * @vblank_min: Minimum vertical blanking in lines 122 * @vblank_max: Maximum vertical blanking in lines 123 * @link_freq_idx: Link frequency index 124 * @reg_list: Register list for sensor mode 125 */ 126 struct ov9282_mode { 127 u32 width; 128 u32 height; 129 u32 hblank_min[2]; 130 u32 vblank; 131 u32 vblank_min; 132 u32 vblank_max; 133 u32 link_freq_idx; 134 struct v4l2_rect crop; 135 struct ov9282_reg_list reg_list; 136 }; 137 138 /** 139 * struct ov9282 - ov9282 sensor device structure 140 * @dev: Pointer to generic device 141 * @client: Pointer to i2c client 142 * @sd: V4L2 sub-device 143 * @pad: Media pad. Only one pad supported 144 * @reset_gpio: Sensor reset gpio 145 * @inclk: Sensor input clock 146 * @ctrl_handler: V4L2 control handler 147 * @link_freq_ctrl: Pointer to link frequency control 148 * @hblank_ctrl: Pointer to horizontal blanking control 149 * @vblank_ctrl: Pointer to vertical blanking control 150 * @exp_ctrl: Pointer to exposure control 151 * @again_ctrl: Pointer to analog gain control 152 * @vblank: Vertical blanking in lines 153 * @cur_mode: Pointer to current selected sensor mode 154 * @code: Mbus code currently selected 155 * @mutex: Mutex for serializing sensor controls 156 * @streaming: Flag indicating streaming state 157 */ 158 struct ov9282 { 159 struct device *dev; 160 struct i2c_client *client; 161 struct v4l2_subdev sd; 162 struct media_pad pad; 163 struct gpio_desc *reset_gpio; 164 struct clk *inclk; 165 struct v4l2_ctrl_handler ctrl_handler; 166 struct v4l2_ctrl *link_freq_ctrl; 167 struct v4l2_ctrl *hblank_ctrl; 168 struct v4l2_ctrl *vblank_ctrl; 169 struct { 170 struct v4l2_ctrl *exp_ctrl; 171 struct v4l2_ctrl *again_ctrl; 172 }; 173 struct v4l2_ctrl *pixel_rate; 174 u32 vblank; 175 bool noncontinuous_clock; 176 const struct ov9282_mode *cur_mode; 177 u32 code; 178 struct mutex mutex; 179 bool streaming; 180 }; 181 182 static const s64 link_freq[] = { 183 OV9282_LINK_FREQ, 184 }; 185 186 /* 187 * Common registers 188 * 189 * Note: Do NOT include a software reset (0x0103, 0x01) in any of these 190 * register arrays as some settings are written as part of ov9282_power_on, 191 * and the reset will clear them. 192 */ 193 static const struct ov9282_reg common_regs[] = { 194 {0x0302, 0x32}, 195 {0x030e, 0x02}, 196 {0x3001, 0x00}, 197 {0x3004, 0x00}, 198 {0x3005, 0x00}, 199 {0x3006, 0x04}, 200 {0x3011, 0x0a}, 201 {0x3013, 0x18}, 202 {0x301c, 0xf0}, 203 {0x3022, 0x01}, 204 {0x3030, 0x10}, 205 {0x3039, 0x32}, 206 {0x303a, 0x00}, 207 {0x3503, 0x08}, 208 {0x3505, 0x8c}, 209 {0x3507, 0x03}, 210 {0x3508, 0x00}, 211 {0x3610, 0x80}, 212 {0x3611, 0xa0}, 213 {0x3620, 0x6e}, 214 {0x3632, 0x56}, 215 {0x3633, 0x78}, 216 {0x3666, 0x00}, 217 {0x366f, 0x5a}, 218 {0x3680, 0x84}, 219 {0x3712, 0x80}, 220 {0x372d, 0x22}, 221 {0x3731, 0x80}, 222 {0x3732, 0x30}, 223 {0x377d, 0x22}, 224 {0x3788, 0x02}, 225 {0x3789, 0xa4}, 226 {0x378a, 0x00}, 227 {0x378b, 0x4a}, 228 {0x3799, 0x20}, 229 {0x3881, 0x42}, 230 {0x38a8, 0x02}, 231 {0x38a9, 0x80}, 232 {0x38b1, 0x00}, 233 {0x38c4, 0x00}, 234 {0x38c5, 0xc0}, 235 {0x38c6, 0x04}, 236 {0x38c7, 0x80}, 237 {0x3920, 0xff}, 238 {0x4010, 0x40}, 239 {0x4043, 0x40}, 240 {0x4307, 0x30}, 241 {0x4317, 0x00}, 242 {0x4501, 0x00}, 243 {0x450a, 0x08}, 244 {0x4601, 0x04}, 245 {0x470f, 0x00}, 246 {0x4f07, 0x00}, 247 {0x5000, 0x9f}, 248 {0x5001, 0x00}, 249 {0x5e00, 0x00}, 250 {0x5d00, 0x07}, 251 {0x5d01, 0x00}, 252 {0x0101, 0x01}, 253 {0x1000, 0x03}, 254 {0x5a08, 0x84}, 255 }; 256 257 struct ov9282_reg_list common_regs_list = { 258 .num_of_regs = ARRAY_SIZE(common_regs), 259 .regs = common_regs, 260 }; 261 262 #define MODE_1280_800 0 263 #define MODE_1280_720 1 264 #define MODE_640_400 2 265 266 #define DEFAULT_MODE MODE_1280_720 267 268 /* Sensor mode registers */ 269 static const struct ov9282_reg mode_1280x800_regs[] = { 270 {0x3778, 0x00}, 271 {0x3800, 0x00}, 272 {0x3801, 0x00}, 273 {0x3802, 0x00}, 274 {0x3803, 0x00}, 275 {0x3804, 0x05}, 276 {0x3805, 0x0f}, 277 {0x3806, 0x03}, 278 {0x3807, 0x2f}, 279 {0x3808, 0x05}, 280 {0x3809, 0x00}, 281 {0x380a, 0x03}, 282 {0x380b, 0x20}, 283 {0x3810, 0x00}, 284 {0x3811, 0x08}, 285 {0x3812, 0x00}, 286 {0x3813, 0x08}, 287 {0x3814, 0x11}, 288 {0x3815, 0x11}, 289 {0x3820, 0x40}, 290 {0x3821, 0x00}, 291 {0x4003, 0x40}, 292 {0x4008, 0x04}, 293 {0x4009, 0x0b}, 294 {0x400c, 0x00}, 295 {0x400d, 0x07}, 296 {0x4507, 0x00}, 297 {0x4509, 0x00}, 298 }; 299 300 static const struct ov9282_reg mode_1280x720_regs[] = { 301 {0x3778, 0x00}, 302 {0x3800, 0x00}, 303 {0x3801, 0x00}, 304 {0x3802, 0x00}, 305 {0x3803, 0x00}, 306 {0x3804, 0x05}, 307 {0x3805, 0x0f}, 308 {0x3806, 0x02}, 309 {0x3807, 0xdf}, 310 {0x3808, 0x05}, 311 {0x3809, 0x00}, 312 {0x380a, 0x02}, 313 {0x380b, 0xd0}, 314 {0x3810, 0x00}, 315 {0x3811, 0x08}, 316 {0x3812, 0x00}, 317 {0x3813, 0x08}, 318 {0x3814, 0x11}, 319 {0x3815, 0x11}, 320 {0x3820, 0x3c}, 321 {0x3821, 0x84}, 322 {0x4003, 0x40}, 323 {0x4008, 0x02}, 324 {0x4009, 0x05}, 325 {0x400c, 0x00}, 326 {0x400d, 0x03}, 327 {0x4507, 0x00}, 328 {0x4509, 0x80}, 329 }; 330 331 static const struct ov9282_reg mode_640x400_regs[] = { 332 {0x3778, 0x10}, 333 {0x3800, 0x00}, 334 {0x3801, 0x00}, 335 {0x3802, 0x00}, 336 {0x3803, 0x00}, 337 {0x3804, 0x05}, 338 {0x3805, 0x0f}, 339 {0x3806, 0x03}, 340 {0x3807, 0x2f}, 341 {0x3808, 0x02}, 342 {0x3809, 0x80}, 343 {0x380a, 0x01}, 344 {0x380b, 0x90}, 345 {0x3810, 0x00}, 346 {0x3811, 0x04}, 347 {0x3812, 0x00}, 348 {0x3813, 0x04}, 349 {0x3814, 0x31}, 350 {0x3815, 0x22}, 351 {0x3820, 0x60}, 352 {0x3821, 0x01}, 353 {0x4008, 0x02}, 354 {0x4009, 0x05}, 355 {0x400c, 0x00}, 356 {0x400d, 0x03}, 357 {0x4507, 0x03}, 358 {0x4509, 0x80}, 359 }; 360 361 /* Supported sensor mode configurations */ 362 static const struct ov9282_mode supported_modes[] = { 363 [MODE_1280_800] = { 364 .width = 1280, 365 .height = 800, 366 .hblank_min = { 250, 176 }, 367 .vblank = 1022, 368 .vblank_min = 110, 369 .vblank_max = 51540, 370 .link_freq_idx = 0, 371 .crop = { 372 .left = OV9282_PIXEL_ARRAY_LEFT, 373 .top = OV9282_PIXEL_ARRAY_TOP, 374 .width = 1280, 375 .height = 800 376 }, 377 .reg_list = { 378 .num_of_regs = ARRAY_SIZE(mode_1280x800_regs), 379 .regs = mode_1280x800_regs, 380 }, 381 }, 382 [MODE_1280_720] = { 383 .width = 1280, 384 .height = 720, 385 .hblank_min = { 250, 176 }, 386 .vblank = 1022, 387 .vblank_min = 41, 388 .vblank_max = 51540, 389 .link_freq_idx = 0, 390 .crop = { 391 /* 392 * Note that this mode takes the top 720 lines from the 393 * 800 of the sensor. It does not take a middle crop. 394 */ 395 .left = OV9282_PIXEL_ARRAY_LEFT, 396 .top = OV9282_PIXEL_ARRAY_TOP, 397 .width = 1280, 398 .height = 720 399 }, 400 .reg_list = { 401 .num_of_regs = ARRAY_SIZE(mode_1280x720_regs), 402 .regs = mode_1280x720_regs, 403 }, 404 }, 405 [MODE_640_400] = { 406 .width = 640, 407 .height = 400, 408 .hblank_min = { 890, 816 }, 409 .vblank = 1022, 410 .vblank_min = 22, 411 .vblank_max = 51540, 412 .link_freq_idx = 0, 413 .crop = { 414 .left = OV9282_PIXEL_ARRAY_LEFT, 415 .top = OV9282_PIXEL_ARRAY_TOP, 416 .width = 1280, 417 .height = 800 418 }, 419 .reg_list = { 420 .num_of_regs = ARRAY_SIZE(mode_640x400_regs), 421 .regs = mode_640x400_regs, 422 }, 423 }, 424 }; 425 426 /** 427 * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device. 428 * @subdev: pointer to ov9282 V4L2 sub-device 429 * 430 * Return: pointer to ov9282 device 431 */ 432 static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev) 433 { 434 return container_of(subdev, struct ov9282, sd); 435 } 436 437 /** 438 * ov9282_read_reg() - Read registers. 439 * @ov9282: pointer to ov9282 device 440 * @reg: register address 441 * @len: length of bytes to read. Max supported bytes is 4 442 * @val: pointer to register value to be filled. 443 * 444 * Return: 0 if successful, error code otherwise. 445 */ 446 static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val) 447 { 448 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd); 449 struct i2c_msg msgs[2] = {0}; 450 u8 addr_buf[2] = {0}; 451 u8 data_buf[4] = {0}; 452 int ret; 453 454 if (WARN_ON(len > 4)) 455 return -EINVAL; 456 457 put_unaligned_be16(reg, addr_buf); 458 459 /* Write register address */ 460 msgs[0].addr = client->addr; 461 msgs[0].flags = 0; 462 msgs[0].len = ARRAY_SIZE(addr_buf); 463 msgs[0].buf = addr_buf; 464 465 /* Read data from register */ 466 msgs[1].addr = client->addr; 467 msgs[1].flags = I2C_M_RD; 468 msgs[1].len = len; 469 msgs[1].buf = &data_buf[4 - len]; 470 471 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 472 if (ret != ARRAY_SIZE(msgs)) 473 return -EIO; 474 475 *val = get_unaligned_be32(data_buf); 476 477 return 0; 478 } 479 480 /** 481 * ov9282_write_reg() - Write register 482 * @ov9282: pointer to ov9282 device 483 * @reg: register address 484 * @len: length of bytes. Max supported bytes is 4 485 * @val: register value 486 * 487 * Return: 0 if successful, error code otherwise. 488 */ 489 static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val) 490 { 491 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd); 492 u8 buf[6] = {0}; 493 494 if (WARN_ON(len > 4)) 495 return -EINVAL; 496 497 put_unaligned_be16(reg, buf); 498 put_unaligned_be32(val << (8 * (4 - len)), buf + 2); 499 if (i2c_master_send(client, buf, len + 2) != len + 2) 500 return -EIO; 501 502 return 0; 503 } 504 505 /** 506 * ov9282_write_regs() - Write a list of registers 507 * @ov9282: pointer to ov9282 device 508 * @regs: list of registers to be written 509 * @len: length of registers array 510 * 511 * Return: 0 if successful, error code otherwise. 512 */ 513 static int ov9282_write_regs(struct ov9282 *ov9282, 514 const struct ov9282_reg *regs, u32 len) 515 { 516 unsigned int i; 517 int ret; 518 519 for (i = 0; i < len; i++) { 520 ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val); 521 if (ret) 522 return ret; 523 } 524 525 return 0; 526 } 527 528 /** 529 * ov9282_update_controls() - Update control ranges based on streaming mode 530 * @ov9282: pointer to ov9282 device 531 * @mode: pointer to ov9282_mode sensor mode 532 * @fmt: pointer to the requested mode 533 * 534 * Return: 0 if successful, error code otherwise. 535 */ 536 static int ov9282_update_controls(struct ov9282 *ov9282, 537 const struct ov9282_mode *mode, 538 const struct v4l2_subdev_format *fmt) 539 { 540 u32 hblank_min; 541 s64 pixel_rate; 542 int ret; 543 544 ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx); 545 if (ret) 546 return ret; 547 548 pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ? 549 OV9282_PIXEL_RATE_10BIT : OV9282_PIXEL_RATE_8BIT; 550 ret = __v4l2_ctrl_modify_range(ov9282->pixel_rate, pixel_rate, 551 pixel_rate, 1, pixel_rate); 552 if (ret) 553 return ret; 554 555 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1]; 556 ret = __v4l2_ctrl_modify_range(ov9282->hblank_ctrl, hblank_min, 557 OV9282_TIMING_HTS_MAX - mode->width, 1, 558 hblank_min); 559 if (ret) 560 return ret; 561 562 return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min, 563 mode->vblank_max, 1, mode->vblank); 564 } 565 566 /** 567 * ov9282_update_exp_gain() - Set updated exposure and gain 568 * @ov9282: pointer to ov9282 device 569 * @exposure: updated exposure value 570 * @gain: updated analog gain value 571 * 572 * Return: 0 if successful, error code otherwise. 573 */ 574 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain) 575 { 576 int ret; 577 578 dev_dbg(ov9282->dev, "Set exp %u, analog gain %u", 579 exposure, gain); 580 581 ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1); 582 if (ret) 583 return ret; 584 585 ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4); 586 if (ret) 587 goto error_release_group_hold; 588 589 ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain); 590 591 error_release_group_hold: 592 ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0); 593 594 return ret; 595 } 596 597 static int ov9282_set_ctrl_hflip(struct ov9282 *ov9282, int value) 598 { 599 u32 current_val; 600 int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1, 601 ¤t_val); 602 if (ret) 603 return ret; 604 605 if (value) 606 current_val |= OV9282_FLIP_BIT; 607 else 608 current_val &= ~OV9282_FLIP_BIT; 609 610 return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1, 611 current_val); 612 } 613 614 static int ov9282_set_ctrl_vflip(struct ov9282 *ov9282, int value) 615 { 616 u32 current_val; 617 int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1, 618 ¤t_val); 619 if (ret) 620 return ret; 621 622 if (value) 623 current_val |= OV9282_FLIP_BIT; 624 else 625 current_val &= ~OV9282_FLIP_BIT; 626 627 return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1, 628 current_val); 629 } 630 631 /** 632 * ov9282_set_ctrl() - Set subdevice control 633 * @ctrl: pointer to v4l2_ctrl structure 634 * 635 * Supported controls: 636 * - V4L2_CID_VBLANK 637 * - cluster controls: 638 * - V4L2_CID_ANALOGUE_GAIN 639 * - V4L2_CID_EXPOSURE 640 * 641 * Return: 0 if successful, error code otherwise. 642 */ 643 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl) 644 { 645 struct ov9282 *ov9282 = 646 container_of(ctrl->handler, struct ov9282, ctrl_handler); 647 u32 analog_gain; 648 u32 exposure; 649 u32 lpfr; 650 int ret; 651 652 switch (ctrl->id) { 653 case V4L2_CID_VBLANK: 654 ov9282->vblank = ov9282->vblank_ctrl->val; 655 656 dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u", 657 ov9282->vblank, 658 ov9282->vblank + ov9282->cur_mode->height); 659 660 ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl, 661 OV9282_EXPOSURE_MIN, 662 ov9282->vblank + 663 ov9282->cur_mode->height - 664 OV9282_EXPOSURE_OFFSET, 665 1, OV9282_EXPOSURE_DEFAULT); 666 break; 667 } 668 669 /* Set controls only if sensor is in power on state */ 670 if (!pm_runtime_get_if_in_use(ov9282->dev)) 671 return 0; 672 673 switch (ctrl->id) { 674 case V4L2_CID_EXPOSURE: 675 exposure = ctrl->val; 676 analog_gain = ov9282->again_ctrl->val; 677 678 dev_dbg(ov9282->dev, "Received exp %u, analog gain %u", 679 exposure, analog_gain); 680 681 ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain); 682 break; 683 case V4L2_CID_VBLANK: 684 lpfr = ov9282->vblank + ov9282->cur_mode->height; 685 ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr); 686 break; 687 case V4L2_CID_HFLIP: 688 ret = ov9282_set_ctrl_hflip(ov9282, ctrl->val); 689 break; 690 case V4L2_CID_VFLIP: 691 ret = ov9282_set_ctrl_vflip(ov9282, ctrl->val); 692 break; 693 case V4L2_CID_HBLANK: 694 ret = ov9282_write_reg(ov9282, OV9282_REG_TIMING_HTS, 2, 695 (ctrl->val + ov9282->cur_mode->width) >> 1); 696 break; 697 default: 698 dev_err(ov9282->dev, "Invalid control %d", ctrl->id); 699 ret = -EINVAL; 700 } 701 702 pm_runtime_put(ov9282->dev); 703 704 return ret; 705 } 706 707 /* V4l2 subdevice control ops*/ 708 static const struct v4l2_ctrl_ops ov9282_ctrl_ops = { 709 .s_ctrl = ov9282_set_ctrl, 710 }; 711 712 /** 713 * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes 714 * @sd: pointer to ov9282 V4L2 sub-device structure 715 * @sd_state: V4L2 sub-device configuration 716 * @code: V4L2 sub-device code enumeration need to be filled 717 * 718 * Return: 0 if successful, error code otherwise. 719 */ 720 static int ov9282_enum_mbus_code(struct v4l2_subdev *sd, 721 struct v4l2_subdev_state *sd_state, 722 struct v4l2_subdev_mbus_code_enum *code) 723 { 724 switch (code->index) { 725 case 0: 726 code->code = MEDIA_BUS_FMT_Y10_1X10; 727 break; 728 case 1: 729 code->code = MEDIA_BUS_FMT_Y8_1X8; 730 break; 731 default: 732 return -EINVAL; 733 } 734 735 return 0; 736 } 737 738 /** 739 * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes 740 * @sd: pointer to ov9282 V4L2 sub-device structure 741 * @sd_state: V4L2 sub-device configuration 742 * @fsize: V4L2 sub-device size enumeration need to be filled 743 * 744 * Return: 0 if successful, error code otherwise. 745 */ 746 static int ov9282_enum_frame_size(struct v4l2_subdev *sd, 747 struct v4l2_subdev_state *sd_state, 748 struct v4l2_subdev_frame_size_enum *fsize) 749 { 750 if (fsize->index >= ARRAY_SIZE(supported_modes)) 751 return -EINVAL; 752 753 if (fsize->code != MEDIA_BUS_FMT_Y10_1X10 && 754 fsize->code != MEDIA_BUS_FMT_Y8_1X8) 755 return -EINVAL; 756 757 fsize->min_width = supported_modes[fsize->index].width; 758 fsize->max_width = fsize->min_width; 759 fsize->min_height = supported_modes[fsize->index].height; 760 fsize->max_height = fsize->min_height; 761 762 return 0; 763 } 764 765 /** 766 * ov9282_fill_pad_format() - Fill subdevice pad format 767 * from selected sensor mode 768 * @ov9282: pointer to ov9282 device 769 * @mode: pointer to ov9282_mode sensor mode 770 * @code: mbus code to be stored 771 * @fmt: V4L2 sub-device format need to be filled 772 */ 773 static void ov9282_fill_pad_format(struct ov9282 *ov9282, 774 const struct ov9282_mode *mode, 775 u32 code, 776 struct v4l2_subdev_format *fmt) 777 { 778 fmt->format.width = mode->width; 779 fmt->format.height = mode->height; 780 fmt->format.code = code; 781 fmt->format.field = V4L2_FIELD_NONE; 782 fmt->format.colorspace = V4L2_COLORSPACE_RAW; 783 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 784 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; 785 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; 786 } 787 788 /** 789 * ov9282_get_pad_format() - Get subdevice pad format 790 * @sd: pointer to ov9282 V4L2 sub-device structure 791 * @sd_state: V4L2 sub-device configuration 792 * @fmt: V4L2 sub-device format need to be set 793 * 794 * Return: 0 if successful, error code otherwise. 795 */ 796 static int ov9282_get_pad_format(struct v4l2_subdev *sd, 797 struct v4l2_subdev_state *sd_state, 798 struct v4l2_subdev_format *fmt) 799 { 800 struct ov9282 *ov9282 = to_ov9282(sd); 801 802 mutex_lock(&ov9282->mutex); 803 804 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 805 struct v4l2_mbus_framefmt *framefmt; 806 807 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); 808 fmt->format = *framefmt; 809 } else { 810 ov9282_fill_pad_format(ov9282, ov9282->cur_mode, ov9282->code, 811 fmt); 812 } 813 814 mutex_unlock(&ov9282->mutex); 815 816 return 0; 817 } 818 819 /** 820 * ov9282_set_pad_format() - Set subdevice pad format 821 * @sd: pointer to ov9282 V4L2 sub-device structure 822 * @sd_state: V4L2 sub-device configuration 823 * @fmt: V4L2 sub-device format need to be set 824 * 825 * Return: 0 if successful, error code otherwise. 826 */ 827 static int ov9282_set_pad_format(struct v4l2_subdev *sd, 828 struct v4l2_subdev_state *sd_state, 829 struct v4l2_subdev_format *fmt) 830 { 831 struct ov9282 *ov9282 = to_ov9282(sd); 832 const struct ov9282_mode *mode; 833 u32 code; 834 int ret = 0; 835 836 mutex_lock(&ov9282->mutex); 837 838 mode = v4l2_find_nearest_size(supported_modes, 839 ARRAY_SIZE(supported_modes), 840 width, height, 841 fmt->format.width, 842 fmt->format.height); 843 if (fmt->format.code == MEDIA_BUS_FMT_Y8_1X8) 844 code = MEDIA_BUS_FMT_Y8_1X8; 845 else 846 code = MEDIA_BUS_FMT_Y10_1X10; 847 848 ov9282_fill_pad_format(ov9282, mode, code, fmt); 849 850 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 851 struct v4l2_mbus_framefmt *framefmt; 852 853 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); 854 *framefmt = fmt->format; 855 } else { 856 ret = ov9282_update_controls(ov9282, mode, fmt); 857 if (!ret) { 858 ov9282->cur_mode = mode; 859 ov9282->code = code; 860 } 861 } 862 863 mutex_unlock(&ov9282->mutex); 864 865 return ret; 866 } 867 868 /** 869 * ov9282_init_pad_cfg() - Initialize sub-device pad configuration 870 * @sd: pointer to ov9282 V4L2 sub-device structure 871 * @sd_state: V4L2 sub-device configuration 872 * 873 * Return: 0 if successful, error code otherwise. 874 */ 875 static int ov9282_init_pad_cfg(struct v4l2_subdev *sd, 876 struct v4l2_subdev_state *sd_state) 877 { 878 struct ov9282 *ov9282 = to_ov9282(sd); 879 struct v4l2_subdev_format fmt = { 0 }; 880 881 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE; 882 ov9282_fill_pad_format(ov9282, &supported_modes[DEFAULT_MODE], 883 ov9282->code, &fmt); 884 885 return ov9282_set_pad_format(sd, sd_state, &fmt); 886 } 887 888 static const struct v4l2_rect * 889 __ov9282_get_pad_crop(struct ov9282 *ov9282, 890 struct v4l2_subdev_state *sd_state, 891 unsigned int pad, enum v4l2_subdev_format_whence which) 892 { 893 switch (which) { 894 case V4L2_SUBDEV_FORMAT_TRY: 895 return v4l2_subdev_get_try_crop(&ov9282->sd, sd_state, pad); 896 case V4L2_SUBDEV_FORMAT_ACTIVE: 897 return &ov9282->cur_mode->crop; 898 } 899 900 return NULL; 901 } 902 903 static int ov9282_get_selection(struct v4l2_subdev *sd, 904 struct v4l2_subdev_state *sd_state, 905 struct v4l2_subdev_selection *sel) 906 { 907 switch (sel->target) { 908 case V4L2_SEL_TGT_CROP: { 909 struct ov9282 *ov9282 = to_ov9282(sd); 910 911 mutex_lock(&ov9282->mutex); 912 sel->r = *__ov9282_get_pad_crop(ov9282, sd_state, sel->pad, 913 sel->which); 914 mutex_unlock(&ov9282->mutex); 915 916 return 0; 917 } 918 919 case V4L2_SEL_TGT_NATIVE_SIZE: 920 sel->r.top = 0; 921 sel->r.left = 0; 922 sel->r.width = OV9282_NATIVE_WIDTH; 923 sel->r.height = OV9282_NATIVE_HEIGHT; 924 925 return 0; 926 927 case V4L2_SEL_TGT_CROP_DEFAULT: 928 case V4L2_SEL_TGT_CROP_BOUNDS: 929 sel->r.top = OV9282_PIXEL_ARRAY_TOP; 930 sel->r.left = OV9282_PIXEL_ARRAY_LEFT; 931 sel->r.width = OV9282_PIXEL_ARRAY_WIDTH; 932 sel->r.height = OV9282_PIXEL_ARRAY_HEIGHT; 933 934 return 0; 935 } 936 937 return -EINVAL; 938 } 939 940 /** 941 * ov9282_start_streaming() - Start sensor stream 942 * @ov9282: pointer to ov9282 device 943 * 944 * Return: 0 if successful, error code otherwise. 945 */ 946 static int ov9282_start_streaming(struct ov9282 *ov9282) 947 { 948 const struct ov9282_reg bitdepth_regs[2][2] = { 949 { 950 {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW10}, 951 {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW10}, 952 }, { 953 {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW8}, 954 {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW8}, 955 } 956 }; 957 const struct ov9282_reg_list *reg_list; 958 int bitdepth_index; 959 int ret; 960 961 /* Write common registers */ 962 ret = ov9282_write_regs(ov9282, common_regs_list.regs, 963 common_regs_list.num_of_regs); 964 if (ret) { 965 dev_err(ov9282->dev, "fail to write common registers"); 966 return ret; 967 } 968 969 bitdepth_index = ov9282->code == MEDIA_BUS_FMT_Y10_1X10 ? 0 : 1; 970 ret = ov9282_write_regs(ov9282, bitdepth_regs[bitdepth_index], 2); 971 if (ret) { 972 dev_err(ov9282->dev, "fail to write bitdepth regs"); 973 return ret; 974 } 975 976 /* Write sensor mode registers */ 977 reg_list = &ov9282->cur_mode->reg_list; 978 ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs); 979 if (ret) { 980 dev_err(ov9282->dev, "fail to write initial registers"); 981 return ret; 982 } 983 984 /* Setup handler will write actual exposure and gain */ 985 ret = __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler); 986 if (ret) { 987 dev_err(ov9282->dev, "fail to setup handler"); 988 return ret; 989 } 990 991 /* Start streaming */ 992 ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT, 993 1, OV9282_MODE_STREAMING); 994 if (ret) { 995 dev_err(ov9282->dev, "fail to start streaming"); 996 return ret; 997 } 998 999 return 0; 1000 } 1001 1002 /** 1003 * ov9282_stop_streaming() - Stop sensor stream 1004 * @ov9282: pointer to ov9282 device 1005 * 1006 * Return: 0 if successful, error code otherwise. 1007 */ 1008 static int ov9282_stop_streaming(struct ov9282 *ov9282) 1009 { 1010 return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT, 1011 1, OV9282_MODE_STANDBY); 1012 } 1013 1014 /** 1015 * ov9282_set_stream() - Enable sensor streaming 1016 * @sd: pointer to ov9282 subdevice 1017 * @enable: set to enable sensor streaming 1018 * 1019 * Return: 0 if successful, error code otherwise. 1020 */ 1021 static int ov9282_set_stream(struct v4l2_subdev *sd, int enable) 1022 { 1023 struct ov9282 *ov9282 = to_ov9282(sd); 1024 int ret; 1025 1026 mutex_lock(&ov9282->mutex); 1027 1028 if (ov9282->streaming == enable) { 1029 mutex_unlock(&ov9282->mutex); 1030 return 0; 1031 } 1032 1033 if (enable) { 1034 ret = pm_runtime_resume_and_get(ov9282->dev); 1035 if (ret) 1036 goto error_unlock; 1037 1038 ret = ov9282_start_streaming(ov9282); 1039 if (ret) 1040 goto error_power_off; 1041 } else { 1042 ov9282_stop_streaming(ov9282); 1043 pm_runtime_put(ov9282->dev); 1044 } 1045 1046 ov9282->streaming = enable; 1047 1048 mutex_unlock(&ov9282->mutex); 1049 1050 return 0; 1051 1052 error_power_off: 1053 pm_runtime_put(ov9282->dev); 1054 error_unlock: 1055 mutex_unlock(&ov9282->mutex); 1056 1057 return ret; 1058 } 1059 1060 /** 1061 * ov9282_detect() - Detect ov9282 sensor 1062 * @ov9282: pointer to ov9282 device 1063 * 1064 * Return: 0 if successful, -EIO if sensor id does not match 1065 */ 1066 static int ov9282_detect(struct ov9282 *ov9282) 1067 { 1068 int ret; 1069 u32 val; 1070 1071 ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 2, &val); 1072 if (ret) 1073 return ret; 1074 1075 if (val != OV9282_ID) { 1076 dev_err(ov9282->dev, "chip id mismatch: %x!=%x", 1077 OV9282_ID, val); 1078 return -ENXIO; 1079 } 1080 1081 return 0; 1082 } 1083 1084 /** 1085 * ov9282_parse_hw_config() - Parse HW configuration and check if supported 1086 * @ov9282: pointer to ov9282 device 1087 * 1088 * Return: 0 if successful, error code otherwise. 1089 */ 1090 static int ov9282_parse_hw_config(struct ov9282 *ov9282) 1091 { 1092 struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev); 1093 struct v4l2_fwnode_endpoint bus_cfg = { 1094 .bus_type = V4L2_MBUS_CSI2_DPHY 1095 }; 1096 struct fwnode_handle *ep; 1097 unsigned long rate; 1098 unsigned int i; 1099 int ret; 1100 1101 if (!fwnode) 1102 return -ENXIO; 1103 1104 /* Request optional reset pin */ 1105 ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset", 1106 GPIOD_OUT_LOW); 1107 if (IS_ERR(ov9282->reset_gpio)) { 1108 dev_err(ov9282->dev, "failed to get reset gpio %ld", 1109 PTR_ERR(ov9282->reset_gpio)); 1110 return PTR_ERR(ov9282->reset_gpio); 1111 } 1112 1113 /* Get sensor input clock */ 1114 ov9282->inclk = devm_clk_get(ov9282->dev, NULL); 1115 if (IS_ERR(ov9282->inclk)) { 1116 dev_err(ov9282->dev, "could not get inclk"); 1117 return PTR_ERR(ov9282->inclk); 1118 } 1119 1120 rate = clk_get_rate(ov9282->inclk); 1121 if (rate != OV9282_INCLK_RATE) { 1122 dev_err(ov9282->dev, "inclk frequency mismatch"); 1123 return -EINVAL; 1124 } 1125 1126 ep = fwnode_graph_get_next_endpoint(fwnode, NULL); 1127 if (!ep) 1128 return -ENXIO; 1129 1130 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); 1131 fwnode_handle_put(ep); 1132 if (ret) 1133 return ret; 1134 1135 ov9282->noncontinuous_clock = 1136 bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK; 1137 1138 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) { 1139 dev_err(ov9282->dev, 1140 "number of CSI2 data lanes %d is not supported", 1141 bus_cfg.bus.mipi_csi2.num_data_lanes); 1142 ret = -EINVAL; 1143 goto done_endpoint_free; 1144 } 1145 1146 if (!bus_cfg.nr_of_link_frequencies) { 1147 dev_err(ov9282->dev, "no link frequencies defined"); 1148 ret = -EINVAL; 1149 goto done_endpoint_free; 1150 } 1151 1152 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) 1153 if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ) 1154 goto done_endpoint_free; 1155 1156 ret = -EINVAL; 1157 1158 done_endpoint_free: 1159 v4l2_fwnode_endpoint_free(&bus_cfg); 1160 1161 return ret; 1162 } 1163 1164 /* V4l2 subdevice ops */ 1165 static const struct v4l2_subdev_core_ops ov9282_core_ops = { 1166 .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 1167 .unsubscribe_event = v4l2_event_subdev_unsubscribe, 1168 }; 1169 1170 static const struct v4l2_subdev_video_ops ov9282_video_ops = { 1171 .s_stream = ov9282_set_stream, 1172 }; 1173 1174 static const struct v4l2_subdev_pad_ops ov9282_pad_ops = { 1175 .init_cfg = ov9282_init_pad_cfg, 1176 .enum_mbus_code = ov9282_enum_mbus_code, 1177 .enum_frame_size = ov9282_enum_frame_size, 1178 .get_fmt = ov9282_get_pad_format, 1179 .set_fmt = ov9282_set_pad_format, 1180 .get_selection = ov9282_get_selection, 1181 }; 1182 1183 static const struct v4l2_subdev_ops ov9282_subdev_ops = { 1184 .core = &ov9282_core_ops, 1185 .video = &ov9282_video_ops, 1186 .pad = &ov9282_pad_ops, 1187 }; 1188 1189 /** 1190 * ov9282_power_on() - Sensor power on sequence 1191 * @dev: pointer to i2c device 1192 * 1193 * Return: 0 if successful, error code otherwise. 1194 */ 1195 static int ov9282_power_on(struct device *dev) 1196 { 1197 struct v4l2_subdev *sd = dev_get_drvdata(dev); 1198 struct ov9282 *ov9282 = to_ov9282(sd); 1199 int ret; 1200 1201 usleep_range(400, 600); 1202 1203 gpiod_set_value_cansleep(ov9282->reset_gpio, 1); 1204 1205 ret = clk_prepare_enable(ov9282->inclk); 1206 if (ret) { 1207 dev_err(ov9282->dev, "fail to enable inclk"); 1208 goto error_reset; 1209 } 1210 1211 usleep_range(400, 600); 1212 1213 ret = ov9282_write_reg(ov9282, OV9282_REG_MIPI_CTRL00, 1, 1214 ov9282->noncontinuous_clock ? 1215 OV9282_GATED_CLOCK : 0); 1216 if (ret) { 1217 dev_err(ov9282->dev, "fail to write MIPI_CTRL00"); 1218 return ret; 1219 } 1220 1221 return 0; 1222 1223 error_reset: 1224 gpiod_set_value_cansleep(ov9282->reset_gpio, 0); 1225 1226 return ret; 1227 } 1228 1229 /** 1230 * ov9282_power_off() - Sensor power off sequence 1231 * @dev: pointer to i2c device 1232 * 1233 * Return: 0 if successful, error code otherwise. 1234 */ 1235 static int ov9282_power_off(struct device *dev) 1236 { 1237 struct v4l2_subdev *sd = dev_get_drvdata(dev); 1238 struct ov9282 *ov9282 = to_ov9282(sd); 1239 1240 gpiod_set_value_cansleep(ov9282->reset_gpio, 0); 1241 1242 clk_disable_unprepare(ov9282->inclk); 1243 1244 return 0; 1245 } 1246 1247 /** 1248 * ov9282_init_controls() - Initialize sensor subdevice controls 1249 * @ov9282: pointer to ov9282 device 1250 * 1251 * Return: 0 if successful, error code otherwise. 1252 */ 1253 static int ov9282_init_controls(struct ov9282 *ov9282) 1254 { 1255 struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler; 1256 const struct ov9282_mode *mode = ov9282->cur_mode; 1257 struct v4l2_fwnode_device_properties props; 1258 u32 hblank_min; 1259 u32 lpfr; 1260 int ret; 1261 1262 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10); 1263 if (ret) 1264 return ret; 1265 1266 /* Serialize controls with sensor device */ 1267 ctrl_hdlr->lock = &ov9282->mutex; 1268 1269 /* Initialize exposure and gain */ 1270 lpfr = mode->vblank + mode->height; 1271 ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 1272 &ov9282_ctrl_ops, 1273 V4L2_CID_EXPOSURE, 1274 OV9282_EXPOSURE_MIN, 1275 lpfr - OV9282_EXPOSURE_OFFSET, 1276 OV9282_EXPOSURE_STEP, 1277 OV9282_EXPOSURE_DEFAULT); 1278 1279 ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 1280 &ov9282_ctrl_ops, 1281 V4L2_CID_ANALOGUE_GAIN, 1282 OV9282_AGAIN_MIN, 1283 OV9282_AGAIN_MAX, 1284 OV9282_AGAIN_STEP, 1285 OV9282_AGAIN_DEFAULT); 1286 1287 v4l2_ctrl_cluster(2, &ov9282->exp_ctrl); 1288 1289 ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 1290 &ov9282_ctrl_ops, 1291 V4L2_CID_VBLANK, 1292 mode->vblank_min, 1293 mode->vblank_max, 1294 1, mode->vblank); 1295 1296 v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_VFLIP, 1297 0, 1, 1, 1); 1298 1299 v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_HFLIP, 1300 0, 1, 1, 1); 1301 1302 /* Read only controls */ 1303 ov9282->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, 1304 V4L2_CID_PIXEL_RATE, 1305 OV9282_PIXEL_RATE_10BIT, 1306 OV9282_PIXEL_RATE_10BIT, 1, 1307 OV9282_PIXEL_RATE_10BIT); 1308 1309 ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, 1310 &ov9282_ctrl_ops, 1311 V4L2_CID_LINK_FREQ, 1312 ARRAY_SIZE(link_freq) - 1313 1, 1314 mode->link_freq_idx, 1315 link_freq); 1316 if (ov9282->link_freq_ctrl) 1317 ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1318 1319 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1]; 1320 ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 1321 &ov9282_ctrl_ops, 1322 V4L2_CID_HBLANK, 1323 hblank_min, 1324 OV9282_TIMING_HTS_MAX - mode->width, 1325 1, hblank_min); 1326 1327 ret = v4l2_fwnode_device_parse(ov9282->dev, &props); 1328 if (!ret) { 1329 /* Failure sets ctrl_hdlr->error, which we check afterwards anyway */ 1330 v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov9282_ctrl_ops, 1331 &props); 1332 } 1333 1334 if (ctrl_hdlr->error || ret) { 1335 dev_err(ov9282->dev, "control init failed: %d", 1336 ctrl_hdlr->error); 1337 v4l2_ctrl_handler_free(ctrl_hdlr); 1338 return ctrl_hdlr->error; 1339 } 1340 1341 ov9282->sd.ctrl_handler = ctrl_hdlr; 1342 1343 return 0; 1344 } 1345 1346 /** 1347 * ov9282_probe() - I2C client device binding 1348 * @client: pointer to i2c client device 1349 * 1350 * Return: 0 if successful, error code otherwise. 1351 */ 1352 static int ov9282_probe(struct i2c_client *client) 1353 { 1354 struct ov9282 *ov9282; 1355 int ret; 1356 1357 ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL); 1358 if (!ov9282) 1359 return -ENOMEM; 1360 1361 ov9282->dev = &client->dev; 1362 1363 /* Initialize subdev */ 1364 v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops); 1365 1366 ret = ov9282_parse_hw_config(ov9282); 1367 if (ret) { 1368 dev_err(ov9282->dev, "HW configuration is not supported"); 1369 return ret; 1370 } 1371 1372 mutex_init(&ov9282->mutex); 1373 1374 ret = ov9282_power_on(ov9282->dev); 1375 if (ret) { 1376 dev_err(ov9282->dev, "failed to power-on the sensor"); 1377 goto error_mutex_destroy; 1378 } 1379 1380 /* Check module identity */ 1381 ret = ov9282_detect(ov9282); 1382 if (ret) { 1383 dev_err(ov9282->dev, "failed to find sensor: %d", ret); 1384 goto error_power_off; 1385 } 1386 1387 /* Set default mode to first mode */ 1388 ov9282->cur_mode = &supported_modes[DEFAULT_MODE]; 1389 ov9282->code = MEDIA_BUS_FMT_Y10_1X10; 1390 ov9282->vblank = ov9282->cur_mode->vblank; 1391 1392 ret = ov9282_init_controls(ov9282); 1393 if (ret) { 1394 dev_err(ov9282->dev, "failed to init controls: %d", ret); 1395 goto error_power_off; 1396 } 1397 1398 /* Initialize subdev */ 1399 ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | 1400 V4L2_SUBDEV_FL_HAS_EVENTS; 1401 ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1402 1403 /* Initialize source pad */ 1404 ov9282->pad.flags = MEDIA_PAD_FL_SOURCE; 1405 ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad); 1406 if (ret) { 1407 dev_err(ov9282->dev, "failed to init entity pads: %d", ret); 1408 goto error_handler_free; 1409 } 1410 1411 ret = v4l2_async_register_subdev_sensor(&ov9282->sd); 1412 if (ret < 0) { 1413 dev_err(ov9282->dev, 1414 "failed to register async subdev: %d", ret); 1415 goto error_media_entity; 1416 } 1417 1418 pm_runtime_set_active(ov9282->dev); 1419 pm_runtime_enable(ov9282->dev); 1420 pm_runtime_idle(ov9282->dev); 1421 1422 return 0; 1423 1424 error_media_entity: 1425 media_entity_cleanup(&ov9282->sd.entity); 1426 error_handler_free: 1427 v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler); 1428 error_power_off: 1429 ov9282_power_off(ov9282->dev); 1430 error_mutex_destroy: 1431 mutex_destroy(&ov9282->mutex); 1432 1433 return ret; 1434 } 1435 1436 /** 1437 * ov9282_remove() - I2C client device unbinding 1438 * @client: pointer to I2C client device 1439 * 1440 * Return: 0 if successful, error code otherwise. 1441 */ 1442 static void ov9282_remove(struct i2c_client *client) 1443 { 1444 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1445 struct ov9282 *ov9282 = to_ov9282(sd); 1446 1447 v4l2_async_unregister_subdev(sd); 1448 media_entity_cleanup(&sd->entity); 1449 v4l2_ctrl_handler_free(sd->ctrl_handler); 1450 1451 pm_runtime_disable(&client->dev); 1452 if (!pm_runtime_status_suspended(&client->dev)) 1453 ov9282_power_off(&client->dev); 1454 pm_runtime_set_suspended(&client->dev); 1455 1456 mutex_destroy(&ov9282->mutex); 1457 } 1458 1459 static const struct dev_pm_ops ov9282_pm_ops = { 1460 SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL) 1461 }; 1462 1463 static const struct of_device_id ov9282_of_match[] = { 1464 { .compatible = "ovti,ov9282" }, 1465 { } 1466 }; 1467 1468 MODULE_DEVICE_TABLE(of, ov9282_of_match); 1469 1470 static struct i2c_driver ov9282_driver = { 1471 .probe_new = ov9282_probe, 1472 .remove = ov9282_remove, 1473 .driver = { 1474 .name = "ov9282", 1475 .pm = &ov9282_pm_ops, 1476 .of_match_table = ov9282_of_match, 1477 }, 1478 }; 1479 1480 module_i2c_driver(ov9282_driver); 1481 1482 MODULE_DESCRIPTION("OmniVision ov9282 sensor driver"); 1483 MODULE_LICENSE("GPL"); 1484