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_set_gain(struct v4l2_subdev *sd, int gain) 263 264 { 265 struct i2c_client *client = v4l2_get_subdevdata(sd); 266 int ret; 267 u8 again, dgain; 268 269 if (gain < 0x20) 270 gain = 0x20; 271 if (gain > 0x80) 272 gain = 0x80; 273 274 if (gain >= 0x20 && gain < 0x40) { 275 again = 0x0; /* sqrt(2) */ 276 dgain = gain; 277 } else { 278 again = 0x2; /* 2 * sqrt(2) */ 279 dgain = gain / 2; 280 } 281 282 dev_dbg(&client->dev, "gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain); 283 284 /* set analog gain */ 285 ret = gc0310_write_reg(client, GC0310_8BIT, 286 GC0310_AGC_ADJ, again); 287 if (ret) 288 return ret; 289 290 /* set digital gain */ 291 ret = gc0310_write_reg(client, GC0310_8BIT, 292 GC0310_DGC_ADJ, dgain); 293 if (ret) 294 return ret; 295 296 return 0; 297 } 298 299 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg, 300 int gain, int digitgain) 301 302 { 303 struct i2c_client *client = v4l2_get_subdevdata(sd); 304 int ret; 305 306 dev_dbg(&client->dev, "coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain); 307 308 /* set exposure */ 309 ret = gc0310_write_reg(client, GC0310_8BIT, 310 GC0310_AEC_PK_EXPO_L, 311 coarse_itg & 0xff); 312 if (ret) 313 return ret; 314 315 ret = gc0310_write_reg(client, GC0310_8BIT, 316 GC0310_AEC_PK_EXPO_H, 317 (coarse_itg >> 8) & 0x0f); 318 if (ret) 319 return ret; 320 321 ret = gc0310_set_gain(sd, gain); 322 if (ret) 323 return ret; 324 325 return ret; 326 } 327 328 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure, 329 int gain, int digitgain) 330 { 331 struct gc0310_device *dev = to_gc0310_sensor(sd); 332 int ret; 333 334 mutex_lock(&dev->input_lock); 335 ret = __gc0310_set_exposure(sd, exposure, gain, digitgain); 336 mutex_unlock(&dev->input_lock); 337 338 return ret; 339 } 340 341 static long gc0310_s_exposure(struct v4l2_subdev *sd, 342 struct atomisp_exposure *exposure) 343 { 344 int exp = exposure->integration_time[0]; 345 int gain = exposure->gain[0]; 346 int digitgain = exposure->gain[1]; 347 348 /* we should not accept the invalid value below. */ 349 if (gain == 0) { 350 struct i2c_client *client = v4l2_get_subdevdata(sd); 351 352 v4l2_err(client, "%s: invalid value\n", __func__); 353 return -EINVAL; 354 } 355 356 return gc0310_set_exposure(sd, exp, gain, digitgain); 357 } 358 359 /* TO DO */ 360 static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value) 361 { 362 return 0; 363 } 364 365 /* TO DO */ 366 static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value) 367 { 368 return 0; 369 } 370 371 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 372 { 373 switch (cmd) { 374 case ATOMISP_IOC_S_EXPOSURE: 375 return gc0310_s_exposure(sd, arg); 376 default: 377 return -EINVAL; 378 } 379 return 0; 380 } 381 382 /* This returns the exposure time being used. This should only be used 383 * for filling in EXIF data, not for actual image processing. 384 */ 385 static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value) 386 { 387 struct i2c_client *client = v4l2_get_subdevdata(sd); 388 u8 reg_v; 389 int ret; 390 391 /* get exposure */ 392 ret = gc0310_read_reg(client, GC0310_8BIT, 393 GC0310_AEC_PK_EXPO_L, 394 ®_v); 395 if (ret) 396 goto err; 397 398 *value = reg_v; 399 ret = gc0310_read_reg(client, GC0310_8BIT, 400 GC0310_AEC_PK_EXPO_H, 401 ®_v); 402 if (ret) 403 goto err; 404 405 *value = *value + (reg_v << 8); 406 err: 407 return ret; 408 } 409 410 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl) 411 { 412 struct gc0310_device *dev = 413 container_of(ctrl->handler, struct gc0310_device, ctrl_handler); 414 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd); 415 int ret = 0; 416 417 switch (ctrl->id) { 418 case V4L2_CID_VFLIP: 419 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n", 420 __func__, ctrl->val); 421 ret = gc0310_v_flip(&dev->sd, ctrl->val); 422 break; 423 case V4L2_CID_HFLIP: 424 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n", 425 __func__, ctrl->val); 426 ret = gc0310_h_flip(&dev->sd, ctrl->val); 427 break; 428 default: 429 ret = -EINVAL; 430 } 431 return ret; 432 } 433 434 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 435 { 436 struct gc0310_device *dev = 437 container_of(ctrl->handler, struct gc0310_device, ctrl_handler); 438 int ret = 0; 439 440 switch (ctrl->id) { 441 case V4L2_CID_EXPOSURE_ABSOLUTE: 442 ret = gc0310_q_exposure(&dev->sd, &ctrl->val); 443 break; 444 case V4L2_CID_BIN_FACTOR_HORZ: 445 ret = gc0310_g_bin_factor_x(&dev->sd, &ctrl->val); 446 break; 447 case V4L2_CID_BIN_FACTOR_VERT: 448 ret = gc0310_g_bin_factor_y(&dev->sd, &ctrl->val); 449 break; 450 default: 451 ret = -EINVAL; 452 } 453 454 return ret; 455 } 456 457 static const struct v4l2_ctrl_ops ctrl_ops = { 458 .s_ctrl = gc0310_s_ctrl, 459 .g_volatile_ctrl = gc0310_g_volatile_ctrl 460 }; 461 462 static const struct v4l2_ctrl_config gc0310_controls[] = { 463 { 464 .ops = &ctrl_ops, 465 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 466 .type = V4L2_CTRL_TYPE_INTEGER, 467 .name = "exposure", 468 .min = 0x0, 469 .max = 0xffff, 470 .step = 0x01, 471 .def = 0x00, 472 .flags = 0, 473 }, 474 { 475 .ops = &ctrl_ops, 476 .id = V4L2_CID_VFLIP, 477 .type = V4L2_CTRL_TYPE_BOOLEAN, 478 .name = "Flip", 479 .min = 0, 480 .max = 1, 481 .step = 1, 482 .def = 0, 483 }, 484 { 485 .ops = &ctrl_ops, 486 .id = V4L2_CID_HFLIP, 487 .type = V4L2_CTRL_TYPE_BOOLEAN, 488 .name = "Mirror", 489 .min = 0, 490 .max = 1, 491 .step = 1, 492 .def = 0, 493 }, 494 { 495 .ops = &ctrl_ops, 496 .id = V4L2_CID_BIN_FACTOR_HORZ, 497 .type = V4L2_CTRL_TYPE_INTEGER, 498 .name = "horizontal binning factor", 499 .min = 0, 500 .max = GC0310_BIN_FACTOR_MAX, 501 .step = 1, 502 .def = 0, 503 .flags = 0, 504 }, 505 { 506 .ops = &ctrl_ops, 507 .id = V4L2_CID_BIN_FACTOR_VERT, 508 .type = V4L2_CTRL_TYPE_INTEGER, 509 .name = "vertical binning factor", 510 .min = 0, 511 .max = GC0310_BIN_FACTOR_MAX, 512 .step = 1, 513 .def = 0, 514 .flags = 0, 515 }, 516 }; 517 518 static int gc0310_init(struct v4l2_subdev *sd) 519 { 520 int ret; 521 struct i2c_client *client = v4l2_get_subdevdata(sd); 522 struct gc0310_device *dev = to_gc0310_sensor(sd); 523 524 mutex_lock(&dev->input_lock); 525 526 /* set initial registers */ 527 ret = gc0310_write_reg_array(client, gc0310_reset_register); 528 529 /* restore settings */ 530 gc0310_res = gc0310_res_preview; 531 N_RES = N_RES_PREVIEW; 532 533 mutex_unlock(&dev->input_lock); 534 535 return ret; 536 } 537 538 static int power_ctrl(struct v4l2_subdev *sd, bool flag) 539 { 540 int ret = 0; 541 struct gc0310_device *dev = to_gc0310_sensor(sd); 542 543 if (!dev || !dev->platform_data) 544 return -ENODEV; 545 546 if (flag) { 547 /* The upstream module driver (written to Crystal 548 * Cove) had this logic to pulse the rails low first. 549 * This appears to break things on the MRD7 with the 550 * X-Powers PMIC... 551 * 552 * ret = dev->platform_data->v1p8_ctrl(sd, 0); 553 * ret |= dev->platform_data->v2p8_ctrl(sd, 0); 554 * mdelay(50); 555 */ 556 ret |= dev->platform_data->v1p8_ctrl(sd, 1); 557 ret |= dev->platform_data->v2p8_ctrl(sd, 1); 558 usleep_range(10000, 15000); 559 } 560 561 if (!flag || ret) { 562 ret |= dev->platform_data->v1p8_ctrl(sd, 0); 563 ret |= dev->platform_data->v2p8_ctrl(sd, 0); 564 } 565 return ret; 566 } 567 568 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 569 { 570 int ret; 571 struct gc0310_device *dev = to_gc0310_sensor(sd); 572 573 if (!dev || !dev->platform_data) 574 return -ENODEV; 575 576 /* GPIO0 == "reset" (active low), GPIO1 == "power down" */ 577 if (flag) { 578 /* Pulse reset, then release power down */ 579 ret = dev->platform_data->gpio0_ctrl(sd, 0); 580 usleep_range(5000, 10000); 581 ret |= dev->platform_data->gpio0_ctrl(sd, 1); 582 usleep_range(10000, 15000); 583 ret |= dev->platform_data->gpio1_ctrl(sd, 0); 584 usleep_range(10000, 15000); 585 } else { 586 ret = dev->platform_data->gpio1_ctrl(sd, 1); 587 ret |= dev->platform_data->gpio0_ctrl(sd, 0); 588 } 589 return ret; 590 } 591 592 static int power_up(struct v4l2_subdev *sd) 593 { 594 struct gc0310_device *dev = to_gc0310_sensor(sd); 595 struct i2c_client *client = v4l2_get_subdevdata(sd); 596 int ret; 597 598 if (!dev->platform_data) { 599 dev_err(&client->dev, 600 "no camera_sensor_platform_data"); 601 return -ENODEV; 602 } 603 604 if (dev->power_on) 605 return 0; /* Already on */ 606 607 /* power control */ 608 ret = power_ctrl(sd, 1); 609 if (ret) 610 goto fail_power; 611 612 /* flis clock control */ 613 ret = dev->platform_data->flisclk_ctrl(sd, 1); 614 if (ret) 615 goto fail_clk; 616 617 /* gpio ctrl */ 618 ret = gpio_ctrl(sd, 1); 619 if (ret) { 620 ret = gpio_ctrl(sd, 1); 621 if (ret) 622 goto fail_gpio; 623 } 624 625 msleep(100); 626 627 dev->power_on = true; 628 return 0; 629 630 fail_gpio: 631 dev->platform_data->flisclk_ctrl(sd, 0); 632 fail_clk: 633 power_ctrl(sd, 0); 634 fail_power: 635 dev_err(&client->dev, "sensor power-up failed\n"); 636 637 return ret; 638 } 639 640 static int power_down(struct v4l2_subdev *sd) 641 { 642 struct gc0310_device *dev = to_gc0310_sensor(sd); 643 struct i2c_client *client = v4l2_get_subdevdata(sd); 644 int ret = 0; 645 646 if (!dev->platform_data) { 647 dev_err(&client->dev, 648 "no camera_sensor_platform_data"); 649 return -ENODEV; 650 } 651 652 if (!dev->power_on) 653 return 0; /* Already off */ 654 655 /* gpio ctrl */ 656 ret = gpio_ctrl(sd, 0); 657 if (ret) { 658 ret = gpio_ctrl(sd, 0); 659 if (ret) 660 dev_err(&client->dev, "gpio failed 2\n"); 661 } 662 663 ret = dev->platform_data->flisclk_ctrl(sd, 0); 664 if (ret) 665 dev_err(&client->dev, "flisclk failed\n"); 666 667 /* power control */ 668 ret = power_ctrl(sd, 0); 669 if (ret) 670 dev_err(&client->dev, "vprog failed.\n"); 671 672 dev->power_on = false; 673 return ret; 674 } 675 676 static int gc0310_s_power(struct v4l2_subdev *sd, int on) 677 { 678 int ret; 679 680 if (on == 0) 681 return power_down(sd); 682 683 ret = power_up(sd); 684 if (ret) 685 return ret; 686 687 return gc0310_init(sd); 688 } 689 690 /* TODO: remove it. */ 691 static int startup(struct v4l2_subdev *sd) 692 { 693 struct gc0310_device *dev = to_gc0310_sensor(sd); 694 struct i2c_client *client = v4l2_get_subdevdata(sd); 695 int ret = 0; 696 697 ret = gc0310_write_reg_array(client, dev->res->regs); 698 if (ret) { 699 dev_err(&client->dev, "gc0310 write register err.\n"); 700 return ret; 701 } 702 703 return ret; 704 } 705 706 static int gc0310_set_fmt(struct v4l2_subdev *sd, 707 struct v4l2_subdev_state *sd_state, 708 struct v4l2_subdev_format *format) 709 { 710 struct v4l2_mbus_framefmt *fmt = &format->format; 711 struct gc0310_device *dev = to_gc0310_sensor(sd); 712 struct i2c_client *client = v4l2_get_subdevdata(sd); 713 struct camera_mipi_info *gc0310_info = NULL; 714 struct gc0310_resolution *res; 715 int ret = 0; 716 717 if (format->pad) 718 return -EINVAL; 719 720 if (!fmt) 721 return -EINVAL; 722 723 gc0310_info = v4l2_get_subdev_hostdata(sd); 724 if (!gc0310_info) 725 return -EINVAL; 726 727 mutex_lock(&dev->input_lock); 728 729 res = v4l2_find_nearest_size(gc0310_res_preview, 730 ARRAY_SIZE(gc0310_res_preview), width, 731 height, fmt->width, fmt->height); 732 if (!res) 733 res = &gc0310_res_preview[N_RES - 1]; 734 735 fmt->width = res->width; 736 fmt->height = res->height; 737 dev->res = res; 738 739 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8; 740 741 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 742 sd_state->pads->try_fmt = *fmt; 743 mutex_unlock(&dev->input_lock); 744 return 0; 745 } 746 747 /* s_power has not been called yet for std v4l2 clients (camorama) */ 748 power_up(sd); 749 750 dev_dbg(&client->dev, "%s: before gc0310_write_reg_array %s\n", 751 __func__, dev->res->desc); 752 ret = startup(sd); 753 if (ret) { 754 dev_err(&client->dev, "gc0310 startup err\n"); 755 goto err; 756 } 757 758 err: 759 mutex_unlock(&dev->input_lock); 760 return ret; 761 } 762 763 static int gc0310_get_fmt(struct v4l2_subdev *sd, 764 struct v4l2_subdev_state *sd_state, 765 struct v4l2_subdev_format *format) 766 { 767 struct v4l2_mbus_framefmt *fmt = &format->format; 768 struct gc0310_device *dev = to_gc0310_sensor(sd); 769 770 if (format->pad) 771 return -EINVAL; 772 773 if (!fmt) 774 return -EINVAL; 775 776 fmt->width = dev->res->width; 777 fmt->height = dev->res->height; 778 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8; 779 780 return 0; 781 } 782 783 static int gc0310_detect(struct i2c_client *client) 784 { 785 struct i2c_adapter *adapter = client->adapter; 786 u8 high, low; 787 int ret; 788 u16 id; 789 790 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 791 return -ENODEV; 792 793 ret = gc0310_read_reg(client, GC0310_8BIT, 794 GC0310_SC_CMMN_CHIP_ID_H, &high); 795 if (ret) { 796 dev_err(&client->dev, "read sensor_id_high failed\n"); 797 return -ENODEV; 798 } 799 ret = gc0310_read_reg(client, GC0310_8BIT, 800 GC0310_SC_CMMN_CHIP_ID_L, &low); 801 if (ret) { 802 dev_err(&client->dev, "read sensor_id_low failed\n"); 803 return -ENODEV; 804 } 805 id = ((((u16)high) << 8) | (u16)low); 806 dev_dbg(&client->dev, "sensor ID = 0x%x\n", id); 807 808 if (id != GC0310_ID) { 809 dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id, 810 GC0310_ID); 811 return -ENODEV; 812 } 813 814 dev_dbg(&client->dev, "detect gc0310 success\n"); 815 816 return 0; 817 } 818 819 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable) 820 { 821 struct gc0310_device *dev = to_gc0310_sensor(sd); 822 struct i2c_client *client = v4l2_get_subdevdata(sd); 823 int ret; 824 825 dev_dbg(&client->dev, "%s S enable=%d\n", __func__, enable); 826 mutex_lock(&dev->input_lock); 827 828 if (enable) { 829 /* enable per frame MIPI and sensor ctrl reset */ 830 ret = gc0310_write_reg(client, GC0310_8BIT, 831 0xFE, 0x30); 832 if (ret) { 833 mutex_unlock(&dev->input_lock); 834 return ret; 835 } 836 } 837 838 ret = gc0310_write_reg(client, GC0310_8BIT, 839 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3); 840 if (ret) { 841 mutex_unlock(&dev->input_lock); 842 return ret; 843 } 844 845 ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM, 846 enable ? GC0310_START_STREAMING : 847 GC0310_STOP_STREAMING); 848 if (ret) { 849 mutex_unlock(&dev->input_lock); 850 return ret; 851 } 852 853 ret = gc0310_write_reg(client, GC0310_8BIT, 854 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0); 855 if (ret) { 856 mutex_unlock(&dev->input_lock); 857 return ret; 858 } 859 860 mutex_unlock(&dev->input_lock); 861 return ret; 862 } 863 864 static int gc0310_s_config(struct v4l2_subdev *sd, 865 int irq, void *platform_data) 866 { 867 struct gc0310_device *dev = to_gc0310_sensor(sd); 868 struct i2c_client *client = v4l2_get_subdevdata(sd); 869 int ret = 0; 870 871 if (!platform_data) 872 return -ENODEV; 873 874 dev->platform_data = 875 (struct camera_sensor_platform_data *)platform_data; 876 877 mutex_lock(&dev->input_lock); 878 /* power off the module, then power on it in future 879 * as first power on by board may not fulfill the 880 * power on sequqence needed by the module 881 */ 882 dev->power_on = true; /* force power_down() to run */ 883 ret = power_down(sd); 884 if (ret) { 885 dev_err(&client->dev, "gc0310 power-off err.\n"); 886 goto fail_power_off; 887 } 888 889 ret = power_up(sd); 890 if (ret) { 891 dev_err(&client->dev, "gc0310 power-up err.\n"); 892 goto fail_power_on; 893 } 894 895 ret = dev->platform_data->csi_cfg(sd, 1); 896 if (ret) 897 goto fail_csi_cfg; 898 899 /* config & detect sensor */ 900 ret = gc0310_detect(client); 901 if (ret) { 902 dev_err(&client->dev, "gc0310_detect err s_config.\n"); 903 goto fail_csi_cfg; 904 } 905 906 /* turn off sensor, after probed */ 907 ret = power_down(sd); 908 if (ret) { 909 dev_err(&client->dev, "gc0310 power-off err.\n"); 910 goto fail_csi_cfg; 911 } 912 mutex_unlock(&dev->input_lock); 913 914 return 0; 915 916 fail_csi_cfg: 917 dev->platform_data->csi_cfg(sd, 0); 918 fail_power_on: 919 power_down(sd); 920 dev_err(&client->dev, "sensor power-gating failed\n"); 921 fail_power_off: 922 mutex_unlock(&dev->input_lock); 923 return ret; 924 } 925 926 static int gc0310_g_frame_interval(struct v4l2_subdev *sd, 927 struct v4l2_subdev_frame_interval *interval) 928 { 929 struct gc0310_device *dev = to_gc0310_sensor(sd); 930 931 interval->interval.numerator = 1; 932 interval->interval.denominator = dev->res->fps; 933 934 return 0; 935 } 936 937 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd, 938 struct v4l2_subdev_state *sd_state, 939 struct v4l2_subdev_mbus_code_enum *code) 940 { 941 if (code->index >= MAX_FMTS) 942 return -EINVAL; 943 944 code->code = MEDIA_BUS_FMT_SGRBG8_1X8; 945 return 0; 946 } 947 948 static int gc0310_enum_frame_size(struct v4l2_subdev *sd, 949 struct v4l2_subdev_state *sd_state, 950 struct v4l2_subdev_frame_size_enum *fse) 951 { 952 int index = fse->index; 953 954 if (index >= N_RES) 955 return -EINVAL; 956 957 fse->min_width = gc0310_res[index].width; 958 fse->min_height = gc0310_res[index].height; 959 fse->max_width = gc0310_res[index].width; 960 fse->max_height = gc0310_res[index].height; 961 962 return 0; 963 } 964 965 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) 966 { 967 struct gc0310_device *dev = to_gc0310_sensor(sd); 968 969 mutex_lock(&dev->input_lock); 970 *frames = dev->res->skip_frames; 971 mutex_unlock(&dev->input_lock); 972 973 return 0; 974 } 975 976 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = { 977 .g_skip_frames = gc0310_g_skip_frames, 978 }; 979 980 static const struct v4l2_subdev_video_ops gc0310_video_ops = { 981 .s_stream = gc0310_s_stream, 982 .g_frame_interval = gc0310_g_frame_interval, 983 }; 984 985 static const struct v4l2_subdev_core_ops gc0310_core_ops = { 986 .s_power = gc0310_s_power, 987 .ioctl = gc0310_ioctl, 988 }; 989 990 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = { 991 .enum_mbus_code = gc0310_enum_mbus_code, 992 .enum_frame_size = gc0310_enum_frame_size, 993 .get_fmt = gc0310_get_fmt, 994 .set_fmt = gc0310_set_fmt, 995 }; 996 997 static const struct v4l2_subdev_ops gc0310_ops = { 998 .core = &gc0310_core_ops, 999 .video = &gc0310_video_ops, 1000 .pad = &gc0310_pad_ops, 1001 .sensor = &gc0310_sensor_ops, 1002 }; 1003 1004 static void gc0310_remove(struct i2c_client *client) 1005 { 1006 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1007 struct gc0310_device *dev = to_gc0310_sensor(sd); 1008 1009 dev_dbg(&client->dev, "gc0310_remove...\n"); 1010 1011 dev->platform_data->csi_cfg(sd, 0); 1012 1013 v4l2_device_unregister_subdev(sd); 1014 media_entity_cleanup(&dev->sd.entity); 1015 v4l2_ctrl_handler_free(&dev->ctrl_handler); 1016 kfree(dev); 1017 } 1018 1019 static int gc0310_probe(struct i2c_client *client) 1020 { 1021 struct gc0310_device *dev; 1022 int ret; 1023 void *pdata; 1024 unsigned int i; 1025 1026 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1027 if (!dev) 1028 return -ENOMEM; 1029 1030 mutex_init(&dev->input_lock); 1031 1032 dev->res = &gc0310_res_preview[0]; 1033 v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops); 1034 1035 pdata = gmin_camera_platform_data(&dev->sd, 1036 ATOMISP_INPUT_FORMAT_RAW_8, 1037 atomisp_bayer_order_grbg); 1038 if (!pdata) { 1039 ret = -EINVAL; 1040 goto out_free; 1041 } 1042 1043 ret = gc0310_s_config(&dev->sd, client->irq, pdata); 1044 if (ret) 1045 goto out_free; 1046 1047 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA); 1048 if (ret) 1049 goto out_free; 1050 1051 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1052 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 1053 dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8; 1054 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1055 ret = 1056 v4l2_ctrl_handler_init(&dev->ctrl_handler, 1057 ARRAY_SIZE(gc0310_controls)); 1058 if (ret) { 1059 gc0310_remove(client); 1060 return ret; 1061 } 1062 1063 for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++) 1064 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i], 1065 NULL); 1066 1067 if (dev->ctrl_handler.error) { 1068 gc0310_remove(client); 1069 return dev->ctrl_handler.error; 1070 } 1071 1072 /* Use same lock for controls as for everything else. */ 1073 dev->ctrl_handler.lock = &dev->input_lock; 1074 dev->sd.ctrl_handler = &dev->ctrl_handler; 1075 1076 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 1077 if (ret) 1078 gc0310_remove(client); 1079 1080 return ret; 1081 out_free: 1082 v4l2_device_unregister_subdev(&dev->sd); 1083 kfree(dev); 1084 return ret; 1085 } 1086 1087 static const struct acpi_device_id gc0310_acpi_match[] = { 1088 {"XXGC0310"}, 1089 {"INT0310"}, 1090 {}, 1091 }; 1092 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match); 1093 1094 static struct i2c_driver gc0310_driver = { 1095 .driver = { 1096 .name = "gc0310", 1097 .acpi_match_table = gc0310_acpi_match, 1098 }, 1099 .probe_new = gc0310_probe, 1100 .remove = gc0310_remove, 1101 }; 1102 module_i2c_driver(gc0310_driver); 1103 1104 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>"); 1105 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors"); 1106 MODULE_LICENSE("GPL"); 1107