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