1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Omnivision OV2680 CMOS Image Sensor driver 4 * 5 * Copyright (C) 2018 Linaro Ltd 6 * 7 * Based on OV5640 Sensor Driver 8 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved. 9 * Copyright (C) 2014-2017 Mentor Graphics Inc. 10 * 11 */ 12 13 #include <asm/unaligned.h> 14 #include <linux/clk.h> 15 #include <linux/delay.h> 16 #include <linux/err.h> 17 #include <linux/i2c.h> 18 #include <linux/init.h> 19 #include <linux/mod_devicetable.h> 20 #include <linux/module.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/regulator/consumer.h> 23 24 #include <media/v4l2-common.h> 25 #include <media/v4l2-ctrls.h> 26 #include <media/v4l2-subdev.h> 27 28 #define OV2680_XVCLK_VALUE 24000000 29 30 #define OV2680_CHIP_ID 0x2680 31 32 #define OV2680_REG_STREAM_CTRL 0x0100 33 #define OV2680_REG_SOFT_RESET 0x0103 34 35 #define OV2680_REG_CHIP_ID_HIGH 0x300a 36 #define OV2680_REG_CHIP_ID_LOW 0x300b 37 38 #define OV2680_REG_R_MANUAL 0x3503 39 #define OV2680_REG_GAIN_PK 0x350a 40 #define OV2680_REG_EXPOSURE_PK_HIGH 0x3500 41 #define OV2680_REG_TIMING_HTS 0x380c 42 #define OV2680_REG_TIMING_VTS 0x380e 43 #define OV2680_REG_FORMAT1 0x3820 44 #define OV2680_REG_FORMAT2 0x3821 45 46 #define OV2680_REG_ISP_CTRL00 0x5080 47 48 #define OV2680_FRAME_RATE 30 49 50 #define OV2680_REG_VALUE_8BIT 1 51 #define OV2680_REG_VALUE_16BIT 2 52 #define OV2680_REG_VALUE_24BIT 3 53 54 #define OV2680_WIDTH_MAX 1600 55 #define OV2680_HEIGHT_MAX 1200 56 57 #define OV2680_DEFAULT_WIDTH 800 58 #define OV2680_DEFAULT_HEIGHT 600 59 60 enum ov2680_mode_id { 61 OV2680_MODE_QUXGA_800_600, 62 OV2680_MODE_720P_1280_720, 63 OV2680_MODE_UXGA_1600_1200, 64 OV2680_MODE_MAX, 65 }; 66 67 struct reg_value { 68 u16 reg_addr; 69 u8 val; 70 }; 71 72 static const char * const ov2680_supply_name[] = { 73 "DOVDD", 74 "DVDD", 75 "AVDD", 76 }; 77 78 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name) 79 80 struct ov2680_mode_info { 81 const char *name; 82 enum ov2680_mode_id id; 83 u32 width; 84 u32 height; 85 const struct reg_value *reg_data; 86 u32 reg_data_size; 87 }; 88 89 struct ov2680_ctrls { 90 struct v4l2_ctrl_handler handler; 91 struct v4l2_ctrl *exposure; 92 struct v4l2_ctrl *gain; 93 struct v4l2_ctrl *hflip; 94 struct v4l2_ctrl *vflip; 95 struct v4l2_ctrl *test_pattern; 96 }; 97 98 struct ov2680_dev { 99 struct i2c_client *i2c_client; 100 struct v4l2_subdev sd; 101 102 struct media_pad pad; 103 struct clk *xvclk; 104 u32 xvclk_freq; 105 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES]; 106 107 struct gpio_desc *reset_gpio; 108 struct mutex lock; /* protect members */ 109 110 bool mode_pending_changes; 111 bool is_enabled; 112 bool is_streaming; 113 114 struct ov2680_ctrls ctrls; 115 struct v4l2_mbus_framefmt fmt; 116 struct v4l2_fract frame_interval; 117 118 const struct ov2680_mode_info *current_mode; 119 }; 120 121 static const char * const test_pattern_menu[] = { 122 "Disabled", 123 "Color Bars", 124 "Random Data", 125 "Square", 126 "Black Image", 127 }; 128 129 static const int ov2680_hv_flip_bayer_order[] = { 130 MEDIA_BUS_FMT_SBGGR10_1X10, 131 MEDIA_BUS_FMT_SGRBG10_1X10, 132 MEDIA_BUS_FMT_SGBRG10_1X10, 133 MEDIA_BUS_FMT_SRGGB10_1X10, 134 }; 135 136 static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = { 137 {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20}, 138 {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac}, 139 {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04}, 140 {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00}, 141 {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0}, 142 {0x3503, 0x03}, 143 }; 144 145 static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = { 146 {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02}, 147 {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05}, 148 {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11}, 149 {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00}, 150 }; 151 152 static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = { 153 {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06}, 154 {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06}, 155 {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00}, 156 {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0}, 157 {0x4008, 0x00}, {0x4837, 0x18} 158 }; 159 160 static const struct ov2680_mode_info ov2680_mode_init_data = { 161 "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600, 162 ov2680_setting_30fps_QUXGA_800_600, 163 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600), 164 }; 165 166 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = { 167 {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 168 800, 600, ov2680_setting_30fps_QUXGA_800_600, 169 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)}, 170 {"mode_720p_1280_720", OV2680_MODE_720P_1280_720, 171 1280, 720, ov2680_setting_30fps_720P_1280_720, 172 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)}, 173 {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200, 174 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200, 175 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)}, 176 }; 177 178 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd) 179 { 180 return container_of(sd, struct ov2680_dev, sd); 181 } 182 183 static struct device *ov2680_to_dev(struct ov2680_dev *sensor) 184 { 185 return &sensor->i2c_client->dev; 186 } 187 188 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 189 { 190 return &container_of(ctrl->handler, struct ov2680_dev, 191 ctrls.handler)->sd; 192 } 193 194 static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg, 195 unsigned int len, u32 val) 196 { 197 struct i2c_client *client = sensor->i2c_client; 198 u8 buf[6]; 199 int ret; 200 201 if (len > 4) 202 return -EINVAL; 203 204 put_unaligned_be16(reg, buf); 205 put_unaligned_be32(val << (8 * (4 - len)), buf + 2); 206 ret = i2c_master_send(client, buf, len + 2); 207 if (ret != len + 2) { 208 dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret); 209 return -EIO; 210 } 211 212 return 0; 213 } 214 215 #define ov2680_write_reg(s, r, v) \ 216 __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v) 217 218 #define ov2680_write_reg16(s, r, v) \ 219 __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v) 220 221 #define ov2680_write_reg24(s, r, v) \ 222 __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v) 223 224 static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg, 225 unsigned int len, u32 *val) 226 { 227 struct i2c_client *client = sensor->i2c_client; 228 struct i2c_msg msgs[2]; 229 u8 addr_buf[2] = { reg >> 8, reg & 0xff }; 230 u8 data_buf[4] = { 0, }; 231 int ret; 232 233 if (len > 4) 234 return -EINVAL; 235 236 msgs[0].addr = client->addr; 237 msgs[0].flags = 0; 238 msgs[0].len = ARRAY_SIZE(addr_buf); 239 msgs[0].buf = addr_buf; 240 241 msgs[1].addr = client->addr; 242 msgs[1].flags = I2C_M_RD; 243 msgs[1].len = len; 244 msgs[1].buf = &data_buf[4 - len]; 245 246 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 247 if (ret != ARRAY_SIZE(msgs)) { 248 dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret); 249 return -EIO; 250 } 251 252 *val = get_unaligned_be32(data_buf); 253 254 return 0; 255 } 256 257 #define ov2680_read_reg(s, r, v) \ 258 __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v) 259 260 #define ov2680_read_reg16(s, r, v) \ 261 __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v) 262 263 #define ov2680_read_reg24(s, r, v) \ 264 __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v) 265 266 static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val) 267 { 268 u32 readval; 269 int ret; 270 271 ret = ov2680_read_reg(sensor, reg, &readval); 272 if (ret < 0) 273 return ret; 274 275 readval &= ~mask; 276 val &= mask; 277 val |= readval; 278 279 return ov2680_write_reg(sensor, reg, val); 280 } 281 282 static int ov2680_load_regs(struct ov2680_dev *sensor, 283 const struct ov2680_mode_info *mode) 284 { 285 const struct reg_value *regs = mode->reg_data; 286 unsigned int i; 287 int ret = 0; 288 u16 reg_addr; 289 u8 val; 290 291 for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 292 reg_addr = regs->reg_addr; 293 val = regs->val; 294 295 ret = ov2680_write_reg(sensor, reg_addr, val); 296 if (ret) 297 break; 298 } 299 300 return ret; 301 } 302 303 static void ov2680_power_up(struct ov2680_dev *sensor) 304 { 305 if (!sensor->reset_gpio) 306 return; 307 308 gpiod_set_value(sensor->reset_gpio, 0); 309 usleep_range(5000, 10000); 310 } 311 312 static void ov2680_power_down(struct ov2680_dev *sensor) 313 { 314 if (!sensor->reset_gpio) 315 return; 316 317 gpiod_set_value(sensor->reset_gpio, 1); 318 usleep_range(5000, 10000); 319 } 320 321 static void ov2680_set_bayer_order(struct ov2680_dev *sensor, 322 struct v4l2_mbus_framefmt *fmt) 323 { 324 int hv_flip = 0; 325 326 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val) 327 hv_flip += 1; 328 329 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val) 330 hv_flip += 2; 331 332 fmt->code = ov2680_hv_flip_bayer_order[hv_flip]; 333 } 334 335 static void ov2680_fill_format(struct ov2680_dev *sensor, 336 struct v4l2_mbus_framefmt *fmt, 337 unsigned int width, unsigned int height) 338 { 339 memset(fmt, 0, sizeof(*fmt)); 340 fmt->width = width; 341 fmt->height = height; 342 fmt->field = V4L2_FIELD_NONE; 343 fmt->colorspace = V4L2_COLORSPACE_SRGB; 344 ov2680_set_bayer_order(sensor, fmt); 345 } 346 347 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val) 348 { 349 int ret; 350 351 if (sensor->is_streaming) 352 return -EBUSY; 353 354 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, 355 BIT(2), val ? BIT(2) : 0); 356 if (ret < 0) 357 return ret; 358 359 ov2680_set_bayer_order(sensor, &sensor->fmt); 360 return 0; 361 } 362 363 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val) 364 { 365 int ret; 366 367 if (sensor->is_streaming) 368 return -EBUSY; 369 370 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, 371 BIT(2), val ? BIT(2) : 0); 372 if (ret < 0) 373 return ret; 374 375 ov2680_set_bayer_order(sensor, &sensor->fmt); 376 return 0; 377 } 378 379 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value) 380 { 381 int ret; 382 383 if (!value) 384 return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0); 385 386 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1); 387 if (ret < 0) 388 return ret; 389 390 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7)); 391 if (ret < 0) 392 return ret; 393 394 return 0; 395 } 396 397 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain) 398 { 399 return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain); 400 } 401 402 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp) 403 { 404 return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, 405 exp << 4); 406 } 407 408 static int ov2680_stream_enable(struct ov2680_dev *sensor) 409 { 410 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1); 411 } 412 413 static int ov2680_stream_disable(struct ov2680_dev *sensor) 414 { 415 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0); 416 } 417 418 static int ov2680_mode_set(struct ov2680_dev *sensor) 419 { 420 int ret; 421 422 ret = ov2680_load_regs(sensor, sensor->current_mode); 423 if (ret < 0) 424 return ret; 425 426 /* Restore value of all ctrls */ 427 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 428 if (ret < 0) 429 return ret; 430 431 sensor->mode_pending_changes = false; 432 433 return 0; 434 } 435 436 static int ov2680_mode_restore(struct ov2680_dev *sensor) 437 { 438 int ret; 439 440 ret = ov2680_load_regs(sensor, &ov2680_mode_init_data); 441 if (ret < 0) 442 return ret; 443 444 return ov2680_mode_set(sensor); 445 } 446 447 static int ov2680_power_off(struct ov2680_dev *sensor) 448 { 449 if (!sensor->is_enabled) 450 return 0; 451 452 clk_disable_unprepare(sensor->xvclk); 453 ov2680_power_down(sensor); 454 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 455 sensor->is_enabled = false; 456 457 return 0; 458 } 459 460 static int ov2680_power_on(struct ov2680_dev *sensor) 461 { 462 struct device *dev = ov2680_to_dev(sensor); 463 int ret; 464 465 if (sensor->is_enabled) 466 return 0; 467 468 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies); 469 if (ret < 0) { 470 dev_err(dev, "failed to enable regulators: %d\n", ret); 471 return ret; 472 } 473 474 if (!sensor->reset_gpio) { 475 ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01); 476 if (ret != 0) { 477 dev_err(dev, "sensor soft reset failed\n"); 478 return ret; 479 } 480 usleep_range(1000, 2000); 481 } else { 482 ov2680_power_down(sensor); 483 ov2680_power_up(sensor); 484 } 485 486 ret = clk_prepare_enable(sensor->xvclk); 487 if (ret < 0) 488 return ret; 489 490 sensor->is_enabled = true; 491 492 /* Set clock lane into LP-11 state */ 493 ov2680_stream_enable(sensor); 494 usleep_range(1000, 2000); 495 ov2680_stream_disable(sensor); 496 497 return 0; 498 } 499 500 static int ov2680_s_power(struct v4l2_subdev *sd, int on) 501 { 502 struct ov2680_dev *sensor = to_ov2680_dev(sd); 503 int ret = 0; 504 505 mutex_lock(&sensor->lock); 506 507 if (on) 508 ret = ov2680_power_on(sensor); 509 else 510 ret = ov2680_power_off(sensor); 511 512 if (on && ret == 0) 513 ret = ov2680_mode_restore(sensor); 514 515 mutex_unlock(&sensor->lock); 516 517 return ret; 518 } 519 520 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd, 521 struct v4l2_subdev_frame_interval *fi) 522 { 523 struct ov2680_dev *sensor = to_ov2680_dev(sd); 524 525 mutex_lock(&sensor->lock); 526 fi->interval = sensor->frame_interval; 527 mutex_unlock(&sensor->lock); 528 529 return 0; 530 } 531 532 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable) 533 { 534 struct ov2680_dev *sensor = to_ov2680_dev(sd); 535 int ret = 0; 536 537 mutex_lock(&sensor->lock); 538 539 if (sensor->is_streaming == !!enable) 540 goto unlock; 541 542 if (enable && sensor->mode_pending_changes) { 543 ret = ov2680_mode_set(sensor); 544 if (ret < 0) 545 goto unlock; 546 } 547 548 if (enable) 549 ret = ov2680_stream_enable(sensor); 550 else 551 ret = ov2680_stream_disable(sensor); 552 553 sensor->is_streaming = !!enable; 554 555 unlock: 556 mutex_unlock(&sensor->lock); 557 558 return ret; 559 } 560 561 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd, 562 struct v4l2_subdev_state *sd_state, 563 struct v4l2_subdev_mbus_code_enum *code) 564 { 565 struct ov2680_dev *sensor = to_ov2680_dev(sd); 566 567 if (code->pad != 0 || code->index != 0) 568 return -EINVAL; 569 570 code->code = sensor->fmt.code; 571 572 return 0; 573 } 574 575 static int ov2680_get_fmt(struct v4l2_subdev *sd, 576 struct v4l2_subdev_state *sd_state, 577 struct v4l2_subdev_format *format) 578 { 579 struct ov2680_dev *sensor = to_ov2680_dev(sd); 580 struct v4l2_mbus_framefmt *fmt = NULL; 581 582 if (format->pad != 0) 583 return -EINVAL; 584 585 mutex_lock(&sensor->lock); 586 587 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 588 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, 589 format->pad); 590 } else { 591 fmt = &sensor->fmt; 592 } 593 594 format->format = *fmt; 595 596 mutex_unlock(&sensor->lock); 597 598 return 0; 599 } 600 601 static int ov2680_set_fmt(struct v4l2_subdev *sd, 602 struct v4l2_subdev_state *sd_state, 603 struct v4l2_subdev_format *format) 604 { 605 struct ov2680_dev *sensor = to_ov2680_dev(sd); 606 struct v4l2_mbus_framefmt *fmt = &format->format; 607 struct v4l2_mbus_framefmt *try_fmt; 608 const struct ov2680_mode_info *mode; 609 int ret = 0; 610 611 if (format->pad != 0) 612 return -EINVAL; 613 614 mode = v4l2_find_nearest_size(ov2680_mode_data, 615 ARRAY_SIZE(ov2680_mode_data), width, 616 height, fmt->width, fmt->height); 617 if (!mode) 618 return -EINVAL; 619 620 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 621 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0); 622 format->format = *try_fmt; 623 return 0; 624 } 625 626 mutex_lock(&sensor->lock); 627 628 if (sensor->is_streaming) { 629 ret = -EBUSY; 630 goto unlock; 631 } 632 633 ov2680_fill_format(sensor, fmt, mode->width, mode->height); 634 635 sensor->current_mode = mode; 636 sensor->fmt = format->format; 637 sensor->mode_pending_changes = true; 638 639 unlock: 640 mutex_unlock(&sensor->lock); 641 642 return ret; 643 } 644 645 static int ov2680_init_cfg(struct v4l2_subdev *sd, 646 struct v4l2_subdev_state *sd_state) 647 { 648 struct ov2680_dev *sensor = to_ov2680_dev(sd); 649 650 ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt, 651 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 652 return 0; 653 } 654 655 static int ov2680_enum_frame_size(struct v4l2_subdev *sd, 656 struct v4l2_subdev_state *sd_state, 657 struct v4l2_subdev_frame_size_enum *fse) 658 { 659 int index = fse->index; 660 661 if (index >= OV2680_MODE_MAX || index < 0) 662 return -EINVAL; 663 664 fse->min_width = ov2680_mode_data[index].width; 665 fse->min_height = ov2680_mode_data[index].height; 666 fse->max_width = ov2680_mode_data[index].width; 667 fse->max_height = ov2680_mode_data[index].height; 668 669 return 0; 670 } 671 672 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd, 673 struct v4l2_subdev_state *sd_state, 674 struct v4l2_subdev_frame_interval_enum *fie) 675 { 676 struct v4l2_fract tpf; 677 678 if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX || 679 fie->height > OV2680_HEIGHT_MAX || 680 fie->which > V4L2_SUBDEV_FORMAT_ACTIVE) 681 return -EINVAL; 682 683 tpf.denominator = OV2680_FRAME_RATE; 684 tpf.numerator = 1; 685 686 fie->interval = tpf; 687 688 return 0; 689 } 690 691 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl) 692 { 693 struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 694 struct ov2680_dev *sensor = to_ov2680_dev(sd); 695 696 if (!sensor->is_enabled) 697 return 0; 698 699 switch (ctrl->id) { 700 case V4L2_CID_GAIN: 701 return ov2680_gain_set(sensor, ctrl->val); 702 case V4L2_CID_EXPOSURE: 703 return ov2680_exposure_set(sensor, ctrl->val); 704 case V4L2_CID_VFLIP: 705 return ov2680_set_vflip(sensor, ctrl->val); 706 case V4L2_CID_HFLIP: 707 return ov2680_set_hflip(sensor, ctrl->val); 708 case V4L2_CID_TEST_PATTERN: 709 return ov2680_test_pattern_set(sensor, ctrl->val); 710 default: 711 break; 712 } 713 714 return -EINVAL; 715 } 716 717 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = { 718 .s_ctrl = ov2680_s_ctrl, 719 }; 720 721 static const struct v4l2_subdev_core_ops ov2680_core_ops = { 722 .s_power = ov2680_s_power, 723 }; 724 725 static const struct v4l2_subdev_video_ops ov2680_video_ops = { 726 .g_frame_interval = ov2680_s_g_frame_interval, 727 .s_frame_interval = ov2680_s_g_frame_interval, 728 .s_stream = ov2680_s_stream, 729 }; 730 731 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = { 732 .init_cfg = ov2680_init_cfg, 733 .enum_mbus_code = ov2680_enum_mbus_code, 734 .get_fmt = ov2680_get_fmt, 735 .set_fmt = ov2680_set_fmt, 736 .enum_frame_size = ov2680_enum_frame_size, 737 .enum_frame_interval = ov2680_enum_frame_interval, 738 }; 739 740 static const struct v4l2_subdev_ops ov2680_subdev_ops = { 741 .core = &ov2680_core_ops, 742 .video = &ov2680_video_ops, 743 .pad = &ov2680_pad_ops, 744 }; 745 746 static int ov2680_mode_init(struct ov2680_dev *sensor) 747 { 748 const struct ov2680_mode_info *init_mode; 749 750 /* set initial mode */ 751 ov2680_fill_format(sensor, &sensor->fmt, 752 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 753 754 sensor->frame_interval.denominator = OV2680_FRAME_RATE; 755 sensor->frame_interval.numerator = 1; 756 757 init_mode = &ov2680_mode_init_data; 758 759 sensor->current_mode = init_mode; 760 761 sensor->mode_pending_changes = true; 762 763 return 0; 764 } 765 766 static int ov2680_v4l2_register(struct ov2680_dev *sensor) 767 { 768 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops; 769 struct ov2680_ctrls *ctrls = &sensor->ctrls; 770 struct v4l2_ctrl_handler *hdl = &ctrls->handler; 771 int ret = 0; 772 773 v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client, 774 &ov2680_subdev_ops); 775 776 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 777 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 778 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 779 780 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 781 if (ret < 0) 782 return ret; 783 784 v4l2_ctrl_handler_init(hdl, 5); 785 786 hdl->lock = &sensor->lock; 787 788 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 789 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 790 791 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl, 792 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN, 793 ARRAY_SIZE(test_pattern_menu) - 1, 794 0, 0, test_pattern_menu); 795 796 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 797 0, 32767, 1, 0); 798 799 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0); 800 801 if (hdl->error) { 802 ret = hdl->error; 803 goto cleanup_entity; 804 } 805 806 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 807 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 808 809 sensor->sd.ctrl_handler = hdl; 810 811 ret = v4l2_async_register_subdev(&sensor->sd); 812 if (ret < 0) 813 goto cleanup_entity; 814 815 return 0; 816 817 cleanup_entity: 818 media_entity_cleanup(&sensor->sd.entity); 819 v4l2_ctrl_handler_free(hdl); 820 821 return ret; 822 } 823 824 static int ov2680_get_regulators(struct ov2680_dev *sensor) 825 { 826 int i; 827 828 for (i = 0; i < OV2680_NUM_SUPPLIES; i++) 829 sensor->supplies[i].supply = ov2680_supply_name[i]; 830 831 return devm_regulator_bulk_get(&sensor->i2c_client->dev, 832 OV2680_NUM_SUPPLIES, 833 sensor->supplies); 834 } 835 836 static int ov2680_check_id(struct ov2680_dev *sensor) 837 { 838 struct device *dev = ov2680_to_dev(sensor); 839 u32 chip_id; 840 int ret; 841 842 ov2680_power_on(sensor); 843 844 ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id); 845 if (ret < 0) { 846 dev_err(dev, "failed to read chip id high\n"); 847 return -ENODEV; 848 } 849 850 if (chip_id != OV2680_CHIP_ID) { 851 dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n", 852 chip_id, OV2680_CHIP_ID); 853 return -ENODEV; 854 } 855 856 return 0; 857 } 858 859 static int ov2680_parse_dt(struct ov2680_dev *sensor) 860 { 861 struct device *dev = ov2680_to_dev(sensor); 862 int ret; 863 864 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 865 GPIOD_OUT_HIGH); 866 ret = PTR_ERR_OR_ZERO(sensor->reset_gpio); 867 if (ret < 0) { 868 dev_dbg(dev, "error while getting reset gpio: %d\n", ret); 869 return ret; 870 } 871 872 sensor->xvclk = devm_clk_get(dev, "xvclk"); 873 if (IS_ERR(sensor->xvclk)) { 874 dev_err(dev, "xvclk clock missing or invalid\n"); 875 return PTR_ERR(sensor->xvclk); 876 } 877 878 sensor->xvclk_freq = clk_get_rate(sensor->xvclk); 879 if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) { 880 dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n", 881 sensor->xvclk_freq, OV2680_XVCLK_VALUE); 882 return -EINVAL; 883 } 884 885 return 0; 886 } 887 888 static int ov2680_probe(struct i2c_client *client) 889 { 890 struct device *dev = &client->dev; 891 struct ov2680_dev *sensor; 892 int ret; 893 894 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 895 if (!sensor) 896 return -ENOMEM; 897 898 sensor->i2c_client = client; 899 900 ret = ov2680_parse_dt(sensor); 901 if (ret < 0) 902 return -EINVAL; 903 904 ret = ov2680_mode_init(sensor); 905 if (ret < 0) 906 return ret; 907 908 ret = ov2680_get_regulators(sensor); 909 if (ret < 0) { 910 dev_err(dev, "failed to get regulators\n"); 911 return ret; 912 } 913 914 mutex_init(&sensor->lock); 915 916 ret = ov2680_check_id(sensor); 917 if (ret < 0) 918 goto lock_destroy; 919 920 ret = ov2680_v4l2_register(sensor); 921 if (ret < 0) 922 goto lock_destroy; 923 924 dev_info(dev, "ov2680 init correctly\n"); 925 926 return 0; 927 928 lock_destroy: 929 dev_err(dev, "ov2680 init fail: %d\n", ret); 930 mutex_destroy(&sensor->lock); 931 932 return ret; 933 } 934 935 static void ov2680_remove(struct i2c_client *client) 936 { 937 struct v4l2_subdev *sd = i2c_get_clientdata(client); 938 struct ov2680_dev *sensor = to_ov2680_dev(sd); 939 940 v4l2_async_unregister_subdev(&sensor->sd); 941 mutex_destroy(&sensor->lock); 942 media_entity_cleanup(&sensor->sd.entity); 943 v4l2_ctrl_handler_free(&sensor->ctrls.handler); 944 } 945 946 static int __maybe_unused ov2680_suspend(struct device *dev) 947 { 948 struct v4l2_subdev *sd = dev_get_drvdata(dev); 949 struct ov2680_dev *sensor = to_ov2680_dev(sd); 950 951 if (sensor->is_streaming) 952 ov2680_stream_disable(sensor); 953 954 return 0; 955 } 956 957 static int __maybe_unused ov2680_resume(struct device *dev) 958 { 959 struct v4l2_subdev *sd = dev_get_drvdata(dev); 960 struct ov2680_dev *sensor = to_ov2680_dev(sd); 961 int ret; 962 963 if (sensor->is_streaming) { 964 ret = ov2680_stream_enable(sensor); 965 if (ret < 0) 966 goto stream_disable; 967 } 968 969 return 0; 970 971 stream_disable: 972 ov2680_stream_disable(sensor); 973 sensor->is_streaming = false; 974 975 return ret; 976 } 977 978 static const struct dev_pm_ops ov2680_pm_ops = { 979 SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume) 980 }; 981 982 static const struct of_device_id ov2680_dt_ids[] = { 983 { .compatible = "ovti,ov2680" }, 984 { /* sentinel */ }, 985 }; 986 MODULE_DEVICE_TABLE(of, ov2680_dt_ids); 987 988 static struct i2c_driver ov2680_i2c_driver = { 989 .driver = { 990 .name = "ov2680", 991 .pm = &ov2680_pm_ops, 992 .of_match_table = ov2680_dt_ids, 993 }, 994 .probe = ov2680_probe, 995 .remove = ov2680_remove, 996 }; 997 module_i2c_driver(ov2680_i2c_driver); 998 999 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>"); 1000 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver"); 1001 MODULE_LICENSE("GPL v2"); 1002