1 /* 2 * Support for OmniVision OV5693 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 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 <linux/io.h> 33 #include <linux/acpi.h> 34 #include "../../include/linux/atomisp_gmin_platform.h" 35 36 #include "ov5693.h" 37 #include "ad5823.h" 38 39 #define __cci_delay(t) \ 40 do { \ 41 if ((t) < 10) { \ 42 usleep_range((t) * 1000, ((t) + 1) * 1000); \ 43 } else { \ 44 msleep((t)); \ 45 } \ 46 } while (0) 47 48 /* Value 30ms reached through experimentation on byt ecs. 49 * The DS specifies a much lower value but when using a smaller value 50 * the I2C bus sometimes locks up permanently when starting the camera. 51 * This issue could not be reproduced on cht, so we can reduce the 52 * delay value to a lower value when insmod. 53 */ 54 static uint up_delay = 30; 55 module_param(up_delay, uint, 0644); 56 MODULE_PARM_DESC(up_delay, 57 "Delay prior to the first CCI transaction for ov5693"); 58 59 static int vcm_ad_i2c_wr8(struct i2c_client *client, u8 reg, u8 val) 60 { 61 int err; 62 struct i2c_msg msg; 63 u8 buf[2]; 64 65 buf[0] = reg; 66 buf[1] = val; 67 68 msg.addr = VCM_ADDR; 69 msg.flags = 0; 70 msg.len = 2; 71 msg.buf = &buf[0]; 72 73 err = i2c_transfer(client->adapter, &msg, 1); 74 if (err != 1) { 75 dev_err(&client->dev, "%s: vcm i2c fail, err code = %d\n", 76 __func__, err); 77 return -EIO; 78 } 79 return 0; 80 } 81 82 static int ad5823_i2c_write(struct i2c_client *client, u8 reg, u8 val) 83 { 84 struct i2c_msg msg; 85 u8 buf[2]; 86 87 buf[0] = reg; 88 buf[1] = val; 89 msg.addr = AD5823_VCM_ADDR; 90 msg.flags = 0; 91 msg.len = 0x02; 92 msg.buf = &buf[0]; 93 94 if (i2c_transfer(client->adapter, &msg, 1) != 1) 95 return -EIO; 96 return 0; 97 } 98 99 static int ad5823_i2c_read(struct i2c_client *client, u8 reg, u8 *val) 100 { 101 struct i2c_msg msg[2]; 102 u8 buf[2]; 103 104 buf[0] = reg; 105 buf[1] = 0; 106 107 msg[0].addr = AD5823_VCM_ADDR; 108 msg[0].flags = 0; 109 msg[0].len = 0x01; 110 msg[0].buf = &buf[0]; 111 112 msg[1].addr = 0x0c; 113 msg[1].flags = I2C_M_RD; 114 msg[1].len = 0x01; 115 msg[1].buf = &buf[1]; 116 *val = 0; 117 if (i2c_transfer(client->adapter, msg, 2) != 2) 118 return -EIO; 119 *val = buf[1]; 120 return 0; 121 } 122 123 static const u32 ov5693_embedded_effective_size = 28; 124 125 /* i2c read/write stuff */ 126 static int ov5693_read_reg(struct i2c_client *client, 127 u16 data_length, u16 reg, u16 *val) 128 { 129 int err; 130 struct i2c_msg msg[2]; 131 unsigned char data[6]; 132 133 if (!client->adapter) { 134 dev_err(&client->dev, "%s error, no client->adapter\n", 135 __func__); 136 return -ENODEV; 137 } 138 139 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT 140 && data_length != OV5693_32BIT) { 141 dev_err(&client->dev, "%s error, invalid data length\n", 142 __func__); 143 return -EINVAL; 144 } 145 146 memset(msg, 0, sizeof(msg)); 147 148 msg[0].addr = client->addr; 149 msg[0].flags = 0; 150 msg[0].len = I2C_MSG_LENGTH; 151 msg[0].buf = data; 152 153 /* high byte goes out first */ 154 data[0] = (u8)(reg >> 8); 155 data[1] = (u8)(reg & 0xff); 156 157 msg[1].addr = client->addr; 158 msg[1].len = data_length; 159 msg[1].flags = I2C_M_RD; 160 msg[1].buf = data; 161 162 err = i2c_transfer(client->adapter, msg, 2); 163 if (err != 2) { 164 if (err >= 0) 165 err = -EIO; 166 dev_err(&client->dev, 167 "read from offset 0x%x error %d", reg, err); 168 return err; 169 } 170 171 *val = 0; 172 /* high byte comes first */ 173 if (data_length == OV5693_8BIT) 174 *val = (u8)data[0]; 175 else if (data_length == OV5693_16BIT) 176 *val = be16_to_cpu(*(__be16 *)&data[0]); 177 else 178 *val = be32_to_cpu(*(__be32 *)&data[0]); 179 180 return 0; 181 } 182 183 static int ov5693_i2c_write(struct i2c_client *client, u16 len, u8 *data) 184 { 185 struct i2c_msg msg; 186 const int num_msg = 1; 187 int ret; 188 189 msg.addr = client->addr; 190 msg.flags = 0; 191 msg.len = len; 192 msg.buf = data; 193 ret = i2c_transfer(client->adapter, &msg, 1); 194 195 return ret == num_msg ? 0 : -EIO; 196 } 197 198 static int vcm_dw_i2c_write(struct i2c_client *client, u16 data) 199 { 200 struct i2c_msg msg; 201 const int num_msg = 1; 202 int ret; 203 __be16 val; 204 205 val = cpu_to_be16(data); 206 msg.addr = VCM_ADDR; 207 msg.flags = 0; 208 msg.len = OV5693_16BIT; 209 msg.buf = (void *)&val; 210 211 ret = i2c_transfer(client->adapter, &msg, 1); 212 213 return ret == num_msg ? 0 : -EIO; 214 } 215 216 /* 217 * Theory: per datasheet, the two VCMs both allow for a 2-byte read. 218 * The DW9714 doesn't actually specify what this does (it has a 219 * two-byte write-only protocol, but specifies the read sequence as 220 * legal), but it returns the same data (zeroes) always, after an 221 * undocumented initial NAK. The AD5823 has a one-byte address 222 * register to which all writes go, and subsequent reads will cycle 223 * through the 8 bytes of registers. Notably, the default values (the 224 * device is always power-cycled affirmatively, so we can rely on 225 * these) in AD5823 are not pairwise repetitions of the same 16 bit 226 * word. So all we have to do is sequentially read two bytes at a 227 * time and see if we detect a difference in any of the first four 228 * pairs. 229 */ 230 static int vcm_detect(struct i2c_client *client) 231 { 232 int i, ret; 233 struct i2c_msg msg; 234 u16 data0 = 0, data; 235 236 for (i = 0; i < 4; i++) { 237 msg.addr = VCM_ADDR; 238 msg.flags = I2C_M_RD; 239 msg.len = sizeof(data); 240 msg.buf = (u8 *)&data; 241 ret = i2c_transfer(client->adapter, &msg, 1); 242 243 /* 244 * DW9714 always fails the first read and returns 245 * zeroes for subsequent ones 246 */ 247 if (i == 0 && ret == -EREMOTEIO) { 248 data0 = 0; 249 continue; 250 } 251 252 if (i == 0) 253 data0 = data; 254 255 if (data != data0) 256 return VCM_AD5823; 257 } 258 return ret == 1 ? VCM_DW9714 : ret; 259 } 260 261 static int ov5693_write_reg(struct i2c_client *client, u16 data_length, 262 u16 reg, u16 val) 263 { 264 int ret; 265 unsigned char data[4] = {0}; 266 __be16 *wreg = (void *)data; 267 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */ 268 269 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT) { 270 dev_err(&client->dev, 271 "%s error, invalid data_length\n", __func__); 272 return -EINVAL; 273 } 274 275 /* high byte goes out first */ 276 *wreg = cpu_to_be16(reg); 277 278 if (data_length == OV5693_8BIT) { 279 data[2] = (u8)(val); 280 } else { 281 /* OV5693_16BIT */ 282 __be16 *wdata = (void *)&data[2]; 283 284 *wdata = cpu_to_be16(val); 285 } 286 287 ret = ov5693_i2c_write(client, len, data); 288 if (ret) 289 dev_err(&client->dev, 290 "write error: wrote 0x%x to offset 0x%x error %d", 291 val, reg, ret); 292 293 return ret; 294 } 295 296 /* 297 * ov5693_write_reg_array - Initializes a list of OV5693 registers 298 * @client: i2c driver client structure 299 * @reglist: list of registers to be written 300 * 301 * This function initializes a list of registers. When consecutive addresses 302 * are found in a row on the list, this function creates a buffer and sends 303 * consecutive data in a single i2c_transfer(). 304 * 305 * __ov5693_flush_reg_array, __ov5693_buf_reg_array() and 306 * __ov5693_write_reg_is_consecutive() are internal functions to 307 * ov5693_write_reg_array_fast() and should be not used anywhere else. 308 * 309 */ 310 311 static int __ov5693_flush_reg_array(struct i2c_client *client, 312 struct ov5693_write_ctrl *ctrl) 313 { 314 u16 size; 315 __be16 *reg = (void *)&ctrl->buffer.addr; 316 317 if (ctrl->index == 0) 318 return 0; 319 320 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */ 321 322 *reg = cpu_to_be16(ctrl->buffer.addr); 323 ctrl->index = 0; 324 325 return ov5693_i2c_write(client, size, (u8 *)reg); 326 } 327 328 static int __ov5693_buf_reg_array(struct i2c_client *client, 329 struct ov5693_write_ctrl *ctrl, 330 const struct ov5693_reg *next) 331 { 332 int size; 333 __be16 *data16; 334 335 switch (next->type) { 336 case OV5693_8BIT: 337 size = 1; 338 ctrl->buffer.data[ctrl->index] = (u8)next->val; 339 break; 340 case OV5693_16BIT: 341 size = 2; 342 343 data16 = (void *)&ctrl->buffer.data[ctrl->index]; 344 *data16 = cpu_to_be16((u16)next->val); 345 break; 346 default: 347 return -EINVAL; 348 } 349 350 /* When first item is added, we need to store its starting address */ 351 if (ctrl->index == 0) 352 ctrl->buffer.addr = next->reg; 353 354 ctrl->index += size; 355 356 /* 357 * Buffer cannot guarantee free space for u32? Better flush it to avoid 358 * possible lack of memory for next item. 359 */ 360 if (ctrl->index + sizeof(u16) >= OV5693_MAX_WRITE_BUF_SIZE) 361 return __ov5693_flush_reg_array(client, ctrl); 362 363 return 0; 364 } 365 366 static int __ov5693_write_reg_is_consecutive(struct i2c_client *client, 367 struct ov5693_write_ctrl *ctrl, 368 const struct ov5693_reg *next) 369 { 370 if (ctrl->index == 0) 371 return 1; 372 373 return ctrl->buffer.addr + ctrl->index == next->reg; 374 } 375 376 static int ov5693_write_reg_array(struct i2c_client *client, 377 const struct ov5693_reg *reglist) 378 { 379 const struct ov5693_reg *next = reglist; 380 struct ov5693_write_ctrl ctrl; 381 int err; 382 383 ctrl.index = 0; 384 for (; next->type != OV5693_TOK_TERM; next++) { 385 switch (next->type & OV5693_TOK_MASK) { 386 case OV5693_TOK_DELAY: 387 err = __ov5693_flush_reg_array(client, &ctrl); 388 if (err) 389 return err; 390 msleep(next->val); 391 break; 392 default: 393 /* 394 * If next address is not consecutive, data needs to be 395 * flushed before proceed. 396 */ 397 if (!__ov5693_write_reg_is_consecutive(client, &ctrl, 398 next)) { 399 err = __ov5693_flush_reg_array(client, &ctrl); 400 if (err) 401 return err; 402 } 403 err = __ov5693_buf_reg_array(client, &ctrl, next); 404 if (err) { 405 dev_err(&client->dev, 406 "%s: write error, aborted\n", 407 __func__); 408 return err; 409 } 410 break; 411 } 412 } 413 414 return __ov5693_flush_reg_array(client, &ctrl); 415 } 416 417 static int ov5693_g_focal(struct v4l2_subdev *sd, s32 *val) 418 { 419 *val = (OV5693_FOCAL_LENGTH_NUM << 16) | OV5693_FOCAL_LENGTH_DEM; 420 return 0; 421 } 422 423 static int ov5693_g_fnumber(struct v4l2_subdev *sd, s32 *val) 424 { 425 /*const f number for imx*/ 426 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 16) | OV5693_F_NUMBER_DEM; 427 return 0; 428 } 429 430 static int ov5693_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) 431 { 432 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 24) | 433 (OV5693_F_NUMBER_DEM << 16) | 434 (OV5693_F_NUMBER_DEFAULT_NUM << 8) | OV5693_F_NUMBER_DEM; 435 return 0; 436 } 437 438 static int ov5693_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val) 439 { 440 struct ov5693_device *dev = to_ov5693_sensor(sd); 441 442 *val = ov5693_res[dev->fmt_idx].bin_factor_x; 443 444 return 0; 445 } 446 447 static int ov5693_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val) 448 { 449 struct ov5693_device *dev = to_ov5693_sensor(sd); 450 451 *val = ov5693_res[dev->fmt_idx].bin_factor_y; 452 453 return 0; 454 } 455 456 static int ov5693_get_intg_factor(struct i2c_client *client, 457 struct camera_mipi_info *info, 458 const struct ov5693_resolution *res) 459 { 460 struct v4l2_subdev *sd = i2c_get_clientdata(client); 461 struct ov5693_device *dev = to_ov5693_sensor(sd); 462 struct atomisp_sensor_mode_data *buf = &info->data; 463 unsigned int pix_clk_freq_hz; 464 u16 reg_val; 465 int ret; 466 467 if (!info) 468 return -EINVAL; 469 470 /* pixel clock */ 471 pix_clk_freq_hz = res->pix_clk_freq * 1000000; 472 473 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz; 474 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz; 475 476 /* get integration time */ 477 buf->coarse_integration_time_min = OV5693_COARSE_INTG_TIME_MIN; 478 buf->coarse_integration_time_max_margin = 479 OV5693_COARSE_INTG_TIME_MAX_MARGIN; 480 481 buf->fine_integration_time_min = OV5693_FINE_INTG_TIME_MIN; 482 buf->fine_integration_time_max_margin = 483 OV5693_FINE_INTG_TIME_MAX_MARGIN; 484 485 buf->fine_integration_time_def = OV5693_FINE_INTG_TIME_MIN; 486 buf->frame_length_lines = res->lines_per_frame; 487 buf->line_length_pck = res->pixels_per_line; 488 buf->read_mode = res->bin_mode; 489 490 /* get the cropping and output resolution to ISP for this mode. */ 491 ret = ov5693_read_reg(client, OV5693_16BIT, 492 OV5693_HORIZONTAL_START_H, ®_val); 493 if (ret) 494 return ret; 495 buf->crop_horizontal_start = reg_val; 496 497 ret = ov5693_read_reg(client, OV5693_16BIT, 498 OV5693_VERTICAL_START_H, ®_val); 499 if (ret) 500 return ret; 501 buf->crop_vertical_start = reg_val; 502 503 ret = ov5693_read_reg(client, OV5693_16BIT, 504 OV5693_HORIZONTAL_END_H, ®_val); 505 if (ret) 506 return ret; 507 buf->crop_horizontal_end = reg_val; 508 509 ret = ov5693_read_reg(client, OV5693_16BIT, 510 OV5693_VERTICAL_END_H, ®_val); 511 if (ret) 512 return ret; 513 buf->crop_vertical_end = reg_val; 514 515 ret = ov5693_read_reg(client, OV5693_16BIT, 516 OV5693_HORIZONTAL_OUTPUT_SIZE_H, ®_val); 517 if (ret) 518 return ret; 519 buf->output_width = reg_val; 520 521 ret = ov5693_read_reg(client, OV5693_16BIT, 522 OV5693_VERTICAL_OUTPUT_SIZE_H, ®_val); 523 if (ret) 524 return ret; 525 buf->output_height = reg_val; 526 527 buf->binning_factor_x = res->bin_factor_x ? 528 res->bin_factor_x : 1; 529 buf->binning_factor_y = res->bin_factor_y ? 530 res->bin_factor_y : 1; 531 return 0; 532 } 533 534 static long __ov5693_set_exposure(struct v4l2_subdev *sd, int coarse_itg, 535 int gain, int digitgain) 536 537 { 538 struct i2c_client *client = v4l2_get_subdevdata(sd); 539 struct ov5693_device *dev = to_ov5693_sensor(sd); 540 u16 vts, hts; 541 int ret, exp_val; 542 543 hts = ov5693_res[dev->fmt_idx].pixels_per_line; 544 vts = ov5693_res[dev->fmt_idx].lines_per_frame; 545 /* 546 * If coarse_itg is larger than 1<<15, can not write to reg directly. 547 * The way is to write coarse_itg/2 to the reg, meanwhile write 2*hts 548 * to the reg. 549 */ 550 if (coarse_itg > (1 << 15)) { 551 hts = hts * 2; 552 coarse_itg = (int)coarse_itg / 2; 553 } 554 /* group hold */ 555 ret = ov5693_write_reg(client, OV5693_8BIT, 556 OV5693_GROUP_ACCESS, 0x00); 557 if (ret) { 558 dev_err(&client->dev, "%s: write %x error, aborted\n", 559 __func__, OV5693_GROUP_ACCESS); 560 return ret; 561 } 562 563 ret = ov5693_write_reg(client, OV5693_8BIT, 564 OV5693_TIMING_HTS_H, (hts >> 8) & 0xFF); 565 if (ret) { 566 dev_err(&client->dev, "%s: write %x error, aborted\n", 567 __func__, OV5693_TIMING_HTS_H); 568 return ret; 569 } 570 571 ret = ov5693_write_reg(client, OV5693_8BIT, 572 OV5693_TIMING_HTS_L, hts & 0xFF); 573 if (ret) { 574 dev_err(&client->dev, "%s: write %x error, aborted\n", 575 __func__, OV5693_TIMING_HTS_L); 576 return ret; 577 } 578 /* Increase the VTS to match exposure + MARGIN */ 579 if (coarse_itg > vts - OV5693_INTEGRATION_TIME_MARGIN) 580 vts = (u16)coarse_itg + OV5693_INTEGRATION_TIME_MARGIN; 581 582 ret = ov5693_write_reg(client, OV5693_8BIT, 583 OV5693_TIMING_VTS_H, (vts >> 8) & 0xFF); 584 if (ret) { 585 dev_err(&client->dev, "%s: write %x error, aborted\n", 586 __func__, OV5693_TIMING_VTS_H); 587 return ret; 588 } 589 590 ret = ov5693_write_reg(client, OV5693_8BIT, 591 OV5693_TIMING_VTS_L, vts & 0xFF); 592 if (ret) { 593 dev_err(&client->dev, "%s: write %x error, aborted\n", 594 __func__, OV5693_TIMING_VTS_L); 595 return ret; 596 } 597 598 /* set exposure */ 599 600 /* Lower four bit should be 0*/ 601 exp_val = coarse_itg << 4; 602 ret = ov5693_write_reg(client, OV5693_8BIT, 603 OV5693_EXPOSURE_L, exp_val & 0xFF); 604 if (ret) { 605 dev_err(&client->dev, "%s: write %x error, aborted\n", 606 __func__, OV5693_EXPOSURE_L); 607 return ret; 608 } 609 610 ret = ov5693_write_reg(client, OV5693_8BIT, 611 OV5693_EXPOSURE_M, (exp_val >> 8) & 0xFF); 612 if (ret) { 613 dev_err(&client->dev, "%s: write %x error, aborted\n", 614 __func__, OV5693_EXPOSURE_M); 615 return ret; 616 } 617 618 ret = ov5693_write_reg(client, OV5693_8BIT, 619 OV5693_EXPOSURE_H, (exp_val >> 16) & 0x0F); 620 if (ret) { 621 dev_err(&client->dev, "%s: write %x error, aborted\n", 622 __func__, OV5693_EXPOSURE_H); 623 return ret; 624 } 625 626 /* Analog gain */ 627 ret = ov5693_write_reg(client, OV5693_8BIT, 628 OV5693_AGC_L, gain & 0xff); 629 if (ret) { 630 dev_err(&client->dev, "%s: write %x error, aborted\n", 631 __func__, OV5693_AGC_L); 632 return ret; 633 } 634 635 ret = ov5693_write_reg(client, OV5693_8BIT, 636 OV5693_AGC_H, (gain >> 8) & 0xff); 637 if (ret) { 638 dev_err(&client->dev, "%s: write %x error, aborted\n", 639 __func__, OV5693_AGC_H); 640 return ret; 641 } 642 643 /* Digital gain */ 644 if (digitgain) { 645 ret = ov5693_write_reg(client, OV5693_16BIT, 646 OV5693_MWB_RED_GAIN_H, digitgain); 647 if (ret) { 648 dev_err(&client->dev, "%s: write %x error, aborted\n", 649 __func__, OV5693_MWB_RED_GAIN_H); 650 return ret; 651 } 652 653 ret = ov5693_write_reg(client, OV5693_16BIT, 654 OV5693_MWB_GREEN_GAIN_H, digitgain); 655 if (ret) { 656 dev_err(&client->dev, "%s: write %x error, aborted\n", 657 __func__, OV5693_MWB_RED_GAIN_H); 658 return ret; 659 } 660 661 ret = ov5693_write_reg(client, OV5693_16BIT, 662 OV5693_MWB_BLUE_GAIN_H, digitgain); 663 if (ret) { 664 dev_err(&client->dev, "%s: write %x error, aborted\n", 665 __func__, OV5693_MWB_RED_GAIN_H); 666 return ret; 667 } 668 } 669 670 /* End group */ 671 ret = ov5693_write_reg(client, OV5693_8BIT, 672 OV5693_GROUP_ACCESS, 0x10); 673 if (ret) 674 return ret; 675 676 /* Delay launch group */ 677 ret = ov5693_write_reg(client, OV5693_8BIT, 678 OV5693_GROUP_ACCESS, 0xa0); 679 if (ret) 680 return ret; 681 return ret; 682 } 683 684 static int ov5693_set_exposure(struct v4l2_subdev *sd, int exposure, 685 int gain, int digitgain) 686 { 687 struct ov5693_device *dev = to_ov5693_sensor(sd); 688 int ret; 689 690 mutex_lock(&dev->input_lock); 691 ret = __ov5693_set_exposure(sd, exposure, gain, digitgain); 692 mutex_unlock(&dev->input_lock); 693 694 return ret; 695 } 696 697 static long ov5693_s_exposure(struct v4l2_subdev *sd, 698 struct atomisp_exposure *exposure) 699 { 700 u16 coarse_itg = exposure->integration_time[0]; 701 u16 analog_gain = exposure->gain[0]; 702 u16 digital_gain = exposure->gain[1]; 703 704 /* we should not accept the invalid value below */ 705 if (analog_gain == 0) { 706 struct i2c_client *client = v4l2_get_subdevdata(sd); 707 708 v4l2_err(client, "%s: invalid value\n", __func__); 709 return -EINVAL; 710 } 711 return ov5693_set_exposure(sd, coarse_itg, analog_gain, digital_gain); 712 } 713 714 static int ov5693_read_otp_reg_array(struct i2c_client *client, u16 size, 715 u16 addr, u8 *buf) 716 { 717 u16 index; 718 int ret; 719 u16 *pVal = NULL; 720 721 for (index = 0; index <= size; index++) { 722 pVal = (u16 *)(buf + index); 723 ret = 724 ov5693_read_reg(client, OV5693_8BIT, addr + index, 725 pVal); 726 if (ret) 727 return ret; 728 } 729 730 return 0; 731 } 732 733 static int __ov5693_otp_read(struct v4l2_subdev *sd, u8 *buf) 734 { 735 struct i2c_client *client = v4l2_get_subdevdata(sd); 736 struct ov5693_device *dev = to_ov5693_sensor(sd); 737 int ret; 738 int i; 739 u8 *b = buf; 740 741 dev->otp_size = 0; 742 for (i = 1; i < OV5693_OTP_BANK_MAX; i++) { 743 /*set bank NO and OTP read mode. */ 744 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_BANK_REG, 745 (i | 0xc0)); //[7:6] 2'b11 [5:0] bank no 746 if (ret) { 747 dev_err(&client->dev, "failed to prepare OTP page\n"); 748 return ret; 749 } 750 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0)); 751 752 /*enable read */ 753 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_READ_REG, 754 OV5693_OTP_MODE_READ); // enable :1 755 if (ret) { 756 dev_err(&client->dev, 757 "failed to set OTP reading mode page"); 758 return ret; 759 } 760 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ); 761 762 /* Reading the OTP data array */ 763 ret = ov5693_read_otp_reg_array(client, OV5693_OTP_BANK_SIZE, 764 OV5693_OTP_START_ADDR, 765 b); 766 if (ret) { 767 dev_err(&client->dev, "failed to read OTP data\n"); 768 return ret; 769 } 770 771 //pr_debug("BANK[%2d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, *b, *(b+1), *(b+2), *(b+3), *(b+4), *(b+5), *(b+6), *(b+7), *(b+8), *(b+9), *(b+10), *(b+11), *(b+12), *(b+13), *(b+14), *(b+15)); 772 773 //Intel OTP map, try to read 320byts first. 774 if (i == 21) { 775 if ((*b) == 0) { 776 dev->otp_size = 320; 777 break; 778 } else { 779 b = buf; 780 continue; 781 } 782 } else if (i == 783 24) { //if the first 320bytes data doesn't not exist, try to read the next 32bytes data. 784 if ((*b) == 0) { 785 dev->otp_size = 32; 786 break; 787 } else { 788 b = buf; 789 continue; 790 } 791 } else if (i == 792 27) { //if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again. 793 if ((*b) == 0) { 794 dev->otp_size = 32; 795 break; 796 } else { 797 dev->otp_size = 0; // no OTP data. 798 break; 799 } 800 } 801 802 b = b + OV5693_OTP_BANK_SIZE; 803 } 804 return 0; 805 } 806 807 /* 808 * Read otp data and store it into a kmalloced buffer. 809 * The caller must kfree the buffer when no more needed. 810 * @size: set to the size of the returned otp data. 811 */ 812 static void *ov5693_otp_read(struct v4l2_subdev *sd) 813 { 814 struct i2c_client *client = v4l2_get_subdevdata(sd); 815 u8 *buf; 816 int ret; 817 818 buf = devm_kzalloc(&client->dev, (OV5693_OTP_DATA_SIZE + 16), GFP_KERNEL); 819 if (!buf) 820 return ERR_PTR(-ENOMEM); 821 822 //otp valid after mipi on and sw stream on 823 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x00); 824 825 ret = ov5693_write_reg(client, OV5693_8BIT, 826 OV5693_SW_STREAM, OV5693_START_STREAMING); 827 828 ret = __ov5693_otp_read(sd, buf); 829 830 //mipi off and sw stream off after otp read 831 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x0f); 832 833 ret = ov5693_write_reg(client, OV5693_8BIT, 834 OV5693_SW_STREAM, OV5693_STOP_STREAMING); 835 836 /* Driver has failed to find valid data */ 837 if (ret) { 838 dev_err(&client->dev, "sensor found no valid OTP data\n"); 839 return ERR_PTR(ret); 840 } 841 842 return buf; 843 } 844 845 static int ov5693_g_priv_int_data(struct v4l2_subdev *sd, 846 struct v4l2_private_int_data *priv) 847 { 848 struct i2c_client *client = v4l2_get_subdevdata(sd); 849 struct ov5693_device *dev = to_ov5693_sensor(sd); 850 u8 __user *to = priv->data; 851 u32 read_size = priv->size; 852 int ret; 853 854 /* No need to copy data if size is 0 */ 855 if (!read_size) 856 goto out; 857 858 if (IS_ERR(dev->otp_data)) { 859 dev_err(&client->dev, "OTP data not available"); 860 return PTR_ERR(dev->otp_data); 861 } 862 863 /* Correct read_size value only if bigger than maximum */ 864 if (read_size > OV5693_OTP_DATA_SIZE) 865 read_size = OV5693_OTP_DATA_SIZE; 866 867 ret = copy_to_user(to, dev->otp_data, read_size); 868 if (ret) { 869 dev_err(&client->dev, "%s: failed to copy OTP data to user\n", 870 __func__); 871 return -EFAULT; 872 } 873 874 pr_debug("%s read_size:%d\n", __func__, read_size); 875 876 out: 877 /* Return correct size */ 878 priv->size = dev->otp_size; 879 880 return 0; 881 } 882 883 static long ov5693_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 884 { 885 switch (cmd) { 886 case ATOMISP_IOC_S_EXPOSURE: 887 return ov5693_s_exposure(sd, arg); 888 case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA: 889 return ov5693_g_priv_int_data(sd, arg); 890 default: 891 return -EINVAL; 892 } 893 return 0; 894 } 895 896 /* 897 * This returns the exposure time being used. This should only be used 898 * for filling in EXIF data, not for actual image processing. 899 */ 900 static int ov5693_q_exposure(struct v4l2_subdev *sd, s32 *value) 901 { 902 struct i2c_client *client = v4l2_get_subdevdata(sd); 903 u16 reg_v, reg_v2; 904 int ret; 905 906 /* get exposure */ 907 ret = ov5693_read_reg(client, OV5693_8BIT, 908 OV5693_EXPOSURE_L, 909 ®_v); 910 if (ret) 911 goto err; 912 913 ret = ov5693_read_reg(client, OV5693_8BIT, 914 OV5693_EXPOSURE_M, 915 ®_v2); 916 if (ret) 917 goto err; 918 919 reg_v += reg_v2 << 8; 920 ret = ov5693_read_reg(client, OV5693_8BIT, 921 OV5693_EXPOSURE_H, 922 ®_v2); 923 if (ret) 924 goto err; 925 926 *value = reg_v + (((u32)reg_v2 << 16)); 927 err: 928 return ret; 929 } 930 931 static int ad5823_t_focus_vcm(struct v4l2_subdev *sd, u16 val) 932 { 933 struct i2c_client *client = v4l2_get_subdevdata(sd); 934 int ret = -EINVAL; 935 u8 vcm_code; 936 937 ret = ad5823_i2c_read(client, AD5823_REG_VCM_CODE_MSB, &vcm_code); 938 if (ret) 939 return ret; 940 941 /* set reg VCM_CODE_MSB Bit[1:0] */ 942 vcm_code = (vcm_code & VCM_CODE_MSB_MASK) | 943 ((val >> 8) & ~VCM_CODE_MSB_MASK); 944 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, vcm_code); 945 if (ret) 946 return ret; 947 948 /* set reg VCM_CODE_LSB Bit[7:0] */ 949 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_LSB, (val & 0xff)); 950 if (ret) 951 return ret; 952 953 /* set required vcm move time */ 954 vcm_code = AD5823_RESONANCE_PERIOD / AD5823_RESONANCE_COEF 955 - AD5823_HIGH_FREQ_RANGE; 956 ret = ad5823_i2c_write(client, AD5823_REG_VCM_MOVE_TIME, vcm_code); 957 958 return ret; 959 } 960 961 static int ad5823_t_focus_abs(struct v4l2_subdev *sd, s32 value) 962 { 963 value = min(value, AD5823_MAX_FOCUS_POS); 964 return ad5823_t_focus_vcm(sd, value); 965 } 966 967 static int ov5693_t_focus_abs(struct v4l2_subdev *sd, s32 value) 968 { 969 struct ov5693_device *dev = to_ov5693_sensor(sd); 970 struct i2c_client *client = v4l2_get_subdevdata(sd); 971 int ret = 0; 972 973 dev_dbg(&client->dev, "%s: FOCUS_POS: 0x%x\n", __func__, value); 974 value = clamp(value, 0, OV5693_VCM_MAX_FOCUS_POS); 975 if (dev->vcm == VCM_DW9714) { 976 if (dev->vcm_update) { 977 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_OFF); 978 if (ret) 979 return ret; 980 ret = vcm_dw_i2c_write(client, DIRECT_VCM); 981 if (ret) 982 return ret; 983 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_ON); 984 if (ret) 985 return ret; 986 dev->vcm_update = false; 987 } 988 ret = vcm_dw_i2c_write(client, 989 vcm_val(value, VCM_DEFAULT_S)); 990 } else if (dev->vcm == VCM_AD5823) { 991 ad5823_t_focus_abs(sd, value); 992 } 993 if (ret == 0) { 994 dev->number_of_steps = value - dev->focus; 995 dev->focus = value; 996 dev->timestamp_t_focus_abs = ktime_get(); 997 } else 998 dev_err(&client->dev, 999 "%s: i2c failed. ret %d\n", __func__, ret); 1000 1001 return ret; 1002 } 1003 1004 static int ov5693_t_focus_rel(struct v4l2_subdev *sd, s32 value) 1005 { 1006 struct ov5693_device *dev = to_ov5693_sensor(sd); 1007 1008 return ov5693_t_focus_abs(sd, dev->focus + value); 1009 } 1010 1011 #define DELAY_PER_STEP_NS 1000000 1012 #define DELAY_MAX_PER_STEP_NS (1000000 * 1023) 1013 static int ov5693_q_focus_status(struct v4l2_subdev *sd, s32 *value) 1014 { 1015 u32 status = 0; 1016 struct ov5693_device *dev = to_ov5693_sensor(sd); 1017 ktime_t temptime; 1018 ktime_t timedelay = ns_to_ktime(min_t(u32, 1019 abs(dev->number_of_steps) * DELAY_PER_STEP_NS, 1020 DELAY_MAX_PER_STEP_NS)); 1021 1022 temptime = ktime_sub(ktime_get(), (dev->timestamp_t_focus_abs)); 1023 if (ktime_compare(temptime, timedelay) <= 0) { 1024 status |= ATOMISP_FOCUS_STATUS_MOVING; 1025 status |= ATOMISP_FOCUS_HP_IN_PROGRESS; 1026 } else { 1027 status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE; 1028 status |= ATOMISP_FOCUS_HP_COMPLETE; 1029 } 1030 1031 *value = status; 1032 1033 return 0; 1034 } 1035 1036 static int ov5693_q_focus_abs(struct v4l2_subdev *sd, s32 *value) 1037 { 1038 struct ov5693_device *dev = to_ov5693_sensor(sd); 1039 s32 val; 1040 1041 ov5693_q_focus_status(sd, &val); 1042 1043 if (val & ATOMISP_FOCUS_STATUS_MOVING) 1044 *value = dev->focus - dev->number_of_steps; 1045 else 1046 *value = dev->focus; 1047 1048 return 0; 1049 } 1050 1051 static int ov5693_t_vcm_slew(struct v4l2_subdev *sd, s32 value) 1052 { 1053 struct ov5693_device *dev = to_ov5693_sensor(sd); 1054 1055 dev->number_of_steps = value; 1056 dev->vcm_update = true; 1057 return 0; 1058 } 1059 1060 static int ov5693_t_vcm_timing(struct v4l2_subdev *sd, s32 value) 1061 { 1062 struct ov5693_device *dev = to_ov5693_sensor(sd); 1063 1064 dev->number_of_steps = value; 1065 dev->vcm_update = true; 1066 return 0; 1067 } 1068 1069 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl) 1070 { 1071 struct ov5693_device *dev = 1072 container_of(ctrl->handler, struct ov5693_device, ctrl_handler); 1073 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd); 1074 int ret = 0; 1075 1076 switch (ctrl->id) { 1077 case V4L2_CID_FOCUS_ABSOLUTE: 1078 dev_dbg(&client->dev, "%s: CID_FOCUS_ABSOLUTE:%d.\n", 1079 __func__, ctrl->val); 1080 ret = ov5693_t_focus_abs(&dev->sd, ctrl->val); 1081 break; 1082 case V4L2_CID_FOCUS_RELATIVE: 1083 dev_dbg(&client->dev, "%s: CID_FOCUS_RELATIVE:%d.\n", 1084 __func__, ctrl->val); 1085 ret = ov5693_t_focus_rel(&dev->sd, ctrl->val); 1086 break; 1087 case V4L2_CID_VCM_SLEW: 1088 ret = ov5693_t_vcm_slew(&dev->sd, ctrl->val); 1089 break; 1090 case V4L2_CID_VCM_TIMING: 1091 ret = ov5693_t_vcm_timing(&dev->sd, ctrl->val); 1092 break; 1093 default: 1094 ret = -EINVAL; 1095 } 1096 return ret; 1097 } 1098 1099 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 1100 { 1101 struct ov5693_device *dev = 1102 container_of(ctrl->handler, struct ov5693_device, ctrl_handler); 1103 int ret = 0; 1104 1105 switch (ctrl->id) { 1106 case V4L2_CID_EXPOSURE_ABSOLUTE: 1107 ret = ov5693_q_exposure(&dev->sd, &ctrl->val); 1108 break; 1109 case V4L2_CID_FOCAL_ABSOLUTE: 1110 ret = ov5693_g_focal(&dev->sd, &ctrl->val); 1111 break; 1112 case V4L2_CID_FNUMBER_ABSOLUTE: 1113 ret = ov5693_g_fnumber(&dev->sd, &ctrl->val); 1114 break; 1115 case V4L2_CID_FNUMBER_RANGE: 1116 ret = ov5693_g_fnumber_range(&dev->sd, &ctrl->val); 1117 break; 1118 case V4L2_CID_FOCUS_ABSOLUTE: 1119 ret = ov5693_q_focus_abs(&dev->sd, &ctrl->val); 1120 break; 1121 case V4L2_CID_FOCUS_STATUS: 1122 ret = ov5693_q_focus_status(&dev->sd, &ctrl->val); 1123 break; 1124 case V4L2_CID_BIN_FACTOR_HORZ: 1125 ret = ov5693_g_bin_factor_x(&dev->sd, &ctrl->val); 1126 break; 1127 case V4L2_CID_BIN_FACTOR_VERT: 1128 ret = ov5693_g_bin_factor_y(&dev->sd, &ctrl->val); 1129 break; 1130 default: 1131 ret = -EINVAL; 1132 } 1133 1134 return ret; 1135 } 1136 1137 static const struct v4l2_ctrl_ops ctrl_ops = { 1138 .s_ctrl = ov5693_s_ctrl, 1139 .g_volatile_ctrl = ov5693_g_volatile_ctrl 1140 }; 1141 1142 static const struct v4l2_ctrl_config ov5693_controls[] = { 1143 { 1144 .ops = &ctrl_ops, 1145 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 1146 .type = V4L2_CTRL_TYPE_INTEGER, 1147 .name = "exposure", 1148 .min = 0x0, 1149 .max = 0xffff, 1150 .step = 0x01, 1151 .def = 0x00, 1152 .flags = 0, 1153 }, 1154 { 1155 .ops = &ctrl_ops, 1156 .id = V4L2_CID_FOCAL_ABSOLUTE, 1157 .type = V4L2_CTRL_TYPE_INTEGER, 1158 .name = "focal length", 1159 .min = OV5693_FOCAL_LENGTH_DEFAULT, 1160 .max = OV5693_FOCAL_LENGTH_DEFAULT, 1161 .step = 0x01, 1162 .def = OV5693_FOCAL_LENGTH_DEFAULT, 1163 .flags = 0, 1164 }, 1165 { 1166 .ops = &ctrl_ops, 1167 .id = V4L2_CID_FNUMBER_ABSOLUTE, 1168 .type = V4L2_CTRL_TYPE_INTEGER, 1169 .name = "f-number", 1170 .min = OV5693_F_NUMBER_DEFAULT, 1171 .max = OV5693_F_NUMBER_DEFAULT, 1172 .step = 0x01, 1173 .def = OV5693_F_NUMBER_DEFAULT, 1174 .flags = 0, 1175 }, 1176 { 1177 .ops = &ctrl_ops, 1178 .id = V4L2_CID_FNUMBER_RANGE, 1179 .type = V4L2_CTRL_TYPE_INTEGER, 1180 .name = "f-number range", 1181 .min = OV5693_F_NUMBER_RANGE, 1182 .max = OV5693_F_NUMBER_RANGE, 1183 .step = 0x01, 1184 .def = OV5693_F_NUMBER_RANGE, 1185 .flags = 0, 1186 }, 1187 { 1188 .ops = &ctrl_ops, 1189 .id = V4L2_CID_FOCUS_ABSOLUTE, 1190 .type = V4L2_CTRL_TYPE_INTEGER, 1191 .name = "focus move absolute", 1192 .min = 0, 1193 .max = OV5693_VCM_MAX_FOCUS_POS, 1194 .step = 1, 1195 .def = 0, 1196 .flags = 0, 1197 }, 1198 { 1199 .ops = &ctrl_ops, 1200 .id = V4L2_CID_FOCUS_RELATIVE, 1201 .type = V4L2_CTRL_TYPE_INTEGER, 1202 .name = "focus move relative", 1203 .min = OV5693_VCM_MAX_FOCUS_NEG, 1204 .max = OV5693_VCM_MAX_FOCUS_POS, 1205 .step = 1, 1206 .def = 0, 1207 .flags = 0, 1208 }, 1209 { 1210 .ops = &ctrl_ops, 1211 .id = V4L2_CID_FOCUS_STATUS, 1212 .type = V4L2_CTRL_TYPE_INTEGER, 1213 .name = "focus status", 1214 .min = 0, 1215 .max = 100, /* allow enum to grow in the future */ 1216 .step = 1, 1217 .def = 0, 1218 .flags = 0, 1219 }, 1220 { 1221 .ops = &ctrl_ops, 1222 .id = V4L2_CID_VCM_SLEW, 1223 .type = V4L2_CTRL_TYPE_INTEGER, 1224 .name = "vcm slew", 1225 .min = 0, 1226 .max = OV5693_VCM_SLEW_STEP_MAX, 1227 .step = 1, 1228 .def = 0, 1229 .flags = 0, 1230 }, 1231 { 1232 .ops = &ctrl_ops, 1233 .id = V4L2_CID_VCM_TIMING, 1234 .type = V4L2_CTRL_TYPE_INTEGER, 1235 .name = "vcm step time", 1236 .min = 0, 1237 .max = OV5693_VCM_SLEW_TIME_MAX, 1238 .step = 1, 1239 .def = 0, 1240 .flags = 0, 1241 }, 1242 { 1243 .ops = &ctrl_ops, 1244 .id = V4L2_CID_BIN_FACTOR_HORZ, 1245 .type = V4L2_CTRL_TYPE_INTEGER, 1246 .name = "horizontal binning factor", 1247 .min = 0, 1248 .max = OV5693_BIN_FACTOR_MAX, 1249 .step = 1, 1250 .def = 0, 1251 .flags = 0, 1252 }, 1253 { 1254 .ops = &ctrl_ops, 1255 .id = V4L2_CID_BIN_FACTOR_VERT, 1256 .type = V4L2_CTRL_TYPE_INTEGER, 1257 .name = "vertical binning factor", 1258 .min = 0, 1259 .max = OV5693_BIN_FACTOR_MAX, 1260 .step = 1, 1261 .def = 0, 1262 .flags = 0, 1263 }, 1264 }; 1265 1266 static int ov5693_init(struct v4l2_subdev *sd) 1267 { 1268 struct ov5693_device *dev = to_ov5693_sensor(sd); 1269 struct i2c_client *client = v4l2_get_subdevdata(sd); 1270 int ret; 1271 1272 pr_info("%s\n", __func__); 1273 mutex_lock(&dev->input_lock); 1274 dev->vcm_update = false; 1275 1276 if (dev->vcm == VCM_AD5823) { 1277 ret = vcm_ad_i2c_wr8(client, 0x01, 0x01); /* vcm init test */ 1278 if (ret) 1279 dev_err(&client->dev, 1280 "vcm reset failed\n"); 1281 /*change the mode*/ 1282 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, 1283 AD5823_RING_CTRL_ENABLE); 1284 if (ret) 1285 dev_err(&client->dev, 1286 "vcm enable ringing failed\n"); 1287 ret = ad5823_i2c_write(client, AD5823_REG_MODE, 1288 AD5823_ARC_RES1); 1289 if (ret) 1290 dev_err(&client->dev, 1291 "vcm change mode failed\n"); 1292 } 1293 1294 /*change initial focus value for ad5823*/ 1295 if (dev->vcm == VCM_AD5823) { 1296 dev->focus = AD5823_INIT_FOCUS_POS; 1297 ov5693_t_focus_abs(sd, AD5823_INIT_FOCUS_POS); 1298 } else { 1299 dev->focus = 0; 1300 ov5693_t_focus_abs(sd, 0); 1301 } 1302 1303 mutex_unlock(&dev->input_lock); 1304 1305 return 0; 1306 } 1307 1308 static int power_ctrl(struct v4l2_subdev *sd, bool flag) 1309 { 1310 int ret; 1311 struct ov5693_device *dev = to_ov5693_sensor(sd); 1312 1313 if (!dev || !dev->platform_data) 1314 return -ENODEV; 1315 1316 /* 1317 * This driver assumes "internal DVDD, PWDNB tied to DOVDD". 1318 * In this set up only gpio0 (XSHUTDN) should be available 1319 * but in some products (for example ECS) gpio1 (PWDNB) is 1320 * also available. If gpio1 is available we emulate it being 1321 * tied to DOVDD here. 1322 */ 1323 if (flag) { 1324 ret = dev->platform_data->v2p8_ctrl(sd, 1); 1325 dev->platform_data->gpio1_ctrl(sd, 1); 1326 if (ret == 0) { 1327 ret = dev->platform_data->v1p8_ctrl(sd, 1); 1328 if (ret) { 1329 dev->platform_data->gpio1_ctrl(sd, 0); 1330 ret = dev->platform_data->v2p8_ctrl(sd, 0); 1331 } 1332 } 1333 } else { 1334 dev->platform_data->gpio1_ctrl(sd, 0); 1335 ret = dev->platform_data->v1p8_ctrl(sd, 0); 1336 ret |= dev->platform_data->v2p8_ctrl(sd, 0); 1337 } 1338 1339 return ret; 1340 } 1341 1342 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 1343 { 1344 struct ov5693_device *dev = to_ov5693_sensor(sd); 1345 1346 if (!dev || !dev->platform_data) 1347 return -ENODEV; 1348 1349 return dev->platform_data->gpio0_ctrl(sd, flag); 1350 } 1351 1352 static int __power_up(struct v4l2_subdev *sd) 1353 { 1354 struct ov5693_device *dev = to_ov5693_sensor(sd); 1355 struct i2c_client *client = v4l2_get_subdevdata(sd); 1356 int ret; 1357 1358 if (!dev->platform_data) { 1359 dev_err(&client->dev, 1360 "no camera_sensor_platform_data"); 1361 return -ENODEV; 1362 } 1363 1364 /* power control */ 1365 ret = power_ctrl(sd, 1); 1366 if (ret) 1367 goto fail_power; 1368 1369 /* according to DS, at least 5ms is needed between DOVDD and PWDN */ 1370 /* add this delay time to 10~11ms*/ 1371 usleep_range(10000, 11000); 1372 1373 /* gpio ctrl */ 1374 ret = gpio_ctrl(sd, 1); 1375 if (ret) { 1376 ret = gpio_ctrl(sd, 1); 1377 if (ret) 1378 goto fail_power; 1379 } 1380 1381 /* flis clock control */ 1382 ret = dev->platform_data->flisclk_ctrl(sd, 1); 1383 if (ret) 1384 goto fail_clk; 1385 1386 __cci_delay(up_delay); 1387 1388 return 0; 1389 1390 fail_clk: 1391 gpio_ctrl(sd, 0); 1392 fail_power: 1393 power_ctrl(sd, 0); 1394 dev_err(&client->dev, "sensor power-up failed\n"); 1395 1396 return ret; 1397 } 1398 1399 static int power_down(struct v4l2_subdev *sd) 1400 { 1401 struct ov5693_device *dev = to_ov5693_sensor(sd); 1402 struct i2c_client *client = v4l2_get_subdevdata(sd); 1403 int ret = 0; 1404 1405 dev->focus = OV5693_INVALID_CONFIG; 1406 if (!dev->platform_data) { 1407 dev_err(&client->dev, 1408 "no camera_sensor_platform_data"); 1409 return -ENODEV; 1410 } 1411 1412 ret = dev->platform_data->flisclk_ctrl(sd, 0); 1413 if (ret) 1414 dev_err(&client->dev, "flisclk failed\n"); 1415 1416 /* gpio ctrl */ 1417 ret = gpio_ctrl(sd, 0); 1418 if (ret) { 1419 ret = gpio_ctrl(sd, 0); 1420 if (ret) 1421 dev_err(&client->dev, "gpio failed 2\n"); 1422 } 1423 1424 /* power control */ 1425 ret = power_ctrl(sd, 0); 1426 if (ret) 1427 dev_err(&client->dev, "vprog failed.\n"); 1428 1429 return ret; 1430 } 1431 1432 static int power_up(struct v4l2_subdev *sd) 1433 { 1434 static const int retry_count = 4; 1435 int i, ret; 1436 1437 for (i = 0; i < retry_count; i++) { 1438 ret = __power_up(sd); 1439 if (!ret) 1440 return 0; 1441 1442 power_down(sd); 1443 } 1444 return ret; 1445 } 1446 1447 static int ov5693_s_power(struct v4l2_subdev *sd, int on) 1448 { 1449 int ret; 1450 1451 pr_info("%s: on %d\n", __func__, on); 1452 if (on == 0) 1453 return power_down(sd); 1454 else { 1455 ret = power_up(sd); 1456 if (!ret) { 1457 ret = ov5693_init(sd); 1458 /* restore settings */ 1459 ov5693_res = ov5693_res_preview; 1460 N_RES = N_RES_PREVIEW; 1461 } 1462 } 1463 return ret; 1464 } 1465 1466 /* 1467 * distance - calculate the distance 1468 * @res: resolution 1469 * @w: width 1470 * @h: height 1471 * 1472 * Get the gap between res_w/res_h and w/h. 1473 * distance = (res_w/res_h - w/h) / (w/h) * 8192 1474 * res->width/height smaller than w/h wouldn't be considered. 1475 * The gap of ratio larger than 1/8 wouldn't be considered. 1476 * Returns the value of gap or -1 if fail. 1477 */ 1478 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024 1479 static int distance(struct ov5693_resolution *res, u32 w, u32 h) 1480 { 1481 int ratio; 1482 int distance; 1483 1484 if (w == 0 || h == 0 || 1485 res->width < w || res->height < h) 1486 return -1; 1487 1488 ratio = res->width << 13; 1489 ratio /= w; 1490 ratio *= h; 1491 ratio /= res->height; 1492 1493 distance = abs(ratio - 8192); 1494 1495 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH) 1496 return -1; 1497 1498 return distance; 1499 } 1500 1501 /* Return the nearest higher resolution index 1502 * Firstly try to find the approximate aspect ratio resolution 1503 * If we find multiple same AR resolutions, choose the 1504 * minimal size. 1505 */ 1506 static int nearest_resolution_index(int w, int h) 1507 { 1508 int i; 1509 int idx = -1; 1510 int dist; 1511 int min_dist = INT_MAX; 1512 int min_res_w = INT_MAX; 1513 struct ov5693_resolution *tmp_res = NULL; 1514 1515 for (i = 0; i < N_RES; i++) { 1516 tmp_res = &ov5693_res[i]; 1517 dist = distance(tmp_res, w, h); 1518 if (dist == -1) 1519 continue; 1520 if (dist < min_dist) { 1521 min_dist = dist; 1522 idx = i; 1523 min_res_w = ov5693_res[i].width; 1524 continue; 1525 } 1526 if (dist == min_dist && ov5693_res[i].width < min_res_w) 1527 idx = i; 1528 } 1529 1530 return idx; 1531 } 1532 1533 static int get_resolution_index(int w, int h) 1534 { 1535 int i; 1536 1537 for (i = 0; i < N_RES; i++) { 1538 if (w != ov5693_res[i].width) 1539 continue; 1540 if (h != ov5693_res[i].height) 1541 continue; 1542 1543 return i; 1544 } 1545 1546 return -1; 1547 } 1548 1549 /* TODO: remove it. */ 1550 static int startup(struct v4l2_subdev *sd) 1551 { 1552 struct ov5693_device *dev = to_ov5693_sensor(sd); 1553 struct i2c_client *client = v4l2_get_subdevdata(sd); 1554 int ret = 0; 1555 1556 ret = ov5693_write_reg(client, OV5693_8BIT, 1557 OV5693_SW_RESET, 0x01); 1558 if (ret) { 1559 dev_err(&client->dev, "ov5693 reset err.\n"); 1560 return ret; 1561 } 1562 1563 ret = ov5693_write_reg_array(client, ov5693_global_setting); 1564 if (ret) { 1565 dev_err(&client->dev, "ov5693 write register err.\n"); 1566 return ret; 1567 } 1568 1569 ret = ov5693_write_reg_array(client, ov5693_res[dev->fmt_idx].regs); 1570 if (ret) { 1571 dev_err(&client->dev, "ov5693 write register err.\n"); 1572 return ret; 1573 } 1574 1575 return ret; 1576 } 1577 1578 static int ov5693_set_fmt(struct v4l2_subdev *sd, 1579 struct v4l2_subdev_pad_config *cfg, 1580 struct v4l2_subdev_format *format) 1581 { 1582 struct v4l2_mbus_framefmt *fmt = &format->format; 1583 struct ov5693_device *dev = to_ov5693_sensor(sd); 1584 struct i2c_client *client = v4l2_get_subdevdata(sd); 1585 struct camera_mipi_info *ov5693_info = NULL; 1586 int ret = 0; 1587 int idx; 1588 1589 if (format->pad) 1590 return -EINVAL; 1591 if (!fmt) 1592 return -EINVAL; 1593 ov5693_info = v4l2_get_subdev_hostdata(sd); 1594 if (!ov5693_info) 1595 return -EINVAL; 1596 1597 mutex_lock(&dev->input_lock); 1598 idx = nearest_resolution_index(fmt->width, fmt->height); 1599 if (idx == -1) { 1600 /* return the largest resolution */ 1601 fmt->width = ov5693_res[N_RES - 1].width; 1602 fmt->height = ov5693_res[N_RES - 1].height; 1603 } else { 1604 fmt->width = ov5693_res[idx].width; 1605 fmt->height = ov5693_res[idx].height; 1606 } 1607 1608 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10; 1609 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 1610 cfg->try_fmt = *fmt; 1611 mutex_unlock(&dev->input_lock); 1612 return 0; 1613 } 1614 1615 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height); 1616 if (dev->fmt_idx == -1) { 1617 dev_err(&client->dev, "get resolution fail\n"); 1618 mutex_unlock(&dev->input_lock); 1619 return -EINVAL; 1620 } 1621 1622 ret = startup(sd); 1623 if (ret) { 1624 int i = 0; 1625 1626 dev_err(&client->dev, "ov5693 startup err, retry to power up\n"); 1627 for (i = 0; i < OV5693_POWER_UP_RETRY_NUM; i++) { 1628 dev_err(&client->dev, 1629 "ov5693 retry to power up %d/%d times, result: ", 1630 i + 1, OV5693_POWER_UP_RETRY_NUM); 1631 power_down(sd); 1632 ret = power_up(sd); 1633 if (!ret) { 1634 mutex_unlock(&dev->input_lock); 1635 ov5693_init(sd); 1636 mutex_lock(&dev->input_lock); 1637 } else { 1638 dev_err(&client->dev, "power up failed, continue\n"); 1639 continue; 1640 } 1641 ret = startup(sd); 1642 if (ret) { 1643 dev_err(&client->dev, " startup FAILED!\n"); 1644 } else { 1645 dev_err(&client->dev, " startup SUCCESS!\n"); 1646 break; 1647 } 1648 } 1649 } 1650 1651 /* 1652 * After sensor settings are set to HW, sometimes stream is started. 1653 * This would cause ISP timeout because ISP is not ready to receive 1654 * data yet. So add stop streaming here. 1655 */ 1656 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM, 1657 OV5693_STOP_STREAMING); 1658 if (ret) 1659 dev_warn(&client->dev, "ov5693 stream off err\n"); 1660 1661 ret = ov5693_get_intg_factor(client, ov5693_info, 1662 &ov5693_res[dev->fmt_idx]); 1663 if (ret) { 1664 dev_err(&client->dev, "failed to get integration_factor\n"); 1665 goto err; 1666 } 1667 1668 ov5693_info->metadata_width = fmt->width * 10 / 8; 1669 ov5693_info->metadata_height = 1; 1670 ov5693_info->metadata_effective_width = &ov5693_embedded_effective_size; 1671 1672 err: 1673 mutex_unlock(&dev->input_lock); 1674 return ret; 1675 } 1676 1677 static int ov5693_get_fmt(struct v4l2_subdev *sd, 1678 struct v4l2_subdev_pad_config *cfg, 1679 struct v4l2_subdev_format *format) 1680 { 1681 struct v4l2_mbus_framefmt *fmt = &format->format; 1682 struct ov5693_device *dev = to_ov5693_sensor(sd); 1683 1684 if (format->pad) 1685 return -EINVAL; 1686 1687 if (!fmt) 1688 return -EINVAL; 1689 1690 fmt->width = ov5693_res[dev->fmt_idx].width; 1691 fmt->height = ov5693_res[dev->fmt_idx].height; 1692 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10; 1693 1694 return 0; 1695 } 1696 1697 static int ov5693_detect(struct i2c_client *client) 1698 { 1699 struct i2c_adapter *adapter = client->adapter; 1700 u16 high, low; 1701 int ret; 1702 u16 id; 1703 u8 revision; 1704 1705 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 1706 return -ENODEV; 1707 1708 ret = ov5693_read_reg(client, OV5693_8BIT, 1709 OV5693_SC_CMMN_CHIP_ID_H, &high); 1710 if (ret) { 1711 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high); 1712 return -ENODEV; 1713 } 1714 ret = ov5693_read_reg(client, OV5693_8BIT, 1715 OV5693_SC_CMMN_CHIP_ID_L, &low); 1716 id = ((((u16)high) << 8) | (u16)low); 1717 1718 if (id != OV5693_ID) { 1719 dev_err(&client->dev, "sensor ID error 0x%x\n", id); 1720 return -ENODEV; 1721 } 1722 1723 ret = ov5693_read_reg(client, OV5693_8BIT, 1724 OV5693_SC_CMMN_SUB_ID, &high); 1725 revision = (u8)high & 0x0f; 1726 1727 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision); 1728 dev_dbg(&client->dev, "detect ov5693 success\n"); 1729 return 0; 1730 } 1731 1732 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable) 1733 { 1734 struct ov5693_device *dev = to_ov5693_sensor(sd); 1735 struct i2c_client *client = v4l2_get_subdevdata(sd); 1736 int ret; 1737 1738 mutex_lock(&dev->input_lock); 1739 1740 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM, 1741 enable ? OV5693_START_STREAMING : 1742 OV5693_STOP_STREAMING); 1743 1744 mutex_unlock(&dev->input_lock); 1745 1746 return ret; 1747 } 1748 1749 static int ov5693_s_config(struct v4l2_subdev *sd, 1750 int irq, void *platform_data) 1751 { 1752 struct ov5693_device *dev = to_ov5693_sensor(sd); 1753 struct i2c_client *client = v4l2_get_subdevdata(sd); 1754 int ret = 0; 1755 1756 if (!platform_data) 1757 return -ENODEV; 1758 1759 dev->platform_data = 1760 (struct camera_sensor_platform_data *)platform_data; 1761 1762 mutex_lock(&dev->input_lock); 1763 /* power off the module, then power on it in future 1764 * as first power on by board may not fulfill the 1765 * power on sequqence needed by the module 1766 */ 1767 ret = power_down(sd); 1768 if (ret) { 1769 dev_err(&client->dev, "ov5693 power-off err.\n"); 1770 goto fail_power_off; 1771 } 1772 1773 ret = power_up(sd); 1774 if (ret) { 1775 dev_err(&client->dev, "ov5693 power-up err.\n"); 1776 goto fail_power_on; 1777 } 1778 1779 if (!dev->vcm) 1780 dev->vcm = vcm_detect(client); 1781 1782 ret = dev->platform_data->csi_cfg(sd, 1); 1783 if (ret) 1784 goto fail_csi_cfg; 1785 1786 /* config & detect sensor */ 1787 ret = ov5693_detect(client); 1788 if (ret) { 1789 dev_err(&client->dev, "ov5693_detect err s_config.\n"); 1790 goto fail_csi_cfg; 1791 } 1792 1793 dev->otp_data = ov5693_otp_read(sd); 1794 1795 /* turn off sensor, after probed */ 1796 ret = power_down(sd); 1797 if (ret) { 1798 dev_err(&client->dev, "ov5693 power-off err.\n"); 1799 goto fail_csi_cfg; 1800 } 1801 mutex_unlock(&dev->input_lock); 1802 1803 return ret; 1804 1805 fail_csi_cfg: 1806 dev->platform_data->csi_cfg(sd, 0); 1807 fail_power_on: 1808 power_down(sd); 1809 dev_err(&client->dev, "sensor power-gating failed\n"); 1810 fail_power_off: 1811 mutex_unlock(&dev->input_lock); 1812 return ret; 1813 } 1814 1815 static int ov5693_g_frame_interval(struct v4l2_subdev *sd, 1816 struct v4l2_subdev_frame_interval *interval) 1817 { 1818 struct ov5693_device *dev = to_ov5693_sensor(sd); 1819 1820 interval->interval.numerator = 1; 1821 interval->interval.denominator = ov5693_res[dev->fmt_idx].fps; 1822 1823 return 0; 1824 } 1825 1826 static int ov5693_enum_mbus_code(struct v4l2_subdev *sd, 1827 struct v4l2_subdev_pad_config *cfg, 1828 struct v4l2_subdev_mbus_code_enum *code) 1829 { 1830 if (code->index >= MAX_FMTS) 1831 return -EINVAL; 1832 1833 code->code = MEDIA_BUS_FMT_SBGGR10_1X10; 1834 return 0; 1835 } 1836 1837 static int ov5693_enum_frame_size(struct v4l2_subdev *sd, 1838 struct v4l2_subdev_pad_config *cfg, 1839 struct v4l2_subdev_frame_size_enum *fse) 1840 { 1841 int index = fse->index; 1842 1843 if (index >= N_RES) 1844 return -EINVAL; 1845 1846 fse->min_width = ov5693_res[index].width; 1847 fse->min_height = ov5693_res[index].height; 1848 fse->max_width = ov5693_res[index].width; 1849 fse->max_height = ov5693_res[index].height; 1850 1851 return 0; 1852 } 1853 1854 static const struct v4l2_subdev_video_ops ov5693_video_ops = { 1855 .s_stream = ov5693_s_stream, 1856 .g_frame_interval = ov5693_g_frame_interval, 1857 }; 1858 1859 static const struct v4l2_subdev_core_ops ov5693_core_ops = { 1860 .s_power = ov5693_s_power, 1861 .ioctl = ov5693_ioctl, 1862 }; 1863 1864 static const struct v4l2_subdev_pad_ops ov5693_pad_ops = { 1865 .enum_mbus_code = ov5693_enum_mbus_code, 1866 .enum_frame_size = ov5693_enum_frame_size, 1867 .get_fmt = ov5693_get_fmt, 1868 .set_fmt = ov5693_set_fmt, 1869 }; 1870 1871 static const struct v4l2_subdev_ops ov5693_ops = { 1872 .core = &ov5693_core_ops, 1873 .video = &ov5693_video_ops, 1874 .pad = &ov5693_pad_ops, 1875 }; 1876 1877 static int ov5693_remove(struct i2c_client *client) 1878 { 1879 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1880 struct ov5693_device *dev = to_ov5693_sensor(sd); 1881 1882 dev_dbg(&client->dev, "ov5693_remove...\n"); 1883 1884 dev->platform_data->csi_cfg(sd, 0); 1885 1886 v4l2_device_unregister_subdev(sd); 1887 1888 atomisp_gmin_remove_subdev(sd); 1889 1890 media_entity_cleanup(&dev->sd.entity); 1891 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1892 kfree(dev); 1893 1894 return 0; 1895 } 1896 1897 static int ov5693_probe(struct i2c_client *client) 1898 { 1899 struct ov5693_device *dev; 1900 int i2c; 1901 int ret = 0; 1902 void *pdata; 1903 unsigned int i; 1904 acpi_handle handle; 1905 struct acpi_device *adev; 1906 1907 handle = ACPI_HANDLE(&client->dev); 1908 if (!handle || acpi_bus_get_device(handle, &adev)) { 1909 dev_err(&client->dev, "Error could not get ACPI device\n"); 1910 return -ENODEV; 1911 } 1912 pr_info("%s: ACPI detected it on bus ID=%s, HID=%s\n", 1913 __func__, acpi_device_bid(adev), acpi_device_hid(adev)); 1914 // FIXME: may need to release resources allocated by acpi_bus_get_device() 1915 1916 /* 1917 * Firmware workaround: Some modules use a "secondary default" 1918 * address of 0x10 which doesn't appear on schematics, and 1919 * some BIOS versions haven't gotten the memo. Work around 1920 * via config. 1921 */ 1922 i2c = gmin_get_var_int(&client->dev, false, "I2CAddr", -1); 1923 if (i2c != -1) { 1924 dev_info(&client->dev, 1925 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n", 1926 client->addr, i2c); 1927 client->addr = i2c; 1928 } 1929 1930 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1931 if (!dev) 1932 return -ENOMEM; 1933 1934 mutex_init(&dev->input_lock); 1935 1936 dev->fmt_idx = 0; 1937 v4l2_i2c_subdev_init(&dev->sd, client, &ov5693_ops); 1938 1939 pdata = gmin_camera_platform_data(&dev->sd, 1940 ATOMISP_INPUT_FORMAT_RAW_10, 1941 atomisp_bayer_order_bggr); 1942 if (!pdata) 1943 goto out_free; 1944 1945 ret = ov5693_s_config(&dev->sd, client->irq, pdata); 1946 if (ret) 1947 goto out_free; 1948 1949 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA); 1950 if (ret) 1951 goto out_free; 1952 1953 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1954 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 1955 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10; 1956 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1957 ret = 1958 v4l2_ctrl_handler_init(&dev->ctrl_handler, 1959 ARRAY_SIZE(ov5693_controls)); 1960 if (ret) { 1961 ov5693_remove(client); 1962 return ret; 1963 } 1964 1965 for (i = 0; i < ARRAY_SIZE(ov5693_controls); i++) 1966 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov5693_controls[i], 1967 NULL); 1968 1969 if (dev->ctrl_handler.error) { 1970 ov5693_remove(client); 1971 return dev->ctrl_handler.error; 1972 } 1973 1974 /* Use same lock for controls as for everything else. */ 1975 dev->ctrl_handler.lock = &dev->input_lock; 1976 dev->sd.ctrl_handler = &dev->ctrl_handler; 1977 1978 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 1979 if (ret) 1980 ov5693_remove(client); 1981 1982 return ret; 1983 out_free: 1984 v4l2_device_unregister_subdev(&dev->sd); 1985 kfree(dev); 1986 return ret; 1987 } 1988 1989 static const struct acpi_device_id ov5693_acpi_match[] = { 1990 {"INT33BE"}, 1991 {}, 1992 }; 1993 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match); 1994 1995 static struct i2c_driver ov5693_driver = { 1996 .driver = { 1997 .name = "ov5693", 1998 .acpi_match_table = ov5693_acpi_match, 1999 }, 2000 .probe_new = ov5693_probe, 2001 .remove = ov5693_remove, 2002 }; 2003 module_i2c_driver(ov5693_driver); 2004 2005 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors"); 2006 MODULE_LICENSE("GPL"); 2007