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 /* 38 * gc0310_write_reg_array - Initializes a list of GC0310 registers 39 * @client: i2c driver client structure 40 * @reglist: list of registers to be written 41 * @count: number of register, value pairs in the list 42 */ 43 static int gc0310_write_reg_array(struct i2c_client *client, 44 const struct gc0310_reg *reglist, int count) 45 { 46 int i, err; 47 48 for (i = 0; i < count; i++) { 49 err = i2c_smbus_write_byte_data(client, reglist[i].reg, reglist[i].val); 50 if (err) { 51 dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d", 52 reglist[i].val, reglist[i].reg, err); 53 return err; 54 } 55 } 56 57 return 0; 58 } 59 60 static int gc0310_set_gain(struct v4l2_subdev *sd, int gain) 61 62 { 63 struct i2c_client *client = v4l2_get_subdevdata(sd); 64 int ret; 65 u8 again, dgain; 66 67 if (gain < 0x20) 68 gain = 0x20; 69 if (gain > 0x80) 70 gain = 0x80; 71 72 if (gain >= 0x20 && gain < 0x40) { 73 again = 0x0; /* sqrt(2) */ 74 dgain = gain; 75 } else { 76 again = 0x2; /* 2 * sqrt(2) */ 77 dgain = gain / 2; 78 } 79 80 dev_dbg(&client->dev, "gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain); 81 82 /* set analog gain */ 83 ret = i2c_smbus_write_byte_data(client, GC0310_AGC_ADJ, again); 84 if (ret) 85 return ret; 86 87 /* set digital gain */ 88 ret = i2c_smbus_write_byte_data(client, GC0310_DGC_ADJ, dgain); 89 if (ret) 90 return ret; 91 92 return 0; 93 } 94 95 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg, 96 int gain, int digitgain) 97 98 { 99 struct i2c_client *client = v4l2_get_subdevdata(sd); 100 int ret; 101 102 dev_dbg(&client->dev, "coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain); 103 104 /* set exposure */ 105 ret = i2c_smbus_write_word_swapped(client, GC0310_AEC_PK_EXPO_H, coarse_itg); 106 if (ret) 107 return ret; 108 109 ret = gc0310_set_gain(sd, gain); 110 if (ret) 111 return ret; 112 113 return ret; 114 } 115 116 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure, 117 int gain, int digitgain) 118 { 119 struct gc0310_device *dev = to_gc0310_sensor(sd); 120 int ret; 121 122 mutex_lock(&dev->input_lock); 123 ret = __gc0310_set_exposure(sd, exposure, gain, digitgain); 124 mutex_unlock(&dev->input_lock); 125 126 return ret; 127 } 128 129 static long gc0310_s_exposure(struct v4l2_subdev *sd, 130 struct atomisp_exposure *exposure) 131 { 132 int exp = exposure->integration_time[0]; 133 int gain = exposure->gain[0]; 134 int digitgain = exposure->gain[1]; 135 136 /* we should not accept the invalid value below. */ 137 if (gain == 0) { 138 struct i2c_client *client = v4l2_get_subdevdata(sd); 139 140 v4l2_err(client, "%s: invalid value\n", __func__); 141 return -EINVAL; 142 } 143 144 return gc0310_set_exposure(sd, exp, gain, digitgain); 145 } 146 147 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 148 { 149 switch (cmd) { 150 case ATOMISP_IOC_S_EXPOSURE: 151 return gc0310_s_exposure(sd, arg); 152 default: 153 return -EINVAL; 154 } 155 return 0; 156 } 157 158 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl) 159 { 160 int ret = 0; 161 162 switch (ctrl->id) { 163 default: 164 ret = -EINVAL; 165 } 166 return ret; 167 } 168 169 static const struct v4l2_ctrl_ops ctrl_ops = { 170 .s_ctrl = gc0310_s_ctrl, 171 }; 172 173 static int gc0310_init(struct v4l2_subdev *sd) 174 { 175 int ret; 176 struct i2c_client *client = v4l2_get_subdevdata(sd); 177 struct gc0310_device *dev = to_gc0310_sensor(sd); 178 179 mutex_lock(&dev->input_lock); 180 181 /* set initial registers */ 182 ret = gc0310_write_reg_array(client, gc0310_reset_register, 183 ARRAY_SIZE(gc0310_reset_register)); 184 185 /* restore settings */ 186 gc0310_res = gc0310_res_preview; 187 N_RES = N_RES_PREVIEW; 188 189 mutex_unlock(&dev->input_lock); 190 191 return ret; 192 } 193 194 static int power_ctrl(struct v4l2_subdev *sd, bool flag) 195 { 196 int ret = 0; 197 struct gc0310_device *dev = to_gc0310_sensor(sd); 198 199 if (!dev || !dev->platform_data) 200 return -ENODEV; 201 202 if (flag) { 203 /* The upstream module driver (written to Crystal 204 * Cove) had this logic to pulse the rails low first. 205 * This appears to break things on the MRD7 with the 206 * X-Powers PMIC... 207 * 208 * ret = dev->platform_data->v1p8_ctrl(sd, 0); 209 * ret |= dev->platform_data->v2p8_ctrl(sd, 0); 210 * mdelay(50); 211 */ 212 ret |= dev->platform_data->v1p8_ctrl(sd, 1); 213 ret |= dev->platform_data->v2p8_ctrl(sd, 1); 214 usleep_range(10000, 15000); 215 } 216 217 if (!flag || ret) { 218 ret |= dev->platform_data->v1p8_ctrl(sd, 0); 219 ret |= dev->platform_data->v2p8_ctrl(sd, 0); 220 } 221 return ret; 222 } 223 224 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) 225 { 226 int ret; 227 struct gc0310_device *dev = to_gc0310_sensor(sd); 228 229 if (!dev || !dev->platform_data) 230 return -ENODEV; 231 232 /* GPIO0 == "reset" (active low), GPIO1 == "power down" */ 233 if (flag) { 234 /* Pulse reset, then release power down */ 235 ret = dev->platform_data->gpio0_ctrl(sd, 0); 236 usleep_range(5000, 10000); 237 ret |= dev->platform_data->gpio0_ctrl(sd, 1); 238 usleep_range(10000, 15000); 239 ret |= dev->platform_data->gpio1_ctrl(sd, 0); 240 usleep_range(10000, 15000); 241 } else { 242 ret = dev->platform_data->gpio1_ctrl(sd, 1); 243 ret |= dev->platform_data->gpio0_ctrl(sd, 0); 244 } 245 return ret; 246 } 247 248 static int power_up(struct v4l2_subdev *sd) 249 { 250 struct gc0310_device *dev = to_gc0310_sensor(sd); 251 struct i2c_client *client = v4l2_get_subdevdata(sd); 252 int ret; 253 254 if (!dev->platform_data) { 255 dev_err(&client->dev, 256 "no camera_sensor_platform_data"); 257 return -ENODEV; 258 } 259 260 if (dev->power_on) 261 return 0; /* Already on */ 262 263 /* power control */ 264 ret = power_ctrl(sd, 1); 265 if (ret) 266 goto fail_power; 267 268 /* flis clock control */ 269 ret = dev->platform_data->flisclk_ctrl(sd, 1); 270 if (ret) 271 goto fail_clk; 272 273 /* gpio ctrl */ 274 ret = gpio_ctrl(sd, 1); 275 if (ret) { 276 ret = gpio_ctrl(sd, 1); 277 if (ret) 278 goto fail_gpio; 279 } 280 281 msleep(100); 282 283 dev->power_on = true; 284 return 0; 285 286 fail_gpio: 287 dev->platform_data->flisclk_ctrl(sd, 0); 288 fail_clk: 289 power_ctrl(sd, 0); 290 fail_power: 291 dev_err(&client->dev, "sensor power-up failed\n"); 292 293 return ret; 294 } 295 296 static int power_down(struct v4l2_subdev *sd) 297 { 298 struct gc0310_device *dev = to_gc0310_sensor(sd); 299 struct i2c_client *client = v4l2_get_subdevdata(sd); 300 int ret = 0; 301 302 if (!dev->platform_data) { 303 dev_err(&client->dev, 304 "no camera_sensor_platform_data"); 305 return -ENODEV; 306 } 307 308 if (!dev->power_on) 309 return 0; /* Already off */ 310 311 /* gpio ctrl */ 312 ret = gpio_ctrl(sd, 0); 313 if (ret) { 314 ret = gpio_ctrl(sd, 0); 315 if (ret) 316 dev_err(&client->dev, "gpio failed 2\n"); 317 } 318 319 ret = dev->platform_data->flisclk_ctrl(sd, 0); 320 if (ret) 321 dev_err(&client->dev, "flisclk failed\n"); 322 323 /* power control */ 324 ret = power_ctrl(sd, 0); 325 if (ret) 326 dev_err(&client->dev, "vprog failed.\n"); 327 328 dev->power_on = false; 329 return ret; 330 } 331 332 static int gc0310_s_power(struct v4l2_subdev *sd, int on) 333 { 334 int ret; 335 336 if (on == 0) 337 return power_down(sd); 338 339 ret = power_up(sd); 340 if (ret) 341 return ret; 342 343 return gc0310_init(sd); 344 } 345 346 /* TODO: remove it. */ 347 static int startup(struct v4l2_subdev *sd) 348 { 349 struct gc0310_device *dev = to_gc0310_sensor(sd); 350 struct i2c_client *client = v4l2_get_subdevdata(sd); 351 int ret = 0; 352 353 ret = gc0310_write_reg_array(client, dev->res->regs, dev->res->reg_count); 354 if (ret) { 355 dev_err(&client->dev, "gc0310 write register err.\n"); 356 return ret; 357 } 358 359 return ret; 360 } 361 362 static int gc0310_set_fmt(struct v4l2_subdev *sd, 363 struct v4l2_subdev_state *sd_state, 364 struct v4l2_subdev_format *format) 365 { 366 struct v4l2_mbus_framefmt *fmt = &format->format; 367 struct gc0310_device *dev = to_gc0310_sensor(sd); 368 struct i2c_client *client = v4l2_get_subdevdata(sd); 369 struct camera_mipi_info *gc0310_info = NULL; 370 struct gc0310_resolution *res; 371 int ret = 0; 372 373 if (format->pad) 374 return -EINVAL; 375 376 if (!fmt) 377 return -EINVAL; 378 379 gc0310_info = v4l2_get_subdev_hostdata(sd); 380 if (!gc0310_info) 381 return -EINVAL; 382 383 mutex_lock(&dev->input_lock); 384 385 res = v4l2_find_nearest_size(gc0310_res_preview, 386 ARRAY_SIZE(gc0310_res_preview), width, 387 height, fmt->width, fmt->height); 388 if (!res) 389 res = &gc0310_res_preview[N_RES - 1]; 390 391 fmt->width = res->width; 392 fmt->height = res->height; 393 dev->res = res; 394 395 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8; 396 397 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 398 sd_state->pads->try_fmt = *fmt; 399 mutex_unlock(&dev->input_lock); 400 return 0; 401 } 402 403 /* s_power has not been called yet for std v4l2 clients (camorama) */ 404 power_up(sd); 405 406 dev_dbg(&client->dev, "%s: before gc0310_write_reg_array %s\n", 407 __func__, dev->res->desc); 408 ret = startup(sd); 409 if (ret) { 410 dev_err(&client->dev, "gc0310 startup err\n"); 411 goto err; 412 } 413 414 err: 415 mutex_unlock(&dev->input_lock); 416 return ret; 417 } 418 419 static int gc0310_get_fmt(struct v4l2_subdev *sd, 420 struct v4l2_subdev_state *sd_state, 421 struct v4l2_subdev_format *format) 422 { 423 struct v4l2_mbus_framefmt *fmt = &format->format; 424 struct gc0310_device *dev = to_gc0310_sensor(sd); 425 426 if (format->pad) 427 return -EINVAL; 428 429 if (!fmt) 430 return -EINVAL; 431 432 fmt->width = dev->res->width; 433 fmt->height = dev->res->height; 434 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8; 435 436 return 0; 437 } 438 439 static int gc0310_detect(struct i2c_client *client) 440 { 441 struct i2c_adapter *adapter = client->adapter; 442 int ret; 443 444 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) 445 return -ENODEV; 446 447 ret = i2c_smbus_read_word_swapped(client, GC0310_SC_CMMN_CHIP_ID_H); 448 if (ret < 0) { 449 dev_err(&client->dev, "read sensor_id failed: %d\n", ret); 450 return -ENODEV; 451 } 452 453 dev_dbg(&client->dev, "sensor ID = 0x%x\n", ret); 454 455 if (ret != GC0310_ID) { 456 dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", 457 ret, GC0310_ID); 458 return -ENODEV; 459 } 460 461 dev_dbg(&client->dev, "detect gc0310 success\n"); 462 463 return 0; 464 } 465 466 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable) 467 { 468 struct gc0310_device *dev = to_gc0310_sensor(sd); 469 struct i2c_client *client = v4l2_get_subdevdata(sd); 470 int ret; 471 472 dev_dbg(&client->dev, "%s S enable=%d\n", __func__, enable); 473 mutex_lock(&dev->input_lock); 474 475 if (enable) { 476 /* enable per frame MIPI and sensor ctrl reset */ 477 ret = i2c_smbus_write_byte_data(client, 0xFE, 0x30); 478 if (ret) { 479 mutex_unlock(&dev->input_lock); 480 return ret; 481 } 482 } 483 484 ret = i2c_smbus_write_byte_data(client, GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3); 485 if (ret) { 486 mutex_unlock(&dev->input_lock); 487 return ret; 488 } 489 490 ret = i2c_smbus_write_byte_data(client, GC0310_SW_STREAM, 491 enable ? GC0310_START_STREAMING : GC0310_STOP_STREAMING); 492 if (ret) { 493 mutex_unlock(&dev->input_lock); 494 return ret; 495 } 496 497 ret = i2c_smbus_write_byte_data(client, GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0); 498 if (ret) { 499 mutex_unlock(&dev->input_lock); 500 return ret; 501 } 502 503 mutex_unlock(&dev->input_lock); 504 return ret; 505 } 506 507 static int gc0310_s_config(struct v4l2_subdev *sd, 508 int irq, void *platform_data) 509 { 510 struct gc0310_device *dev = to_gc0310_sensor(sd); 511 struct i2c_client *client = v4l2_get_subdevdata(sd); 512 int ret = 0; 513 514 if (!platform_data) 515 return -ENODEV; 516 517 dev->platform_data = 518 (struct camera_sensor_platform_data *)platform_data; 519 520 mutex_lock(&dev->input_lock); 521 /* power off the module, then power on it in future 522 * as first power on by board may not fulfill the 523 * power on sequqence needed by the module 524 */ 525 dev->power_on = true; /* force power_down() to run */ 526 ret = power_down(sd); 527 if (ret) { 528 dev_err(&client->dev, "gc0310 power-off err.\n"); 529 goto fail_power_off; 530 } 531 532 ret = power_up(sd); 533 if (ret) { 534 dev_err(&client->dev, "gc0310 power-up err.\n"); 535 goto fail_power_on; 536 } 537 538 ret = dev->platform_data->csi_cfg(sd, 1); 539 if (ret) 540 goto fail_csi_cfg; 541 542 /* config & detect sensor */ 543 ret = gc0310_detect(client); 544 if (ret) { 545 dev_err(&client->dev, "gc0310_detect err s_config.\n"); 546 goto fail_csi_cfg; 547 } 548 549 /* turn off sensor, after probed */ 550 ret = power_down(sd); 551 if (ret) { 552 dev_err(&client->dev, "gc0310 power-off err.\n"); 553 goto fail_csi_cfg; 554 } 555 mutex_unlock(&dev->input_lock); 556 557 return 0; 558 559 fail_csi_cfg: 560 dev->platform_data->csi_cfg(sd, 0); 561 fail_power_on: 562 power_down(sd); 563 dev_err(&client->dev, "sensor power-gating failed\n"); 564 fail_power_off: 565 mutex_unlock(&dev->input_lock); 566 return ret; 567 } 568 569 static int gc0310_g_frame_interval(struct v4l2_subdev *sd, 570 struct v4l2_subdev_frame_interval *interval) 571 { 572 struct gc0310_device *dev = to_gc0310_sensor(sd); 573 574 interval->interval.numerator = 1; 575 interval->interval.denominator = dev->res->fps; 576 577 return 0; 578 } 579 580 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd, 581 struct v4l2_subdev_state *sd_state, 582 struct v4l2_subdev_mbus_code_enum *code) 583 { 584 if (code->index >= MAX_FMTS) 585 return -EINVAL; 586 587 code->code = MEDIA_BUS_FMT_SGRBG8_1X8; 588 return 0; 589 } 590 591 static int gc0310_enum_frame_size(struct v4l2_subdev *sd, 592 struct v4l2_subdev_state *sd_state, 593 struct v4l2_subdev_frame_size_enum *fse) 594 { 595 int index = fse->index; 596 597 if (index >= N_RES) 598 return -EINVAL; 599 600 fse->min_width = gc0310_res[index].width; 601 fse->min_height = gc0310_res[index].height; 602 fse->max_width = gc0310_res[index].width; 603 fse->max_height = gc0310_res[index].height; 604 605 return 0; 606 } 607 608 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) 609 { 610 struct gc0310_device *dev = to_gc0310_sensor(sd); 611 612 mutex_lock(&dev->input_lock); 613 *frames = dev->res->skip_frames; 614 mutex_unlock(&dev->input_lock); 615 616 return 0; 617 } 618 619 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = { 620 .g_skip_frames = gc0310_g_skip_frames, 621 }; 622 623 static const struct v4l2_subdev_video_ops gc0310_video_ops = { 624 .s_stream = gc0310_s_stream, 625 .g_frame_interval = gc0310_g_frame_interval, 626 }; 627 628 static const struct v4l2_subdev_core_ops gc0310_core_ops = { 629 .s_power = gc0310_s_power, 630 .ioctl = gc0310_ioctl, 631 }; 632 633 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = { 634 .enum_mbus_code = gc0310_enum_mbus_code, 635 .enum_frame_size = gc0310_enum_frame_size, 636 .get_fmt = gc0310_get_fmt, 637 .set_fmt = gc0310_set_fmt, 638 }; 639 640 static const struct v4l2_subdev_ops gc0310_ops = { 641 .core = &gc0310_core_ops, 642 .video = &gc0310_video_ops, 643 .pad = &gc0310_pad_ops, 644 .sensor = &gc0310_sensor_ops, 645 }; 646 647 static void gc0310_remove(struct i2c_client *client) 648 { 649 struct v4l2_subdev *sd = i2c_get_clientdata(client); 650 struct gc0310_device *dev = to_gc0310_sensor(sd); 651 652 dev_dbg(&client->dev, "gc0310_remove...\n"); 653 654 dev->platform_data->csi_cfg(sd, 0); 655 656 v4l2_device_unregister_subdev(sd); 657 media_entity_cleanup(&dev->sd.entity); 658 v4l2_ctrl_handler_free(&dev->ctrl_handler); 659 kfree(dev); 660 } 661 662 static int gc0310_probe(struct i2c_client *client) 663 { 664 struct gc0310_device *dev; 665 int ret; 666 void *pdata; 667 668 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 669 if (!dev) 670 return -ENOMEM; 671 672 mutex_init(&dev->input_lock); 673 674 dev->res = &gc0310_res_preview[0]; 675 v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops); 676 677 pdata = gmin_camera_platform_data(&dev->sd, 678 ATOMISP_INPUT_FORMAT_RAW_8, 679 atomisp_bayer_order_grbg); 680 if (!pdata) { 681 ret = -EINVAL; 682 goto out_free; 683 } 684 685 ret = gc0310_s_config(&dev->sd, client->irq, pdata); 686 if (ret) 687 goto out_free; 688 689 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA); 690 if (ret) 691 goto out_free; 692 693 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 694 dev->pad.flags = MEDIA_PAD_FL_SOURCE; 695 dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8; 696 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 697 ret = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0); 698 if (ret) { 699 gc0310_remove(client); 700 return ret; 701 } 702 703 if (dev->ctrl_handler.error) { 704 gc0310_remove(client); 705 return dev->ctrl_handler.error; 706 } 707 708 /* Use same lock for controls as for everything else. */ 709 dev->ctrl_handler.lock = &dev->input_lock; 710 dev->sd.ctrl_handler = &dev->ctrl_handler; 711 712 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); 713 if (ret) 714 gc0310_remove(client); 715 716 return ret; 717 out_free: 718 v4l2_device_unregister_subdev(&dev->sd); 719 kfree(dev); 720 return ret; 721 } 722 723 static const struct acpi_device_id gc0310_acpi_match[] = { 724 {"XXGC0310"}, 725 {"INT0310"}, 726 {}, 727 }; 728 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match); 729 730 static struct i2c_driver gc0310_driver = { 731 .driver = { 732 .name = "gc0310", 733 .acpi_match_table = gc0310_acpi_match, 734 }, 735 .probe_new = gc0310_probe, 736 .remove = gc0310_remove, 737 }; 738 module_i2c_driver(gc0310_driver); 739 740 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>"); 741 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors"); 742 MODULE_LICENSE("GPL"); 743