1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2020 MediaTek Inc. 3 4 #include <linux/clk.h> 5 #include <linux/delay.h> 6 #include <linux/device.h> 7 #include <linux/gpio/consumer.h> 8 #include <linux/i2c.h> 9 #include <linux/module.h> 10 #include <linux/pm_runtime.h> 11 #include <linux/regulator/consumer.h> 12 #include <media/media-entity.h> 13 #include <media/v4l2-async.h> 14 #include <media/v4l2-ctrls.h> 15 #include <media/v4l2-fwnode.h> 16 #include <media/v4l2-subdev.h> 17 18 #define OV02A10_ID 0x2509 19 #define OV02A10_ID_MASK GENMASK(15, 0) 20 21 #define OV02A10_REG_CHIP_ID 0x02 22 23 /* Bit[1] vertical upside down */ 24 /* Bit[0] horizontal mirror */ 25 #define REG_MIRROR_FLIP_CONTROL 0x3f 26 27 /* Orientation */ 28 #define REG_MIRROR_FLIP_ENABLE 0x03 29 30 /* Bit[2:0] MIPI transmission speed select */ 31 #define TX_SPEED_AREA_SEL 0xa1 32 #define OV02A10_MIPI_TX_SPEED_DEFAULT 0x04 33 34 #define REG_PAGE_SWITCH 0xfd 35 #define REG_GLOBAL_EFFECTIVE 0x01 36 #define REG_ENABLE BIT(0) 37 38 #define REG_SC_CTRL_MODE 0xac 39 #define SC_CTRL_MODE_STANDBY 0x00 40 #define SC_CTRL_MODE_STREAMING 0x01 41 42 /* Exposure control */ 43 #define OV02A10_EXP_SHIFT 8 44 #define OV02A10_REG_EXPOSURE_H 0x03 45 #define OV02A10_REG_EXPOSURE_L 0x04 46 #define OV02A10_EXPOSURE_MIN 4 47 #define OV02A10_EXPOSURE_MAX_MARGIN 4 48 #define OV02A10_EXPOSURE_STEP 1 49 50 /* Vblanking control */ 51 #define OV02A10_VTS_SHIFT 8 52 #define OV02A10_REG_VTS_H 0x05 53 #define OV02A10_REG_VTS_L 0x06 54 #define OV02A10_VTS_MAX 0x209f 55 #define OV02A10_BASE_LINES 1224 56 57 /* Analog gain control */ 58 #define OV02A10_REG_GAIN 0x24 59 #define OV02A10_GAIN_MIN 0x10 60 #define OV02A10_GAIN_MAX 0xf8 61 #define OV02A10_GAIN_STEP 0x01 62 #define OV02A10_GAIN_DEFAULT 0x40 63 64 /* Test pattern control */ 65 #define OV02A10_REG_TEST_PATTERN 0xb6 66 67 #define HZ_PER_MHZ 1000000L 68 #define OV02A10_LINK_FREQ_390MHZ (390 * HZ_PER_MHZ) 69 #define OV02A10_ECLK_FREQ (24 * HZ_PER_MHZ) 70 71 /* Number of lanes supported by this driver */ 72 #define OV02A10_DATA_LANES 1 73 74 /* Bits per sample of sensor output */ 75 #define OV02A10_BITS_PER_SAMPLE 10 76 77 static const char * const ov02a10_supply_names[] = { 78 "dovdd", /* Digital I/O power */ 79 "avdd", /* Analog power */ 80 "dvdd", /* Digital core power */ 81 }; 82 83 struct ov02a10_reg { 84 u8 addr; 85 u8 val; 86 }; 87 88 struct ov02a10_reg_list { 89 u32 num_of_regs; 90 const struct ov02a10_reg *regs; 91 }; 92 93 struct ov02a10_mode { 94 u32 width; 95 u32 height; 96 u32 exp_def; 97 u32 hts_def; 98 u32 vts_def; 99 const struct ov02a10_reg_list reg_list; 100 }; 101 102 struct ov02a10 { 103 u32 eclk_freq; 104 /* Indication of MIPI transmission speed select */ 105 u32 mipi_clock_voltage; 106 107 struct clk *eclk; 108 struct gpio_desc *pd_gpio; 109 struct gpio_desc *rst_gpio; 110 struct regulator_bulk_data supplies[ARRAY_SIZE(ov02a10_supply_names)]; 111 112 bool streaming; 113 bool upside_down; 114 115 /* 116 * Serialize control access, get/set format, get selection 117 * and start streaming. 118 */ 119 struct mutex mutex; 120 struct v4l2_subdev subdev; 121 struct media_pad pad; 122 struct v4l2_mbus_framefmt fmt; 123 struct v4l2_ctrl_handler ctrl_handler; 124 struct v4l2_ctrl *exposure; 125 126 const struct ov02a10_mode *cur_mode; 127 }; 128 129 static inline struct ov02a10 *to_ov02a10(struct v4l2_subdev *sd) 130 { 131 return container_of(sd, struct ov02a10, subdev); 132 } 133 134 /* 135 * eclk 24Mhz 136 * pclk 39Mhz 137 * linelength 934(0x3a6) 138 * framelength 1390(0x56E) 139 * grabwindow_width 1600 140 * grabwindow_height 1200 141 * max_framerate 30fps 142 * mipi_datarate per lane 780Mbps 143 */ 144 static const struct ov02a10_reg ov02a10_1600x1200_regs[] = { 145 {0xfd, 0x01}, 146 {0xac, 0x00}, 147 {0xfd, 0x00}, 148 {0x2f, 0x29}, 149 {0x34, 0x00}, 150 {0x35, 0x21}, 151 {0x30, 0x15}, 152 {0x33, 0x01}, 153 {0xfd, 0x01}, 154 {0x44, 0x00}, 155 {0x2a, 0x4c}, 156 {0x2b, 0x1e}, 157 {0x2c, 0x60}, 158 {0x25, 0x11}, 159 {0x03, 0x01}, 160 {0x04, 0xae}, 161 {0x09, 0x00}, 162 {0x0a, 0x02}, 163 {0x06, 0xa6}, 164 {0x31, 0x00}, 165 {0x24, 0x40}, 166 {0x01, 0x01}, 167 {0xfb, 0x73}, 168 {0xfd, 0x01}, 169 {0x16, 0x04}, 170 {0x1c, 0x09}, 171 {0x21, 0x42}, 172 {0x12, 0x04}, 173 {0x13, 0x10}, 174 {0x11, 0x40}, 175 {0x33, 0x81}, 176 {0xd0, 0x00}, 177 {0xd1, 0x01}, 178 {0xd2, 0x00}, 179 {0x50, 0x10}, 180 {0x51, 0x23}, 181 {0x52, 0x20}, 182 {0x53, 0x10}, 183 {0x54, 0x02}, 184 {0x55, 0x20}, 185 {0x56, 0x02}, 186 {0x58, 0x48}, 187 {0x5d, 0x15}, 188 {0x5e, 0x05}, 189 {0x66, 0x66}, 190 {0x68, 0x68}, 191 {0x6b, 0x00}, 192 {0x6c, 0x00}, 193 {0x6f, 0x40}, 194 {0x70, 0x40}, 195 {0x71, 0x0a}, 196 {0x72, 0xf0}, 197 {0x73, 0x10}, 198 {0x75, 0x80}, 199 {0x76, 0x10}, 200 {0x84, 0x00}, 201 {0x85, 0x10}, 202 {0x86, 0x10}, 203 {0x87, 0x00}, 204 {0x8a, 0x22}, 205 {0x8b, 0x22}, 206 {0x19, 0xf1}, 207 {0x29, 0x01}, 208 {0xfd, 0x01}, 209 {0x9d, 0x16}, 210 {0xa0, 0x29}, 211 {0xa1, 0x04}, 212 {0xad, 0x62}, 213 {0xae, 0x00}, 214 {0xaf, 0x85}, 215 {0xb1, 0x01}, 216 {0x8e, 0x06}, 217 {0x8f, 0x40}, 218 {0x90, 0x04}, 219 {0x91, 0xb0}, 220 {0x45, 0x01}, 221 {0x46, 0x00}, 222 {0x47, 0x6c}, 223 {0x48, 0x03}, 224 {0x49, 0x8b}, 225 {0x4a, 0x00}, 226 {0x4b, 0x07}, 227 {0x4c, 0x04}, 228 {0x4d, 0xb7}, 229 {0xf0, 0x40}, 230 {0xf1, 0x40}, 231 {0xf2, 0x40}, 232 {0xf3, 0x40}, 233 {0x3f, 0x00}, 234 {0xfd, 0x01}, 235 {0x05, 0x00}, 236 {0x06, 0xa6}, 237 {0xfd, 0x01}, 238 }; 239 240 static const char * const ov02a10_test_pattern_menu[] = { 241 "Disabled", 242 "Eight Vertical Colour Bars", 243 }; 244 245 static const s64 link_freq_menu_items[] = { 246 OV02A10_LINK_FREQ_390MHZ, 247 }; 248 249 static u64 to_pixel_rate(u32 f_index) 250 { 251 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV02A10_DATA_LANES; 252 253 do_div(pixel_rate, OV02A10_BITS_PER_SAMPLE); 254 255 return pixel_rate; 256 } 257 258 static const struct ov02a10_mode supported_modes[] = { 259 { 260 .width = 1600, 261 .height = 1200, 262 .exp_def = 0x01ae, 263 .hts_def = 0x03a6, 264 .vts_def = 0x056e, 265 .reg_list = { 266 .num_of_regs = ARRAY_SIZE(ov02a10_1600x1200_regs), 267 .regs = ov02a10_1600x1200_regs, 268 }, 269 }, 270 }; 271 272 static int ov02a10_write_array(struct ov02a10 *ov02a10, 273 const struct ov02a10_reg_list *r_list) 274 { 275 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 276 unsigned int i; 277 int ret; 278 279 for (i = 0; i < r_list->num_of_regs; i++) { 280 ret = i2c_smbus_write_byte_data(client, r_list->regs[i].addr, 281 r_list->regs[i].val); 282 if (ret < 0) 283 return ret; 284 } 285 286 return 0; 287 } 288 289 static void ov02a10_fill_fmt(const struct ov02a10_mode *mode, 290 struct v4l2_mbus_framefmt *fmt) 291 { 292 fmt->width = mode->width; 293 fmt->height = mode->height; 294 fmt->field = V4L2_FIELD_NONE; 295 } 296 297 static int ov02a10_set_fmt(struct v4l2_subdev *sd, 298 struct v4l2_subdev_pad_config *cfg, 299 struct v4l2_subdev_format *fmt) 300 { 301 struct ov02a10 *ov02a10 = to_ov02a10(sd); 302 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format; 303 struct v4l2_mbus_framefmt *frame_fmt; 304 int ret = 0; 305 306 mutex_lock(&ov02a10->mutex); 307 308 if (ov02a10->streaming && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 309 ret = -EBUSY; 310 goto out_unlock; 311 } 312 313 /* Only one sensor mode supported */ 314 mbus_fmt->code = ov02a10->fmt.code; 315 ov02a10_fill_fmt(ov02a10->cur_mode, mbus_fmt); 316 317 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) 318 frame_fmt = v4l2_subdev_get_try_format(sd, cfg, 0); 319 else 320 frame_fmt = &ov02a10->fmt; 321 322 *frame_fmt = *mbus_fmt; 323 324 out_unlock: 325 mutex_unlock(&ov02a10->mutex); 326 return ret; 327 } 328 329 static int ov02a10_get_fmt(struct v4l2_subdev *sd, 330 struct v4l2_subdev_pad_config *cfg, 331 struct v4l2_subdev_format *fmt) 332 { 333 struct ov02a10 *ov02a10 = to_ov02a10(sd); 334 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format; 335 336 mutex_lock(&ov02a10->mutex); 337 338 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 339 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad); 340 } else { 341 fmt->format = ov02a10->fmt; 342 mbus_fmt->code = ov02a10->fmt.code; 343 ov02a10_fill_fmt(ov02a10->cur_mode, mbus_fmt); 344 } 345 346 mutex_unlock(&ov02a10->mutex); 347 348 return 0; 349 } 350 351 static int ov02a10_enum_mbus_code(struct v4l2_subdev *sd, 352 struct v4l2_subdev_pad_config *cfg, 353 struct v4l2_subdev_mbus_code_enum *code) 354 { 355 struct ov02a10 *ov02a10 = to_ov02a10(sd); 356 357 if (code->index != 0) 358 return -EINVAL; 359 360 code->code = ov02a10->fmt.code; 361 362 return 0; 363 } 364 365 static int ov02a10_enum_frame_sizes(struct v4l2_subdev *sd, 366 struct v4l2_subdev_pad_config *cfg, 367 struct v4l2_subdev_frame_size_enum *fse) 368 { 369 if (fse->index >= ARRAY_SIZE(supported_modes)) 370 return -EINVAL; 371 372 fse->min_width = supported_modes[fse->index].width; 373 fse->max_width = supported_modes[fse->index].width; 374 fse->max_height = supported_modes[fse->index].height; 375 fse->min_height = supported_modes[fse->index].height; 376 377 return 0; 378 } 379 380 static int ov02a10_check_sensor_id(struct ov02a10 *ov02a10) 381 { 382 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 383 u16 chip_id; 384 int ret; 385 386 /* Validate the chip ID */ 387 ret = i2c_smbus_read_word_swapped(client, OV02A10_REG_CHIP_ID); 388 if (ret < 0) 389 return ret; 390 391 chip_id = le16_to_cpu((__force __le16)ret); 392 393 if ((chip_id & OV02A10_ID_MASK) != OV02A10_ID) { 394 dev_err(&client->dev, "unexpected sensor id(0x%04x)\n", chip_id); 395 return -EINVAL; 396 } 397 398 return 0; 399 } 400 401 static int ov02a10_power_on(struct device *dev) 402 { 403 struct i2c_client *client = to_i2c_client(dev); 404 struct v4l2_subdev *sd = i2c_get_clientdata(client); 405 struct ov02a10 *ov02a10 = to_ov02a10(sd); 406 int ret; 407 408 gpiod_set_value_cansleep(ov02a10->rst_gpio, 1); 409 gpiod_set_value_cansleep(ov02a10->pd_gpio, 1); 410 411 ret = clk_prepare_enable(ov02a10->eclk); 412 if (ret < 0) { 413 dev_err(dev, "failed to enable eclk\n"); 414 return ret; 415 } 416 417 ret = regulator_bulk_enable(ARRAY_SIZE(ov02a10_supply_names), 418 ov02a10->supplies); 419 if (ret < 0) { 420 dev_err(dev, "failed to enable regulators\n"); 421 goto disable_clk; 422 } 423 usleep_range(5000, 6000); 424 425 gpiod_set_value_cansleep(ov02a10->pd_gpio, 0); 426 usleep_range(5000, 6000); 427 428 gpiod_set_value_cansleep(ov02a10->rst_gpio, 0); 429 usleep_range(5000, 6000); 430 431 ret = ov02a10_check_sensor_id(ov02a10); 432 if (ret) 433 goto disable_regulator; 434 435 return 0; 436 437 disable_regulator: 438 regulator_bulk_disable(ARRAY_SIZE(ov02a10_supply_names), 439 ov02a10->supplies); 440 disable_clk: 441 clk_disable_unprepare(ov02a10->eclk); 442 443 return ret; 444 } 445 446 static int ov02a10_power_off(struct device *dev) 447 { 448 struct i2c_client *client = to_i2c_client(dev); 449 struct v4l2_subdev *sd = i2c_get_clientdata(client); 450 struct ov02a10 *ov02a10 = to_ov02a10(sd); 451 452 gpiod_set_value_cansleep(ov02a10->rst_gpio, 1); 453 clk_disable_unprepare(ov02a10->eclk); 454 gpiod_set_value_cansleep(ov02a10->pd_gpio, 1); 455 regulator_bulk_disable(ARRAY_SIZE(ov02a10_supply_names), 456 ov02a10->supplies); 457 458 return 0; 459 } 460 461 static int __ov02a10_start_stream(struct ov02a10 *ov02a10) 462 { 463 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 464 const struct ov02a10_reg_list *reg_list; 465 int ret; 466 467 /* Apply default values of current mode */ 468 reg_list = &ov02a10->cur_mode->reg_list; 469 ret = ov02a10_write_array(ov02a10, reg_list); 470 if (ret) 471 return ret; 472 473 /* Apply customized values from user */ 474 ret = __v4l2_ctrl_handler_setup(ov02a10->subdev.ctrl_handler); 475 if (ret) 476 return ret; 477 478 /* Set orientation to 180 degree */ 479 if (ov02a10->upside_down) { 480 ret = i2c_smbus_write_byte_data(client, REG_MIRROR_FLIP_CONTROL, 481 REG_MIRROR_FLIP_ENABLE); 482 if (ret < 0) { 483 dev_err(&client->dev, "failed to set orientation\n"); 484 return ret; 485 } 486 ret = i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE, 487 REG_ENABLE); 488 if (ret < 0) 489 return ret; 490 } 491 492 /* Set MIPI TX speed according to DT property */ 493 if (ov02a10->mipi_clock_voltage != OV02A10_MIPI_TX_SPEED_DEFAULT) { 494 ret = i2c_smbus_write_byte_data(client, TX_SPEED_AREA_SEL, 495 ov02a10->mipi_clock_voltage); 496 if (ret < 0) 497 return ret; 498 } 499 500 /* Set stream on register */ 501 return i2c_smbus_write_byte_data(client, REG_SC_CTRL_MODE, 502 SC_CTRL_MODE_STREAMING); 503 } 504 505 static int __ov02a10_stop_stream(struct ov02a10 *ov02a10) 506 { 507 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 508 509 return i2c_smbus_write_byte_data(client, REG_SC_CTRL_MODE, 510 SC_CTRL_MODE_STANDBY); 511 } 512 513 static int ov02a10_entity_init_cfg(struct v4l2_subdev *sd, 514 struct v4l2_subdev_pad_config *cfg) 515 { 516 struct v4l2_subdev_format fmt = { 517 .which = V4L2_SUBDEV_FORMAT_TRY, 518 .format = { 519 .width = 1600, 520 .height = 1200, 521 } 522 }; 523 524 ov02a10_set_fmt(sd, cfg, &fmt); 525 526 return 0; 527 } 528 529 static int ov02a10_s_stream(struct v4l2_subdev *sd, int on) 530 { 531 struct ov02a10 *ov02a10 = to_ov02a10(sd); 532 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 533 int ret; 534 535 mutex_lock(&ov02a10->mutex); 536 537 if (ov02a10->streaming == on) { 538 ret = 0; 539 goto unlock_and_return; 540 } 541 542 if (on) { 543 ret = pm_runtime_resume_and_get(&client->dev); 544 if (ret < 0) 545 goto unlock_and_return; 546 547 ret = __ov02a10_start_stream(ov02a10); 548 if (ret) { 549 __ov02a10_stop_stream(ov02a10); 550 ov02a10->streaming = !on; 551 goto err_rpm_put; 552 } 553 } else { 554 __ov02a10_stop_stream(ov02a10); 555 pm_runtime_put(&client->dev); 556 } 557 558 ov02a10->streaming = on; 559 mutex_unlock(&ov02a10->mutex); 560 561 return 0; 562 563 err_rpm_put: 564 pm_runtime_put(&client->dev); 565 unlock_and_return: 566 mutex_unlock(&ov02a10->mutex); 567 568 return ret; 569 } 570 571 static const struct dev_pm_ops ov02a10_pm_ops = { 572 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 573 pm_runtime_force_resume) 574 SET_RUNTIME_PM_OPS(ov02a10_power_off, ov02a10_power_on, NULL) 575 }; 576 577 static int ov02a10_set_exposure(struct ov02a10 *ov02a10, int val) 578 { 579 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 580 int ret; 581 582 ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE); 583 if (ret < 0) 584 return ret; 585 586 ret = i2c_smbus_write_byte_data(client, OV02A10_REG_EXPOSURE_H, 587 val >> OV02A10_EXP_SHIFT); 588 if (ret < 0) 589 return ret; 590 591 ret = i2c_smbus_write_byte_data(client, OV02A10_REG_EXPOSURE_L, val); 592 if (ret < 0) 593 return ret; 594 595 return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE, 596 REG_ENABLE); 597 } 598 599 static int ov02a10_set_gain(struct ov02a10 *ov02a10, int val) 600 { 601 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 602 int ret; 603 604 ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE); 605 if (ret < 0) 606 return ret; 607 608 ret = i2c_smbus_write_byte_data(client, OV02A10_REG_GAIN, val); 609 if (ret < 0) 610 return ret; 611 612 return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE, 613 REG_ENABLE); 614 } 615 616 static int ov02a10_set_vblank(struct ov02a10 *ov02a10, int val) 617 { 618 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 619 u32 vts = val + ov02a10->cur_mode->height - OV02A10_BASE_LINES; 620 int ret; 621 622 ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE); 623 if (ret < 0) 624 return ret; 625 626 ret = i2c_smbus_write_byte_data(client, OV02A10_REG_VTS_H, 627 vts >> OV02A10_VTS_SHIFT); 628 if (ret < 0) 629 return ret; 630 631 ret = i2c_smbus_write_byte_data(client, OV02A10_REG_VTS_L, vts); 632 if (ret < 0) 633 return ret; 634 635 return i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE, 636 REG_ENABLE); 637 } 638 639 static int ov02a10_set_test_pattern(struct ov02a10 *ov02a10, int pattern) 640 { 641 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 642 int ret; 643 644 ret = i2c_smbus_write_byte_data(client, REG_PAGE_SWITCH, REG_ENABLE); 645 if (ret < 0) 646 return ret; 647 648 ret = i2c_smbus_write_byte_data(client, OV02A10_REG_TEST_PATTERN, 649 pattern); 650 if (ret < 0) 651 return ret; 652 653 ret = i2c_smbus_write_byte_data(client, REG_GLOBAL_EFFECTIVE, 654 REG_ENABLE); 655 if (ret < 0) 656 return ret; 657 658 return i2c_smbus_write_byte_data(client, REG_SC_CTRL_MODE, 659 SC_CTRL_MODE_STREAMING); 660 } 661 662 static int ov02a10_set_ctrl(struct v4l2_ctrl *ctrl) 663 { 664 struct ov02a10 *ov02a10 = container_of(ctrl->handler, 665 struct ov02a10, ctrl_handler); 666 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 667 s64 max_expo; 668 int ret; 669 670 /* Propagate change of current control to all related controls */ 671 if (ctrl->id == V4L2_CID_VBLANK) { 672 /* Update max exposure while meeting expected vblanking */ 673 max_expo = ov02a10->cur_mode->height + ctrl->val - 674 OV02A10_EXPOSURE_MAX_MARGIN; 675 __v4l2_ctrl_modify_range(ov02a10->exposure, 676 ov02a10->exposure->minimum, max_expo, 677 ov02a10->exposure->step, 678 ov02a10->exposure->default_value); 679 } 680 681 /* V4L2 controls values will be applied only when power is already up */ 682 if (!pm_runtime_get_if_in_use(&client->dev)) 683 return 0; 684 685 switch (ctrl->id) { 686 case V4L2_CID_EXPOSURE: 687 ret = ov02a10_set_exposure(ov02a10, ctrl->val); 688 break; 689 case V4L2_CID_ANALOGUE_GAIN: 690 ret = ov02a10_set_gain(ov02a10, ctrl->val); 691 break; 692 case V4L2_CID_VBLANK: 693 ret = ov02a10_set_vblank(ov02a10, ctrl->val); 694 break; 695 case V4L2_CID_TEST_PATTERN: 696 ret = ov02a10_set_test_pattern(ov02a10, ctrl->val); 697 break; 698 default: 699 ret = -EINVAL; 700 break; 701 } 702 703 pm_runtime_put(&client->dev); 704 705 return ret; 706 } 707 708 static const struct v4l2_subdev_video_ops ov02a10_video_ops = { 709 .s_stream = ov02a10_s_stream, 710 }; 711 712 static const struct v4l2_subdev_pad_ops ov02a10_pad_ops = { 713 .init_cfg = ov02a10_entity_init_cfg, 714 .enum_mbus_code = ov02a10_enum_mbus_code, 715 .enum_frame_size = ov02a10_enum_frame_sizes, 716 .get_fmt = ov02a10_get_fmt, 717 .set_fmt = ov02a10_set_fmt, 718 }; 719 720 static const struct v4l2_subdev_ops ov02a10_subdev_ops = { 721 .video = &ov02a10_video_ops, 722 .pad = &ov02a10_pad_ops, 723 }; 724 725 static const struct media_entity_operations ov02a10_subdev_entity_ops = { 726 .link_validate = v4l2_subdev_link_validate, 727 }; 728 729 static const struct v4l2_ctrl_ops ov02a10_ctrl_ops = { 730 .s_ctrl = ov02a10_set_ctrl, 731 }; 732 733 static int ov02a10_initialize_controls(struct ov02a10 *ov02a10) 734 { 735 struct i2c_client *client = v4l2_get_subdevdata(&ov02a10->subdev); 736 const struct ov02a10_mode *mode; 737 struct v4l2_ctrl_handler *handler; 738 struct v4l2_ctrl *ctrl; 739 s64 exposure_max; 740 s64 vblank_def; 741 s64 pixel_rate; 742 s64 h_blank; 743 int ret; 744 745 handler = &ov02a10->ctrl_handler; 746 mode = ov02a10->cur_mode; 747 ret = v4l2_ctrl_handler_init(handler, 7); 748 if (ret) 749 return ret; 750 751 handler->lock = &ov02a10->mutex; 752 753 ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ, 0, 0, 754 link_freq_menu_items); 755 if (ctrl) 756 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 757 758 pixel_rate = to_pixel_rate(0); 759 v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 0, pixel_rate, 1, 760 pixel_rate); 761 762 h_blank = mode->hts_def - mode->width; 763 v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK, h_blank, h_blank, 1, 764 h_blank); 765 766 vblank_def = mode->vts_def - mode->height; 767 v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops, V4L2_CID_VBLANK, 768 vblank_def, OV02A10_VTS_MAX - mode->height, 1, 769 vblank_def); 770 771 exposure_max = mode->vts_def - 4; 772 ov02a10->exposure = v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops, 773 V4L2_CID_EXPOSURE, 774 OV02A10_EXPOSURE_MIN, 775 exposure_max, 776 OV02A10_EXPOSURE_STEP, 777 mode->exp_def); 778 779 v4l2_ctrl_new_std(handler, &ov02a10_ctrl_ops, 780 V4L2_CID_ANALOGUE_GAIN, OV02A10_GAIN_MIN, 781 OV02A10_GAIN_MAX, OV02A10_GAIN_STEP, 782 OV02A10_GAIN_DEFAULT); 783 784 v4l2_ctrl_new_std_menu_items(handler, &ov02a10_ctrl_ops, 785 V4L2_CID_TEST_PATTERN, 786 ARRAY_SIZE(ov02a10_test_pattern_menu) - 1, 787 0, 0, ov02a10_test_pattern_menu); 788 789 if (handler->error) { 790 ret = handler->error; 791 dev_err(&client->dev, "failed to init controls(%d)\n", ret); 792 goto err_free_handler; 793 } 794 795 ov02a10->subdev.ctrl_handler = handler; 796 797 return 0; 798 799 err_free_handler: 800 v4l2_ctrl_handler_free(handler); 801 802 return ret; 803 } 804 805 static int ov02a10_check_hwcfg(struct device *dev, struct ov02a10 *ov02a10) 806 { 807 struct fwnode_handle *ep; 808 struct fwnode_handle *fwnode = dev_fwnode(dev); 809 struct v4l2_fwnode_endpoint bus_cfg = { 810 .bus_type = V4L2_MBUS_CSI2_DPHY, 811 }; 812 unsigned int i, j; 813 u32 clk_volt; 814 int ret; 815 816 if (!fwnode) 817 return -EINVAL; 818 819 ep = fwnode_graph_get_next_endpoint(fwnode, NULL); 820 if (!ep) 821 return -ENXIO; 822 823 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); 824 fwnode_handle_put(ep); 825 if (ret) 826 return ret; 827 828 /* Optional indication of MIPI clock voltage unit */ 829 ret = fwnode_property_read_u32(ep, "ovti,mipi-clock-voltage", 830 &clk_volt); 831 832 if (!ret) 833 ov02a10->mipi_clock_voltage = clk_volt; 834 835 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) { 836 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) { 837 if (link_freq_menu_items[i] == 838 bus_cfg.link_frequencies[j]) 839 break; 840 } 841 842 if (j == bus_cfg.nr_of_link_frequencies) { 843 dev_err(dev, "no link frequency %lld supported\n", 844 link_freq_menu_items[i]); 845 ret = -EINVAL; 846 break; 847 } 848 } 849 850 v4l2_fwnode_endpoint_free(&bus_cfg); 851 852 return ret; 853 } 854 855 static int ov02a10_probe(struct i2c_client *client) 856 { 857 struct device *dev = &client->dev; 858 struct ov02a10 *ov02a10; 859 unsigned int i; 860 unsigned int rotation; 861 int ret; 862 863 ov02a10 = devm_kzalloc(dev, sizeof(*ov02a10), GFP_KERNEL); 864 if (!ov02a10) 865 return -ENOMEM; 866 867 ret = ov02a10_check_hwcfg(dev, ov02a10); 868 if (ret) 869 return dev_err_probe(dev, ret, 870 "failed to check HW configuration\n"); 871 872 v4l2_i2c_subdev_init(&ov02a10->subdev, client, &ov02a10_subdev_ops); 873 874 ov02a10->mipi_clock_voltage = OV02A10_MIPI_TX_SPEED_DEFAULT; 875 ov02a10->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10; 876 877 /* Optional indication of physical rotation of sensor */ 878 rotation = 0; 879 device_property_read_u32(dev, "rotation", &rotation); 880 if (rotation == 180) { 881 ov02a10->upside_down = true; 882 ov02a10->fmt.code = MEDIA_BUS_FMT_SRGGB10_1X10; 883 } 884 885 ov02a10->eclk = devm_clk_get(dev, "eclk"); 886 if (IS_ERR(ov02a10->eclk)) 887 return dev_err_probe(dev, PTR_ERR(ov02a10->eclk), 888 "failed to get eclk\n"); 889 890 ret = device_property_read_u32(dev, "clock-frequency", 891 &ov02a10->eclk_freq); 892 if (ret < 0) 893 return dev_err_probe(dev, ret, 894 "failed to get eclk frequency\n"); 895 896 ret = clk_set_rate(ov02a10->eclk, ov02a10->eclk_freq); 897 if (ret < 0) 898 return dev_err_probe(dev, ret, 899 "failed to set eclk frequency (24MHz)\n"); 900 901 if (clk_get_rate(ov02a10->eclk) != OV02A10_ECLK_FREQ) 902 dev_warn(dev, "eclk mismatched, mode is based on 24MHz\n"); 903 904 ov02a10->pd_gpio = devm_gpiod_get(dev, "powerdown", GPIOD_OUT_HIGH); 905 if (IS_ERR(ov02a10->pd_gpio)) 906 return dev_err_probe(dev, PTR_ERR(ov02a10->pd_gpio), 907 "failed to get powerdown-gpios\n"); 908 909 ov02a10->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 910 if (IS_ERR(ov02a10->rst_gpio)) 911 return dev_err_probe(dev, PTR_ERR(ov02a10->rst_gpio), 912 "failed to get reset-gpios\n"); 913 914 for (i = 0; i < ARRAY_SIZE(ov02a10_supply_names); i++) 915 ov02a10->supplies[i].supply = ov02a10_supply_names[i]; 916 917 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov02a10_supply_names), 918 ov02a10->supplies); 919 if (ret) 920 return dev_err_probe(dev, ret, "failed to get regulators\n"); 921 922 mutex_init(&ov02a10->mutex); 923 924 /* Set default mode */ 925 ov02a10->cur_mode = &supported_modes[0]; 926 927 ret = ov02a10_initialize_controls(ov02a10); 928 if (ret) { 929 dev_err_probe(dev, ret, "failed to initialize controls\n"); 930 goto err_destroy_mutex; 931 } 932 933 /* Initialize subdev */ 934 ov02a10->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 935 ov02a10->subdev.entity.ops = &ov02a10_subdev_entity_ops; 936 ov02a10->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; 937 ov02a10->pad.flags = MEDIA_PAD_FL_SOURCE; 938 939 ret = media_entity_pads_init(&ov02a10->subdev.entity, 1, &ov02a10->pad); 940 if (ret < 0) { 941 dev_err_probe(dev, ret, "failed to initialize entity pads\n"); 942 goto err_free_handler; 943 } 944 945 pm_runtime_enable(dev); 946 if (!pm_runtime_enabled(dev)) { 947 ret = ov02a10_power_on(dev); 948 if (ret < 0) { 949 dev_err_probe(dev, ret, "failed to power on\n"); 950 goto err_clean_entity; 951 } 952 } 953 954 ret = v4l2_async_register_subdev(&ov02a10->subdev); 955 if (ret) { 956 dev_err_probe(dev, ret, "failed to register V4L2 subdev\n"); 957 goto err_power_off; 958 } 959 960 return 0; 961 962 err_power_off: 963 if (pm_runtime_enabled(dev)) 964 pm_runtime_disable(dev); 965 else 966 ov02a10_power_off(dev); 967 err_clean_entity: 968 media_entity_cleanup(&ov02a10->subdev.entity); 969 err_free_handler: 970 v4l2_ctrl_handler_free(ov02a10->subdev.ctrl_handler); 971 err_destroy_mutex: 972 mutex_destroy(&ov02a10->mutex); 973 974 return ret; 975 } 976 977 static int ov02a10_remove(struct i2c_client *client) 978 { 979 struct v4l2_subdev *sd = i2c_get_clientdata(client); 980 struct ov02a10 *ov02a10 = to_ov02a10(sd); 981 982 v4l2_async_unregister_subdev(sd); 983 media_entity_cleanup(&sd->entity); 984 v4l2_ctrl_handler_free(sd->ctrl_handler); 985 pm_runtime_disable(&client->dev); 986 if (!pm_runtime_status_suspended(&client->dev)) 987 ov02a10_power_off(&client->dev); 988 pm_runtime_set_suspended(&client->dev); 989 mutex_destroy(&ov02a10->mutex); 990 991 return 0; 992 } 993 994 static const struct of_device_id ov02a10_of_match[] = { 995 { .compatible = "ovti,ov02a10" }, 996 {} 997 }; 998 MODULE_DEVICE_TABLE(of, ov02a10_of_match); 999 1000 static struct i2c_driver ov02a10_i2c_driver = { 1001 .driver = { 1002 .name = "ov02a10", 1003 .pm = &ov02a10_pm_ops, 1004 .of_match_table = ov02a10_of_match, 1005 }, 1006 .probe_new = &ov02a10_probe, 1007 .remove = &ov02a10_remove, 1008 }; 1009 module_i2c_driver(ov02a10_i2c_driver); 1010 1011 MODULE_AUTHOR("Dongchun Zhu <dongchun.zhu@mediatek.com>"); 1012 MODULE_DESCRIPTION("OmniVision OV02A10 sensor driver"); 1013 MODULE_LICENSE("GPL v2"); 1014