1 /* 2 * Support for GalaxyCore GC0310 VGA camera sensor. 3 * 4 * Copyright (c) 2013 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 <linux/io.h> 32 #include "../include/linux/atomisp_gmin_platform.h" 33 34 #include "gc0310.h" 35 36 /* i2c read/write stuff */ 37 static int gc0310_read_reg(struct i2c_client *client, 38 u16 data_length, u8 reg, u8 *val) 39 { 40 int err; 41 struct i2c_msg msg[2]; 42 unsigned char data[1]; 43 44 if (!client->adapter) { 45 dev_err(&client->dev, "%s error, no client->adapter\n", 46 __func__); 47 return -ENODEV; 48 } 49 50 if (data_length != GC0310_8BIT) { 51 dev_err(&client->dev, "%s error, invalid data length\n", 52 __func__); 53 return -EINVAL; 54 } 55 56 memset(msg, 0, sizeof(msg)); 57 58 msg[0].addr = client->addr; 59 msg[0].flags = 0; 60 msg[0].len = I2C_MSG_LENGTH; 61 msg[0].buf = data; 62 63 /* high byte goes out first */ 64 data[0] = (u8)(reg & 0xff); 65 66 msg[1].addr = client->addr; 67 msg[1].len = data_length; 68 msg[1].flags = I2C_M_RD; 69 msg[1].buf = data; 70 71 err = i2c_transfer(client->adapter, msg, 2); 72 if (err != 2) { 73 if (err >= 0) 74 err = -EIO; 75 dev_err(&client->dev, 76 "read from offset 0x%x error %d", reg, err); 77 return err; 78 } 79 80 *val = 0; 81 /* high byte comes first */ 82 if (data_length == GC0310_8BIT) 83 *val = (u8)data[0]; 84 85 return 0; 86 } 87 88 static int gc0310_i2c_write(struct i2c_client *client, u16 len, u8 *data) 89 { 90 struct i2c_msg msg; 91 const int num_msg = 1; 92 int ret; 93 94 msg.addr = client->addr; 95 msg.flags = 0; 96 msg.len = len; 97 msg.buf = data; 98 ret = i2c_transfer(client->adapter, &msg, 1); 99 100 return ret == num_msg ? 0 : -EIO; 101 } 102 103 static int gc0310_write_reg(struct i2c_client *client, u16 data_length, 104 u8 reg, u8 val) 105 { 106 int ret; 107 unsigned char data[2] = {0}; 108 u8 *wreg = (u8 *)data; 109 const u16 len = data_length + sizeof(u8); /* 8-bit address + data */ 110 111 if (data_length != GC0310_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 *wreg = (u8)(reg & 0xff); 119 120 if (data_length == GC0310_8BIT) 121 data[1] = (u8)(val); 122 123 ret = gc0310_i2c_write(client, len, data); 124 if (ret) 125 dev_err(&client->dev, 126 "write error: wrote 0x%x to offset 0x%x error %d", 127 val, reg, ret); 128 129 return ret; 130 } 131 132 /* 133 * gc0310_write_reg_array - Initializes a list of GC0310 registers 134 * @client: i2c driver client structure 135 * @reglist: list of registers to be written 136 * 137 * This function initializes a list of registers. When consecutive addresses 138 * are found in a row on the list, this function creates a buffer and sends 139 * consecutive data in a single i2c_transfer(). 140 * 141 * __gc0310_flush_reg_array, __gc0310_buf_reg_array() and 142 * __gc0310_write_reg_is_consecutive() are internal functions to 143 * gc0310_write_reg_array_fast() and should be not used anywhere else. 144 * 145 */ 146 147 static int __gc0310_flush_reg_array(struct i2c_client *client, 148 struct gc0310_write_ctrl *ctrl) 149 { 150 u16 size; 151 152 if (ctrl->index == 0) 153 return 0; 154 155 size = sizeof(u8) + ctrl->index; /* 8-bit address + data */ 156 ctrl->buffer.addr = (u8)(ctrl->buffer.addr); 157 ctrl->index = 0; 158 159 return gc0310_i2c_write(client, size, (u8 *)&ctrl->buffer); 160 } 161 162 static int __gc0310_buf_reg_array(struct i2c_client *client, 163 struct gc0310_write_ctrl *ctrl, 164 const struct gc0310_reg *next) 165 { 166 int size; 167 168 switch (next->type) { 169 case GC0310_8BIT: 170 size = 1; 171 ctrl->buffer.data[ctrl->index] = (u8)next->val; 172 break; 173 default: 174 return -EINVAL; 175 } 176 177 /* When first item is added, we need to store its starting address */ 178 if (ctrl->index == 0) 179 ctrl->buffer.addr = next->reg; 180 181 ctrl->index += size; 182 183 /* 184 * Buffer cannot guarantee free space for u32? Better flush it to avoid 185 * possible lack of memory for next item. 186 */ 187 if (ctrl->index + sizeof(u8) >= GC0310_MAX_WRITE_BUF_SIZE) 188 return __gc0310_flush_reg_array(client, ctrl); 189 190 return 0; 191 } 192 193 static int __gc0310_write_reg_is_consecutive(struct i2c_client *client, 194 struct gc0310_write_ctrl *ctrl, 195 const struct gc0310_reg *next) 196 { 197 if (ctrl->index == 0) 198 return 1; 199 200 return ctrl->buffer.addr + ctrl->index == next->reg; 201 } 202 203 static int gc0310_write_reg_array(struct i2c_client *client, 204 const struct gc0310_reg *reglist) 205 { 206 const struct gc0310_reg *next = reglist; 207 struct gc0310_write_ctrl ctrl; 208 int err; 209 210 ctrl.index = 0; 211 for (; next->type != GC0310_TOK_TERM; next++) { 212 switch (next->type & GC0310_TOK_MASK) { 213 case GC0310_TOK_DELAY: 214 err = __gc0310_flush_reg_array(client, &ctrl); 215 if (err) 216 return err; 217 msleep(next->val); 218 break; 219 default: 220 /* 221 * If next address is not consecutive, data needs to be 222 * flushed before proceed. 223 */ 224 if (!__gc0310_write_reg_is_consecutive(client, &ctrl, 225 next)) { 226 err = __gc0310_flush_reg_array(client, &ctrl); 227 if (err) 228 return err; 229 } 230 err = __gc0310_buf_reg_array(client, &ctrl, next); 231 if (err) { 232 dev_err(&client->dev, "%s: write error, aborted\n", 233 __func__); 234 return err; 235 } 236 break; 237 } 238 } 239 240 return __gc0310_flush_reg_array(client, &ctrl); 241 } 242 243 static int gc0310_g_focal(struct v4l2_subdev *sd, s32 *val) 244 { 245 *val = (GC0310_FOCAL_LENGTH_NUM << 16) | GC0310_FOCAL_LENGTH_DEM; 246 return 0; 247 } 248 249 static int gc0310_g_fnumber(struct v4l2_subdev *sd, s32 *val) 250 { 251 /*const f number for imx*/ 252 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 16) | GC0310_F_NUMBER_DEM; 253 return 0; 254 } 255 256 static int gc0310_g_fnumber_range(struct v4l2_subdev *sd, s32 *val) 257 { 258 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 24) | 259 (GC0310_F_NUMBER_DEM << 16) | 260 (GC0310_F_NUMBER_DEFAULT_NUM << 8) | GC0310_F_NUMBER_DEM; 261 return 0; 262 } 263 264 static int gc0310_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val) 265 { 266 struct gc0310_device *dev = to_gc0310_sensor(sd); 267 268 *val = gc0310_res[dev->fmt_idx].bin_factor_x; 269 270 return 0; 271 } 272 273 static int gc0310_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val) 274 { 275 struct gc0310_device *dev = to_gc0310_sensor(sd); 276 277 *val = gc0310_res[dev->fmt_idx].bin_factor_y; 278 279 return 0; 280 } 281 282 static int gc0310_get_intg_factor(struct i2c_client *client, 283 struct camera_mipi_info *info, 284 const struct gc0310_resolution *res) 285 { 286 struct v4l2_subdev *sd = i2c_get_clientdata(client); 287 struct gc0310_device *dev = to_gc0310_sensor(sd); 288 struct atomisp_sensor_mode_data *buf = &info->data; 289 u16 val; 290 u8 reg_val; 291 int ret; 292 unsigned int hori_blanking; 293 unsigned int vert_blanking; 294 unsigned int sh_delay; 295 296 if (!info) 297 return -EINVAL; 298 299 /* pixel clock calculattion */ 300 dev->vt_pix_clk_freq_mhz = 14400000; // 16.8MHz 301 buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz; 302 pr_info("vt_pix_clk_freq_mhz=%d\n", buf->vt_pix_clk_freq_mhz); 303 304 /* get integration time */ 305 buf->coarse_integration_time_min = GC0310_COARSE_INTG_TIME_MIN; 306 buf->coarse_integration_time_max_margin = 307 GC0310_COARSE_INTG_TIME_MAX_MARGIN; 308 309 buf->fine_integration_time_min = GC0310_FINE_INTG_TIME_MIN; 310 buf->fine_integration_time_max_margin = 311 GC0310_FINE_INTG_TIME_MAX_MARGIN; 312 313 buf->fine_integration_time_def = GC0310_FINE_INTG_TIME_MIN; 314 buf->read_mode = res->bin_mode; 315 316 /* get the cropping and output resolution to ISP for this mode. */ 317 /* Getting crop_horizontal_start */ 318 ret = gc0310_read_reg(client, GC0310_8BIT, 319 GC0310_H_CROP_START_H, ®_val); 320 if (ret) 321 return ret; 322 val = (reg_val & 0xFF) << 8; 323 ret = gc0310_read_reg(client, GC0310_8BIT, 324 GC0310_H_CROP_START_L, ®_val); 325 if (ret) 326 return ret; 327 buf->crop_horizontal_start = val | (reg_val & 0xFF); 328 pr_info("crop_horizontal_start=%d\n", buf->crop_horizontal_start); 329 330 /* Getting crop_vertical_start */ 331 ret = gc0310_read_reg(client, GC0310_8BIT, 332 GC0310_V_CROP_START_H, ®_val); 333 if (ret) 334 return ret; 335 val = (reg_val & 0xFF) << 8; 336 ret = gc0310_read_reg(client, GC0310_8BIT, 337 GC0310_V_CROP_START_L, ®_val); 338 if (ret) 339 return ret; 340 buf->crop_vertical_start = val | (reg_val & 0xFF); 341 pr_info("crop_vertical_start=%d\n", buf->crop_vertical_start); 342 343 /* Getting output_width */ 344 ret = gc0310_read_reg(client, GC0310_8BIT, 345 GC0310_H_OUTSIZE_H, ®_val); 346 if (ret) 347 return ret; 348 val = (reg_val & 0xFF) << 8; 349 ret = gc0310_read_reg(client, GC0310_8BIT, 350 GC0310_H_OUTSIZE_L, ®_val); 351 if (ret) 352 return ret; 353 buf->output_width = val | (reg_val & 0xFF); 354 pr_info("output_width=%d\n", buf->output_width); 355 356 /* Getting output_height */ 357 ret = gc0310_read_reg(client, GC0310_8BIT, 358 GC0310_V_OUTSIZE_H, ®_val); 359 if (ret) 360 return ret; 361 val = (reg_val & 0xFF) << 8; 362 ret = gc0310_read_reg(client, GC0310_8BIT, 363 GC0310_V_OUTSIZE_L, ®_val); 364 if (ret) 365 return ret; 366 buf->output_height = val | (reg_val & 0xFF); 367 pr_info("output_height=%d\n", buf->output_height); 368 369 buf->crop_horizontal_end = buf->crop_horizontal_start + buf->output_width - 1; 370 buf->crop_vertical_end = buf->crop_vertical_start + buf->output_height - 1; 371 pr_info("crop_horizontal_end=%d\n", buf->crop_horizontal_end); 372 pr_info("crop_vertical_end=%d\n", buf->crop_vertical_end); 373 374 /* Getting line_length_pck */ 375 ret = gc0310_read_reg(client, GC0310_8BIT, 376 GC0310_H_BLANKING_H, ®_val); 377 if (ret) 378 return ret; 379 val = (reg_val & 0xFF) << 8; 380 ret = gc0310_read_reg(client, GC0310_8BIT, 381 GC0310_H_BLANKING_L, ®_val); 382 if (ret) 383 return ret; 384 hori_blanking = val | (reg_val & 0xFF); 385 ret = gc0310_read_reg(client, GC0310_8BIT, 386 GC0310_SH_DELAY, ®_val); 387 if (ret) 388 return ret; 389 sh_delay = reg_val; 390 buf->line_length_pck = buf->output_width + hori_blanking + sh_delay + 4; 391 pr_info("hori_blanking=%d sh_delay=%d line_length_pck=%d\n", hori_blanking, 392 sh_delay, buf->line_length_pck); 393 394 /* Getting frame_length_lines */ 395 ret = gc0310_read_reg(client, GC0310_8BIT, 396 GC0310_V_BLANKING_H, ®_val); 397 if (ret) 398 return ret; 399 val = (reg_val & 0xFF) << 8; 400 ret = gc0310_read_reg(client, GC0310_8BIT, 401 GC0310_V_BLANKING_L, ®_val); 402 if (ret) 403 return ret; 404 vert_blanking = val | (reg_val & 0xFF); 405 buf->frame_length_lines = buf->output_height + vert_blanking; 406 pr_info("vert_blanking=%d frame_length_lines=%d\n", vert_blanking, 407 buf->frame_length_lines); 408 409 buf->binning_factor_x = res->bin_factor_x ? 410 res->bin_factor_x : 1; 411 buf->binning_factor_y = res->bin_factor_y ? 412 res->bin_factor_y : 1; 413 return 0; 414 } 415 416 static int gc0310_set_gain(struct v4l2_subdev *sd, int gain) 417 418 { 419 struct i2c_client *client = v4l2_get_subdevdata(sd); 420 int ret; 421 u8 again, dgain; 422 423 if (gain < 0x20) 424 gain = 0x20; 425 if (gain > 0x80) 426 gain = 0x80; 427 428 if (gain >= 0x20 && gain < 0x40) { 429 again = 0x0; /* sqrt(2) */ 430 dgain = gain; 431 } else { 432 again = 0x2; /* 2 * sqrt(2) */ 433 dgain = gain / 2; 434 } 435 436 pr_info("gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain); 437 438 /* set analog gain */ 439 ret = gc0310_write_reg(client, GC0310_8BIT, 440 GC0310_AGC_ADJ, again); 441 if (ret) 442 return ret; 443 444 /* set digital gain */ 445 ret = gc0310_write_reg(client, GC0310_8BIT, 446 GC0310_DGC_ADJ, dgain); 447 if (ret) 448 return ret; 449 450 return 0; 451 } 452 453 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg, 454 int gain, int digitgain) 455 456 { 457 struct i2c_client *client = v4l2_get_subdevdata(sd); 458 int ret; 459 460 pr_info("coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain); 461 462 /* set exposure */ 463 ret = gc0310_write_reg(client, GC0310_8BIT, 464 GC0310_AEC_PK_EXPO_L, 465 coarse_itg & 0xff); 466 if (ret) 467 return ret; 468 469 ret = gc0310_write_reg(client, GC0310_8BIT, 470 GC0310_AEC_PK_EXPO_H, 471 (coarse_itg >> 8) & 0x0f); 472 if (ret) 473 return ret; 474 475 ret = gc0310_set_gain(sd, gain); 476 if (ret) 477 return ret; 478 479 return ret; 480 } 481 482 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure, 483 int gain, int digitgain) 484 { 485 struct gc0310_device *dev = to_gc0310_sensor(sd); 486 int ret; 487 488 mutex_lock(&dev->input_lock); 489 ret = __gc0310_set_exposure(sd, exposure, gain, digitgain); 490 mutex_unlock(&dev->input_lock); 491 492 return ret; 493 } 494 495 static long gc0310_s_exposure(struct v4l2_subdev *sd, 496 struct atomisp_exposure *exposure) 497 { 498 int exp = exposure->integration_time[0]; 499 int gain = exposure->gain[0]; 500 int digitgain = exposure->gain[1]; 501 502 /* we should not accept the invalid value below. */ 503 if (gain == 0) { 504 struct i2c_client *client = v4l2_get_subdevdata(sd); 505 506 v4l2_err(client, "%s: invalid value\n", __func__); 507 return -EINVAL; 508 } 509 510 return gc0310_set_exposure(sd, exp, gain, digitgain); 511 } 512 513 /* TO DO */ 514 static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value) 515 { 516 return 0; 517 } 518 519 /* TO DO */ 520 static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value) 521 { 522 return 0; 523 } 524 525 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 526 { 527 switch (cmd) { 528 case ATOMISP_IOC_S_EXPOSURE: 529 return gc0310_s_exposure(sd, arg); 530 default: 531 return -EINVAL; 532 } 533 return 0; 534 } 535 536 /* This returns the exposure time being used. This should only be used 537 * for filling in EXIF data, not for actual image processing. 538 */ 539 static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value) 540 { 541 struct i2c_client *client = v4l2_get_subdevdata(sd); 542 u8 reg_v; 543 int ret; 544 545 /* get exposure */ 546 ret = gc0310_read_reg(client, GC0310_8BIT, 547 GC0310_AEC_PK_EXPO_L, 548 ®_v); 549 if (ret) 550 goto err; 551 552 *value = reg_v; 553 ret = gc0310_read_reg(client, GC0310_8BIT, 554 GC0310_AEC_PK_EXPO_H, 555 ®_v); 556 if (ret) 557 goto err; 558 559 *value = *value + (reg_v << 8); 560 err: 561 return ret; 562 } 563 564 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl) 565 { 566 struct gc0310_device *dev = 567 container_of(ctrl->handler, struct gc0310_device, ctrl_handler); 568 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd); 569 int ret = 0; 570 571 switch (ctrl->id) { 572 case V4L2_CID_VFLIP: 573 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n", 574 __func__, ctrl->val); 575 ret = gc0310_v_flip(&dev->sd, ctrl->val); 576 break; 577 case V4L2_CID_HFLIP: 578 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n", 579 __func__, ctrl->val); 580 ret = gc0310_h_flip(&dev->sd, ctrl->val); 581 break; 582 default: 583 ret = -EINVAL; 584 } 585 return ret; 586 } 587 588 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 589 { 590 struct gc0310_device *dev = 591 container_of(ctrl->handler, struct gc0310_device, ctrl_handler); 592 int ret = 0; 593 594 switch (ctrl->id) { 595 case V4L2_CID_EXPOSURE_ABSOLUTE: 596 ret = gc0310_q_exposure(&dev->sd, &ctrl->val); 597 break; 598 case V4L2_CID_FOCAL_ABSOLUTE: 599 ret = gc0310_g_focal(&dev->sd, &ctrl->val); 600 break; 601 case V4L2_CID_FNUMBER_ABSOLUTE: 602 ret = gc0310_g_fnumber(&dev->sd, &ctrl->val); 603 break; 604 case V4L2_CID_FNUMBER_RANGE: 605 ret = gc0310_g_fnumber_range(&dev->sd, &ctrl->val); 606 break; 607 case V4L2_CID_BIN_FACTOR_HORZ: 608 ret = gc0310_g_bin_factor_x(&dev->sd, &ctrl->val); 609 break; 610 case V4L2_CID_BIN_FACTOR_VERT: 611 ret = gc0310_g_bin_factor_y(&dev->sd, &ctrl->val); 612 break; 613 default: 614 ret = -EINVAL; 615 } 616 617 return ret; 618 } 619 620 static const struct v4l2_ctrl_ops ctrl_ops = { 621 .s_ctrl = gc0310_s_ctrl, 622 .g_volatile_ctrl = gc0310_g_volatile_ctrl 623 }; 624 625 static const struct v4l2_ctrl_config gc0310_controls[] = { 626 { 627 .ops = &ctrl_ops, 628 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 629 .type = V4L2_CTRL_TYPE_INTEGER, 630 .name = "exposure", 631 .min = 0x0, 632 .max = 0xffff, 633 .step = 0x01, 634 .def = 0x00, 635 .flags = 0, 636 }, 637 { 638 .ops = &ctrl_ops, 639 .id = V4L2_CID_VFLIP, 640 .type = V4L2_CTRL_TYPE_BOOLEAN, 641 .name = "Flip", 642 .min = 0, 643 .max = 1, 644 .step = 1, 645 .def = 0, 646 }, 647 { 648 .ops = &ctrl_ops, 649 .id = V4L2_CID_HFLIP, 650 .type = V4L2_CTRL_TYPE_BOOLEAN, 651 .name = "Mirror", 652 .min = 0, 653 .max = 1, 654 .step = 1, 655 .def = 0, 656 }, 657 { 658 .ops = &ctrl_ops, 659 .id = V4L2_CID_FOCAL_ABSOLUTE, 660 .type = V4L2_CTRL_TYPE_INTEGER, 661 .name = "focal length", 662 .min = GC0310_FOCAL_LENGTH_DEFAULT, 663 .max = GC0310_FOCAL_LENGTH_DEFAULT, 664 .step = 0x01, 665 .def = GC0310_FOCAL_LENGTH_DEFAULT, 666 .flags = 0, 667 }, 668 { 669 .ops = &ctrl_ops, 670 .id = V4L2_CID_FNUMBER_ABSOLUTE, 671 .type = V4L2_CTRL_TYPE_INTEGER, 672 .name = "f-number", 673 .min = GC0310_F_NUMBER_DEFAULT, 674 .max = GC0310_F_NUMBER_DEFAULT, 675 .step = 0x01, 676 .def = GC0310_F_NUMBER_DEFAULT, 677 .flags = 0, 678 }, 679 { 680 .ops = &ctrl_ops, 681 .id = V4L2_CID_FNUMBER_RANGE, 682 .type = V4L2_CTRL_TYPE_INTEGER, 683 .name = "f-number range", 684 .min = GC0310_F_NUMBER_RANGE, 685 .max = GC0310_F_NUMBER_RANGE, 686 .step = 0x01, 687 .def = GC0310_F_NUMBER_RANGE, 688 .flags = 0, 689 }, 690 { 691 .ops = &ctrl_ops, 692 .id = V4L2_CID_BIN_FACTOR_HORZ, 693 .type = V4L2_CTRL_TYPE_INTEGER, 694 .name = "horizontal binning factor", 695 .min = 0, 696 .max = GC0310_BIN_FACTOR_MAX, 697 .step = 1, 698 .def = 0, 699 .flags = 0, 700 }, 701 { 702 .ops = &ctrl_ops, 703 .id = V4L2_CID_BIN_FACTOR_VERT, 704 .type = V4L2_CTRL_TYPE_INTEGER, 705 .name = "vertical binning factor", 706 .min = 0, 707 .max = GC0310_BIN_FACTOR_MAX, 708 .step = 1, 709 .def = 0, 710 .flags = 0, 711 }, 712 }; 713 714 static int gc0310_init(struct v4l2_subdev *sd) 715 { 716 int ret; 717 struct i2c_client *client = v4l2_get_subdevdata(sd); 718 struct gc0310_device *dev = to_gc0310_sensor(sd); 719 720 pr_info("%s S\n", __func__); 721 mutex_lock(&dev->input_lock); 722 723 /* set initial registers */ 724 ret = gc0310_write_reg_array(client, gc0310_reset_register); 725 726 /* restore settings */ 727 gc0310_res = gc0310_res_preview; 728 N_RES = N_RES_PREVIEW; 729 730 mutex_unlock(&dev->input_lock); 731 732 pr_info("%s E\n", __func__); 733 return ret; 734 } 735 736 static int power_ctrl(struct v4l2_subdev *sd, bool flag) 737 { 738 int ret = 0; 739 struct gc0310_device *dev = to_gc0310_sensor(sd); 740 741 if (!dev || !dev->platform_data) 742 return -ENODEV; 743 744 if (flag) { 745 /* The upstream module driver (written to Crystal 746 * Cove) had this logic to pulse the rails low first. 747 * This appears to break things on the MRD7 with the 748 * X-Powers PMIC... 749 * 750 * ret = dev->platform_data->v1p8_ctrl(sd, 0); 751 * ret |= dev->platform_data->v2p8_ctrl(sd, 0); 752 * mdelay(50); 753 */ 754 ret |= dev->platform_data->v1p8_ctrl(sd, 1); 755 ret |= dev->platform_data->v2p8_ctrl(sd, 1); 756 usleep_range(10000, 15000); 757 } 758 759 if (!flag || ret) { 760 ret |= dev->platform_data->v1p8_ctrl(sd, 0); 761 ret |= dev->platform_data->v2p8_ctrl(sd, 0); 762 } 763 return ret; 764 } 765 766 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 767 { 768 int ret; 769 struct gc0310_device *dev = to_gc0310_sensor(sd); 770 771 if (!dev || !dev->platform_data) 772 return -ENODEV; 773 774 /* GPIO0 == "reset" (active low), GPIO1 == "power down" */ 775 if (flag) { 776 /* Pulse reset, then release power down */ 777 ret = dev->platform_data->gpio0_ctrl(sd, 0); 778 usleep_range(5000, 10000); 779 ret |= dev->platform_data->gpio0_ctrl(sd, 1); 780 usleep_range(10000, 15000); 781 ret |= dev->platform_data->gpio1_ctrl(sd, 0); 782 usleep_range(10000, 15000); 783 } else { 784 ret = dev->platform_data->gpio1_ctrl(sd, 1); 785 ret |= dev->platform_data->gpio0_ctrl(sd, 0); 786 } 787 return ret; 788 } 789 790 static int power_down(struct v4l2_subdev *sd); 791 792 static int power_up(struct v4l2_subdev *sd) 793 { 794 struct gc0310_device *dev = to_gc0310_sensor(sd); 795 struct i2c_client *client = v4l2_get_subdevdata(sd); 796 int ret; 797 798 pr_info("%s S\n", __func__); 799 if (!dev->platform_data) { 800 dev_err(&client->dev, 801 "no camera_sensor_platform_data"); 802 return -ENODEV; 803 } 804 805 /* power control */ 806 ret = power_ctrl(sd, 1); 807 if (ret) 808 goto fail_power; 809 810 /* flis clock control */ 811 ret = dev->platform_data->flisclk_ctrl(sd, 1); 812 if (ret) 813 goto fail_clk; 814 815 /* gpio ctrl */ 816 ret = gpio_ctrl(sd, 1); 817 if (ret) { 818 ret = gpio_ctrl(sd, 1); 819 if (ret) 820 goto fail_gpio; 821 } 822 823 msleep(100); 824 825 pr_info("%s E\n", __func__); 826 return 0; 827 828 fail_gpio: 829 dev->platform_data->flisclk_ctrl(sd, 0); 830 fail_clk: 831 power_ctrl(sd, 0); 832 fail_power: 833 dev_err(&client->dev, "sensor power-up failed\n"); 834 835 return ret; 836 } 837 838 static int power_down(struct v4l2_subdev *sd) 839 { 840 struct gc0310_device *dev = to_gc0310_sensor(sd); 841 struct i2c_client *client = v4l2_get_subdevdata(sd); 842 int ret = 0; 843 844 if (!dev->platform_data) { 845 dev_err(&client->dev, 846 "no camera_sensor_platform_data"); 847 return -ENODEV; 848 } 849 850 /* gpio ctrl */ 851 ret = gpio_ctrl(sd, 0); 852 if (ret) { 853 ret = gpio_ctrl(sd, 0); 854 if (ret) 855 dev_err(&client->dev, "gpio failed 2\n"); 856 } 857 858 ret = dev->platform_data->flisclk_ctrl(sd, 0); 859 if (ret) 860 dev_err(&client->dev, "flisclk failed\n"); 861 862 /* power control */ 863 ret = power_ctrl(sd, 0); 864 if (ret) 865 dev_err(&client->dev, "vprog failed.\n"); 866 867 return ret; 868 } 869 870 static int gc0310_s_power(struct v4l2_subdev *sd, int on) 871 { 872 int ret; 873 874 if (on == 0) 875 return power_down(sd); 876 else { 877 ret = power_up(sd); 878 if (!ret) 879 return gc0310_init(sd); 880 } 881 return ret; 882 } 883 884 /* 885 * distance - calculate the distance 886 * @res: resolution 887 * @w: width 888 * @h: height 889 * 890 * Get the gap between resolution and w/h. 891 * res->width/height smaller than w/h wouldn't be considered. 892 * Returns the value of gap or -1 if fail. 893 */ 894 #define LARGEST_ALLOWED_RATIO_MISMATCH 800 895 static int distance(struct gc0310_resolution *res, u32 w, u32 h) 896 { 897 unsigned int w_ratio = (res->width << 13) / w; 898 unsigned int h_ratio; 899 int match; 900 901 if (h == 0) 902 return -1; 903 h_ratio = (res->height << 13) / h; 904 if (h_ratio == 0) 905 return -1; 906 match = abs(((w_ratio << 13) / h_ratio) - 8192); 907 908 if ((w_ratio < 8192) || (h_ratio < 8192) || 909 (match > LARGEST_ALLOWED_RATIO_MISMATCH)) 910 return -1; 911 912 return w_ratio + h_ratio; 913 } 914 915 /* Return the nearest higher resolution index */ 916 static int nearest_resolution_index(int w, int h) 917 { 918 int i; 919 int idx = -1; 920 int dist; 921 int min_dist = INT_MAX; 922 struct gc0310_resolution *tmp_res = NULL; 923 924 for (i = 0; i < N_RES; i++) { 925 tmp_res = &gc0310_res[i]; 926 dist = distance(tmp_res, w, h); 927 if (dist == -1) 928 continue; 929 if (dist < min_dist) { 930 min_dist = dist; 931 idx = i; 932 } 933 } 934 935 return idx; 936 } 937 938 static int get_resolution_index(int w, int h) 939 { 940 int i; 941 942 for (i = 0; i < N_RES; i++) { 943 if (w != gc0310_res[i].width) 944 continue; 945 if (h != gc0310_res[i].height) 946 continue; 947 948 return i; 949 } 950 951 return -1; 952 } 953 954 /* TODO: remove it. */ 955 static int startup(struct v4l2_subdev *sd) 956 { 957 struct gc0310_device *dev = to_gc0310_sensor(sd); 958 struct i2c_client *client = v4l2_get_subdevdata(sd); 959 int ret = 0; 960 961 pr_info("%s S\n", __func__); 962 963 ret = gc0310_write_reg_array(client, gc0310_res[dev->fmt_idx].regs); 964 if (ret) { 965 dev_err(&client->dev, "gc0310 write register err.\n"); 966 return ret; 967 } 968 969 pr_info("%s E\n", __func__); 970 return ret; 971 } 972 973 static int gc0310_set_fmt(struct v4l2_subdev *sd, 974 struct v4l2_subdev_pad_config *cfg, 975 struct v4l2_subdev_format *format) 976 { 977 struct v4l2_mbus_framefmt *fmt = &format->format; 978 struct gc0310_device *dev = to_gc0310_sensor(sd); 979 struct i2c_client *client = v4l2_get_subdevdata(sd); 980 struct camera_mipi_info *gc0310_info = NULL; 981 int ret = 0; 982 int idx = 0; 983 984 pr_info("%s S\n", __func__); 985 986 if (format->pad) 987 return -EINVAL; 988 989 if (!fmt) 990 return -EINVAL; 991 992 gc0310_info = v4l2_get_subdev_hostdata(sd); 993 if (!gc0310_info) 994 return -EINVAL; 995 996 mutex_lock(&dev->input_lock); 997 998 idx = nearest_resolution_index(fmt->width, fmt->height); 999 if (idx == -1) { 1000 /* return the largest resolution */ 1001 fmt->width = gc0310_res[N_RES - 1].width; 1002 fmt->height = gc0310_res[N_RES - 1].height; 1003 } else { 1004 fmt->width = gc0310_res[idx].width; 1005 fmt->height = gc0310_res[idx].height; 1006 } 1007 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8; 1008 1009 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 1010 cfg->try_fmt = *fmt; 1011 mutex_unlock(&dev->input_lock); 1012 return 0; 1013 } 1014 1015 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height); 1016 if (dev->fmt_idx == -1) { 1017 dev_err(&client->dev, "get resolution fail\n"); 1018 mutex_unlock(&dev->input_lock); 1019 return -EINVAL; 1020 } 1021 1022 printk("%s: before gc0310_write_reg_array %s\n", __func__, 1023 gc0310_res[dev->fmt_idx].desc); 1024 ret = startup(sd); 1025 if (ret) { 1026 dev_err(&client->dev, "gc0310 startup err\n"); 1027 goto err; 1028 } 1029 1030 ret = gc0310_get_intg_factor(client, gc0310_info, 1031 &gc0310_res[dev->fmt_idx]); 1032 if (ret) { 1033 dev_err(&client->dev, "failed to get integration_factor\n"); 1034 goto err; 1035 } 1036 1037 pr_info("%s E\n", __func__); 1038 err: 1039 mutex_unlock(&dev->input_lock); 1040 return ret; 1041 } 1042 1043 static int gc0310_get_fmt(struct v4l2_subdev *sd, 1044 struct v4l2_subdev_pad_config *cfg, 1045 struct v4l2_subdev_format *format) 1046 { 1047 struct v4l2_mbus_framefmt *fmt = &format->format; 1048 struct gc0310_device *dev = to_gc0310_sensor(sd); 1049 1050 if (format->pad) 1051 return -EINVAL; 1052 1053 if (!fmt) 1054 return -EINVAL; 1055 1056 fmt->width = gc0310_res[dev->fmt_idx].width; 1057 fmt->height = gc0310_res[dev->fmt_idx].height; 1058 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8; 1059 1060 return 0; 1061 } 1062 1063 static int gc0310_detect(struct i2c_client *client) 1064 { 1065 struct i2c_adapter *adapter = client->adapter; 1066 u8 high, low; 1067 int ret; 1068 u16 id; 1069 1070 pr_info("%s S\n", __func__); 1071 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 1072 return -ENODEV; 1073 1074 ret = gc0310_read_reg(client, GC0310_8BIT, 1075 GC0310_SC_CMMN_CHIP_ID_H, &high); 1076 if (ret) { 1077 dev_err(&client->dev, "read sensor_id_high failed\n"); 1078 return -ENODEV; 1079 } 1080 ret = gc0310_read_reg(client, GC0310_8BIT, 1081 GC0310_SC_CMMN_CHIP_ID_L, &low); 1082 if (ret) { 1083 dev_err(&client->dev, "read sensor_id_low failed\n"); 1084 return -ENODEV; 1085 } 1086 id = ((((u16)high) << 8) | (u16)low); 1087 pr_info("sensor ID = 0x%x\n", id); 1088 1089 if (id != GC0310_ID) { 1090 dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id, 1091 GC0310_ID); 1092 return -ENODEV; 1093 } 1094 1095 dev_dbg(&client->dev, "detect gc0310 success\n"); 1096 1097 pr_info("%s E\n", __func__); 1098 1099 return 0; 1100 } 1101 1102 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable) 1103 { 1104 struct gc0310_device *dev = to_gc0310_sensor(sd); 1105 struct i2c_client *client = v4l2_get_subdevdata(sd); 1106 int ret; 1107 1108 pr_info("%s S enable=%d\n", __func__, enable); 1109 mutex_lock(&dev->input_lock); 1110 1111 if (enable) { 1112 /* enable per frame MIPI and sensor ctrl reset */ 1113 ret = gc0310_write_reg(client, GC0310_8BIT, 1114 0xFE, 0x30); 1115 if (ret) { 1116 mutex_unlock(&dev->input_lock); 1117 return ret; 1118 } 1119 } 1120 1121 ret = gc0310_write_reg(client, GC0310_8BIT, 1122 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3); 1123 if (ret) { 1124 mutex_unlock(&dev->input_lock); 1125 return ret; 1126 } 1127 1128 ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM, 1129 enable ? GC0310_START_STREAMING : 1130 GC0310_STOP_STREAMING); 1131 if (ret) { 1132 mutex_unlock(&dev->input_lock); 1133 return ret; 1134 } 1135 1136 ret = gc0310_write_reg(client, GC0310_8BIT, 1137 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0); 1138 if (ret) { 1139 mutex_unlock(&dev->input_lock); 1140 return ret; 1141 } 1142 1143 mutex_unlock(&dev->input_lock); 1144 pr_info("%s E\n", __func__); 1145 return ret; 1146 } 1147 1148 static int gc0310_s_config(struct v4l2_subdev *sd, 1149 int irq, void *platform_data) 1150 { 1151 struct gc0310_device *dev = to_gc0310_sensor(sd); 1152 struct i2c_client *client = v4l2_get_subdevdata(sd); 1153 int ret = 0; 1154 1155 pr_info("%s S\n", __func__); 1156 if (!platform_data) 1157 return -ENODEV; 1158 1159 dev->platform_data = 1160 (struct camera_sensor_platform_data *)platform_data; 1161 1162 mutex_lock(&dev->input_lock); 1163 /* power off the module, then power on it in future 1164 * as first power on by board may not fulfill the 1165 * power on sequqence needed by the module 1166 */ 1167 ret = power_down(sd); 1168 if (ret) { 1169 dev_err(&client->dev, "gc0310 power-off err.\n"); 1170 goto fail_power_off; 1171 } 1172 1173 ret = power_up(sd); 1174 if (ret) { 1175 dev_err(&client->dev, "gc0310 power-up err.\n"); 1176 goto fail_power_on; 1177 } 1178 1179 ret = dev->platform_data->csi_cfg(sd, 1); 1180 if (ret) 1181 goto fail_csi_cfg; 1182 1183 /* config & detect sensor */ 1184 ret = gc0310_detect(client); 1185 if (ret) { 1186 dev_err(&client->dev, "gc0310_detect err s_config.\n"); 1187 goto fail_csi_cfg; 1188 } 1189 1190 /* turn off sensor, after probed */ 1191 ret = power_down(sd); 1192 if (ret) { 1193 dev_err(&client->dev, "gc0310 power-off err.\n"); 1194 goto fail_csi_cfg; 1195 } 1196 mutex_unlock(&dev->input_lock); 1197 1198 pr_info("%s E\n", __func__); 1199 return 0; 1200 1201 fail_csi_cfg: 1202 dev->platform_data->csi_cfg(sd, 0); 1203 fail_power_on: 1204 power_down(sd); 1205 dev_err(&client->dev, "sensor power-gating failed\n"); 1206 fail_power_off: 1207 mutex_unlock(&dev->input_lock); 1208 return ret; 1209 } 1210 1211 static int gc0310_g_frame_interval(struct v4l2_subdev *sd, 1212 struct v4l2_subdev_frame_interval *interval) 1213 { 1214 struct gc0310_device *dev = to_gc0310_sensor(sd); 1215 1216 interval->interval.numerator = 1; 1217 interval->interval.denominator = gc0310_res[dev->fmt_idx].fps; 1218 1219 return 0; 1220 } 1221 1222 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd, 1223 struct v4l2_subdev_pad_config *cfg, 1224 struct v4l2_subdev_mbus_code_enum *code) 1225 { 1226 if (code->index >= MAX_FMTS) 1227 return -EINVAL; 1228 1229 code->code = MEDIA_BUS_FMT_SGRBG8_1X8; 1230 return 0; 1231 } 1232 1233 static int gc0310_enum_frame_size(struct v4l2_subdev *sd, 1234 struct v4l2_subdev_pad_config *cfg, 1235 struct v4l2_subdev_frame_size_enum *fse) 1236 { 1237 int index = fse->index; 1238 1239 if (index >= N_RES) 1240 return -EINVAL; 1241 1242 fse->min_width = gc0310_res[index].width; 1243 fse->min_height = gc0310_res[index].height; 1244 fse->max_width = gc0310_res[index].width; 1245 fse->max_height = gc0310_res[index].height; 1246 1247 return 0; 1248 } 1249 1250 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) 1251 { 1252 struct gc0310_device *dev = to_gc0310_sensor(sd); 1253 1254 mutex_lock(&dev->input_lock); 1255 *frames = gc0310_res[dev->fmt_idx].skip_frames; 1256 mutex_unlock(&dev->input_lock); 1257 1258 return 0; 1259 } 1260 1261 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = { 1262 .g_skip_frames = gc0310_g_skip_frames, 1263 }; 1264 1265 static const struct v4l2_subdev_video_ops gc0310_video_ops = { 1266 .s_stream = gc0310_s_stream, 1267 .g_frame_interval = gc0310_g_frame_interval, 1268 }; 1269 1270 static const struct v4l2_subdev_core_ops gc0310_core_ops = { 1271 .s_power = gc0310_s_power, 1272 .ioctl = gc0310_ioctl, 1273 }; 1274 1275 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = { 1276 .enum_mbus_code = gc0310_enum_mbus_code, 1277 .enum_frame_size = gc0310_enum_frame_size, 1278 .get_fmt = gc0310_get_fmt, 1279 .set_fmt = gc0310_set_fmt, 1280 }; 1281 1282 static const struct v4l2_subdev_ops gc0310_ops = { 1283 .core = &gc0310_core_ops, 1284 .video = &gc0310_video_ops, 1285 .pad = &gc0310_pad_ops, 1286 .sensor = &gc0310_sensor_ops, 1287 }; 1288 1289 static int gc0310_remove(struct i2c_client *client) 1290 { 1291 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1292 struct gc0310_device *dev = to_gc0310_sensor(sd); 1293 1294 dev_dbg(&client->dev, "gc0310_remove...\n"); 1295 1296 dev->platform_data->csi_cfg(sd, 0); 1297 1298 v4l2_device_unregister_subdev(sd); 1299 media_entity_cleanup(&dev->sd.entity); 1300 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1301 kfree(dev); 1302 1303 return 0; 1304 } 1305 1306 static int gc0310_probe(struct i2c_client *client) 1307 { 1308 struct gc0310_device *dev; 1309 int ret; 1310 void *pdata; 1311 unsigned int i; 1312 1313 pr_info("%s S\n", __func__); 1314 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1315 if (!dev) 1316 return -ENOMEM; 1317 1318 mutex_init(&dev->input_lock); 1319 1320 dev->fmt_idx = 0; 1321 v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops); 1322 1323 pdata = gmin_camera_platform_data(&dev->sd, 1324 ATOMISP_INPUT_FORMAT_RAW_8, 1325 atomisp_bayer_order_grbg); 1326 if (!pdata) { 1327 ret = -EINVAL; 1328 goto out_free; 1329 } 1330 1331 ret = gc0310_s_config(&dev->sd, client->irq, pdata); 1332 if (ret) 1333 goto out_free; 1334 1335 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA); 1336 if (ret) 1337 goto out_free; 1338 1339 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1340 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 1341 dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8; 1342 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1343 ret = 1344 v4l2_ctrl_handler_init(&dev->ctrl_handler, 1345 ARRAY_SIZE(gc0310_controls)); 1346 if (ret) { 1347 gc0310_remove(client); 1348 return ret; 1349 } 1350 1351 for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++) 1352 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i], 1353 NULL); 1354 1355 if (dev->ctrl_handler.error) { 1356 gc0310_remove(client); 1357 return dev->ctrl_handler.error; 1358 } 1359 1360 /* Use same lock for controls as for everything else. */ 1361 dev->ctrl_handler.lock = &dev->input_lock; 1362 dev->sd.ctrl_handler = &dev->ctrl_handler; 1363 1364 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 1365 if (ret) 1366 gc0310_remove(client); 1367 1368 pr_info("%s E\n", __func__); 1369 return ret; 1370 out_free: 1371 v4l2_device_unregister_subdev(&dev->sd); 1372 kfree(dev); 1373 return ret; 1374 } 1375 1376 static const struct acpi_device_id gc0310_acpi_match[] = { 1377 {"XXGC0310"}, 1378 {"INT0310"}, 1379 {}, 1380 }; 1381 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match); 1382 1383 static struct i2c_driver gc0310_driver = { 1384 .driver = { 1385 .name = "gc0310", 1386 .acpi_match_table = gc0310_acpi_match, 1387 }, 1388 .probe_new = gc0310_probe, 1389 .remove = gc0310_remove, 1390 }; 1391 module_i2c_driver(gc0310_driver); 1392 1393 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>"); 1394 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors"); 1395 MODULE_LICENSE("GPL"); 1396