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