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