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