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