1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Support for OmniVision OV2722 1080p HD camera sensor. 4 * 5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License version 9 * 2 as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 */ 17 18#include <linux/module.h> 19#include <linux/types.h> 20#include <linux/kernel.h> 21#include <linux/mm.h> 22#include <linux/string.h> 23#include <linux/errno.h> 24#include <linux/init.h> 25#include <linux/kmod.h> 26#include <linux/device.h> 27#include <linux/delay.h> 28#include <linux/slab.h> 29#include <linux/i2c.h> 30#include <linux/moduleparam.h> 31#include <media/v4l2-device.h> 32#include "../include/linux/atomisp_gmin_platform.h" 33#include <linux/acpi.h> 34#include <linux/io.h> 35 36#include "ov2722.h" 37 38/* i2c read/write stuff */ 39static int ov2722_read_reg(struct i2c_client *client, 40 u16 data_length, u16 reg, u16 *val) 41{ 42 int err; 43 struct i2c_msg msg[2]; 44 unsigned char data[6]; 45 46 if (!client->adapter) { 47 dev_err(&client->dev, "%s error, no client->adapter\n", 48 __func__); 49 return -ENODEV; 50 } 51 52 if (data_length != OV2722_8BIT && data_length != OV2722_16BIT && 53 data_length != OV2722_32BIT) { 54 dev_err(&client->dev, "%s error, invalid data length\n", 55 __func__); 56 return -EINVAL; 57 } 58 59 memset(msg, 0, sizeof(msg)); 60 61 msg[0].addr = client->addr; 62 msg[0].flags = 0; 63 msg[0].len = I2C_MSG_LENGTH; 64 msg[0].buf = data; 65 66 /* high byte goes out first */ 67 data[0] = (u8)(reg >> 8); 68 data[1] = (u8)(reg & 0xff); 69 70 msg[1].addr = client->addr; 71 msg[1].len = data_length; 72 msg[1].flags = I2C_M_RD; 73 msg[1].buf = data; 74 75 err = i2c_transfer(client->adapter, msg, 2); 76 if (err != 2) { 77 if (err >= 0) 78 err = -EIO; 79 dev_err(&client->dev, 80 "read from offset 0x%x error %d", reg, err); 81 return err; 82 } 83 84 *val = 0; 85 /* high byte comes first */ 86 if (data_length == OV2722_8BIT) 87 *val = (u8)data[0]; 88 else if (data_length == OV2722_16BIT) 89 *val = be16_to_cpu(*(__be16 *)&data[0]); 90 else 91 *val = be32_to_cpu(*(__be32 *)&data[0]); 92 93 return 0; 94} 95 96static int ov2722_i2c_write(struct i2c_client *client, u16 len, u8 *data) 97{ 98 struct i2c_msg msg; 99 const int num_msg = 1; 100 int ret; 101 102 msg.addr = client->addr; 103 msg.flags = 0; 104 msg.len = len; 105 msg.buf = data; 106 ret = i2c_transfer(client->adapter, &msg, 1); 107 108 return ret == num_msg ? 0 : -EIO; 109} 110 111static int ov2722_write_reg(struct i2c_client *client, u16 data_length, 112 u16 reg, u16 val) 113{ 114 int ret; 115 unsigned char data[4] = {0}; 116 __be16 *wreg = (__be16 *)data; 117 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */ 118 119 if (data_length != OV2722_8BIT && data_length != OV2722_16BIT) { 120 dev_err(&client->dev, 121 "%s error, invalid data_length\n", __func__); 122 return -EINVAL; 123 } 124 125 /* high byte goes out first */ 126 *wreg = cpu_to_be16(reg); 127 128 if (data_length == OV2722_8BIT) { 129 data[2] = (u8)(val); 130 } else { 131 /* OV2722_16BIT */ 132 __be16 *wdata = (__be16 *)&data[2]; 133 134 *wdata = cpu_to_be16(val); 135 } 136 137 ret = ov2722_i2c_write(client, len, data); 138 if (ret) 139 dev_err(&client->dev, 140 "write error: wrote 0x%x to offset 0x%x error %d", 141 val, reg, ret); 142 143 return ret; 144} 145 146/* 147 * ov2722_write_reg_array - Initializes a list of OV2722 registers 148 * @client: i2c driver client structure 149 * @reglist: list of registers to be written 150 * 151 * This function initializes a list of registers. When consecutive addresses 152 * are found in a row on the list, this function creates a buffer and sends 153 * consecutive data in a single i2c_transfer(). 154 * 155 * __ov2722_flush_reg_array, __ov2722_buf_reg_array() and 156 * __ov2722_write_reg_is_consecutive() are internal functions to 157 * ov2722_write_reg_array_fast() and should be not used anywhere else. 158 * 159 */ 160 161static int __ov2722_flush_reg_array(struct i2c_client *client, 162 struct ov2722_write_ctrl *ctrl) 163{ 164 u16 size; 165 __be16 *data16 = (void *)&ctrl->buffer.addr; 166 167 if (ctrl->index == 0) 168 return 0; 169 170 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */ 171 *data16 = cpu_to_be16(ctrl->buffer.addr); 172 ctrl->index = 0; 173 174 return ov2722_i2c_write(client, size, (u8 *)&ctrl->buffer); 175} 176 177static int __ov2722_buf_reg_array(struct i2c_client *client, 178 struct ov2722_write_ctrl *ctrl, 179 const struct ov2722_reg *next) 180{ 181 int size; 182 __be16 *data16; 183 184 switch (next->type) { 185 case OV2722_8BIT: 186 size = 1; 187 ctrl->buffer.data[ctrl->index] = (u8)next->val; 188 break; 189 case OV2722_16BIT: 190 size = 2; 191 data16 = (void *)&ctrl->buffer.data[ctrl->index]; 192 *data16 = cpu_to_be16((u16)next->val); 193 break; 194 default: 195 return -EINVAL; 196 } 197 198 /* When first item is added, we need to store its starting address */ 199 if (ctrl->index == 0) 200 ctrl->buffer.addr = next->reg; 201 202 ctrl->index += size; 203 204 /* 205 * Buffer cannot guarantee free space for u32? Better flush it to avoid 206 * possible lack of memory for next item. 207 */ 208 if (ctrl->index + sizeof(u16) >= OV2722_MAX_WRITE_BUF_SIZE) 209 return __ov2722_flush_reg_array(client, ctrl); 210 211 return 0; 212} 213 214static int __ov2722_write_reg_is_consecutive(struct i2c_client *client, 215 struct ov2722_write_ctrl *ctrl, 216 const struct ov2722_reg *next) 217{ 218 if (ctrl->index == 0) 219 return 1; 220 221 return ctrl->buffer.addr + ctrl->index == next->reg; 222} 223 224static int ov2722_write_reg_array(struct i2c_client *client, 225 const struct ov2722_reg *reglist) 226{ 227 const struct ov2722_reg *next = reglist; 228 struct ov2722_write_ctrl ctrl; 229 int err; 230 231 ctrl.index = 0; 232 for (; next->type != OV2722_TOK_TERM; next++) { 233 switch (next->type & OV2722_TOK_MASK) { 234 case OV2722_TOK_DELAY: 235 err = __ov2722_flush_reg_array(client, &ctrl); 236 if (err) 237 return err; 238 msleep(next->val); 239 break; 240 default: 241 /* 242 * If next address is not consecutive, data needs to be 243 * flushed before proceed. 244 */ 245 if (!__ov2722_write_reg_is_consecutive(client, &ctrl, 246 next)) { 247 err = __ov2722_flush_reg_array(client, &ctrl); 248 if (err) 249 return err; 250 } 251 err = __ov2722_buf_reg_array(client, &ctrl, next); 252 if (err) { 253 dev_err(&client->dev, "%s: write error, aborted\n", 254 __func__); 255 return err; 256 } 257 break; 258 } 259 } 260 261 return __ov2722_flush_reg_array(client, &ctrl); 262} 263 264static long __ov2722_set_exposure(struct v4l2_subdev *sd, int coarse_itg, 265 int gain, int digitgain) 266 267{ 268 struct i2c_client *client = v4l2_get_subdevdata(sd); 269 struct ov2722_device *dev = to_ov2722_sensor(sd); 270 u16 hts, vts; 271 int ret; 272 273 dev_dbg(&client->dev, "set_exposure without group hold\n"); 274 275 /* clear VTS_DIFF on manual mode */ 276 ret = ov2722_write_reg(client, OV2722_16BIT, OV2722_VTS_DIFF_H, 0); 277 if (ret) 278 return ret; 279 280 hts = dev->pixels_per_line; 281 vts = dev->lines_per_frame; 282 283 if ((coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN) > vts) 284 vts = coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN; 285 286 coarse_itg <<= 4; 287 digitgain <<= 2; 288 289 ret = ov2722_write_reg(client, OV2722_16BIT, 290 OV2722_VTS_H, vts); 291 if (ret) 292 return ret; 293 294 ret = ov2722_write_reg(client, OV2722_16BIT, 295 OV2722_HTS_H, hts); 296 if (ret) 297 return ret; 298 299 /* set exposure */ 300 ret = ov2722_write_reg(client, OV2722_8BIT, 301 OV2722_AEC_PK_EXPO_L, 302 coarse_itg & 0xff); 303 if (ret) 304 return ret; 305 306 ret = ov2722_write_reg(client, OV2722_16BIT, 307 OV2722_AEC_PK_EXPO_H, 308 (coarse_itg >> 8) & 0xfff); 309 if (ret) 310 return ret; 311 312 /* set analog gain */ 313 ret = ov2722_write_reg(client, OV2722_16BIT, 314 OV2722_AGC_ADJ_H, gain); 315 if (ret) 316 return ret; 317 318 /* set digital gain */ 319 ret = ov2722_write_reg(client, OV2722_16BIT, 320 OV2722_MWB_GAIN_R_H, digitgain); 321 if (ret) 322 return ret; 323 324 ret = ov2722_write_reg(client, OV2722_16BIT, 325 OV2722_MWB_GAIN_G_H, digitgain); 326 if (ret) 327 return ret; 328 329 ret = ov2722_write_reg(client, OV2722_16BIT, 330 OV2722_MWB_GAIN_B_H, digitgain); 331 332 return ret; 333} 334 335static int ov2722_set_exposure(struct v4l2_subdev *sd, int exposure, 336 int gain, int digitgain) 337{ 338 struct ov2722_device *dev = to_ov2722_sensor(sd); 339 int ret; 340 341 mutex_lock(&dev->input_lock); 342 ret = __ov2722_set_exposure(sd, exposure, gain, digitgain); 343 mutex_unlock(&dev->input_lock); 344 345 return ret; 346} 347 348static long ov2722_s_exposure(struct v4l2_subdev *sd, 349 struct atomisp_exposure *exposure) 350{ 351 int exp = exposure->integration_time[0]; 352 int gain = exposure->gain[0]; 353 int digitgain = exposure->gain[1]; 354 355 /* we should not accept the invalid value below. */ 356 if (gain == 0) { 357 struct i2c_client *client = v4l2_get_subdevdata(sd); 358 359 v4l2_err(client, "%s: invalid value\n", __func__); 360 return -EINVAL; 361 } 362 363 return ov2722_set_exposure(sd, exp, gain, digitgain); 364} 365 366static long ov2722_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 367{ 368 switch (cmd) { 369 case ATOMISP_IOC_S_EXPOSURE: 370 return ov2722_s_exposure(sd, arg); 371 default: 372 return -EINVAL; 373 } 374 return 0; 375} 376 377/* This returns the exposure time being used. This should only be used 378 * for filling in EXIF data, not for actual image processing. 379 */ 380static int ov2722_q_exposure(struct v4l2_subdev *sd, s32 *value) 381{ 382 struct i2c_client *client = v4l2_get_subdevdata(sd); 383 u16 reg_v, reg_v2; 384 int ret; 385 386 /* get exposure */ 387 ret = ov2722_read_reg(client, OV2722_8BIT, 388 OV2722_AEC_PK_EXPO_L, 389 ®_v); 390 if (ret) 391 goto err; 392 393 ret = ov2722_read_reg(client, OV2722_8BIT, 394 OV2722_AEC_PK_EXPO_M, 395 ®_v2); 396 if (ret) 397 goto err; 398 399 reg_v += reg_v2 << 8; 400 ret = ov2722_read_reg(client, OV2722_8BIT, 401 OV2722_AEC_PK_EXPO_H, 402 ®_v2); 403 if (ret) 404 goto err; 405 406 *value = reg_v + (((u32)reg_v2 << 16)); 407err: 408 return ret; 409} 410 411static int ov2722_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 412{ 413 struct ov2722_device *dev = 414 container_of(ctrl->handler, struct ov2722_device, ctrl_handler); 415 int ret = 0; 416 unsigned int val; 417 418 switch (ctrl->id) { 419 case V4L2_CID_EXPOSURE_ABSOLUTE: 420 ret = ov2722_q_exposure(&dev->sd, &ctrl->val); 421 break; 422 case V4L2_CID_LINK_FREQ: 423 val = dev->res->mipi_freq; 424 if (val == 0) 425 return -EINVAL; 426 427 ctrl->val = val * 1000; /* To Hz */ 428 break; 429 default: 430 ret = -EINVAL; 431 } 432 433 return ret; 434} 435 436static const struct v4l2_ctrl_ops ctrl_ops = { 437 .g_volatile_ctrl = ov2722_g_volatile_ctrl 438}; 439 440static const struct v4l2_ctrl_config ov2722_controls[] = { 441 { 442 .ops = &ctrl_ops, 443 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 444 .type = V4L2_CTRL_TYPE_INTEGER, 445 .name = "exposure", 446 .min = 0x0, 447 .max = 0xffff, 448 .step = 0x01, 449 .def = 0x00, 450 .flags = 0, 451 }, 452 { 453 .ops = &ctrl_ops, 454 .id = V4L2_CID_LINK_FREQ, 455 .name = "Link Frequency", 456 .type = V4L2_CTRL_TYPE_INTEGER, 457 .min = 1, 458 .max = 1500000 * 1000, 459 .step = 1, 460 .def = 1, 461 .flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY, 462 }, 463}; 464 465static int ov2722_init(struct v4l2_subdev *sd) 466{ 467 struct ov2722_device *dev = to_ov2722_sensor(sd); 468 469 mutex_lock(&dev->input_lock); 470 471 /* restore settings */ 472 ov2722_res = ov2722_res_preview; 473 N_RES = N_RES_PREVIEW; 474 475 mutex_unlock(&dev->input_lock); 476 477 return 0; 478} 479 480static int power_ctrl(struct v4l2_subdev *sd, bool flag) 481{ 482 int ret = -1; 483 struct ov2722_device *dev = to_ov2722_sensor(sd); 484 485 if (!dev || !dev->platform_data) 486 return -ENODEV; 487 488 if (flag) { 489 ret = dev->platform_data->v1p8_ctrl(sd, 1); 490 if (ret == 0) { 491 ret = dev->platform_data->v2p8_ctrl(sd, 1); 492 if (ret) 493 dev->platform_data->v1p8_ctrl(sd, 0); 494 } 495 } else { 496 ret = dev->platform_data->v1p8_ctrl(sd, 0); 497 ret |= dev->platform_data->v2p8_ctrl(sd, 0); 498 } 499 500 return ret; 501} 502 503static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 504{ 505 struct ov2722_device *dev = to_ov2722_sensor(sd); 506 int ret = -1; 507 508 if (!dev || !dev->platform_data) 509 return -ENODEV; 510 511 /* Note: the GPIO order is asymmetric: always RESET# 512 * before PWDN# when turning it on or off. 513 */ 514 ret = dev->platform_data->gpio0_ctrl(sd, flag); 515 ret |= dev->platform_data->gpio1_ctrl(sd, flag); 516 return ret; 517} 518 519static int power_up(struct v4l2_subdev *sd) 520{ 521 struct ov2722_device *dev = to_ov2722_sensor(sd); 522 struct i2c_client *client = v4l2_get_subdevdata(sd); 523 int ret; 524 525 if (!dev->platform_data) { 526 dev_err(&client->dev, 527 "no camera_sensor_platform_data"); 528 return -ENODEV; 529 } 530 531 if (dev->power_on == 1) 532 return 0; /* Already on */ 533 534 /* power control */ 535 ret = power_ctrl(sd, 1); 536 if (ret) 537 goto fail_power; 538 539 /* according to DS, at least 5ms is needed between DOVDD and PWDN */ 540 usleep_range(5000, 6000); 541 542 /* gpio ctrl */ 543 ret = gpio_ctrl(sd, 1); 544 if (ret) { 545 ret = gpio_ctrl(sd, 0); 546 if (ret) 547 goto fail_power; 548 } 549 550 /* flis clock control */ 551 ret = dev->platform_data->flisclk_ctrl(sd, 1); 552 if (ret) 553 goto fail_clk; 554 555 /* according to DS, 20ms is needed between PWDN and i2c access */ 556 msleep(20); 557 558 dev->power_on = 1; 559 return 0; 560 561fail_clk: 562 gpio_ctrl(sd, 0); 563fail_power: 564 power_ctrl(sd, 0); 565 dev_err(&client->dev, "sensor power-up failed\n"); 566 567 return ret; 568} 569 570static int power_down(struct v4l2_subdev *sd) 571{ 572 struct ov2722_device *dev = to_ov2722_sensor(sd); 573 struct i2c_client *client = v4l2_get_subdevdata(sd); 574 int ret = 0; 575 576 if (!dev->platform_data) { 577 dev_err(&client->dev, 578 "no camera_sensor_platform_data"); 579 return -ENODEV; 580 } 581 582 if (dev->power_on == 0) 583 return 0; /* Already off */ 584 585 ret = dev->platform_data->flisclk_ctrl(sd, 0); 586 if (ret) 587 dev_err(&client->dev, "flisclk failed\n"); 588 589 /* gpio ctrl */ 590 ret = gpio_ctrl(sd, 0); 591 if (ret) { 592 ret = gpio_ctrl(sd, 0); 593 if (ret) 594 dev_err(&client->dev, "gpio failed 2\n"); 595 } 596 597 /* power control */ 598 ret = power_ctrl(sd, 0); 599 if (ret) 600 dev_err(&client->dev, "vprog failed.\n"); 601 602 dev->power_on = 0; 603 return ret; 604} 605 606static int ov2722_s_power(struct v4l2_subdev *sd, int on) 607{ 608 int ret; 609 610 if (on == 0) 611 return power_down(sd); 612 613 ret = power_up(sd); 614 if (!ret) 615 return ov2722_init(sd); 616 617 return ret; 618} 619 620/* TODO: remove it. */ 621static int startup(struct v4l2_subdev *sd) 622{ 623 struct ov2722_device *dev = to_ov2722_sensor(sd); 624 struct i2c_client *client = v4l2_get_subdevdata(sd); 625 int ret = 0; 626 627 ret = ov2722_write_reg(client, OV2722_8BIT, 628 OV2722_SW_RESET, 0x01); 629 if (ret) { 630 dev_err(&client->dev, "ov2722 reset err.\n"); 631 return ret; 632 } 633 634 ret = ov2722_write_reg_array(client, dev->res->regs); 635 if (ret) { 636 dev_err(&client->dev, "ov2722 write register err.\n"); 637 return ret; 638 } 639 640 return ret; 641} 642 643static int ov2722_set_fmt(struct v4l2_subdev *sd, 644 struct v4l2_subdev_state *sd_state, 645 struct v4l2_subdev_format *format) 646{ 647 struct v4l2_mbus_framefmt *fmt = &format->format; 648 struct ov2722_device *dev = to_ov2722_sensor(sd); 649 struct i2c_client *client = v4l2_get_subdevdata(sd); 650 struct ov2722_resolution *res; 651 struct camera_mipi_info *ov2722_info = NULL; 652 int ret = 0; 653 654 if (format->pad) 655 return -EINVAL; 656 if (!fmt) 657 return -EINVAL; 658 ov2722_info = v4l2_get_subdev_hostdata(sd); 659 if (!ov2722_info) 660 return -EINVAL; 661 662 res = v4l2_find_nearest_size(ov2722_res_preview, 663 ARRAY_SIZE(ov2722_res_preview), width, 664 height, fmt->width, fmt->height); 665 if (!res) 666 res = &ov2722_res_preview[N_RES - 1]; 667 668 fmt->width = res->width; 669 fmt->height = res->height; 670 dev->res = res; 671 672 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; 673 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 674 *v4l2_subdev_state_get_format(sd_state, 0) = *fmt; 675 return 0; 676 } 677 678 mutex_lock(&dev->input_lock); 679 680 /* s_power has not been called yet for std v4l2 clients (camorama) */ 681 power_up(sd); 682 683 dev->pixels_per_line = dev->res->pixels_per_line; 684 dev->lines_per_frame = dev->res->lines_per_frame; 685 686 ret = startup(sd); 687 if (ret) { 688 int i = 0; 689 690 dev_err(&client->dev, "ov2722 startup err, retry to power up\n"); 691 for (i = 0; i < OV2722_POWER_UP_RETRY_NUM; i++) { 692 dev_err(&client->dev, 693 "ov2722 retry to power up %d/%d times, result: ", 694 i + 1, OV2722_POWER_UP_RETRY_NUM); 695 power_down(sd); 696 ret = power_up(sd); 697 if (ret) { 698 dev_err(&client->dev, "power up failed, continue\n"); 699 continue; 700 } 701 ret = startup(sd); 702 if (ret) { 703 dev_err(&client->dev, " startup FAILED!\n"); 704 } else { 705 dev_err(&client->dev, " startup SUCCESS!\n"); 706 break; 707 } 708 } 709 if (ret) { 710 dev_err(&client->dev, "ov2722 startup err\n"); 711 goto err; 712 } 713 } 714 715err: 716 mutex_unlock(&dev->input_lock); 717 return ret; 718} 719 720static int ov2722_get_fmt(struct v4l2_subdev *sd, 721 struct v4l2_subdev_state *sd_state, 722 struct v4l2_subdev_format *format) 723{ 724 struct v4l2_mbus_framefmt *fmt = &format->format; 725 struct ov2722_device *dev = to_ov2722_sensor(sd); 726 727 if (format->pad) 728 return -EINVAL; 729 if (!fmt) 730 return -EINVAL; 731 732 fmt->width = dev->res->width; 733 fmt->height = dev->res->height; 734 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10; 735 736 return 0; 737} 738 739static int ov2722_detect(struct i2c_client *client) 740{ 741 struct i2c_adapter *adapter = client->adapter; 742 u16 high = 0, low = 0; 743 u16 id; 744 u8 revision; 745 746 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 747 return -ENODEV; 748 749 ov2722_read_reg(client, OV2722_8BIT, 750 OV2722_SC_CMMN_CHIP_ID_H, &high); 751 ov2722_read_reg(client, OV2722_8BIT, 752 OV2722_SC_CMMN_CHIP_ID_L, &low); 753 id = (high << 8) | low; 754 755 if ((id != OV2722_ID) && (id != OV2720_ID)) { 756 dev_err(&client->dev, "sensor ID error\n"); 757 return -ENODEV; 758 } 759 760 high = 0; 761 ov2722_read_reg(client, OV2722_8BIT, 762 OV2722_SC_CMMN_SUB_ID, &high); 763 revision = (u8)high & 0x0f; 764 765 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision); 766 dev_dbg(&client->dev, "detect ov2722 success\n"); 767 return 0; 768} 769 770static int ov2722_s_stream(struct v4l2_subdev *sd, int enable) 771{ 772 struct ov2722_device *dev = to_ov2722_sensor(sd); 773 struct i2c_client *client = v4l2_get_subdevdata(sd); 774 int ret; 775 776 mutex_lock(&dev->input_lock); 777 778 ret = ov2722_write_reg(client, OV2722_8BIT, OV2722_SW_STREAM, 779 enable ? OV2722_START_STREAMING : 780 OV2722_STOP_STREAMING); 781 782 mutex_unlock(&dev->input_lock); 783 return ret; 784} 785 786static int ov2722_s_config(struct v4l2_subdev *sd, 787 int irq, void *platform_data) 788{ 789 struct ov2722_device *dev = to_ov2722_sensor(sd); 790 struct i2c_client *client = v4l2_get_subdevdata(sd); 791 int ret = 0; 792 793 if (!platform_data) 794 return -ENODEV; 795 796 dev->platform_data = 797 (struct camera_sensor_platform_data *)platform_data; 798 799 mutex_lock(&dev->input_lock); 800 801 /* power off the module, then power on it in future 802 * as first power on by board may not fulfill the 803 * power on sequqence needed by the module 804 */ 805 ret = power_down(sd); 806 if (ret) { 807 dev_err(&client->dev, "ov2722 power-off err.\n"); 808 goto fail_power_off; 809 } 810 811 ret = power_up(sd); 812 if (ret) { 813 dev_err(&client->dev, "ov2722 power-up err.\n"); 814 goto fail_power_on; 815 } 816 817 ret = dev->platform_data->csi_cfg(sd, 1); 818 if (ret) 819 goto fail_csi_cfg; 820 821 /* config & detect sensor */ 822 ret = ov2722_detect(client); 823 if (ret) { 824 dev_err(&client->dev, "ov2722_detect err s_config.\n"); 825 goto fail_csi_cfg; 826 } 827 828 /* turn off sensor, after probed */ 829 ret = power_down(sd); 830 if (ret) { 831 dev_err(&client->dev, "ov2722 power-off err.\n"); 832 goto fail_csi_cfg; 833 } 834 mutex_unlock(&dev->input_lock); 835 836 return 0; 837 838fail_csi_cfg: 839 dev->platform_data->csi_cfg(sd, 0); 840fail_power_on: 841 power_down(sd); 842 dev_err(&client->dev, "sensor power-gating failed\n"); 843fail_power_off: 844 mutex_unlock(&dev->input_lock); 845 return ret; 846} 847 848static int ov2722_get_frame_interval(struct v4l2_subdev *sd, 849 struct v4l2_subdev_state *sd_state, 850 struct v4l2_subdev_frame_interval *interval) 851{ 852 struct ov2722_device *dev = to_ov2722_sensor(sd); 853 854 /* 855 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2 856 * subdev active state API. 857 */ 858 if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE) 859 return -EINVAL; 860 861 interval->interval.numerator = 1; 862 interval->interval.denominator = dev->res->fps; 863 864 return 0; 865} 866 867static int ov2722_enum_mbus_code(struct v4l2_subdev *sd, 868 struct v4l2_subdev_state *sd_state, 869 struct v4l2_subdev_mbus_code_enum *code) 870{ 871 if (code->index >= MAX_FMTS) 872 return -EINVAL; 873 874 code->code = MEDIA_BUS_FMT_SBGGR10_1X10; 875 return 0; 876} 877 878static int ov2722_enum_frame_size(struct v4l2_subdev *sd, 879 struct v4l2_subdev_state *sd_state, 880 struct v4l2_subdev_frame_size_enum *fse) 881{ 882 int index = fse->index; 883 884 if (index >= N_RES) 885 return -EINVAL; 886 887 fse->min_width = ov2722_res[index].width; 888 fse->min_height = ov2722_res[index].height; 889 fse->max_width = ov2722_res[index].width; 890 fse->max_height = ov2722_res[index].height; 891 892 return 0; 893} 894 895static int ov2722_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) 896{ 897 struct ov2722_device *dev = to_ov2722_sensor(sd); 898 899 mutex_lock(&dev->input_lock); 900 *frames = dev->res->skip_frames; 901 mutex_unlock(&dev->input_lock); 902 903 return 0; 904} 905 906static const struct v4l2_subdev_sensor_ops ov2722_sensor_ops = { 907 .g_skip_frames = ov2722_g_skip_frames, 908}; 909 910static const struct v4l2_subdev_video_ops ov2722_video_ops = { 911 .s_stream = ov2722_s_stream, 912}; 913 914static const struct v4l2_subdev_core_ops ov2722_core_ops = { 915 .s_power = ov2722_s_power, 916 .ioctl = ov2722_ioctl, 917}; 918 919static const struct v4l2_subdev_pad_ops ov2722_pad_ops = { 920 .enum_mbus_code = ov2722_enum_mbus_code, 921 .enum_frame_size = ov2722_enum_frame_size, 922 .get_fmt = ov2722_get_fmt, 923 .set_fmt = ov2722_set_fmt, 924 .get_frame_interval = ov2722_get_frame_interval, 925}; 926 927static const struct v4l2_subdev_ops ov2722_ops = { 928 .core = &ov2722_core_ops, 929 .video = &ov2722_video_ops, 930 .pad = &ov2722_pad_ops, 931 .sensor = &ov2722_sensor_ops, 932}; 933 934static void ov2722_remove(struct i2c_client *client) 935{ 936 struct v4l2_subdev *sd = i2c_get_clientdata(client); 937 struct ov2722_device *dev = to_ov2722_sensor(sd); 938 939 dev->platform_data->csi_cfg(sd, 0); 940 v4l2_ctrl_handler_free(&dev->ctrl_handler); 941 v4l2_device_unregister_subdev(sd); 942 943 atomisp_gmin_remove_subdev(sd); 944 945 media_entity_cleanup(&dev->sd.entity); 946 kfree(dev); 947} 948 949static int __ov2722_init_ctrl_handler(struct ov2722_device *dev) 950{ 951 struct v4l2_ctrl_handler *hdl; 952 unsigned int i; 953 954 hdl = &dev->ctrl_handler; 955 v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ov2722_controls)); 956 for (i = 0; i < ARRAY_SIZE(ov2722_controls); i++) 957 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2722_controls[i], 958 NULL); 959 960 dev->link_freq = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_LINK_FREQ); 961 962 if (dev->ctrl_handler.error || !dev->link_freq) 963 return dev->ctrl_handler.error; 964 965 dev->sd.ctrl_handler = hdl; 966 967 return 0; 968} 969 970static int ov2722_probe(struct i2c_client *client) 971{ 972 struct ov2722_device *dev; 973 void *ovpdev; 974 int ret; 975 976 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 977 if (!dev) 978 return -ENOMEM; 979 980 mutex_init(&dev->input_lock); 981 dev->power_on = -1; 982 983 dev->res = &ov2722_res_preview[0]; 984 v4l2_i2c_subdev_init(&dev->sd, client, &ov2722_ops); 985 986 ovpdev = gmin_camera_platform_data(&dev->sd, 987 ATOMISP_INPUT_FORMAT_RAW_10, 988 atomisp_bayer_order_grbg); 989 990 ret = ov2722_s_config(&dev->sd, client->irq, ovpdev); 991 if (ret) 992 goto out_free; 993 994 ret = __ov2722_init_ctrl_handler(dev); 995 if (ret) 996 goto out_ctrl_handler_free; 997 998 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 999 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 1000 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10; 1001 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1002 1003 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 1004 if (ret) 1005 ov2722_remove(client); 1006 1007 return atomisp_register_i2c_module(&dev->sd, ovpdev, RAW_CAMERA); 1008 1009out_ctrl_handler_free: 1010 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1011 1012out_free: 1013 atomisp_gmin_remove_subdev(&dev->sd); 1014 v4l2_device_unregister_subdev(&dev->sd); 1015 kfree(dev); 1016 return ret; 1017} 1018 1019static const struct acpi_device_id ov2722_acpi_match[] = { 1020 { "INT33FB" }, 1021 {}, 1022}; 1023MODULE_DEVICE_TABLE(acpi, ov2722_acpi_match); 1024 1025static struct i2c_driver ov2722_driver = { 1026 .driver = { 1027 .name = "ov2722", 1028 .acpi_match_table = ov2722_acpi_match, 1029 }, 1030 .probe = ov2722_probe, 1031 .remove = ov2722_remove, 1032}; 1033module_i2c_driver(ov2722_driver); 1034 1035MODULE_AUTHOR("Wei Liu <wei.liu@intel.com>"); 1036MODULE_DESCRIPTION("A low-level driver for OmniVision 2722 sensors"); 1037MODULE_LICENSE("GPL"); 1038