1 /* 2 * Support for OmniVision OV2722 1080p HD camera sensor. 3 * 4 * Copyright (c) 2013 Intel Corporation. All Rights Reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License version 8 * 2 as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 */ 16 17 #include <linux/module.h> 18 #include <linux/types.h> 19 #include <linux/kernel.h> 20 #include <linux/mm.h> 21 #include <linux/string.h> 22 #include <linux/errno.h> 23 #include <linux/init.h> 24 #include <linux/kmod.h> 25 #include <linux/device.h> 26 #include <linux/delay.h> 27 #include <linux/slab.h> 28 #include <linux/i2c.h> 29 #include <linux/moduleparam.h> 30 #include <media/v4l2-device.h> 31 #include "../include/linux/atomisp_gmin_platform.h" 32 #include <linux/acpi.h> 33 #include <linux/io.h> 34 35 #include "ov2722.h" 36 37 /* i2c read/write stuff */ 38 static int ov2722_read_reg(struct i2c_client *client, 39 u16 data_length, u16 reg, u16 *val) 40 { 41 int err; 42 struct i2c_msg msg[2]; 43 unsigned char data[6]; 44 45 if (!client->adapter) { 46 dev_err(&client->dev, "%s error, no client->adapter\n", 47 __func__); 48 return -ENODEV; 49 } 50 51 if (data_length != OV2722_8BIT && data_length != OV2722_16BIT 52 && data_length != OV2722_32BIT) { 53 dev_err(&client->dev, "%s error, invalid data length\n", 54 __func__); 55 return -EINVAL; 56 } 57 58 memset(msg, 0, sizeof(msg)); 59 60 msg[0].addr = client->addr; 61 msg[0].flags = 0; 62 msg[0].len = I2C_MSG_LENGTH; 63 msg[0].buf = data; 64 65 /* high byte goes out first */ 66 data[0] = (u8)(reg >> 8); 67 data[1] = (u8)(reg & 0xff); 68 69 msg[1].addr = client->addr; 70 msg[1].len = data_length; 71 msg[1].flags = I2C_M_RD; 72 msg[1].buf = data; 73 74 err = i2c_transfer(client->adapter, msg, 2); 75 if (err != 2) { 76 if (err >= 0) 77 err = -EIO; 78 dev_err(&client->dev, 79 "read from offset 0x%x error %d", reg, err); 80 return err; 81 } 82 83 *val = 0; 84 /* high byte comes first */ 85 if (data_length == OV2722_8BIT) 86 *val = (u8)data[0]; 87 else if (data_length == OV2722_16BIT) 88 *val = be16_to_cpu(*(__be16 *)&data[0]); 89 else 90 *val = be32_to_cpu(*(__be32 *)&data[0]); 91 92 return 0; 93 } 94 95 static int ov2722_i2c_write(struct i2c_client *client, u16 len, u8 *data) 96 { 97 struct i2c_msg msg; 98 const int num_msg = 1; 99 int ret; 100 101 msg.addr = client->addr; 102 msg.flags = 0; 103 msg.len = len; 104 msg.buf = data; 105 ret = i2c_transfer(client->adapter, &msg, 1); 106 107 return ret == num_msg ? 0 : -EIO; 108 } 109 110 static int ov2722_write_reg(struct i2c_client *client, u16 data_length, 111 u16 reg, u16 val) 112 { 113 int ret; 114 unsigned char data[4] = {0}; 115 __be16 *wreg = (__be16 *)data; 116 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */ 117 118 if (data_length != OV2722_8BIT && data_length != OV2722_16BIT) { 119 dev_err(&client->dev, 120 "%s error, invalid data_length\n", __func__); 121 return -EINVAL; 122 } 123 124 /* high byte goes out first */ 125 *wreg = cpu_to_be16(reg); 126 127 if (data_length == OV2722_8BIT) { 128 data[2] = (u8)(val); 129 } else { 130 /* OV2722_16BIT */ 131 __be16 *wdata = (__be16 *)&data[2]; 132 133 *wdata = cpu_to_be16(val); 134 } 135 136 ret = ov2722_i2c_write(client, len, data); 137 if (ret) 138 dev_err(&client->dev, 139 "write error: wrote 0x%x to offset 0x%x error %d", 140 val, reg, ret); 141 142 return ret; 143 } 144 145 /* 146 * ov2722_write_reg_array - Initializes a list of OV2722 registers 147 * @client: i2c driver client structure 148 * @reglist: list of registers to be written 149 * 150 * This function initializes a list of registers. When consecutive addresses 151 * are found in a row on the list, this function creates a buffer and sends 152 * consecutive data in a single i2c_transfer(). 153 * 154 * __ov2722_flush_reg_array, __ov2722_buf_reg_array() and 155 * __ov2722_write_reg_is_consecutive() are internal functions to 156 * ov2722_write_reg_array_fast() and should be not used anywhere else. 157 * 158 */ 159 160 static int __ov2722_flush_reg_array(struct i2c_client *client, 161 struct ov2722_write_ctrl *ctrl) 162 { 163 u16 size; 164 __be16 *data16 = (void *)&ctrl->buffer.addr; 165 166 if (ctrl->index == 0) 167 return 0; 168 169 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */ 170 *data16 = cpu_to_be16(ctrl->buffer.addr); 171 ctrl->index = 0; 172 173 return ov2722_i2c_write(client, size, (u8 *)&ctrl->buffer); 174 } 175 176 static int __ov2722_buf_reg_array(struct i2c_client *client, 177 struct ov2722_write_ctrl *ctrl, 178 const struct ov2722_reg *next) 179 { 180 int size; 181 __be16 *data16; 182 183 switch (next->type) { 184 case OV2722_8BIT: 185 size = 1; 186 ctrl->buffer.data[ctrl->index] = (u8)next->val; 187 break; 188 case OV2722_16BIT: 189 size = 2; 190 data16 = (void *)&ctrl->buffer.data[ctrl->index]; 191 *data16 = cpu_to_be16((u16)next->val); 192 break; 193 default: 194 return -EINVAL; 195 } 196 197 /* When first item is added, we need to store its starting address */ 198 if (ctrl->index == 0) 199 ctrl->buffer.addr = next->reg; 200 201 ctrl->index += size; 202 203 /* 204 * Buffer cannot guarantee free space for u32? Better flush it to avoid 205 * possible lack of memory for next item. 206 */ 207 if (ctrl->index + sizeof(u16) >= OV2722_MAX_WRITE_BUF_SIZE) 208 return __ov2722_flush_reg_array(client, ctrl); 209 210 return 0; 211 } 212 213 static int __ov2722_write_reg_is_consecutive(struct i2c_client *client, 214 struct ov2722_write_ctrl *ctrl, 215 const struct ov2722_reg *next) 216 { 217 if (ctrl->index == 0) 218 return 1; 219 220 return ctrl->buffer.addr + ctrl->index == next->reg; 221 } 222 223 static int ov2722_write_reg_array(struct i2c_client *client, 224 const struct ov2722_reg *reglist) 225 { 226 const struct ov2722_reg *next = reglist; 227 struct ov2722_write_ctrl ctrl; 228 int err; 229 230 ctrl.index = 0; 231 for (; next->type != OV2722_TOK_TERM; next++) { 232 switch (next->type & OV2722_TOK_MASK) { 233 case OV2722_TOK_DELAY: 234 err = __ov2722_flush_reg_array(client, &ctrl); 235 if (err) 236 return err; 237 msleep(next->val); 238 break; 239 default: 240 /* 241 * If next address is not consecutive, data needs to be 242 * flushed before proceed. 243 */ 244 if (!__ov2722_write_reg_is_consecutive(client, &ctrl, 245 next)) { 246 err = __ov2722_flush_reg_array(client, &ctrl); 247 if (err) 248 return err; 249 } 250 err = __ov2722_buf_reg_array(client, &ctrl, next); 251 if (err) { 252 dev_err(&client->dev, "%s: write error, aborted\n", 253 __func__); 254 return err; 255 } 256 break; 257 } 258 } 259 260 return __ov2722_flush_reg_array(client, &ctrl); 261 } 262 263 static int ov2722_g_focal(struct v4l2_subdev *sd, s32 *val) 264 { 265 *val = (OV2722_FOCAL_LENGTH_NUM << 16) | OV2722_FOCAL_LENGTH_DEM; 266 return 0; 267 } 268 269 static int ov2722_g_fnumber(struct v4l2_subdev *sd, s32 *val) 270 { 271 /*const f number for imx*/ 272 *val = (OV2722_F_NUMBER_DEFAULT_NUM << 16) | OV2722_F_NUMBER_DEM; 273 return 0; 274 } 275 276 static int ov2722_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) 277 { 278 *val = (OV2722_F_NUMBER_DEFAULT_NUM << 24) | 279 (OV2722_F_NUMBER_DEM << 16) | 280 (OV2722_F_NUMBER_DEFAULT_NUM << 8) | OV2722_F_NUMBER_DEM; 281 return 0; 282 } 283 284 static int ov2722_get_intg_factor(struct i2c_client *client, 285 struct camera_mipi_info *info, 286 const struct ov2722_resolution *res) 287 { 288 struct v4l2_subdev *sd = i2c_get_clientdata(client); 289 struct ov2722_device *dev = NULL; 290 struct atomisp_sensor_mode_data *buf = &info->data; 291 const unsigned int ext_clk_freq_hz = 19200000; 292 const unsigned int pll_invariant_div = 10; 293 unsigned int pix_clk_freq_hz; 294 u16 pre_pll_clk_div; 295 u16 pll_multiplier; 296 u16 op_pix_clk_div; 297 u16 reg_val; 298 int ret; 299 300 if (!info) 301 return -EINVAL; 302 303 dev = to_ov2722_sensor(sd); 304 305 /* pixel clock calculattion */ 306 ret = ov2722_read_reg(client, OV2722_8BIT, 307 OV2722_SC_CMMN_PLL_CTRL3, &pre_pll_clk_div); 308 if (ret) 309 return ret; 310 311 ret = ov2722_read_reg(client, OV2722_8BIT, 312 OV2722_SC_CMMN_PLL_MULTIPLIER, &pll_multiplier); 313 if (ret) 314 return ret; 315 316 ret = ov2722_read_reg(client, OV2722_8BIT, 317 OV2722_SC_CMMN_PLL_DEBUG_OPT, &op_pix_clk_div); 318 if (ret) 319 return ret; 320 321 pre_pll_clk_div = (pre_pll_clk_div & 0x70) >> 4; 322 if (!pre_pll_clk_div) 323 return -EINVAL; 324 325 pll_multiplier = pll_multiplier & 0x7f; 326 op_pix_clk_div = op_pix_clk_div & 0x03; 327 pix_clk_freq_hz = ext_clk_freq_hz / pre_pll_clk_div * pll_multiplier 328 * op_pix_clk_div / pll_invariant_div; 329 330 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz; 331 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz; 332 333 /* get integration time */ 334 buf->coarse_integration_time_min = OV2722_COARSE_INTG_TIME_MIN; 335 buf->coarse_integration_time_max_margin = 336 OV2722_COARSE_INTG_TIME_MAX_MARGIN; 337 338 buf->fine_integration_time_min = OV2722_FINE_INTG_TIME_MIN; 339 buf->fine_integration_time_max_margin = 340 OV2722_FINE_INTG_TIME_MAX_MARGIN; 341 342 buf->fine_integration_time_def = OV2722_FINE_INTG_TIME_MIN; 343 buf->frame_length_lines = res->lines_per_frame; 344 buf->line_length_pck = res->pixels_per_line; 345 buf->read_mode = res->bin_mode; 346 347 /* get the cropping and output resolution to ISP for this mode. */ 348 ret = ov2722_read_reg(client, OV2722_16BIT, 349 OV2722_H_CROP_START_H, ®_val); 350 if (ret) 351 return ret; 352 buf->crop_horizontal_start = reg_val; 353 354 ret = ov2722_read_reg(client, OV2722_16BIT, 355 OV2722_V_CROP_START_H, ®_val); 356 if (ret) 357 return ret; 358 buf->crop_vertical_start = reg_val; 359 360 ret = ov2722_read_reg(client, OV2722_16BIT, 361 OV2722_H_CROP_END_H, ®_val); 362 if (ret) 363 return ret; 364 buf->crop_horizontal_end = reg_val; 365 366 ret = ov2722_read_reg(client, OV2722_16BIT, 367 OV2722_V_CROP_END_H, ®_val); 368 if (ret) 369 return ret; 370 buf->crop_vertical_end = reg_val; 371 372 ret = ov2722_read_reg(client, OV2722_16BIT, 373 OV2722_H_OUTSIZE_H, ®_val); 374 if (ret) 375 return ret; 376 buf->output_width = reg_val; 377 378 ret = ov2722_read_reg(client, OV2722_16BIT, 379 OV2722_V_OUTSIZE_H, ®_val); 380 if (ret) 381 return ret; 382 buf->output_height = reg_val; 383 384 buf->binning_factor_x = res->bin_factor_x ? 385 res->bin_factor_x : 1; 386 buf->binning_factor_y = res->bin_factor_y ? 387 res->bin_factor_y : 1; 388 return 0; 389 } 390 391 static long __ov2722_set_exposure(struct v4l2_subdev *sd, int coarse_itg, 392 int gain, int digitgain) 393 394 { 395 struct i2c_client *client = v4l2_get_subdevdata(sd); 396 struct ov2722_device *dev = to_ov2722_sensor(sd); 397 u16 hts, vts; 398 int ret; 399 400 dev_dbg(&client->dev, "set_exposure without group hold\n"); 401 402 /* clear VTS_DIFF on manual mode */ 403 ret = ov2722_write_reg(client, OV2722_16BIT, OV2722_VTS_DIFF_H, 0); 404 if (ret) 405 return ret; 406 407 hts = dev->pixels_per_line; 408 vts = dev->lines_per_frame; 409 410 if ((coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN) > vts) 411 vts = coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN; 412 413 coarse_itg <<= 4; 414 digitgain <<= 2; 415 416 ret = ov2722_write_reg(client, OV2722_16BIT, 417 OV2722_VTS_H, vts); 418 if (ret) 419 return ret; 420 421 ret = ov2722_write_reg(client, OV2722_16BIT, 422 OV2722_HTS_H, hts); 423 if (ret) 424 return ret; 425 426 /* set exposure */ 427 ret = ov2722_write_reg(client, OV2722_8BIT, 428 OV2722_AEC_PK_EXPO_L, 429 coarse_itg & 0xff); 430 if (ret) 431 return ret; 432 433 ret = ov2722_write_reg(client, OV2722_16BIT, 434 OV2722_AEC_PK_EXPO_H, 435 (coarse_itg >> 8) & 0xfff); 436 if (ret) 437 return ret; 438 439 /* set analog gain */ 440 ret = ov2722_write_reg(client, OV2722_16BIT, 441 OV2722_AGC_ADJ_H, gain); 442 if (ret) 443 return ret; 444 445 /* set digital gain */ 446 ret = ov2722_write_reg(client, OV2722_16BIT, 447 OV2722_MWB_GAIN_R_H, digitgain); 448 if (ret) 449 return ret; 450 451 ret = ov2722_write_reg(client, OV2722_16BIT, 452 OV2722_MWB_GAIN_G_H, digitgain); 453 if (ret) 454 return ret; 455 456 ret = ov2722_write_reg(client, OV2722_16BIT, 457 OV2722_MWB_GAIN_B_H, digitgain); 458 459 return ret; 460 } 461 462 static int ov2722_set_exposure(struct v4l2_subdev *sd, int exposure, 463 int gain, int digitgain) 464 { 465 struct ov2722_device *dev = to_ov2722_sensor(sd); 466 int ret; 467 468 mutex_lock(&dev->input_lock); 469 ret = __ov2722_set_exposure(sd, exposure, gain, digitgain); 470 mutex_unlock(&dev->input_lock); 471 472 return ret; 473 } 474 475 static long ov2722_s_exposure(struct v4l2_subdev *sd, 476 struct atomisp_exposure *exposure) 477 { 478 int exp = exposure->integration_time[0]; 479 int gain = exposure->gain[0]; 480 int digitgain = exposure->gain[1]; 481 482 /* we should not accept the invalid value below. */ 483 if (gain == 0) { 484 struct i2c_client *client = v4l2_get_subdevdata(sd); 485 486 v4l2_err(client, "%s: invalid value\n", __func__); 487 return -EINVAL; 488 } 489 490 return ov2722_set_exposure(sd, exp, gain, digitgain); 491 } 492 493 static long ov2722_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 494 { 495 switch (cmd) { 496 case ATOMISP_IOC_S_EXPOSURE: 497 return ov2722_s_exposure(sd, arg); 498 default: 499 return -EINVAL; 500 } 501 return 0; 502 } 503 504 /* This returns the exposure time being used. This should only be used 505 * for filling in EXIF data, not for actual image processing. 506 */ 507 static int ov2722_q_exposure(struct v4l2_subdev *sd, s32 *value) 508 { 509 struct i2c_client *client = v4l2_get_subdevdata(sd); 510 u16 reg_v, reg_v2; 511 int ret; 512 513 /* get exposure */ 514 ret = ov2722_read_reg(client, OV2722_8BIT, 515 OV2722_AEC_PK_EXPO_L, 516 ®_v); 517 if (ret) 518 goto err; 519 520 ret = ov2722_read_reg(client, OV2722_8BIT, 521 OV2722_AEC_PK_EXPO_M, 522 ®_v2); 523 if (ret) 524 goto err; 525 526 reg_v += reg_v2 << 8; 527 ret = ov2722_read_reg(client, OV2722_8BIT, 528 OV2722_AEC_PK_EXPO_H, 529 ®_v2); 530 if (ret) 531 goto err; 532 533 *value = reg_v + (((u32)reg_v2 << 16)); 534 err: 535 return ret; 536 } 537 538 static int ov2722_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 539 { 540 struct ov2722_device *dev = 541 container_of(ctrl->handler, struct ov2722_device, ctrl_handler); 542 int ret = 0; 543 unsigned int val; 544 545 switch (ctrl->id) { 546 case V4L2_CID_EXPOSURE_ABSOLUTE: 547 ret = ov2722_q_exposure(&dev->sd, &ctrl->val); 548 break; 549 case V4L2_CID_FOCAL_ABSOLUTE: 550 ret = ov2722_g_focal(&dev->sd, &ctrl->val); 551 break; 552 case V4L2_CID_FNUMBER_ABSOLUTE: 553 ret = ov2722_g_fnumber(&dev->sd, &ctrl->val); 554 break; 555 case V4L2_CID_FNUMBER_RANGE: 556 ret = ov2722_g_fnumber_range(&dev->sd, &ctrl->val); 557 break; 558 case V4L2_CID_LINK_FREQ: 559 val = ov2722_res[dev->fmt_idx].mipi_freq; 560 if (val == 0) 561 return -EINVAL; 562 563 ctrl->val = val * 1000; /* To Hz */ 564 break; 565 default: 566 ret = -EINVAL; 567 } 568 569 return ret; 570 } 571 572 static const struct v4l2_ctrl_ops ctrl_ops = { 573 .g_volatile_ctrl = ov2722_g_volatile_ctrl 574 }; 575 576 static const struct v4l2_ctrl_config ov2722_controls[] = { 577 { 578 .ops = &ctrl_ops, 579 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 580 .type = V4L2_CTRL_TYPE_INTEGER, 581 .name = "exposure", 582 .min = 0x0, 583 .max = 0xffff, 584 .step = 0x01, 585 .def = 0x00, 586 .flags = 0, 587 }, 588 { 589 .ops = &ctrl_ops, 590 .id = V4L2_CID_FOCAL_ABSOLUTE, 591 .type = V4L2_CTRL_TYPE_INTEGER, 592 .name = "focal length", 593 .min = OV2722_FOCAL_LENGTH_DEFAULT, 594 .max = OV2722_FOCAL_LENGTH_DEFAULT, 595 .step = 0x01, 596 .def = OV2722_FOCAL_LENGTH_DEFAULT, 597 .flags = 0, 598 }, 599 { 600 .ops = &ctrl_ops, 601 .id = V4L2_CID_FNUMBER_ABSOLUTE, 602 .type = V4L2_CTRL_TYPE_INTEGER, 603 .name = "f-number", 604 .min = OV2722_F_NUMBER_DEFAULT, 605 .max = OV2722_F_NUMBER_DEFAULT, 606 .step = 0x01, 607 .def = OV2722_F_NUMBER_DEFAULT, 608 .flags = 0, 609 }, 610 { 611 .ops = &ctrl_ops, 612 .id = V4L2_CID_FNUMBER_RANGE, 613 .type = V4L2_CTRL_TYPE_INTEGER, 614 .name = "f-number range", 615 .min = OV2722_F_NUMBER_RANGE, 616 .max = OV2722_F_NUMBER_RANGE, 617 .step = 0x01, 618 .def = OV2722_F_NUMBER_RANGE, 619 .flags = 0, 620 }, 621 { 622 .ops = &ctrl_ops, 623 .id = V4L2_CID_LINK_FREQ, 624 .name = "Link Frequency", 625 .type = V4L2_CTRL_TYPE_INTEGER, 626 .min = 1, 627 .max = 1500000 * 1000, 628 .step = 1, 629 .def = 1, 630 .flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY, 631 }, 632 }; 633 634 static int ov2722_init(struct v4l2_subdev *sd) 635 { 636 struct ov2722_device *dev = to_ov2722_sensor(sd); 637 638 mutex_lock(&dev->input_lock); 639 640 /* restore settings */ 641 ov2722_res = ov2722_res_preview; 642 N_RES = N_RES_PREVIEW; 643 644 mutex_unlock(&dev->input_lock); 645 646 return 0; 647 } 648 649 static int power_ctrl(struct v4l2_subdev *sd, bool flag) 650 { 651 int ret = -1; 652 struct ov2722_device *dev = to_ov2722_sensor(sd); 653 654 if (!dev || !dev->platform_data) 655 return -ENODEV; 656 657 if (flag) { 658 ret = dev->platform_data->v1p8_ctrl(sd, 1); 659 if (ret == 0) { 660 ret = dev->platform_data->v2p8_ctrl(sd, 1); 661 if (ret) 662 dev->platform_data->v1p8_ctrl(sd, 0); 663 } 664 } else { 665 ret = dev->platform_data->v1p8_ctrl(sd, 0); 666 ret |= dev->platform_data->v2p8_ctrl(sd, 0); 667 } 668 669 return ret; 670 } 671 672 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 673 { 674 struct ov2722_device *dev = to_ov2722_sensor(sd); 675 int ret = -1; 676 677 if (!dev || !dev->platform_data) 678 return -ENODEV; 679 680 /* Note: the GPIO order is asymmetric: always RESET# 681 * before PWDN# when turning it on or off. 682 */ 683 ret = dev->platform_data->gpio0_ctrl(sd, flag); 684 /* 685 *ov2722 PWDN# active high when pull down,opposite to the convention 686 */ 687 ret |= dev->platform_data->gpio1_ctrl(sd, !flag); 688 return ret; 689 } 690 691 static int power_up(struct v4l2_subdev *sd) 692 { 693 struct ov2722_device *dev = to_ov2722_sensor(sd); 694 struct i2c_client *client = v4l2_get_subdevdata(sd); 695 int ret; 696 697 if (!dev->platform_data) { 698 dev_err(&client->dev, 699 "no camera_sensor_platform_data"); 700 return -ENODEV; 701 } 702 703 /* power control */ 704 ret = power_ctrl(sd, 1); 705 if (ret) 706 goto fail_power; 707 708 /* according to DS, at least 5ms is needed between DOVDD and PWDN */ 709 usleep_range(5000, 6000); 710 711 /* gpio ctrl */ 712 ret = gpio_ctrl(sd, 1); 713 if (ret) { 714 ret = gpio_ctrl(sd, 0); 715 if (ret) 716 goto fail_power; 717 } 718 719 /* flis clock control */ 720 ret = dev->platform_data->flisclk_ctrl(sd, 1); 721 if (ret) 722 goto fail_clk; 723 724 /* according to DS, 20ms is needed between PWDN and i2c access */ 725 msleep(20); 726 727 return 0; 728 729 fail_clk: 730 gpio_ctrl(sd, 0); 731 fail_power: 732 power_ctrl(sd, 0); 733 dev_err(&client->dev, "sensor power-up failed\n"); 734 735 return ret; 736 } 737 738 static int power_down(struct v4l2_subdev *sd) 739 { 740 struct ov2722_device *dev = to_ov2722_sensor(sd); 741 struct i2c_client *client = v4l2_get_subdevdata(sd); 742 int ret = 0; 743 744 if (!dev->platform_data) { 745 dev_err(&client->dev, 746 "no camera_sensor_platform_data"); 747 return -ENODEV; 748 } 749 750 ret = dev->platform_data->flisclk_ctrl(sd, 0); 751 if (ret) 752 dev_err(&client->dev, "flisclk failed\n"); 753 754 /* gpio ctrl */ 755 ret = gpio_ctrl(sd, 0); 756 if (ret) { 757 ret = gpio_ctrl(sd, 0); 758 if (ret) 759 dev_err(&client->dev, "gpio failed 2\n"); 760 } 761 762 /* power control */ 763 ret = power_ctrl(sd, 0); 764 if (ret) 765 dev_err(&client->dev, "vprog failed.\n"); 766 767 return ret; 768 } 769 770 static int ov2722_s_power(struct v4l2_subdev *sd, int on) 771 { 772 int ret; 773 774 if (on == 0) 775 return power_down(sd); 776 else { 777 ret = power_up(sd); 778 if (!ret) 779 return ov2722_init(sd); 780 } 781 return ret; 782 } 783 784 /* 785 * distance - calculate the distance 786 * @res: resolution 787 * @w: width 788 * @h: height 789 * 790 * Get the gap between resolution and w/h. 791 * res->width/height smaller than w/h wouldn't be considered. 792 * Returns the value of gap or -1 if fail. 793 */ 794 #define LARGEST_ALLOWED_RATIO_MISMATCH 800 795 static int distance(struct ov2722_resolution *res, u32 w, u32 h) 796 { 797 unsigned int w_ratio = (res->width << 13) / w; 798 unsigned int h_ratio; 799 int match; 800 801 if (h == 0) 802 return -1; 803 h_ratio = (res->height << 13) / h; 804 if (h_ratio == 0) 805 return -1; 806 match = abs(((w_ratio << 13) / h_ratio) - 8192); 807 808 if ((w_ratio < 8192) || (h_ratio < 8192) || 809 (match > LARGEST_ALLOWED_RATIO_MISMATCH)) 810 return -1; 811 812 return w_ratio + h_ratio; 813 } 814 815 /* Return the nearest higher resolution index */ 816 static int nearest_resolution_index(int w, int h) 817 { 818 int i; 819 int idx = -1; 820 int dist; 821 int min_dist = INT_MAX; 822 struct ov2722_resolution *tmp_res = NULL; 823 824 for (i = 0; i < N_RES; i++) { 825 tmp_res = &ov2722_res[i]; 826 dist = distance(tmp_res, w, h); 827 if (dist == -1) 828 continue; 829 if (dist < min_dist) { 830 min_dist = dist; 831 idx = i; 832 } 833 } 834 835 return idx; 836 } 837 838 static int get_resolution_index(int w, int h) 839 { 840 int i; 841 842 for (i = 0; i < N_RES; i++) { 843 if (w != ov2722_res[i].width) 844 continue; 845 if (h != ov2722_res[i].height) 846 continue; 847 848 return i; 849 } 850 851 return -1; 852 } 853 854 /* TODO: remove it. */ 855 static int startup(struct v4l2_subdev *sd) 856 { 857 struct ov2722_device *dev = to_ov2722_sensor(sd); 858 struct i2c_client *client = v4l2_get_subdevdata(sd); 859 int ret = 0; 860 861 ret = ov2722_write_reg(client, OV2722_8BIT, 862 OV2722_SW_RESET, 0x01); 863 if (ret) { 864 dev_err(&client->dev, "ov2722 reset err.\n"); 865 return ret; 866 } 867 868 ret = ov2722_write_reg_array(client, ov2722_res[dev->fmt_idx].regs); 869 if (ret) { 870 dev_err(&client->dev, "ov2722 write register err.\n"); 871 return ret; 872 } 873 874 return ret; 875 } 876 877 static int ov2722_set_fmt(struct v4l2_subdev *sd, 878 struct v4l2_subdev_pad_config *cfg, 879 struct v4l2_subdev_format *format) 880 { 881 struct v4l2_mbus_framefmt *fmt = &format->format; 882 struct ov2722_device *dev = to_ov2722_sensor(sd); 883 struct i2c_client *client = v4l2_get_subdevdata(sd); 884 struct camera_mipi_info *ov2722_info = NULL; 885 int ret = 0; 886 int idx; 887 888 if (format->pad) 889 return -EINVAL; 890 if (!fmt) 891 return -EINVAL; 892 ov2722_info = v4l2_get_subdev_hostdata(sd); 893 if (!ov2722_info) 894 return -EINVAL; 895 896 mutex_lock(&dev->input_lock); 897 idx = nearest_resolution_index(fmt->width, fmt->height); 898 if (idx == -1) { 899 /* return the largest resolution */ 900 fmt->width = ov2722_res[N_RES - 1].width; 901 fmt->height = ov2722_res[N_RES - 1].height; 902 } else { 903 fmt->width = ov2722_res[idx].width; 904 fmt->height = ov2722_res[idx].height; 905 } 906 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; 907 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 908 cfg->try_fmt = *fmt; 909 mutex_unlock(&dev->input_lock); 910 return 0; 911 } 912 913 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height); 914 if (dev->fmt_idx == -1) { 915 dev_err(&client->dev, "get resolution fail\n"); 916 mutex_unlock(&dev->input_lock); 917 return -EINVAL; 918 } 919 920 dev->pixels_per_line = ov2722_res[dev->fmt_idx].pixels_per_line; 921 dev->lines_per_frame = ov2722_res[dev->fmt_idx].lines_per_frame; 922 923 ret = startup(sd); 924 if (ret) { 925 int i = 0; 926 927 dev_err(&client->dev, "ov2722 startup err, retry to power up\n"); 928 for (i = 0; i < OV2722_POWER_UP_RETRY_NUM; i++) { 929 dev_err(&client->dev, 930 "ov2722 retry to power up %d/%d times, result: ", 931 i + 1, OV2722_POWER_UP_RETRY_NUM); 932 power_down(sd); 933 ret = power_up(sd); 934 if (ret) { 935 dev_err(&client->dev, "power up failed, continue\n"); 936 continue; 937 } 938 ret = startup(sd); 939 if (ret) { 940 dev_err(&client->dev, " startup FAILED!\n"); 941 } else { 942 dev_err(&client->dev, " startup SUCCESS!\n"); 943 break; 944 } 945 } 946 if (ret) { 947 dev_err(&client->dev, "ov2722 startup err\n"); 948 goto err; 949 } 950 } 951 952 ret = ov2722_get_intg_factor(client, ov2722_info, 953 &ov2722_res[dev->fmt_idx]); 954 if (ret) 955 dev_err(&client->dev, "failed to get integration_factor\n"); 956 957 err: 958 mutex_unlock(&dev->input_lock); 959 return ret; 960 } 961 962 static int ov2722_get_fmt(struct v4l2_subdev *sd, 963 struct v4l2_subdev_pad_config *cfg, 964 struct v4l2_subdev_format *format) 965 { 966 struct v4l2_mbus_framefmt *fmt = &format->format; 967 struct ov2722_device *dev = to_ov2722_sensor(sd); 968 969 if (format->pad) 970 return -EINVAL; 971 if (!fmt) 972 return -EINVAL; 973 974 fmt->width = ov2722_res[dev->fmt_idx].width; 975 fmt->height = ov2722_res[dev->fmt_idx].height; 976 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10; 977 978 return 0; 979 } 980 981 static int ov2722_detect(struct i2c_client *client) 982 { 983 struct i2c_adapter *adapter = client->adapter; 984 u16 high, low; 985 int ret; 986 u16 id; 987 u8 revision; 988 989 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 990 return -ENODEV; 991 992 ret = ov2722_read_reg(client, OV2722_8BIT, 993 OV2722_SC_CMMN_CHIP_ID_H, &high); 994 if (ret) { 995 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high); 996 return -ENODEV; 997 } 998 ret = ov2722_read_reg(client, OV2722_8BIT, 999 OV2722_SC_CMMN_CHIP_ID_L, &low); 1000 id = (high << 8) | low; 1001 1002 if ((id != OV2722_ID) && (id != OV2720_ID)) { 1003 dev_err(&client->dev, "sensor ID error\n"); 1004 return -ENODEV; 1005 } 1006 1007 ret = ov2722_read_reg(client, OV2722_8BIT, 1008 OV2722_SC_CMMN_SUB_ID, &high); 1009 revision = (u8)high & 0x0f; 1010 1011 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision); 1012 dev_dbg(&client->dev, "detect ov2722 success\n"); 1013 return 0; 1014 } 1015 1016 static int ov2722_s_stream(struct v4l2_subdev *sd, int enable) 1017 { 1018 struct ov2722_device *dev = to_ov2722_sensor(sd); 1019 struct i2c_client *client = v4l2_get_subdevdata(sd); 1020 int ret; 1021 1022 mutex_lock(&dev->input_lock); 1023 1024 ret = ov2722_write_reg(client, OV2722_8BIT, OV2722_SW_STREAM, 1025 enable ? OV2722_START_STREAMING : 1026 OV2722_STOP_STREAMING); 1027 1028 mutex_unlock(&dev->input_lock); 1029 return ret; 1030 } 1031 1032 static int ov2722_s_config(struct v4l2_subdev *sd, 1033 int irq, void *platform_data) 1034 { 1035 struct ov2722_device *dev = to_ov2722_sensor(sd); 1036 struct i2c_client *client = v4l2_get_subdevdata(sd); 1037 int ret = 0; 1038 1039 if (!platform_data) 1040 return -ENODEV; 1041 1042 dev->platform_data = 1043 (struct camera_sensor_platform_data *)platform_data; 1044 1045 mutex_lock(&dev->input_lock); 1046 1047 /* power off the module, then power on it in future 1048 * as first power on by board may not fulfill the 1049 * power on sequqence needed by the module 1050 */ 1051 ret = power_down(sd); 1052 if (ret) { 1053 dev_err(&client->dev, "ov2722 power-off err.\n"); 1054 goto fail_power_off; 1055 } 1056 1057 ret = power_up(sd); 1058 if (ret) { 1059 dev_err(&client->dev, "ov2722 power-up err.\n"); 1060 goto fail_power_on; 1061 } 1062 1063 ret = dev->platform_data->csi_cfg(sd, 1); 1064 if (ret) 1065 goto fail_csi_cfg; 1066 1067 /* config & detect sensor */ 1068 ret = ov2722_detect(client); 1069 if (ret) { 1070 dev_err(&client->dev, "ov2722_detect err s_config.\n"); 1071 goto fail_csi_cfg; 1072 } 1073 1074 /* turn off sensor, after probed */ 1075 ret = power_down(sd); 1076 if (ret) { 1077 dev_err(&client->dev, "ov2722 power-off err.\n"); 1078 goto fail_csi_cfg; 1079 } 1080 mutex_unlock(&dev->input_lock); 1081 1082 return 0; 1083 1084 fail_csi_cfg: 1085 dev->platform_data->csi_cfg(sd, 0); 1086 fail_power_on: 1087 power_down(sd); 1088 dev_err(&client->dev, "sensor power-gating failed\n"); 1089 fail_power_off: 1090 mutex_unlock(&dev->input_lock); 1091 return ret; 1092 } 1093 1094 static int ov2722_g_frame_interval(struct v4l2_subdev *sd, 1095 struct v4l2_subdev_frame_interval *interval) 1096 { 1097 struct ov2722_device *dev = to_ov2722_sensor(sd); 1098 1099 interval->interval.numerator = 1; 1100 interval->interval.denominator = ov2722_res[dev->fmt_idx].fps; 1101 1102 return 0; 1103 } 1104 1105 static int ov2722_enum_mbus_code(struct v4l2_subdev *sd, 1106 struct v4l2_subdev_pad_config *cfg, 1107 struct v4l2_subdev_mbus_code_enum *code) 1108 { 1109 if (code->index >= MAX_FMTS) 1110 return -EINVAL; 1111 1112 code->code = MEDIA_BUS_FMT_SBGGR10_1X10; 1113 return 0; 1114 } 1115 1116 static int ov2722_enum_frame_size(struct v4l2_subdev *sd, 1117 struct v4l2_subdev_pad_config *cfg, 1118 struct v4l2_subdev_frame_size_enum *fse) 1119 { 1120 int index = fse->index; 1121 1122 if (index >= N_RES) 1123 return -EINVAL; 1124 1125 fse->min_width = ov2722_res[index].width; 1126 fse->min_height = ov2722_res[index].height; 1127 fse->max_width = ov2722_res[index].width; 1128 fse->max_height = ov2722_res[index].height; 1129 1130 return 0; 1131 } 1132 1133 static int ov2722_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) 1134 { 1135 struct ov2722_device *dev = to_ov2722_sensor(sd); 1136 1137 mutex_lock(&dev->input_lock); 1138 *frames = ov2722_res[dev->fmt_idx].skip_frames; 1139 mutex_unlock(&dev->input_lock); 1140 1141 return 0; 1142 } 1143 1144 static const struct v4l2_subdev_sensor_ops ov2722_sensor_ops = { 1145 .g_skip_frames = ov2722_g_skip_frames, 1146 }; 1147 1148 static const struct v4l2_subdev_video_ops ov2722_video_ops = { 1149 .s_stream = ov2722_s_stream, 1150 .g_frame_interval = ov2722_g_frame_interval, 1151 }; 1152 1153 static const struct v4l2_subdev_core_ops ov2722_core_ops = { 1154 .s_power = ov2722_s_power, 1155 .ioctl = ov2722_ioctl, 1156 }; 1157 1158 static const struct v4l2_subdev_pad_ops ov2722_pad_ops = { 1159 .enum_mbus_code = ov2722_enum_mbus_code, 1160 .enum_frame_size = ov2722_enum_frame_size, 1161 .get_fmt = ov2722_get_fmt, 1162 .set_fmt = ov2722_set_fmt, 1163 }; 1164 1165 static const struct v4l2_subdev_ops ov2722_ops = { 1166 .core = &ov2722_core_ops, 1167 .video = &ov2722_video_ops, 1168 .pad = &ov2722_pad_ops, 1169 .sensor = &ov2722_sensor_ops, 1170 }; 1171 1172 static int ov2722_remove(struct i2c_client *client) 1173 { 1174 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1175 struct ov2722_device *dev = to_ov2722_sensor(sd); 1176 1177 dev_dbg(&client->dev, "ov2722_remove...\n"); 1178 1179 dev->platform_data->csi_cfg(sd, 0); 1180 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1181 v4l2_device_unregister_subdev(sd); 1182 1183 atomisp_gmin_remove_subdev(sd); 1184 1185 media_entity_cleanup(&dev->sd.entity); 1186 kfree(dev); 1187 1188 return 0; 1189 } 1190 1191 static int __ov2722_init_ctrl_handler(struct ov2722_device *dev) 1192 { 1193 struct v4l2_ctrl_handler *hdl; 1194 unsigned int i; 1195 1196 hdl = &dev->ctrl_handler; 1197 v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ov2722_controls)); 1198 for (i = 0; i < ARRAY_SIZE(ov2722_controls); i++) 1199 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2722_controls[i], 1200 NULL); 1201 1202 dev->link_freq = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_LINK_FREQ); 1203 1204 if (dev->ctrl_handler.error || !dev->link_freq) 1205 return dev->ctrl_handler.error; 1206 1207 dev->sd.ctrl_handler = hdl; 1208 1209 return 0; 1210 } 1211 1212 static int ov2722_probe(struct i2c_client *client) 1213 { 1214 struct ov2722_device *dev; 1215 void *ovpdev; 1216 int ret; 1217 acpi_handle handle; 1218 struct acpi_device *adev; 1219 1220 handle = ACPI_HANDLE(&client->dev); 1221 if (!handle || acpi_bus_get_device(handle, &adev)) { 1222 dev_err(&client->dev, "Error could not get ACPI device\n"); 1223 return -ENODEV; 1224 } 1225 pr_info("%s: ACPI detected it on bus ID=%s, HID=%s\n", 1226 __func__, acpi_device_bid(adev), acpi_device_hid(adev)); 1227 // FIXME: may need to release resources allocated by acpi_bus_get_device() 1228 1229 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1230 if (!dev) 1231 return -ENOMEM; 1232 1233 mutex_init(&dev->input_lock); 1234 1235 dev->fmt_idx = 0; 1236 v4l2_i2c_subdev_init(&dev->sd, client, &ov2722_ops); 1237 1238 ovpdev = gmin_camera_platform_data(&dev->sd, 1239 ATOMISP_INPUT_FORMAT_RAW_10, 1240 atomisp_bayer_order_grbg); 1241 1242 ret = ov2722_s_config(&dev->sd, client->irq, ovpdev); 1243 if (ret) 1244 goto out_free; 1245 1246 ret = __ov2722_init_ctrl_handler(dev); 1247 if (ret) 1248 goto out_ctrl_handler_free; 1249 1250 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1251 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 1252 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10; 1253 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1254 1255 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 1256 if (ret) 1257 ov2722_remove(client); 1258 1259 return atomisp_register_i2c_module(&dev->sd, ovpdev, RAW_CAMERA); 1260 1261 out_ctrl_handler_free: 1262 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1263 1264 out_free: 1265 v4l2_device_unregister_subdev(&dev->sd); 1266 kfree(dev); 1267 return ret; 1268 } 1269 1270 static const struct acpi_device_id ov2722_acpi_match[] = { 1271 { "INT33FB" }, 1272 {}, 1273 }; 1274 MODULE_DEVICE_TABLE(acpi, ov2722_acpi_match); 1275 1276 static struct i2c_driver ov2722_driver = { 1277 .driver = { 1278 .name = "ov2722", 1279 .acpi_match_table = ov2722_acpi_match, 1280 }, 1281 .probe_new = ov2722_probe, 1282 .remove = ov2722_remove, 1283 }; 1284 module_i2c_driver(ov2722_driver); 1285 1286 MODULE_AUTHOR("Wei Liu <wei.liu@intel.com>"); 1287 MODULE_DESCRIPTION("A low-level driver for OmniVision 2722 sensors"); 1288 MODULE_LICENSE("GPL"); 1289