1 /* 2 * Support for GalaxyCore GC2235 2M camera sensor. 3 * 4 * Copyright (c) 2014 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 #include <linux/module.h> 18 #include <linux/types.h> 19 #include <linux/kernel.h> 20 #include <linux/mm.h> 21 #include <linux/string.h> 22 #include <linux/errno.h> 23 #include <linux/init.h> 24 #include <linux/kmod.h> 25 #include <linux/device.h> 26 #include <linux/delay.h> 27 #include <linux/slab.h> 28 #include <linux/i2c.h> 29 #include <linux/moduleparam.h> 30 #include <media/v4l2-device.h> 31 #include "../include/linux/atomisp_gmin_platform.h" 32 #include <linux/acpi.h> 33 #include <linux/io.h> 34 35 #include "gc2235.h" 36 37 /* i2c read/write stuff */ 38 static int gc2235_read_reg(struct i2c_client *client, 39 u16 data_length, u16 reg, u16 *val) 40 { 41 int err; 42 struct i2c_msg msg[2]; 43 unsigned char data[6]; 44 45 if (!client->adapter) { 46 dev_err(&client->dev, "%s error, no client->adapter\n", 47 __func__); 48 return -ENODEV; 49 } 50 51 if (data_length != GC2235_8BIT) { 52 dev_err(&client->dev, "%s error, invalid data length\n", 53 __func__); 54 return -EINVAL; 55 } 56 57 memset(msg, 0, sizeof(msg)); 58 59 msg[0].addr = client->addr; 60 msg[0].flags = 0; 61 msg[0].len = 1; 62 msg[0].buf = data; 63 64 /* high byte goes out first */ 65 data[0] = (u8)(reg & 0xff); 66 67 msg[1].addr = client->addr; 68 msg[1].len = data_length; 69 msg[1].flags = I2C_M_RD; 70 msg[1].buf = data; 71 72 err = i2c_transfer(client->adapter, msg, 2); 73 if (err != 2) { 74 if (err >= 0) 75 err = -EIO; 76 dev_err(&client->dev, 77 "read from offset 0x%x error %d", reg, err); 78 return err; 79 } 80 81 *val = 0; 82 /* high byte comes first */ 83 if (data_length == GC2235_8BIT) 84 *val = (u8)data[0]; 85 86 return 0; 87 } 88 89 static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data) 90 { 91 struct i2c_msg msg; 92 const int num_msg = 1; 93 int ret; 94 95 msg.addr = client->addr; 96 msg.flags = 0; 97 msg.len = len; 98 msg.buf = data; 99 ret = i2c_transfer(client->adapter, &msg, 1); 100 101 return ret == num_msg ? 0 : -EIO; 102 } 103 104 static int gc2235_write_reg(struct i2c_client *client, u16 data_length, 105 u8 reg, u8 val) 106 { 107 int ret; 108 unsigned char data[4] = {0}; 109 const u16 len = data_length + sizeof(u8); /* 16-bit address + data */ 110 111 if (data_length != GC2235_8BIT) { 112 dev_err(&client->dev, 113 "%s error, invalid data_length\n", __func__); 114 return -EINVAL; 115 } 116 117 /* high byte goes out first */ 118 data[0] = reg; 119 data[1] = val; 120 121 ret = gc2235_i2c_write(client, len, data); 122 if (ret) 123 dev_err(&client->dev, 124 "write error: wrote 0x%x to offset 0x%x error %d", 125 val, reg, ret); 126 127 return ret; 128 } 129 130 static int __gc2235_flush_reg_array(struct i2c_client *client, 131 struct gc2235_write_ctrl *ctrl) 132 { 133 u16 size; 134 135 if (ctrl->index == 0) 136 return 0; 137 138 size = sizeof(u8) + ctrl->index; /* 8-bit address + data */ 139 ctrl->index = 0; 140 141 return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer); 142 } 143 144 static int __gc2235_buf_reg_array(struct i2c_client *client, 145 struct gc2235_write_ctrl *ctrl, 146 const struct gc2235_reg *next) 147 { 148 int size; 149 150 if (next->type != GC2235_8BIT) 151 return -EINVAL; 152 153 size = 1; 154 ctrl->buffer.data[ctrl->index] = (u8)next->val; 155 156 /* When first item is added, we need to store its starting address */ 157 if (ctrl->index == 0) 158 ctrl->buffer.addr = next->reg; 159 160 ctrl->index += size; 161 162 /* 163 * Buffer cannot guarantee free space for u32? Better flush it to avoid 164 * possible lack of memory for next item. 165 */ 166 if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE) 167 return __gc2235_flush_reg_array(client, ctrl); 168 169 return 0; 170 } 171 172 static int __gc2235_write_reg_is_consecutive(struct i2c_client *client, 173 struct gc2235_write_ctrl *ctrl, 174 const struct gc2235_reg *next) 175 { 176 if (ctrl->index == 0) 177 return 1; 178 179 return ctrl->buffer.addr + ctrl->index == next->reg; 180 } 181 182 static int gc2235_write_reg_array(struct i2c_client *client, 183 const struct gc2235_reg *reglist) 184 { 185 const struct gc2235_reg *next = reglist; 186 struct gc2235_write_ctrl ctrl; 187 int err; 188 189 ctrl.index = 0; 190 for (; next->type != GC2235_TOK_TERM; next++) { 191 switch (next->type & GC2235_TOK_MASK) { 192 case GC2235_TOK_DELAY: 193 err = __gc2235_flush_reg_array(client, &ctrl); 194 if (err) 195 return err; 196 msleep(next->val); 197 break; 198 default: 199 /* 200 * If next address is not consecutive, data needs to be 201 * flushed before proceed. 202 */ 203 if (!__gc2235_write_reg_is_consecutive(client, &ctrl, 204 next)) { 205 err = __gc2235_flush_reg_array(client, &ctrl); 206 if (err) 207 return err; 208 } 209 err = __gc2235_buf_reg_array(client, &ctrl, next); 210 if (err) { 211 dev_err(&client->dev, "%s: write error, aborted\n", 212 __func__); 213 return err; 214 } 215 break; 216 } 217 } 218 219 return __gc2235_flush_reg_array(client, &ctrl); 220 } 221 222 static int gc2235_g_focal(struct v4l2_subdev *sd, s32 *val) 223 { 224 *val = (GC2235_FOCAL_LENGTH_NUM << 16) | GC2235_FOCAL_LENGTH_DEM; 225 return 0; 226 } 227 228 static int gc2235_g_fnumber(struct v4l2_subdev *sd, s32 *val) 229 { 230 /*const f number for imx*/ 231 *val = (GC2235_F_NUMBER_DEFAULT_NUM << 16) | GC2235_F_NUMBER_DEM; 232 return 0; 233 } 234 235 static int gc2235_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) 236 { 237 *val = (GC2235_F_NUMBER_DEFAULT_NUM << 24) | 238 (GC2235_F_NUMBER_DEM << 16) | 239 (GC2235_F_NUMBER_DEFAULT_NUM << 8) | GC2235_F_NUMBER_DEM; 240 return 0; 241 } 242 243 static int gc2235_get_intg_factor(struct i2c_client *client, 244 struct camera_mipi_info *info, 245 const struct gc2235_resolution *res) 246 { 247 struct v4l2_subdev *sd = i2c_get_clientdata(client); 248 struct gc2235_device *dev = to_gc2235_sensor(sd); 249 struct atomisp_sensor_mode_data *buf = &info->data; 250 u16 reg_val, reg_val_h; 251 int ret; 252 253 if (!info) 254 return -EINVAL; 255 256 /* pixel clock calculattion */ 257 buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz = 30000000; 258 259 /* get integration time */ 260 buf->coarse_integration_time_min = GC2235_COARSE_INTG_TIME_MIN; 261 buf->coarse_integration_time_max_margin = 262 GC2235_COARSE_INTG_TIME_MAX_MARGIN; 263 264 buf->fine_integration_time_min = GC2235_FINE_INTG_TIME_MIN; 265 buf->fine_integration_time_max_margin = 266 GC2235_FINE_INTG_TIME_MAX_MARGIN; 267 268 buf->fine_integration_time_def = GC2235_FINE_INTG_TIME_MIN; 269 buf->frame_length_lines = res->lines_per_frame; 270 buf->line_length_pck = res->pixels_per_line; 271 buf->read_mode = res->bin_mode; 272 273 /* get the cropping and output resolution to ISP for this mode. */ 274 ret = gc2235_read_reg(client, GC2235_8BIT, 275 GC2235_H_CROP_START_H, ®_val_h); 276 ret = gc2235_read_reg(client, GC2235_8BIT, 277 GC2235_H_CROP_START_L, ®_val); 278 if (ret) 279 return ret; 280 281 buf->crop_horizontal_start = (reg_val_h << 8) | reg_val; 282 283 ret = gc2235_read_reg(client, GC2235_8BIT, 284 GC2235_V_CROP_START_H, ®_val_h); 285 ret = gc2235_read_reg(client, GC2235_8BIT, 286 GC2235_V_CROP_START_L, ®_val); 287 if (ret) 288 return ret; 289 290 buf->crop_vertical_start = (reg_val_h << 8) | reg_val; 291 292 ret = gc2235_read_reg(client, GC2235_8BIT, 293 GC2235_H_OUTSIZE_H, ®_val_h); 294 ret = gc2235_read_reg(client, GC2235_8BIT, 295 GC2235_H_OUTSIZE_L, ®_val); 296 if (ret) 297 return ret; 298 buf->output_width = (reg_val_h << 8) | reg_val; 299 300 ret = gc2235_read_reg(client, GC2235_8BIT, 301 GC2235_V_OUTSIZE_H, ®_val_h); 302 ret = gc2235_read_reg(client, GC2235_8BIT, 303 GC2235_V_OUTSIZE_L, ®_val); 304 if (ret) 305 return ret; 306 buf->output_height = (reg_val_h << 8) | reg_val; 307 308 buf->crop_horizontal_end = buf->crop_horizontal_start + 309 buf->output_width - 1; 310 buf->crop_vertical_end = buf->crop_vertical_start + 311 buf->output_height - 1; 312 313 ret = gc2235_read_reg(client, GC2235_8BIT, 314 GC2235_HB_H, ®_val_h); 315 ret = gc2235_read_reg(client, GC2235_8BIT, 316 GC2235_HB_L, ®_val); 317 if (ret) 318 return ret; 319 320 #if 0 321 u16 dummy = (reg_val_h << 8) | reg_val; 322 #endif 323 324 ret = gc2235_read_reg(client, GC2235_8BIT, 325 GC2235_SH_DELAY_H, ®_val_h); 326 ret = gc2235_read_reg(client, GC2235_8BIT, 327 GC2235_SH_DELAY_L, ®_val); 328 329 #if 0 330 buf->line_length_pck = buf->output_width + 16 + dummy + 331 (((u16)reg_val_h << 8) | (u16)reg_val) + 4; 332 #endif 333 ret = gc2235_read_reg(client, GC2235_8BIT, 334 GC2235_VB_H, ®_val_h); 335 ret = gc2235_read_reg(client, GC2235_8BIT, 336 GC2235_VB_L, ®_val); 337 if (ret) 338 return ret; 339 340 #if 0 341 buf->frame_length_lines = buf->output_height + 32 + 342 (((u16)reg_val_h << 8) | (u16)reg_val); 343 #endif 344 buf->binning_factor_x = res->bin_factor_x ? 345 res->bin_factor_x : 1; 346 buf->binning_factor_y = res->bin_factor_y ? 347 res->bin_factor_y : 1; 348 return 0; 349 } 350 351 static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg, 352 int gain, int digitgain) 353 354 { 355 struct i2c_client *client = v4l2_get_subdevdata(sd); 356 u16 coarse_integration = (u16)coarse_itg; 357 int ret = 0; 358 u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0; 359 360 expo_coarse_h = coarse_integration >> 8; 361 expo_coarse_l = coarse_integration & 0xff; 362 363 ret = gc2235_write_reg(client, GC2235_8BIT, 364 GC2235_EXPOSURE_H, expo_coarse_h); 365 ret = gc2235_write_reg(client, GC2235_8BIT, 366 GC2235_EXPOSURE_L, expo_coarse_l); 367 368 if (gain <= 0x58) { 369 gain_val = 0x40; 370 gain_val2 = 0x58; 371 } else if (gain < 256) { 372 gain_val = 0x40; 373 gain_val2 = gain; 374 } else { 375 gain_val2 = 64 * gain / 256; 376 gain_val = 0xff; 377 } 378 379 ret = gc2235_write_reg(client, GC2235_8BIT, 380 GC2235_GLOBAL_GAIN, (u8)gain_val); 381 ret = gc2235_write_reg(client, GC2235_8BIT, 382 GC2235_PRE_GAIN, (u8)gain_val2); 383 384 return ret; 385 } 386 387 static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure, 388 int gain, int digitgain) 389 { 390 struct gc2235_device *dev = to_gc2235_sensor(sd); 391 int ret; 392 393 mutex_lock(&dev->input_lock); 394 ret = __gc2235_set_exposure(sd, exposure, gain, digitgain); 395 mutex_unlock(&dev->input_lock); 396 397 return ret; 398 } 399 400 static long gc2235_s_exposure(struct v4l2_subdev *sd, 401 struct atomisp_exposure *exposure) 402 { 403 int exp = exposure->integration_time[0]; 404 int gain = exposure->gain[0]; 405 int digitgain = exposure->gain[1]; 406 407 /* we should not accept the invalid value below. */ 408 if (gain == 0) { 409 struct i2c_client *client = v4l2_get_subdevdata(sd); 410 411 v4l2_err(client, "%s: invalid value\n", __func__); 412 return -EINVAL; 413 } 414 415 return gc2235_set_exposure(sd, exp, gain, digitgain); 416 } 417 418 static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 419 { 420 switch (cmd) { 421 case ATOMISP_IOC_S_EXPOSURE: 422 return gc2235_s_exposure(sd, arg); 423 default: 424 return -EINVAL; 425 } 426 return 0; 427 } 428 429 /* This returns the exposure time being used. This should only be used 430 * for filling in EXIF data, not for actual image processing. 431 */ 432 static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value) 433 { 434 struct i2c_client *client = v4l2_get_subdevdata(sd); 435 u16 reg_v, reg_v2; 436 int ret; 437 438 /* get exposure */ 439 ret = gc2235_read_reg(client, GC2235_8BIT, 440 GC2235_EXPOSURE_L, 441 ®_v); 442 if (ret) 443 goto err; 444 445 ret = gc2235_read_reg(client, GC2235_8BIT, 446 GC2235_EXPOSURE_H, 447 ®_v2); 448 if (ret) 449 goto err; 450 451 reg_v += reg_v2 << 8; 452 453 *value = reg_v; 454 err: 455 return ret; 456 } 457 458 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 459 { 460 struct gc2235_device *dev = 461 container_of(ctrl->handler, struct gc2235_device, ctrl_handler); 462 int ret = 0; 463 464 switch (ctrl->id) { 465 case V4L2_CID_EXPOSURE_ABSOLUTE: 466 ret = gc2235_q_exposure(&dev->sd, &ctrl->val); 467 break; 468 case V4L2_CID_FOCAL_ABSOLUTE: 469 ret = gc2235_g_focal(&dev->sd, &ctrl->val); 470 break; 471 case V4L2_CID_FNUMBER_ABSOLUTE: 472 ret = gc2235_g_fnumber(&dev->sd, &ctrl->val); 473 break; 474 case V4L2_CID_FNUMBER_RANGE: 475 ret = gc2235_g_fnumber_range(&dev->sd, &ctrl->val); 476 break; 477 default: 478 ret = -EINVAL; 479 } 480 481 return ret; 482 } 483 484 static const struct v4l2_ctrl_ops ctrl_ops = { 485 .g_volatile_ctrl = gc2235_g_volatile_ctrl 486 }; 487 488 static struct v4l2_ctrl_config gc2235_controls[] = { 489 { 490 .ops = &ctrl_ops, 491 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 492 .type = V4L2_CTRL_TYPE_INTEGER, 493 .name = "exposure", 494 .min = 0x0, 495 .max = 0xffff, 496 .step = 0x01, 497 .def = 0x00, 498 .flags = 0, 499 }, 500 { 501 .ops = &ctrl_ops, 502 .id = V4L2_CID_FOCAL_ABSOLUTE, 503 .type = V4L2_CTRL_TYPE_INTEGER, 504 .name = "focal length", 505 .min = GC2235_FOCAL_LENGTH_DEFAULT, 506 .max = GC2235_FOCAL_LENGTH_DEFAULT, 507 .step = 0x01, 508 .def = GC2235_FOCAL_LENGTH_DEFAULT, 509 .flags = 0, 510 }, 511 { 512 .ops = &ctrl_ops, 513 .id = V4L2_CID_FNUMBER_ABSOLUTE, 514 .type = V4L2_CTRL_TYPE_INTEGER, 515 .name = "f-number", 516 .min = GC2235_F_NUMBER_DEFAULT, 517 .max = GC2235_F_NUMBER_DEFAULT, 518 .step = 0x01, 519 .def = GC2235_F_NUMBER_DEFAULT, 520 .flags = 0, 521 }, 522 { 523 .ops = &ctrl_ops, 524 .id = V4L2_CID_FNUMBER_RANGE, 525 .type = V4L2_CTRL_TYPE_INTEGER, 526 .name = "f-number range", 527 .min = GC2235_F_NUMBER_RANGE, 528 .max = GC2235_F_NUMBER_RANGE, 529 .step = 0x01, 530 .def = GC2235_F_NUMBER_RANGE, 531 .flags = 0, 532 }, 533 }; 534 535 static int __gc2235_init(struct v4l2_subdev *sd) 536 { 537 struct i2c_client *client = v4l2_get_subdevdata(sd); 538 539 /* restore settings */ 540 gc2235_res = gc2235_res_preview; 541 N_RES = N_RES_PREVIEW; 542 543 return gc2235_write_reg_array(client, gc2235_init_settings); 544 } 545 546 static int is_init; 547 548 static int power_ctrl(struct v4l2_subdev *sd, bool flag) 549 { 550 int ret = -1; 551 struct gc2235_device *dev = to_gc2235_sensor(sd); 552 553 if (!dev || !dev->platform_data) 554 return -ENODEV; 555 556 if (flag) { 557 ret = dev->platform_data->v1p8_ctrl(sd, 1); 558 usleep_range(60, 90); 559 if (ret == 0) 560 ret |= dev->platform_data->v2p8_ctrl(sd, 1); 561 } else { 562 ret = dev->platform_data->v1p8_ctrl(sd, 0); 563 ret |= dev->platform_data->v2p8_ctrl(sd, 0); 564 } 565 return ret; 566 } 567 568 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 569 { 570 struct gc2235_device *dev = to_gc2235_sensor(sd); 571 int ret = -1; 572 573 if (!dev || !dev->platform_data) 574 return -ENODEV; 575 576 ret |= dev->platform_data->gpio1_ctrl(sd, !flag); 577 usleep_range(60, 90); 578 return dev->platform_data->gpio0_ctrl(sd, flag); 579 } 580 581 static int power_up(struct v4l2_subdev *sd) 582 { 583 struct gc2235_device *dev = to_gc2235_sensor(sd); 584 struct i2c_client *client = v4l2_get_subdevdata(sd); 585 int ret; 586 587 if (!dev->platform_data) { 588 dev_err(&client->dev, 589 "no camera_sensor_platform_data"); 590 return -ENODEV; 591 } 592 /* power control */ 593 ret = power_ctrl(sd, 1); 594 if (ret) 595 goto fail_power; 596 597 /* according to DS, at least 5ms is needed between DOVDD and PWDN */ 598 usleep_range(5000, 6000); 599 600 ret = dev->platform_data->flisclk_ctrl(sd, 1); 601 if (ret) 602 goto fail_clk; 603 usleep_range(5000, 6000); 604 605 /* gpio ctrl */ 606 ret = gpio_ctrl(sd, 1); 607 if (ret) { 608 ret = gpio_ctrl(sd, 1); 609 if (ret) 610 goto fail_power; 611 } 612 613 msleep(5); 614 return 0; 615 616 fail_clk: 617 gpio_ctrl(sd, 0); 618 fail_power: 619 power_ctrl(sd, 0); 620 dev_err(&client->dev, "sensor power-up failed\n"); 621 622 return ret; 623 } 624 625 static int power_down(struct v4l2_subdev *sd) 626 { 627 struct gc2235_device *dev = to_gc2235_sensor(sd); 628 struct i2c_client *client = v4l2_get_subdevdata(sd); 629 int ret = 0; 630 631 if (!dev->platform_data) { 632 dev_err(&client->dev, 633 "no camera_sensor_platform_data"); 634 return -ENODEV; 635 } 636 /* gpio ctrl */ 637 ret = gpio_ctrl(sd, 0); 638 if (ret) { 639 ret = gpio_ctrl(sd, 0); 640 if (ret) 641 dev_err(&client->dev, "gpio failed 2\n"); 642 } 643 644 ret = dev->platform_data->flisclk_ctrl(sd, 0); 645 if (ret) 646 dev_err(&client->dev, "flisclk failed\n"); 647 648 /* power control */ 649 ret = power_ctrl(sd, 0); 650 if (ret) 651 dev_err(&client->dev, "vprog failed.\n"); 652 653 return ret; 654 } 655 656 static int gc2235_s_power(struct v4l2_subdev *sd, int on) 657 { 658 int ret; 659 660 if (on == 0) 661 ret = power_down(sd); 662 else { 663 ret = power_up(sd); 664 if (!ret) 665 ret = __gc2235_init(sd); 666 is_init = 1; 667 } 668 return ret; 669 } 670 671 /* 672 * distance - calculate the distance 673 * @res: resolution 674 * @w: width 675 * @h: height 676 * 677 * Get the gap between resolution and w/h. 678 * res->width/height smaller than w/h wouldn't be considered. 679 * Returns the value of gap or -1 if fail. 680 */ 681 #define LARGEST_ALLOWED_RATIO_MISMATCH 800 682 static int distance(struct gc2235_resolution *res, u32 w, u32 h) 683 { 684 unsigned int w_ratio = (res->width << 13) / w; 685 unsigned int h_ratio; 686 int match; 687 688 if (h == 0) 689 return -1; 690 h_ratio = (res->height << 13) / h; 691 if (h_ratio == 0) 692 return -1; 693 match = abs(((w_ratio << 13) / h_ratio) - 8192); 694 695 if ((w_ratio < 8192) || (h_ratio < 8192) || 696 (match > LARGEST_ALLOWED_RATIO_MISMATCH)) 697 return -1; 698 699 return w_ratio + h_ratio; 700 } 701 702 /* Return the nearest higher resolution index */ 703 static int nearest_resolution_index(int w, int h) 704 { 705 int i; 706 int idx = -1; 707 int dist; 708 int min_dist = INT_MAX; 709 struct gc2235_resolution *tmp_res = NULL; 710 711 for (i = 0; i < N_RES; i++) { 712 tmp_res = &gc2235_res[i]; 713 dist = distance(tmp_res, w, h); 714 if (dist == -1) 715 continue; 716 if (dist < min_dist) { 717 min_dist = dist; 718 idx = i; 719 } 720 } 721 722 return idx; 723 } 724 725 static int get_resolution_index(int w, int h) 726 { 727 int i; 728 729 for (i = 0; i < N_RES; i++) { 730 if (w != gc2235_res[i].width) 731 continue; 732 if (h != gc2235_res[i].height) 733 continue; 734 735 return i; 736 } 737 738 return -1; 739 } 740 741 static int startup(struct v4l2_subdev *sd) 742 { 743 struct gc2235_device *dev = to_gc2235_sensor(sd); 744 struct i2c_client *client = v4l2_get_subdevdata(sd); 745 int ret = 0; 746 747 if (is_init == 0) { 748 /* force gc2235 to do a reset in res change, otherwise it 749 * can not output normal after switching res. and it is not 750 * necessary for first time run up after power on, for the sack 751 * of performance 752 */ 753 power_down(sd); 754 power_up(sd); 755 gc2235_write_reg_array(client, gc2235_init_settings); 756 } 757 758 ret = gc2235_write_reg_array(client, gc2235_res[dev->fmt_idx].regs); 759 if (ret) { 760 dev_err(&client->dev, "gc2235 write register err.\n"); 761 return ret; 762 } 763 is_init = 0; 764 765 return ret; 766 } 767 768 static int gc2235_set_fmt(struct v4l2_subdev *sd, 769 struct v4l2_subdev_pad_config *cfg, 770 struct v4l2_subdev_format *format) 771 { 772 struct v4l2_mbus_framefmt *fmt = &format->format; 773 struct gc2235_device *dev = to_gc2235_sensor(sd); 774 struct i2c_client *client = v4l2_get_subdevdata(sd); 775 struct camera_mipi_info *gc2235_info = NULL; 776 int ret = 0; 777 int idx; 778 779 gc2235_info = v4l2_get_subdev_hostdata(sd); 780 if (!gc2235_info) 781 return -EINVAL; 782 if (format->pad) 783 return -EINVAL; 784 if (!fmt) 785 return -EINVAL; 786 mutex_lock(&dev->input_lock); 787 idx = nearest_resolution_index(fmt->width, fmt->height); 788 if (idx == -1) { 789 /* return the largest resolution */ 790 fmt->width = gc2235_res[N_RES - 1].width; 791 fmt->height = gc2235_res[N_RES - 1].height; 792 } else { 793 fmt->width = gc2235_res[idx].width; 794 fmt->height = gc2235_res[idx].height; 795 } 796 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; 797 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 798 cfg->try_fmt = *fmt; 799 mutex_unlock(&dev->input_lock); 800 return 0; 801 } 802 803 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height); 804 if (dev->fmt_idx == -1) { 805 dev_err(&client->dev, "get resolution fail\n"); 806 mutex_unlock(&dev->input_lock); 807 return -EINVAL; 808 } 809 810 ret = startup(sd); 811 if (ret) { 812 dev_err(&client->dev, "gc2235 startup err\n"); 813 goto err; 814 } 815 816 ret = gc2235_get_intg_factor(client, gc2235_info, 817 &gc2235_res[dev->fmt_idx]); 818 if (ret) 819 dev_err(&client->dev, "failed to get integration_factor\n"); 820 821 err: 822 mutex_unlock(&dev->input_lock); 823 return ret; 824 } 825 826 static int gc2235_get_fmt(struct v4l2_subdev *sd, 827 struct v4l2_subdev_pad_config *cfg, 828 struct v4l2_subdev_format *format) 829 { 830 struct v4l2_mbus_framefmt *fmt = &format->format; 831 struct gc2235_device *dev = to_gc2235_sensor(sd); 832 833 if (format->pad) 834 return -EINVAL; 835 836 if (!fmt) 837 return -EINVAL; 838 839 fmt->width = gc2235_res[dev->fmt_idx].width; 840 fmt->height = gc2235_res[dev->fmt_idx].height; 841 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; 842 843 return 0; 844 } 845 846 static int gc2235_detect(struct i2c_client *client) 847 { 848 struct i2c_adapter *adapter = client->adapter; 849 u16 high, low; 850 int ret; 851 u16 id; 852 853 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 854 return -ENODEV; 855 856 ret = gc2235_read_reg(client, GC2235_8BIT, 857 GC2235_SENSOR_ID_H, &high); 858 if (ret) { 859 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high); 860 return -ENODEV; 861 } 862 ret = gc2235_read_reg(client, GC2235_8BIT, 863 GC2235_SENSOR_ID_L, &low); 864 id = ((high << 8) | low); 865 866 if (id != GC2235_ID) { 867 dev_err(&client->dev, "sensor ID error, 0x%x\n", id); 868 return -ENODEV; 869 } 870 871 dev_info(&client->dev, "detect gc2235 success\n"); 872 return 0; 873 } 874 875 static int gc2235_s_stream(struct v4l2_subdev *sd, int enable) 876 { 877 struct gc2235_device *dev = to_gc2235_sensor(sd); 878 struct i2c_client *client = v4l2_get_subdevdata(sd); 879 int ret; 880 881 mutex_lock(&dev->input_lock); 882 883 if (enable) 884 ret = gc2235_write_reg_array(client, gc2235_stream_on); 885 else 886 ret = gc2235_write_reg_array(client, gc2235_stream_off); 887 888 mutex_unlock(&dev->input_lock); 889 return ret; 890 } 891 892 static int gc2235_s_config(struct v4l2_subdev *sd, 893 int irq, void *platform_data) 894 { 895 struct gc2235_device *dev = to_gc2235_sensor(sd); 896 struct i2c_client *client = v4l2_get_subdevdata(sd); 897 int ret = 0; 898 899 if (!platform_data) 900 return -ENODEV; 901 902 dev->platform_data = 903 (struct camera_sensor_platform_data *)platform_data; 904 905 mutex_lock(&dev->input_lock); 906 /* power off the module, then power on it in future 907 * as first power on by board may not fulfill the 908 * power on sequqence needed by the module 909 */ 910 ret = power_down(sd); 911 if (ret) { 912 dev_err(&client->dev, "gc2235 power-off err.\n"); 913 goto fail_power_off; 914 } 915 916 ret = power_up(sd); 917 if (ret) { 918 dev_err(&client->dev, "gc2235 power-up err.\n"); 919 goto fail_power_on; 920 } 921 922 ret = dev->platform_data->csi_cfg(sd, 1); 923 if (ret) 924 goto fail_csi_cfg; 925 926 /* config & detect sensor */ 927 ret = gc2235_detect(client); 928 if (ret) { 929 dev_err(&client->dev, "gc2235_detect err s_config.\n"); 930 goto fail_csi_cfg; 931 } 932 933 /* turn off sensor, after probed */ 934 ret = power_down(sd); 935 if (ret) { 936 dev_err(&client->dev, "gc2235 power-off err.\n"); 937 goto fail_csi_cfg; 938 } 939 mutex_unlock(&dev->input_lock); 940 941 return 0; 942 943 fail_csi_cfg: 944 dev->platform_data->csi_cfg(sd, 0); 945 fail_power_on: 946 power_down(sd); 947 dev_err(&client->dev, "sensor power-gating failed\n"); 948 fail_power_off: 949 mutex_unlock(&dev->input_lock); 950 return ret; 951 } 952 953 static int gc2235_g_frame_interval(struct v4l2_subdev *sd, 954 struct v4l2_subdev_frame_interval *interval) 955 { 956 struct gc2235_device *dev = to_gc2235_sensor(sd); 957 958 interval->interval.numerator = 1; 959 interval->interval.denominator = gc2235_res[dev->fmt_idx].fps; 960 961 return 0; 962 } 963 964 static int gc2235_enum_mbus_code(struct v4l2_subdev *sd, 965 struct v4l2_subdev_pad_config *cfg, 966 struct v4l2_subdev_mbus_code_enum *code) 967 { 968 if (code->index >= MAX_FMTS) 969 return -EINVAL; 970 971 code->code = MEDIA_BUS_FMT_SBGGR10_1X10; 972 return 0; 973 } 974 975 static int gc2235_enum_frame_size(struct v4l2_subdev *sd, 976 struct v4l2_subdev_pad_config *cfg, 977 struct v4l2_subdev_frame_size_enum *fse) 978 { 979 int index = fse->index; 980 981 if (index >= N_RES) 982 return -EINVAL; 983 984 fse->min_width = gc2235_res[index].width; 985 fse->min_height = gc2235_res[index].height; 986 fse->max_width = gc2235_res[index].width; 987 fse->max_height = gc2235_res[index].height; 988 989 return 0; 990 } 991 992 static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) 993 { 994 struct gc2235_device *dev = to_gc2235_sensor(sd); 995 996 mutex_lock(&dev->input_lock); 997 *frames = gc2235_res[dev->fmt_idx].skip_frames; 998 mutex_unlock(&dev->input_lock); 999 1000 return 0; 1001 } 1002 1003 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = { 1004 .g_skip_frames = gc2235_g_skip_frames, 1005 }; 1006 1007 static const struct v4l2_subdev_video_ops gc2235_video_ops = { 1008 .s_stream = gc2235_s_stream, 1009 .g_frame_interval = gc2235_g_frame_interval, 1010 }; 1011 1012 static const struct v4l2_subdev_core_ops gc2235_core_ops = { 1013 .s_power = gc2235_s_power, 1014 .ioctl = gc2235_ioctl, 1015 }; 1016 1017 static const struct v4l2_subdev_pad_ops gc2235_pad_ops = { 1018 .enum_mbus_code = gc2235_enum_mbus_code, 1019 .enum_frame_size = gc2235_enum_frame_size, 1020 .get_fmt = gc2235_get_fmt, 1021 .set_fmt = gc2235_set_fmt, 1022 }; 1023 1024 static const struct v4l2_subdev_ops gc2235_ops = { 1025 .core = &gc2235_core_ops, 1026 .video = &gc2235_video_ops, 1027 .pad = &gc2235_pad_ops, 1028 .sensor = &gc2235_sensor_ops, 1029 }; 1030 1031 static int gc2235_remove(struct i2c_client *client) 1032 { 1033 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1034 struct gc2235_device *dev = to_gc2235_sensor(sd); 1035 1036 dev_dbg(&client->dev, "gc2235_remove...\n"); 1037 1038 dev->platform_data->csi_cfg(sd, 0); 1039 1040 v4l2_device_unregister_subdev(sd); 1041 media_entity_cleanup(&dev->sd.entity); 1042 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1043 kfree(dev); 1044 1045 return 0; 1046 } 1047 1048 static int gc2235_probe(struct i2c_client *client) 1049 { 1050 struct gc2235_device *dev; 1051 void *gcpdev; 1052 int ret; 1053 unsigned int i; 1054 acpi_handle handle; 1055 struct acpi_device *adev; 1056 1057 handle = ACPI_HANDLE(&client->dev); 1058 if (!handle || acpi_bus_get_device(handle, &adev)) { 1059 dev_err(&client->dev, "Error could not get ACPI device\n"); 1060 return -ENODEV; 1061 } 1062 pr_info("%s: ACPI detected it on bus ID=%s, HID=%s\n", 1063 __func__, acpi_device_bid(adev), acpi_device_hid(adev)); 1064 // FIXME: may need to release resources allocated by acpi_bus_get_device() 1065 1066 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1067 if (!dev) 1068 return -ENOMEM; 1069 1070 mutex_init(&dev->input_lock); 1071 1072 dev->fmt_idx = 0; 1073 v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops); 1074 1075 gcpdev = gmin_camera_platform_data(&dev->sd, 1076 ATOMISP_INPUT_FORMAT_RAW_10, 1077 atomisp_bayer_order_grbg); 1078 1079 ret = gc2235_s_config(&dev->sd, client->irq, gcpdev); 1080 if (ret) 1081 goto out_free; 1082 1083 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1084 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 1085 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10; 1086 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1087 ret = 1088 v4l2_ctrl_handler_init(&dev->ctrl_handler, 1089 ARRAY_SIZE(gc2235_controls)); 1090 if (ret) { 1091 gc2235_remove(client); 1092 return ret; 1093 } 1094 1095 for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++) 1096 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i], 1097 NULL); 1098 1099 if (dev->ctrl_handler.error) { 1100 gc2235_remove(client); 1101 return dev->ctrl_handler.error; 1102 } 1103 1104 /* Use same lock for controls as for everything else. */ 1105 dev->ctrl_handler.lock = &dev->input_lock; 1106 dev->sd.ctrl_handler = &dev->ctrl_handler; 1107 1108 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 1109 if (ret) 1110 gc2235_remove(client); 1111 1112 return atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA); 1113 1114 out_free: 1115 v4l2_device_unregister_subdev(&dev->sd); 1116 kfree(dev); 1117 1118 return ret; 1119 } 1120 1121 static const struct acpi_device_id gc2235_acpi_match[] = { 1122 { "INT33F8" }, 1123 {}, 1124 }; 1125 MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match); 1126 1127 static struct i2c_driver gc2235_driver = { 1128 .driver = { 1129 .name = "gc2235", 1130 .acpi_match_table = gc2235_acpi_match, 1131 }, 1132 .probe_new = gc2235_probe, 1133 .remove = gc2235_remove, 1134 }; 1135 module_i2c_driver(gc2235_driver); 1136 1137 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>"); 1138 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors"); 1139 MODULE_LICENSE("GPL"); 1140