1 /* 2 * Support for mt9m114 Camera Sensor. 3 * 4 * Copyright (c) 2010 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/fs.h> 28 #include <linux/slab.h> 29 #include <linux/delay.h> 30 #include <linux/i2c.h> 31 #include <linux/acpi.h> 32 #include "../include/linux/atomisp_gmin_platform.h" 33 #include <media/v4l2-device.h> 34 35 #include "mt9m114.h" 36 37 #define to_mt9m114_sensor(sd) container_of(sd, struct mt9m114_device, sd) 38 39 /* 40 * TODO: use debug parameter to actually define when debug messages should 41 * be printed. 42 */ 43 static int debug; 44 static int aaalock; 45 module_param(debug, int, 0644); 46 MODULE_PARM_DESC(debug, "Debug level (0-1)"); 47 48 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value); 49 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value); 50 static int mt9m114_wait_state(struct i2c_client *client, int timeout); 51 52 static int 53 mt9m114_read_reg(struct i2c_client *client, u16 data_length, u32 reg, u32 *val) 54 { 55 int err; 56 struct i2c_msg msg[2]; 57 unsigned char data[4]; 58 59 if (!client->adapter) { 60 v4l2_err(client, "%s error, no client->adapter\n", __func__); 61 return -ENODEV; 62 } 63 64 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT 65 && data_length != MISENSOR_32BIT) { 66 v4l2_err(client, "%s error, invalid data length\n", __func__); 67 return -EINVAL; 68 } 69 70 msg[0].addr = client->addr; 71 msg[0].flags = 0; 72 msg[0].len = MSG_LEN_OFFSET; 73 msg[0].buf = data; 74 75 /* high byte goes out first */ 76 data[0] = (u16)(reg >> 8); 77 data[1] = (u16)(reg & 0xff); 78 79 msg[1].addr = client->addr; 80 msg[1].len = data_length; 81 msg[1].flags = I2C_M_RD; 82 msg[1].buf = data; 83 84 err = i2c_transfer(client->adapter, msg, 2); 85 86 if (err >= 0) { 87 *val = 0; 88 /* high byte comes first */ 89 if (data_length == MISENSOR_8BIT) 90 *val = data[0]; 91 else if (data_length == MISENSOR_16BIT) 92 *val = data[1] + (data[0] << 8); 93 else 94 *val = data[3] + (data[2] << 8) + 95 (data[1] << 16) + (data[0] << 24); 96 97 return 0; 98 } 99 100 dev_err(&client->dev, "read from offset 0x%x error %d", reg, err); 101 return err; 102 } 103 104 static int 105 mt9m114_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u32 val) 106 { 107 int num_msg; 108 struct i2c_msg msg; 109 unsigned char data[6] = {0}; 110 __be16 *wreg; 111 int retry = 0; 112 113 if (!client->adapter) { 114 v4l2_err(client, "%s error, no client->adapter\n", __func__); 115 return -ENODEV; 116 } 117 118 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT 119 && data_length != MISENSOR_32BIT) { 120 v4l2_err(client, "%s error, invalid data_length\n", __func__); 121 return -EINVAL; 122 } 123 124 memset(&msg, 0, sizeof(msg)); 125 126 again: 127 msg.addr = client->addr; 128 msg.flags = 0; 129 msg.len = 2 + data_length; 130 msg.buf = data; 131 132 /* high byte goes out first */ 133 wreg = (void *)data; 134 *wreg = cpu_to_be16(reg); 135 136 if (data_length == MISENSOR_8BIT) { 137 data[2] = (u8)(val); 138 } else if (data_length == MISENSOR_16BIT) { 139 u16 *wdata = (void *)&data[2]; 140 141 *wdata = be16_to_cpu(*(__be16 *)&data[2]); 142 } else { 143 /* MISENSOR_32BIT */ 144 u32 *wdata = (void *)&data[2]; 145 146 *wdata = be32_to_cpu(*(__be32 *)&data[2]); 147 } 148 149 num_msg = i2c_transfer(client->adapter, &msg, 1); 150 151 /* 152 * HACK: Need some delay here for Rev 2 sensors otherwise some 153 * registers do not seem to load correctly. 154 */ 155 mdelay(1); 156 157 if (num_msg >= 0) 158 return 0; 159 160 dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d", 161 val, reg, num_msg); 162 if (retry <= I2C_RETRY_COUNT) { 163 dev_dbg(&client->dev, "retrying... %d", retry); 164 retry++; 165 msleep(20); 166 goto again; 167 } 168 169 return num_msg; 170 } 171 172 /** 173 * misensor_rmw_reg - Read/Modify/Write a value to a register in the sensor 174 * device 175 * @client: i2c driver client structure 176 * @data_length: 8/16/32-bits length 177 * @reg: register address 178 * @mask: masked out bits 179 * @set: bits set 180 * 181 * Read/modify/write a value to a register in the sensor device. 182 * Returns zero if successful, or non-zero otherwise. 183 */ 184 static int 185 misensor_rmw_reg(struct i2c_client *client, u16 data_length, u16 reg, 186 u32 mask, u32 set) 187 { 188 int err; 189 u32 val; 190 191 /* Exit when no mask */ 192 if (mask == 0) 193 return 0; 194 195 /* @mask must not exceed data length */ 196 switch (data_length) { 197 case MISENSOR_8BIT: 198 if (mask & ~0xff) 199 return -EINVAL; 200 break; 201 case MISENSOR_16BIT: 202 if (mask & ~0xffff) 203 return -EINVAL; 204 break; 205 case MISENSOR_32BIT: 206 break; 207 default: 208 /* Wrong @data_length */ 209 return -EINVAL; 210 } 211 212 err = mt9m114_read_reg(client, data_length, reg, &val); 213 if (err) { 214 v4l2_err(client, "misensor_rmw_reg error exit, read failed\n"); 215 return -EINVAL; 216 } 217 218 val &= ~mask; 219 220 /* 221 * Perform the OR function if the @set exists. 222 * Shift @set value to target bit location. @set should set only 223 * bits included in @mask. 224 * 225 * REVISIT: This function expects @set to be non-shifted. Its shift 226 * value is then defined to be equal to mask's LSB position. 227 * How about to inform values in their right offset position and avoid 228 * this unneeded shift operation? 229 */ 230 set <<= ffs(mask) - 1; 231 val |= set & mask; 232 233 err = mt9m114_write_reg(client, data_length, reg, val); 234 if (err) { 235 v4l2_err(client, "misensor_rmw_reg error exit, write failed\n"); 236 return -EINVAL; 237 } 238 239 return 0; 240 } 241 242 static int __mt9m114_flush_reg_array(struct i2c_client *client, 243 struct mt9m114_write_ctrl *ctrl) 244 { 245 struct i2c_msg msg; 246 const int num_msg = 1; 247 int ret; 248 int retry = 0; 249 __be16 *data16 = (void *)&ctrl->buffer.addr; 250 251 if (ctrl->index == 0) 252 return 0; 253 254 again: 255 msg.addr = client->addr; 256 msg.flags = 0; 257 msg.len = 2 + ctrl->index; 258 *data16 = cpu_to_be16(ctrl->buffer.addr); 259 msg.buf = (u8 *)&ctrl->buffer; 260 261 ret = i2c_transfer(client->adapter, &msg, num_msg); 262 if (ret != num_msg) { 263 if (++retry <= I2C_RETRY_COUNT) { 264 dev_dbg(&client->dev, "retrying... %d\n", retry); 265 msleep(20); 266 goto again; 267 } 268 dev_err(&client->dev, "%s: i2c transfer error\n", __func__); 269 return -EIO; 270 } 271 272 ctrl->index = 0; 273 274 /* 275 * REVISIT: Previously we had a delay after writing data to sensor. 276 * But it was removed as our tests have shown it is not necessary 277 * anymore. 278 */ 279 280 return 0; 281 } 282 283 static int __mt9m114_buf_reg_array(struct i2c_client *client, 284 struct mt9m114_write_ctrl *ctrl, 285 const struct misensor_reg *next) 286 { 287 __be16 *data16; 288 __be32 *data32; 289 int err; 290 291 /* Insufficient buffer? Let's flush and get more free space. */ 292 if (ctrl->index + next->length >= MT9M114_MAX_WRITE_BUF_SIZE) { 293 err = __mt9m114_flush_reg_array(client, ctrl); 294 if (err) 295 return err; 296 } 297 298 switch (next->length) { 299 case MISENSOR_8BIT: 300 ctrl->buffer.data[ctrl->index] = (u8)next->val; 301 break; 302 case MISENSOR_16BIT: 303 data16 = (__be16 *)&ctrl->buffer.data[ctrl->index]; 304 *data16 = cpu_to_be16((u16)next->val); 305 break; 306 case MISENSOR_32BIT: 307 data32 = (__be32 *)&ctrl->buffer.data[ctrl->index]; 308 *data32 = cpu_to_be32(next->val); 309 break; 310 default: 311 return -EINVAL; 312 } 313 314 /* When first item is added, we need to store its starting address */ 315 if (ctrl->index == 0) 316 ctrl->buffer.addr = next->reg; 317 318 ctrl->index += next->length; 319 320 return 0; 321 } 322 323 static int 324 __mt9m114_write_reg_is_consecutive(struct i2c_client *client, 325 struct mt9m114_write_ctrl *ctrl, 326 const struct misensor_reg *next) 327 { 328 if (ctrl->index == 0) 329 return 1; 330 331 return ctrl->buffer.addr + ctrl->index == next->reg; 332 } 333 334 /* 335 * mt9m114_write_reg_array - Initializes a list of mt9m114 registers 336 * @client: i2c driver client structure 337 * @reglist: list of registers to be written 338 * @poll: completion polling requirement 339 * This function initializes a list of registers. When consecutive addresses 340 * are found in a row on the list, this function creates a buffer and sends 341 * consecutive data in a single i2c_transfer(). 342 * 343 * __mt9m114_flush_reg_array, __mt9m114_buf_reg_array() and 344 * __mt9m114_write_reg_is_consecutive() are internal functions to 345 * mt9m114_write_reg_array() and should be not used anywhere else. 346 * 347 */ 348 static int mt9m114_write_reg_array(struct i2c_client *client, 349 const struct misensor_reg *reglist, 350 int poll) 351 { 352 const struct misensor_reg *next = reglist; 353 struct mt9m114_write_ctrl ctrl; 354 int err; 355 356 if (poll == PRE_POLLING) { 357 err = mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT); 358 if (err) 359 return err; 360 } 361 362 ctrl.index = 0; 363 for (; next->length != MISENSOR_TOK_TERM; next++) { 364 switch (next->length & MISENSOR_TOK_MASK) { 365 case MISENSOR_TOK_DELAY: 366 err = __mt9m114_flush_reg_array(client, &ctrl); 367 if (err) 368 return err; 369 msleep(next->val); 370 break; 371 case MISENSOR_TOK_RMW: 372 err = __mt9m114_flush_reg_array(client, &ctrl); 373 err |= misensor_rmw_reg(client, 374 next->length & 375 ~MISENSOR_TOK_RMW, 376 next->reg, next->val, 377 next->val2); 378 if (err) { 379 dev_err(&client->dev, "%s read err. aborted\n", 380 __func__); 381 return -EINVAL; 382 } 383 break; 384 default: 385 /* 386 * If next address is not consecutive, data needs to be 387 * flushed before proceed. 388 */ 389 if (!__mt9m114_write_reg_is_consecutive(client, &ctrl, 390 next)) { 391 err = __mt9m114_flush_reg_array(client, &ctrl); 392 if (err) 393 return err; 394 } 395 err = __mt9m114_buf_reg_array(client, &ctrl, next); 396 if (err) { 397 v4l2_err(client, "%s: write error, aborted\n", 398 __func__); 399 return err; 400 } 401 break; 402 } 403 } 404 405 err = __mt9m114_flush_reg_array(client, &ctrl); 406 if (err) 407 return err; 408 409 if (poll == POST_POLLING) 410 return mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT); 411 412 return 0; 413 } 414 415 static int mt9m114_wait_state(struct i2c_client *client, int timeout) 416 { 417 int ret; 418 unsigned int val; 419 420 while (timeout-- > 0) { 421 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 0x0080, &val); 422 if (ret) 423 return ret; 424 if ((val & 0x2) == 0) 425 return 0; 426 msleep(20); 427 } 428 429 return -EINVAL; 430 } 431 432 static int mt9m114_set_suspend(struct v4l2_subdev *sd) 433 { 434 struct i2c_client *client = v4l2_get_subdevdata(sd); 435 436 return mt9m114_write_reg_array(client, 437 mt9m114_standby_reg, POST_POLLING); 438 } 439 440 static int mt9m114_init_common(struct v4l2_subdev *sd) 441 { 442 struct i2c_client *client = v4l2_get_subdevdata(sd); 443 444 return mt9m114_write_reg_array(client, mt9m114_common, PRE_POLLING); 445 } 446 447 static int power_ctrl(struct v4l2_subdev *sd, bool flag) 448 { 449 int ret; 450 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 451 452 if (!dev || !dev->platform_data) 453 return -ENODEV; 454 455 if (flag) { 456 ret = dev->platform_data->v2p8_ctrl(sd, 1); 457 if (ret == 0) { 458 ret = dev->platform_data->v1p8_ctrl(sd, 1); 459 if (ret) 460 ret = dev->platform_data->v2p8_ctrl(sd, 0); 461 } 462 } else { 463 ret = dev->platform_data->v2p8_ctrl(sd, 0); 464 ret = dev->platform_data->v1p8_ctrl(sd, 0); 465 } 466 return ret; 467 } 468 469 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 470 { 471 int ret; 472 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 473 474 if (!dev || !dev->platform_data) 475 return -ENODEV; 476 477 /* Note: current modules wire only one GPIO signal (RESET#), 478 * but the schematic wires up two to the connector. BIOS 479 * versions have been unfortunately inconsistent with which 480 * ACPI index RESET# is on, so hit both */ 481 482 if (flag) { 483 ret = dev->platform_data->gpio0_ctrl(sd, 0); 484 ret = dev->platform_data->gpio1_ctrl(sd, 0); 485 msleep(60); 486 ret |= dev->platform_data->gpio0_ctrl(sd, 1); 487 ret |= dev->platform_data->gpio1_ctrl(sd, 1); 488 } else { 489 ret = dev->platform_data->gpio0_ctrl(sd, 0); 490 ret = dev->platform_data->gpio1_ctrl(sd, 0); 491 } 492 return ret; 493 } 494 495 static int power_up(struct v4l2_subdev *sd) 496 { 497 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 498 struct i2c_client *client = v4l2_get_subdevdata(sd); 499 int ret; 500 501 if (!dev->platform_data) { 502 dev_err(&client->dev, "no camera_sensor_platform_data"); 503 return -ENODEV; 504 } 505 506 /* power control */ 507 ret = power_ctrl(sd, 1); 508 if (ret) 509 goto fail_power; 510 511 /* flis clock control */ 512 ret = dev->platform_data->flisclk_ctrl(sd, 1); 513 if (ret) 514 goto fail_clk; 515 516 /* gpio ctrl */ 517 ret = gpio_ctrl(sd, 1); 518 if (ret) 519 dev_err(&client->dev, "gpio failed 1\n"); 520 /* 521 * according to DS, 44ms is needed between power up and first i2c 522 * commend 523 */ 524 msleep(50); 525 526 return 0; 527 528 fail_clk: 529 dev->platform_data->flisclk_ctrl(sd, 0); 530 fail_power: 531 power_ctrl(sd, 0); 532 dev_err(&client->dev, "sensor power-up failed\n"); 533 534 return ret; 535 } 536 537 static int power_down(struct v4l2_subdev *sd) 538 { 539 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 540 struct i2c_client *client = v4l2_get_subdevdata(sd); 541 int ret; 542 543 if (!dev->platform_data) { 544 dev_err(&client->dev, "no camera_sensor_platform_data"); 545 return -ENODEV; 546 } 547 548 ret = dev->platform_data->flisclk_ctrl(sd, 0); 549 if (ret) 550 dev_err(&client->dev, "flisclk failed\n"); 551 552 /* gpio ctrl */ 553 ret = gpio_ctrl(sd, 0); 554 if (ret) 555 dev_err(&client->dev, "gpio failed 1\n"); 556 557 /* power control */ 558 ret = power_ctrl(sd, 0); 559 if (ret) 560 dev_err(&client->dev, "vprog failed.\n"); 561 562 /*according to DS, 20ms is needed after power down*/ 563 msleep(20); 564 565 return ret; 566 } 567 568 static int mt9m114_s_power(struct v4l2_subdev *sd, int power) 569 { 570 if (power == 0) 571 return power_down(sd); 572 else { 573 if (power_up(sd)) 574 return -EINVAL; 575 576 return mt9m114_init_common(sd); 577 } 578 } 579 580 /* 581 * distance - calculate the distance 582 * @res: resolution 583 * @w: width 584 * @h: height 585 * 586 * Get the gap between resolution and w/h. 587 * res->width/height smaller than w/h wouldn't be considered. 588 * Returns the value of gap or -1 if fail. 589 */ 590 #define LARGEST_ALLOWED_RATIO_MISMATCH 600 591 static int distance(struct mt9m114_res_struct const *res, u32 w, u32 h) 592 { 593 unsigned int w_ratio; 594 unsigned int h_ratio; 595 int match; 596 597 if (w == 0) 598 return -1; 599 w_ratio = (res->width << 13) / w; 600 if (h == 0) 601 return -1; 602 h_ratio = (res->height << 13) / h; 603 if (h_ratio == 0) 604 return -1; 605 match = abs(((w_ratio << 13) / h_ratio) - 8192); 606 607 if ((w_ratio < 8192) || (h_ratio < 8192) || 608 (match > LARGEST_ALLOWED_RATIO_MISMATCH)) 609 return -1; 610 611 return w_ratio + h_ratio; 612 } 613 614 /* Return the nearest higher resolution index */ 615 static int nearest_resolution_index(int w, int h) 616 { 617 int i; 618 int idx = -1; 619 int dist; 620 int min_dist = INT_MAX; 621 const struct mt9m114_res_struct *tmp_res = NULL; 622 623 for (i = 0; i < ARRAY_SIZE(mt9m114_res); i++) { 624 tmp_res = &mt9m114_res[i]; 625 dist = distance(tmp_res, w, h); 626 if (dist == -1) 627 continue; 628 if (dist < min_dist) { 629 min_dist = dist; 630 idx = i; 631 } 632 } 633 634 return idx; 635 } 636 637 static int mt9m114_try_res(u32 *w, u32 *h) 638 { 639 int idx = 0; 640 641 if ((*w > MT9M114_RES_960P_SIZE_H) 642 || (*h > MT9M114_RES_960P_SIZE_V)) { 643 *w = MT9M114_RES_960P_SIZE_H; 644 *h = MT9M114_RES_960P_SIZE_V; 645 } else { 646 idx = nearest_resolution_index(*w, *h); 647 648 /* 649 * nearest_resolution_index() doesn't return smaller 650 * resolutions. If it fails, it means the requested 651 * resolution is higher than wecan support. Fallback 652 * to highest possible resolution in this case. 653 */ 654 if (idx == -1) 655 idx = ARRAY_SIZE(mt9m114_res) - 1; 656 657 *w = mt9m114_res[idx].width; 658 *h = mt9m114_res[idx].height; 659 } 660 661 return 0; 662 } 663 664 static struct mt9m114_res_struct *mt9m114_to_res(u32 w, u32 h) 665 { 666 int index; 667 668 for (index = 0; index < N_RES; index++) { 669 if ((mt9m114_res[index].width == w) && 670 (mt9m114_res[index].height == h)) 671 break; 672 } 673 674 /* No mode found */ 675 if (index >= N_RES) 676 return NULL; 677 678 return &mt9m114_res[index]; 679 } 680 681 static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size) 682 { 683 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 684 unsigned short hsize; 685 unsigned short vsize; 686 687 switch (dev->res) { 688 case MT9M114_RES_736P: 689 hsize = MT9M114_RES_736P_SIZE_H; 690 vsize = MT9M114_RES_736P_SIZE_V; 691 break; 692 case MT9M114_RES_864P: 693 hsize = MT9M114_RES_864P_SIZE_H; 694 vsize = MT9M114_RES_864P_SIZE_V; 695 break; 696 case MT9M114_RES_960P: 697 hsize = MT9M114_RES_960P_SIZE_H; 698 vsize = MT9M114_RES_960P_SIZE_V; 699 break; 700 default: 701 v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__, 702 dev->res); 703 return -EINVAL; 704 } 705 706 if (h_size) 707 *h_size = hsize; 708 if (v_size) 709 *v_size = vsize; 710 711 return 0; 712 } 713 714 static int mt9m114_get_intg_factor(struct i2c_client *client, 715 struct camera_mipi_info *info, 716 const struct mt9m114_res_struct *res) 717 { 718 struct atomisp_sensor_mode_data *buf = &info->data; 719 u32 reg_val; 720 int ret; 721 722 if (!info) 723 return -EINVAL; 724 725 ret = mt9m114_read_reg(client, MISENSOR_32BIT, 726 REG_PIXEL_CLK, ®_val); 727 if (ret) 728 return ret; 729 buf->vt_pix_clk_freq_mhz = reg_val; 730 731 /* get integration time */ 732 buf->coarse_integration_time_min = MT9M114_COARSE_INTG_TIME_MIN; 733 buf->coarse_integration_time_max_margin = 734 MT9M114_COARSE_INTG_TIME_MAX_MARGIN; 735 736 buf->fine_integration_time_min = MT9M114_FINE_INTG_TIME_MIN; 737 buf->fine_integration_time_max_margin = 738 MT9M114_FINE_INTG_TIME_MAX_MARGIN; 739 740 buf->fine_integration_time_def = MT9M114_FINE_INTG_TIME_MIN; 741 742 buf->frame_length_lines = res->lines_per_frame; 743 buf->line_length_pck = res->pixels_per_line; 744 buf->read_mode = res->bin_mode; 745 746 /* get the cropping and output resolution to ISP for this mode. */ 747 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 748 REG_H_START, ®_val); 749 if (ret) 750 return ret; 751 buf->crop_horizontal_start = reg_val; 752 753 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 754 REG_V_START, ®_val); 755 if (ret) 756 return ret; 757 buf->crop_vertical_start = reg_val; 758 759 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 760 REG_H_END, ®_val); 761 if (ret) 762 return ret; 763 buf->crop_horizontal_end = reg_val; 764 765 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 766 REG_V_END, ®_val); 767 if (ret) 768 return ret; 769 buf->crop_vertical_end = reg_val; 770 771 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 772 REG_WIDTH, ®_val); 773 if (ret) 774 return ret; 775 buf->output_width = reg_val; 776 777 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 778 REG_HEIGHT, ®_val); 779 if (ret) 780 return ret; 781 buf->output_height = reg_val; 782 783 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 784 REG_TIMING_HTS, ®_val); 785 if (ret) 786 return ret; 787 buf->line_length_pck = reg_val; 788 789 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 790 REG_TIMING_VTS, ®_val); 791 if (ret) 792 return ret; 793 buf->frame_length_lines = reg_val; 794 795 buf->binning_factor_x = res->bin_factor_x ? 796 res->bin_factor_x : 1; 797 buf->binning_factor_y = res->bin_factor_y ? 798 res->bin_factor_y : 1; 799 return 0; 800 } 801 802 static int mt9m114_get_fmt(struct v4l2_subdev *sd, 803 struct v4l2_subdev_pad_config *cfg, 804 struct v4l2_subdev_format *format) 805 { 806 struct v4l2_mbus_framefmt *fmt = &format->format; 807 int width, height; 808 int ret; 809 810 if (format->pad) 811 return -EINVAL; 812 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; 813 814 ret = mt9m114_res2size(sd, &width, &height); 815 if (ret) 816 return ret; 817 fmt->width = width; 818 fmt->height = height; 819 820 return 0; 821 } 822 823 static int mt9m114_set_fmt(struct v4l2_subdev *sd, 824 struct v4l2_subdev_pad_config *cfg, 825 struct v4l2_subdev_format *format) 826 { 827 struct v4l2_mbus_framefmt *fmt = &format->format; 828 struct i2c_client *c = v4l2_get_subdevdata(sd); 829 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 830 struct mt9m114_res_struct *res_index; 831 u32 width = fmt->width; 832 u32 height = fmt->height; 833 struct camera_mipi_info *mt9m114_info = NULL; 834 835 int ret; 836 837 if (format->pad) 838 return -EINVAL; 839 dev->streamon = 0; 840 dev->first_exp = MT9M114_DEFAULT_FIRST_EXP; 841 842 mt9m114_info = v4l2_get_subdev_hostdata(sd); 843 if (!mt9m114_info) 844 return -EINVAL; 845 846 mt9m114_try_res(&width, &height); 847 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 848 cfg->try_fmt = *fmt; 849 return 0; 850 } 851 res_index = mt9m114_to_res(width, height); 852 853 /* Sanity check */ 854 if (unlikely(!res_index)) { 855 WARN_ON(1); 856 return -EINVAL; 857 } 858 859 switch (res_index->res) { 860 case MT9M114_RES_736P: 861 ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING); 862 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, 863 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET); 864 break; 865 case MT9M114_RES_864P: 866 ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING); 867 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, 868 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET); 869 break; 870 case MT9M114_RES_960P: 871 ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING); 872 /* set sensor read_mode to Normal */ 873 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, 874 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET); 875 break; 876 default: 877 v4l2_err(sd, "set resolution: %d failed!\n", res_index->res); 878 return -EINVAL; 879 } 880 881 if (ret) 882 return -EINVAL; 883 884 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING); 885 if (ret < 0) 886 return ret; 887 888 if (mt9m114_set_suspend(sd)) 889 return -EINVAL; 890 891 if (dev->res != res_index->res) { 892 int index; 893 894 /* Switch to different size */ 895 if (width <= 640) { 896 dev->nctx = 0x00; /* Set for context A */ 897 } else { 898 /* 899 * Context B is used for resolutions larger than 640x480 900 * Using YUV for Context B. 901 */ 902 dev->nctx = 0x01; /* set for context B */ 903 } 904 905 /* 906 * Marked current sensor res as being "used" 907 * 908 * REVISIT: We don't need to use an "used" field on each mode 909 * list entry to know which mode is selected. If this 910 * information is really necessary, how about to use a single 911 * variable on sensor dev struct? 912 */ 913 for (index = 0; index < N_RES; index++) { 914 if ((width == mt9m114_res[index].width) && 915 (height == mt9m114_res[index].height)) { 916 mt9m114_res[index].used = true; 917 continue; 918 } 919 mt9m114_res[index].used = false; 920 } 921 } 922 ret = mt9m114_get_intg_factor(c, mt9m114_info, 923 &mt9m114_res[res_index->res]); 924 if (ret) { 925 dev_err(&c->dev, "failed to get integration_factor\n"); 926 return -EINVAL; 927 } 928 /* 929 * mt9m114 - we don't poll for context switch 930 * because it does not happen with streaming disabled. 931 */ 932 dev->res = res_index->res; 933 934 fmt->width = width; 935 fmt->height = height; 936 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; 937 return 0; 938 } 939 940 /* TODO: Update to SOC functions, remove exposure and gain */ 941 static int mt9m114_g_focal(struct v4l2_subdev *sd, s32 *val) 942 { 943 *val = (MT9M114_FOCAL_LENGTH_NUM << 16) | MT9M114_FOCAL_LENGTH_DEM; 944 return 0; 945 } 946 947 static int mt9m114_g_fnumber(struct v4l2_subdev *sd, s32 *val) 948 { 949 /*const f number for mt9m114*/ 950 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 16) | MT9M114_F_NUMBER_DEM; 951 return 0; 952 } 953 954 static int mt9m114_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) 955 { 956 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 24) | 957 (MT9M114_F_NUMBER_DEM << 16) | 958 (MT9M114_F_NUMBER_DEFAULT_NUM << 8) | MT9M114_F_NUMBER_DEM; 959 return 0; 960 } 961 962 /* Horizontal flip the image. */ 963 static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val) 964 { 965 struct i2c_client *c = v4l2_get_subdevdata(sd); 966 int ret; 967 u32 data; 968 969 ret = mt9m114_read_reg(c, MISENSOR_16BIT, 970 (u32)MISENSOR_READ_MODE, &data); 971 if (ret) 972 return ret; 973 *val = !!(data & MISENSOR_HFLIP_MASK); 974 975 return 0; 976 } 977 978 static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val) 979 { 980 struct i2c_client *c = v4l2_get_subdevdata(sd); 981 int ret; 982 u32 data; 983 984 ret = mt9m114_read_reg(c, MISENSOR_16BIT, 985 (u32)MISENSOR_READ_MODE, &data); 986 if (ret) 987 return ret; 988 *val = !!(data & MISENSOR_VFLIP_MASK); 989 990 return 0; 991 } 992 993 static long mt9m114_s_exposure(struct v4l2_subdev *sd, 994 struct atomisp_exposure *exposure) 995 { 996 struct i2c_client *client = v4l2_get_subdevdata(sd); 997 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 998 int ret = 0; 999 unsigned int coarse_integration = 0; 1000 unsigned int FLines = 0; 1001 unsigned int FrameLengthLines = 0; /* ExposureTime.FrameLengthLines; */ 1002 unsigned int AnalogGain, DigitalGain; 1003 u32 AnalogGainToWrite = 0; 1004 1005 dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__, 1006 exposure->integration_time[0], exposure->gain[0], 1007 exposure->gain[1]); 1008 1009 coarse_integration = exposure->integration_time[0]; 1010 /* fine_integration = ExposureTime.FineIntegrationTime; */ 1011 /* FrameLengthLines = ExposureTime.FrameLengthLines; */ 1012 FLines = mt9m114_res[dev->res].lines_per_frame; 1013 AnalogGain = exposure->gain[0]; 1014 DigitalGain = exposure->gain[1]; 1015 if (!dev->streamon) { 1016 /*Save the first exposure values while stream is off*/ 1017 dev->first_exp = coarse_integration; 1018 dev->first_gain = AnalogGain; 1019 dev->first_diggain = DigitalGain; 1020 } 1021 /* DigitalGain = 0x400 * (((u16) DigitalGain) >> 8) + 1022 ((unsigned int)(0x400 * (((u16) DigitalGain) & 0xFF)) >>8); */ 1023 1024 /* set frame length */ 1025 if (FLines < coarse_integration + 6) 1026 FLines = coarse_integration + 6; 1027 if (FLines < FrameLengthLines) 1028 FLines = FrameLengthLines; 1029 ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, FLines); 1030 if (ret) { 1031 v4l2_err(client, "%s: fail to set FLines\n", __func__); 1032 return -EINVAL; 1033 } 1034 1035 /* set coarse integration */ 1036 /* 3A provide real exposure time. 1037 should not translate to any value here. */ 1038 ret = mt9m114_write_reg(client, MISENSOR_16BIT, 1039 REG_EXPO_COARSE, (u16)(coarse_integration)); 1040 if (ret) { 1041 v4l2_err(client, "%s: fail to set exposure time\n", __func__); 1042 return -EINVAL; 1043 } 1044 1045 /* 1046 // set analog/digital gain 1047 switch(AnalogGain) 1048 { 1049 case 0: 1050 AnalogGainToWrite = 0x0; 1051 break; 1052 case 1: 1053 AnalogGainToWrite = 0x20; 1054 break; 1055 case 2: 1056 AnalogGainToWrite = 0x60; 1057 break; 1058 case 4: 1059 AnalogGainToWrite = 0xA0; 1060 break; 1061 case 8: 1062 AnalogGainToWrite = 0xE0; 1063 break; 1064 default: 1065 AnalogGainToWrite = 0x20; 1066 break; 1067 } 1068 */ 1069 if (DigitalGain >= 16 || DigitalGain <= 1) 1070 DigitalGain = 1; 1071 /* AnalogGainToWrite = 1072 (u16)((DigitalGain << 12) | AnalogGainToWrite); */ 1073 AnalogGainToWrite = (u16)((DigitalGain << 12) | (u16)AnalogGain); 1074 ret = mt9m114_write_reg(client, MISENSOR_16BIT, 1075 REG_GAIN, AnalogGainToWrite); 1076 if (ret) { 1077 v4l2_err(client, "%s: fail to set AnalogGainToWrite\n", 1078 __func__); 1079 return -EINVAL; 1080 } 1081 1082 return ret; 1083 } 1084 1085 static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 1086 { 1087 switch (cmd) { 1088 case ATOMISP_IOC_S_EXPOSURE: 1089 return mt9m114_s_exposure(sd, arg); 1090 default: 1091 return -EINVAL; 1092 } 1093 1094 return 0; 1095 } 1096 1097 /* This returns the exposure time being used. This should only be used 1098 for filling in EXIF data, not for actual image processing. */ 1099 static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value) 1100 { 1101 struct i2c_client *client = v4l2_get_subdevdata(sd); 1102 u32 coarse; 1103 int ret; 1104 1105 /* the fine integration time is currently not calculated */ 1106 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 1107 REG_EXPO_COARSE, &coarse); 1108 if (ret) 1109 return ret; 1110 1111 *value = coarse; 1112 return 0; 1113 } 1114 1115 /* 1116 * This function will return the sensor supported max exposure zone number. 1117 * the sensor which supports max exposure zone number is 1. 1118 */ 1119 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val) 1120 { 1121 *val = 1; 1122 1123 return 0; 1124 } 1125 1126 /* 1127 * set exposure metering, average/center_weighted/spot/matrix. 1128 */ 1129 static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val) 1130 { 1131 struct i2c_client *client = v4l2_get_subdevdata(sd); 1132 int ret; 1133 1134 switch (val) { 1135 case V4L2_EXPOSURE_METERING_SPOT: 1136 ret = mt9m114_write_reg_array(client, mt9m114_exp_average, 1137 NO_POLLING); 1138 if (ret) { 1139 dev_err(&client->dev, "write exp_average reg err.\n"); 1140 return ret; 1141 } 1142 break; 1143 case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED: 1144 default: 1145 ret = mt9m114_write_reg_array(client, mt9m114_exp_center, 1146 NO_POLLING); 1147 if (ret) { 1148 dev_err(&client->dev, "write exp_default reg err"); 1149 return ret; 1150 } 1151 } 1152 1153 return 0; 1154 } 1155 1156 /* 1157 * This function is for touch exposure feature. 1158 */ 1159 static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd, 1160 struct v4l2_subdev_pad_config *cfg, 1161 struct v4l2_subdev_selection *sel) 1162 { 1163 struct i2c_client *client = v4l2_get_subdevdata(sd); 1164 struct misensor_reg exp_reg; 1165 int width, height; 1166 int grid_width, grid_height; 1167 int grid_left, grid_top, grid_right, grid_bottom; 1168 int win_left, win_top, win_right, win_bottom; 1169 int i, j; 1170 int ret; 1171 1172 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && 1173 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) 1174 return -EINVAL; 1175 1176 grid_left = sel->r.left; 1177 grid_top = sel->r.top; 1178 grid_right = sel->r.left + sel->r.width - 1; 1179 grid_bottom = sel->r.top + sel->r.height - 1; 1180 1181 ret = mt9m114_res2size(sd, &width, &height); 1182 if (ret) 1183 return ret; 1184 1185 grid_width = width / 5; 1186 grid_height = height / 5; 1187 1188 if (grid_width && grid_height) { 1189 win_left = grid_left / grid_width; 1190 win_top = grid_top / grid_height; 1191 win_right = grid_right / grid_width; 1192 win_bottom = grid_bottom / grid_height; 1193 } else { 1194 dev_err(&client->dev, "Incorrect exp grid.\n"); 1195 return -EINVAL; 1196 } 1197 1198 win_left = clamp_t(int, win_left, 0, 4); 1199 win_top = clamp_t(int, win_top, 0, 4); 1200 win_right = clamp_t(int, win_right, 0, 4); 1201 win_bottom = clamp_t(int, win_bottom, 0, 4); 1202 1203 ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING); 1204 if (ret) { 1205 dev_err(&client->dev, "write exp_average reg err.\n"); 1206 return ret; 1207 } 1208 1209 for (i = win_top; i <= win_bottom; i++) { 1210 for (j = win_left; j <= win_right; j++) { 1211 exp_reg = mt9m114_exp_win[i][j]; 1212 1213 ret = mt9m114_write_reg(client, exp_reg.length, 1214 exp_reg.reg, exp_reg.val); 1215 if (ret) { 1216 dev_err(&client->dev, "write exp_reg err.\n"); 1217 return ret; 1218 } 1219 } 1220 } 1221 1222 return 0; 1223 } 1224 1225 static int mt9m114_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val) 1226 { 1227 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 1228 1229 *val = mt9m114_res[dev->res].bin_factor_x; 1230 1231 return 0; 1232 } 1233 1234 static int mt9m114_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val) 1235 { 1236 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 1237 1238 *val = mt9m114_res[dev->res].bin_factor_y; 1239 1240 return 0; 1241 } 1242 1243 static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val) 1244 { 1245 struct i2c_client *c = v4l2_get_subdevdata(sd); 1246 s32 luma = 0x37; 1247 int err; 1248 1249 /* EV value only support -2 to 2 1250 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17 1251 */ 1252 if (val < -2 || val > 2) 1253 return -EINVAL; 1254 luma += 0x10 * val; 1255 dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma); 1256 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A); 1257 if (err) { 1258 dev_err(&c->dev, "%s logic addr access error\n", __func__); 1259 return err; 1260 } 1261 err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma); 1262 if (err) { 1263 dev_err(&c->dev, "%s write target_average_luma failed\n", 1264 __func__); 1265 return err; 1266 } 1267 udelay(10); 1268 1269 return 0; 1270 } 1271 1272 static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val) 1273 { 1274 struct i2c_client *c = v4l2_get_subdevdata(sd); 1275 int err; 1276 u32 luma; 1277 1278 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A); 1279 if (err) { 1280 dev_err(&c->dev, "%s logic addr access error\n", __func__); 1281 return err; 1282 } 1283 err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma); 1284 if (err) { 1285 dev_err(&c->dev, "%s read target_average_luma failed\n", 1286 __func__); 1287 return err; 1288 } 1289 luma -= 0x17; 1290 luma /= 0x10; 1291 *val = (s32)luma - 2; 1292 dev_dbg(&c->dev, "%s val:%d\n", __func__, *val); 1293 1294 return 0; 1295 } 1296 1297 /* Fake interface 1298 * mt9m114 now can not support 3a_lock 1299 */ 1300 static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val) 1301 { 1302 aaalock = val; 1303 return 0; 1304 } 1305 1306 static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val) 1307 { 1308 if (aaalock) 1309 return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE 1310 | V4L2_LOCK_FOCUS; 1311 return 0; 1312 } 1313 1314 static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl) 1315 { 1316 struct mt9m114_device *dev = 1317 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler); 1318 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd); 1319 int ret = 0; 1320 1321 switch (ctrl->id) { 1322 case V4L2_CID_VFLIP: 1323 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n", 1324 __func__, ctrl->val); 1325 ret = mt9m114_t_vflip(&dev->sd, ctrl->val); 1326 break; 1327 case V4L2_CID_HFLIP: 1328 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n", 1329 __func__, ctrl->val); 1330 ret = mt9m114_t_hflip(&dev->sd, ctrl->val); 1331 break; 1332 case V4L2_CID_EXPOSURE_METERING: 1333 ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val); 1334 break; 1335 case V4L2_CID_EXPOSURE: 1336 ret = mt9m114_s_ev(&dev->sd, ctrl->val); 1337 break; 1338 case V4L2_CID_3A_LOCK: 1339 ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val); 1340 break; 1341 default: 1342 ret = -EINVAL; 1343 } 1344 return ret; 1345 } 1346 1347 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 1348 { 1349 struct mt9m114_device *dev = 1350 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler); 1351 int ret = 0; 1352 1353 switch (ctrl->id) { 1354 case V4L2_CID_VFLIP: 1355 ret = mt9m114_g_vflip(&dev->sd, &ctrl->val); 1356 break; 1357 case V4L2_CID_HFLIP: 1358 ret = mt9m114_g_hflip(&dev->sd, &ctrl->val); 1359 break; 1360 case V4L2_CID_FOCAL_ABSOLUTE: 1361 ret = mt9m114_g_focal(&dev->sd, &ctrl->val); 1362 break; 1363 case V4L2_CID_FNUMBER_ABSOLUTE: 1364 ret = mt9m114_g_fnumber(&dev->sd, &ctrl->val); 1365 break; 1366 case V4L2_CID_FNUMBER_RANGE: 1367 ret = mt9m114_g_fnumber_range(&dev->sd, &ctrl->val); 1368 break; 1369 case V4L2_CID_EXPOSURE_ABSOLUTE: 1370 ret = mt9m114_g_exposure(&dev->sd, &ctrl->val); 1371 break; 1372 case V4L2_CID_EXPOSURE_ZONE_NUM: 1373 ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val); 1374 break; 1375 case V4L2_CID_BIN_FACTOR_HORZ: 1376 ret = mt9m114_g_bin_factor_x(&dev->sd, &ctrl->val); 1377 break; 1378 case V4L2_CID_BIN_FACTOR_VERT: 1379 ret = mt9m114_g_bin_factor_y(&dev->sd, &ctrl->val); 1380 break; 1381 case V4L2_CID_EXPOSURE: 1382 ret = mt9m114_g_ev(&dev->sd, &ctrl->val); 1383 break; 1384 case V4L2_CID_3A_LOCK: 1385 ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val); 1386 break; 1387 default: 1388 ret = -EINVAL; 1389 } 1390 1391 return ret; 1392 } 1393 1394 static const struct v4l2_ctrl_ops ctrl_ops = { 1395 .s_ctrl = mt9m114_s_ctrl, 1396 .g_volatile_ctrl = mt9m114_g_volatile_ctrl 1397 }; 1398 1399 static struct v4l2_ctrl_config mt9m114_controls[] = { 1400 { 1401 .ops = &ctrl_ops, 1402 .id = V4L2_CID_VFLIP, 1403 .name = "Image v-Flip", 1404 .type = V4L2_CTRL_TYPE_INTEGER, 1405 .min = 0, 1406 .max = 1, 1407 .step = 1, 1408 .def = 0, 1409 }, 1410 { 1411 .ops = &ctrl_ops, 1412 .id = V4L2_CID_HFLIP, 1413 .name = "Image h-Flip", 1414 .type = V4L2_CTRL_TYPE_INTEGER, 1415 .min = 0, 1416 .max = 1, 1417 .step = 1, 1418 .def = 0, 1419 }, 1420 { 1421 .ops = &ctrl_ops, 1422 .id = V4L2_CID_FOCAL_ABSOLUTE, 1423 .name = "focal length", 1424 .type = V4L2_CTRL_TYPE_INTEGER, 1425 .min = MT9M114_FOCAL_LENGTH_DEFAULT, 1426 .max = MT9M114_FOCAL_LENGTH_DEFAULT, 1427 .step = 1, 1428 .def = MT9M114_FOCAL_LENGTH_DEFAULT, 1429 .flags = 0, 1430 }, 1431 { 1432 .ops = &ctrl_ops, 1433 .id = V4L2_CID_FNUMBER_ABSOLUTE, 1434 .name = "f-number", 1435 .type = V4L2_CTRL_TYPE_INTEGER, 1436 .min = MT9M114_F_NUMBER_DEFAULT, 1437 .max = MT9M114_F_NUMBER_DEFAULT, 1438 .step = 1, 1439 .def = MT9M114_F_NUMBER_DEFAULT, 1440 .flags = 0, 1441 }, 1442 { 1443 .ops = &ctrl_ops, 1444 .id = V4L2_CID_FNUMBER_RANGE, 1445 .name = "f-number range", 1446 .type = V4L2_CTRL_TYPE_INTEGER, 1447 .min = MT9M114_F_NUMBER_RANGE, 1448 .max = MT9M114_F_NUMBER_RANGE, 1449 .step = 1, 1450 .def = MT9M114_F_NUMBER_RANGE, 1451 .flags = 0, 1452 }, 1453 { 1454 .ops = &ctrl_ops, 1455 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 1456 .name = "exposure", 1457 .type = V4L2_CTRL_TYPE_INTEGER, 1458 .min = 0, 1459 .max = 0xffff, 1460 .step = 1, 1461 .def = 0, 1462 .flags = 0, 1463 }, 1464 { 1465 .ops = &ctrl_ops, 1466 .id = V4L2_CID_EXPOSURE_ZONE_NUM, 1467 .name = "one-time exposure zone number", 1468 .type = V4L2_CTRL_TYPE_INTEGER, 1469 .min = 0, 1470 .max = 0xffff, 1471 .step = 1, 1472 .def = 0, 1473 .flags = 0, 1474 }, 1475 { 1476 .ops = &ctrl_ops, 1477 .id = V4L2_CID_EXPOSURE_METERING, 1478 .name = "metering", 1479 .type = V4L2_CTRL_TYPE_MENU, 1480 .min = 0, 1481 .max = 3, 1482 .step = 0, 1483 .def = 1, 1484 .flags = 0, 1485 }, 1486 { 1487 .ops = &ctrl_ops, 1488 .id = V4L2_CID_BIN_FACTOR_HORZ, 1489 .name = "horizontal binning factor", 1490 .type = V4L2_CTRL_TYPE_INTEGER, 1491 .min = 0, 1492 .max = MT9M114_BIN_FACTOR_MAX, 1493 .step = 1, 1494 .def = 0, 1495 .flags = 0, 1496 }, 1497 { 1498 .ops = &ctrl_ops, 1499 .id = V4L2_CID_BIN_FACTOR_VERT, 1500 .name = "vertical binning factor", 1501 .type = V4L2_CTRL_TYPE_INTEGER, 1502 .min = 0, 1503 .max = MT9M114_BIN_FACTOR_MAX, 1504 .step = 1, 1505 .def = 0, 1506 .flags = 0, 1507 }, 1508 { 1509 .ops = &ctrl_ops, 1510 .id = V4L2_CID_EXPOSURE, 1511 .name = "exposure biasx", 1512 .type = V4L2_CTRL_TYPE_INTEGER, 1513 .min = -2, 1514 .max = 2, 1515 .step = 1, 1516 .def = 0, 1517 .flags = 0, 1518 }, 1519 { 1520 .ops = &ctrl_ops, 1521 .id = V4L2_CID_3A_LOCK, 1522 .name = "3a lock", 1523 .type = V4L2_CTRL_TYPE_BITMASK, 1524 .min = 0, 1525 .max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS, 1526 .step = 1, 1527 .def = 0, 1528 .flags = 0, 1529 }, 1530 }; 1531 1532 static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client) 1533 { 1534 struct i2c_adapter *adapter = client->adapter; 1535 u32 retvalue; 1536 1537 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { 1538 dev_err(&client->dev, "%s: i2c error", __func__); 1539 return -ENODEV; 1540 } 1541 mt9m114_read_reg(client, MISENSOR_16BIT, (u32)MT9M114_PID, &retvalue); 1542 dev->real_model_id = retvalue; 1543 1544 if (retvalue != MT9M114_MOD_ID) { 1545 dev_err(&client->dev, "%s: failed: client->addr = %x\n", 1546 __func__, client->addr); 1547 return -ENODEV; 1548 } 1549 1550 return 0; 1551 } 1552 1553 static int 1554 mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data) 1555 { 1556 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 1557 struct i2c_client *client = v4l2_get_subdevdata(sd); 1558 int ret; 1559 1560 if (!platform_data) 1561 return -ENODEV; 1562 1563 dev->platform_data = 1564 (struct camera_sensor_platform_data *)platform_data; 1565 1566 ret = power_up(sd); 1567 if (ret) { 1568 v4l2_err(client, "mt9m114 power-up err"); 1569 return ret; 1570 } 1571 1572 /* config & detect sensor */ 1573 ret = mt9m114_detect(dev, client); 1574 if (ret) { 1575 v4l2_err(client, "mt9m114_detect err s_config.\n"); 1576 goto fail_detect; 1577 } 1578 1579 ret = dev->platform_data->csi_cfg(sd, 1); 1580 if (ret) 1581 goto fail_csi_cfg; 1582 1583 ret = mt9m114_set_suspend(sd); 1584 if (ret) { 1585 v4l2_err(client, "mt9m114 suspend err"); 1586 return ret; 1587 } 1588 1589 ret = power_down(sd); 1590 if (ret) { 1591 v4l2_err(client, "mt9m114 power down err"); 1592 return ret; 1593 } 1594 1595 return ret; 1596 1597 fail_csi_cfg: 1598 dev->platform_data->csi_cfg(sd, 0); 1599 fail_detect: 1600 power_down(sd); 1601 dev_err(&client->dev, "sensor power-gating failed\n"); 1602 return ret; 1603 } 1604 1605 /* Horizontal flip the image. */ 1606 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value) 1607 { 1608 struct i2c_client *c = v4l2_get_subdevdata(sd); 1609 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 1610 int err; 1611 /* set for direct mode */ 1612 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850); 1613 if (value) { 1614 /* enable H flip ctx A */ 1615 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01); 1616 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01); 1617 /* ctx B */ 1618 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01); 1619 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01); 1620 1621 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, 1622 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN); 1623 1624 dev->bpat = MT9M114_BPAT_GRGRBGBG; 1625 } else { 1626 /* disable H flip ctx A */ 1627 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00); 1628 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00); 1629 /* ctx B */ 1630 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00); 1631 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00); 1632 1633 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, 1634 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS); 1635 1636 dev->bpat = MT9M114_BPAT_BGBGGRGR; 1637 } 1638 1639 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06); 1640 udelay(10); 1641 1642 return !!err; 1643 } 1644 1645 /* Vertically flip the image */ 1646 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value) 1647 { 1648 struct i2c_client *c = v4l2_get_subdevdata(sd); 1649 int err; 1650 /* set for direct mode */ 1651 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850); 1652 if (value >= 1) { 1653 /* enable H flip - ctx A */ 1654 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01); 1655 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01); 1656 /* ctx B */ 1657 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01); 1658 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01); 1659 1660 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, 1661 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN); 1662 } else { 1663 /* disable H flip - ctx A */ 1664 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00); 1665 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00); 1666 /* ctx B */ 1667 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00); 1668 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00); 1669 1670 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE, 1671 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS); 1672 } 1673 1674 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06); 1675 udelay(10); 1676 1677 return !!err; 1678 } 1679 1680 static int mt9m114_g_frame_interval(struct v4l2_subdev *sd, 1681 struct v4l2_subdev_frame_interval *interval) 1682 { 1683 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 1684 1685 interval->interval.numerator = 1; 1686 interval->interval.denominator = mt9m114_res[dev->res].fps; 1687 1688 return 0; 1689 } 1690 1691 static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable) 1692 { 1693 int ret; 1694 struct i2c_client *c = v4l2_get_subdevdata(sd); 1695 struct mt9m114_device *dev = to_mt9m114_sensor(sd); 1696 struct atomisp_exposure exposure; 1697 1698 if (enable) { 1699 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, 1700 POST_POLLING); 1701 if (ret < 0) 1702 return ret; 1703 1704 if (dev->first_exp > MT9M114_MAX_FIRST_EXP) { 1705 exposure.integration_time[0] = dev->first_exp; 1706 exposure.gain[0] = dev->first_gain; 1707 exposure.gain[1] = dev->first_diggain; 1708 mt9m114_s_exposure(sd, &exposure); 1709 } 1710 dev->streamon = 1; 1711 1712 } else { 1713 dev->streamon = 0; 1714 ret = mt9m114_set_suspend(sd); 1715 } 1716 1717 return ret; 1718 } 1719 1720 static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd, 1721 struct v4l2_subdev_pad_config *cfg, 1722 struct v4l2_subdev_mbus_code_enum *code) 1723 { 1724 if (code->index) 1725 return -EINVAL; 1726 code->code = MEDIA_BUS_FMT_SGRBG10_1X10; 1727 1728 return 0; 1729 } 1730 1731 static int mt9m114_enum_frame_size(struct v4l2_subdev *sd, 1732 struct v4l2_subdev_pad_config *cfg, 1733 struct v4l2_subdev_frame_size_enum *fse) 1734 { 1735 unsigned int index = fse->index; 1736 1737 if (index >= N_RES) 1738 return -EINVAL; 1739 1740 fse->min_width = mt9m114_res[index].width; 1741 fse->min_height = mt9m114_res[index].height; 1742 fse->max_width = mt9m114_res[index].width; 1743 fse->max_height = mt9m114_res[index].height; 1744 1745 return 0; 1746 } 1747 1748 static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) 1749 { 1750 int index; 1751 struct mt9m114_device *snr = to_mt9m114_sensor(sd); 1752 1753 if (!frames) 1754 return -EINVAL; 1755 1756 for (index = 0; index < N_RES; index++) { 1757 if (mt9m114_res[index].res == snr->res) 1758 break; 1759 } 1760 1761 if (index >= N_RES) 1762 return -EINVAL; 1763 1764 *frames = mt9m114_res[index].skip_frames; 1765 1766 return 0; 1767 } 1768 1769 static const struct v4l2_subdev_video_ops mt9m114_video_ops = { 1770 .s_stream = mt9m114_s_stream, 1771 .g_frame_interval = mt9m114_g_frame_interval, 1772 }; 1773 1774 static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = { 1775 .g_skip_frames = mt9m114_g_skip_frames, 1776 }; 1777 1778 static const struct v4l2_subdev_core_ops mt9m114_core_ops = { 1779 .s_power = mt9m114_s_power, 1780 .ioctl = mt9m114_ioctl, 1781 }; 1782 1783 /* REVISIT: Do we need pad operations? */ 1784 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = { 1785 .enum_mbus_code = mt9m114_enum_mbus_code, 1786 .enum_frame_size = mt9m114_enum_frame_size, 1787 .get_fmt = mt9m114_get_fmt, 1788 .set_fmt = mt9m114_set_fmt, 1789 .set_selection = mt9m114_s_exposure_selection, 1790 }; 1791 1792 static const struct v4l2_subdev_ops mt9m114_ops = { 1793 .core = &mt9m114_core_ops, 1794 .video = &mt9m114_video_ops, 1795 .pad = &mt9m114_pad_ops, 1796 .sensor = &mt9m114_sensor_ops, 1797 }; 1798 1799 static int mt9m114_remove(struct i2c_client *client) 1800 { 1801 struct mt9m114_device *dev; 1802 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1803 1804 dev = container_of(sd, struct mt9m114_device, sd); 1805 dev->platform_data->csi_cfg(sd, 0); 1806 v4l2_device_unregister_subdev(sd); 1807 media_entity_cleanup(&dev->sd.entity); 1808 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1809 kfree(dev); 1810 return 0; 1811 } 1812 1813 static int mt9m114_probe(struct i2c_client *client) 1814 { 1815 struct mt9m114_device *dev; 1816 int ret = 0; 1817 unsigned int i; 1818 void *pdata; 1819 acpi_handle handle; 1820 struct acpi_device *adev; 1821 1822 handle = ACPI_HANDLE(&client->dev); 1823 if (!handle || acpi_bus_get_device(handle, &adev)) { 1824 dev_err(&client->dev, "Error could not get ACPI device\n"); 1825 return -ENODEV; 1826 } 1827 pr_info("%s: ACPI detected it on bus ID=%s, HID=%s\n", 1828 __func__, acpi_device_bid(adev), acpi_device_hid(adev)); 1829 // FIXME: may need to release resources allocated by acpi_bus_get_device() 1830 1831 /* Setup sensor configuration structure */ 1832 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1833 if (!dev) 1834 return -ENOMEM; 1835 1836 v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops); 1837 pdata = gmin_camera_platform_data(&dev->sd, 1838 ATOMISP_INPUT_FORMAT_RAW_10, 1839 atomisp_bayer_order_grbg); 1840 if (pdata) 1841 ret = mt9m114_s_config(&dev->sd, client->irq, pdata); 1842 if (!pdata || ret) { 1843 v4l2_device_unregister_subdev(&dev->sd); 1844 kfree(dev); 1845 return ret; 1846 } 1847 1848 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA); 1849 if (ret) { 1850 v4l2_device_unregister_subdev(&dev->sd); 1851 kfree(dev); 1852 /* Coverity CID 298095 - return on error */ 1853 return ret; 1854 } 1855 1856 /*TODO add format code here*/ 1857 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1858 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 1859 dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10; 1860 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1861 1862 ret = 1863 v4l2_ctrl_handler_init(&dev->ctrl_handler, 1864 ARRAY_SIZE(mt9m114_controls)); 1865 if (ret) { 1866 mt9m114_remove(client); 1867 return ret; 1868 } 1869 1870 for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++) 1871 v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i], 1872 NULL); 1873 1874 if (dev->ctrl_handler.error) { 1875 mt9m114_remove(client); 1876 return dev->ctrl_handler.error; 1877 } 1878 1879 /* Use same lock for controls as for everything else. */ 1880 dev->ctrl_handler.lock = &dev->input_lock; 1881 dev->sd.ctrl_handler = &dev->ctrl_handler; 1882 1883 /* REVISIT: Do we need media controller? */ 1884 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 1885 if (ret) { 1886 mt9m114_remove(client); 1887 return ret; 1888 } 1889 return 0; 1890 } 1891 1892 static const struct acpi_device_id mt9m114_acpi_match[] = { 1893 { "INT33F0" }, 1894 { "CRMT1040" }, 1895 {}, 1896 }; 1897 MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match); 1898 1899 static struct i2c_driver mt9m114_driver = { 1900 .driver = { 1901 .name = "mt9m114", 1902 .acpi_match_table = mt9m114_acpi_match, 1903 }, 1904 .probe_new = mt9m114_probe, 1905 .remove = mt9m114_remove, 1906 }; 1907 module_i2c_driver(mt9m114_driver); 1908 1909 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>"); 1910 MODULE_LICENSE("GPL"); 1911