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