| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Support for GalaxyCore GC2235 2M camera sensor. |
| 4 |
* |
| 5 |
* Copyright (c) 2014 Intel Corporation. All Rights Reserved. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/types.h> |
| 10 |
#include <linux/kernel.h> |
| 11 |
#include <linux/mm.h> |
| 12 |
#include <linux/string.h> |
| 13 |
#include <linux/errno.h> |
| 14 |
#include <linux/init.h> |
| 15 |
#include <linux/kmod.h> |
| 16 |
#include <linux/device.h> |
| 17 |
#include <linux/delay.h> |
| 18 |
#include <linux/slab.h> |
| 19 |
#include <linux/i2c.h> |
| 20 |
#include <linux/moduleparam.h> |
| 21 |
#include <media/v4l2-device.h> |
| 22 |
#include "../include/linux/atomisp_gmin_platform.h" |
| 23 |
#include <linux/acpi.h> |
| 24 |
#include <linux/io.h> |
| 25 |
|
| 26 |
#include "gc2235.h" |
| 27 |
|
| 28 |
/* i2c read/write stuff */ |
| 29 |
static int gc2235_read_reg(struct i2c_client *client, |
| 30 |
u16 data_length, u16 reg, u16 *val) |
| 31 |
{ |
| 32 |
int err; |
| 33 |
struct i2c_msg msg[2]; |
| 34 |
unsigned char data[6]; |
| 35 |
|
| 36 |
if (!client->adapter) { |
| 37 |
dev_err(&client->dev, "%s error, no client->adapter\n", |
| 38 |
__func__); |
| 39 |
return -ENODEV; |
| 40 |
} |
| 41 |
|
| 42 |
if (data_length != GC2235_8BIT) { |
| 43 |
dev_err(&client->dev, "%s error, invalid data length\n", |
| 44 |
__func__); |
| 45 |
return -EINVAL; |
| 46 |
} |
| 47 |
|
| 48 |
memset(msg, 0, sizeof(msg)); |
| 49 |
|
| 50 |
msg[0].addr = client->addr; |
| 51 |
msg[0].flags = 0; |
| 52 |
msg[0].len = 1; |
| 53 |
msg[0].buf = data; |
| 54 |
|
| 55 |
/* high byte goes out first */ |
| 56 |
data[0] = (u8)(reg & 0xff); |
| 57 |
|
| 58 |
msg[1].addr = client->addr; |
| 59 |
msg[1].len = data_length; |
| 60 |
msg[1].flags = I2C_M_RD; |
| 61 |
msg[1].buf = data; |
| 62 |
|
| 63 |
err = i2c_transfer(client->adapter, msg, 2); |
| 64 |
if (err != 2) { |
| 65 |
if (err >= 0) |
| 66 |
err = -EIO; |
| 67 |
dev_err(&client->dev, |
| 68 |
"read from offset 0x%x error %d", reg, err); |
| 69 |
return err; |
| 70 |
} |
| 71 |
|
| 72 |
*val = 0; |
| 73 |
/* high byte comes first */ |
| 74 |
if (data_length == GC2235_8BIT) |
| 75 |
*val = (u8)data[0]; |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data) |
| 81 |
{ |
| 82 |
struct i2c_msg msg; |
| 83 |
const int num_msg = 1; |
| 84 |
int ret; |
| 85 |
|
| 86 |
msg.addr = client->addr; |
| 87 |
msg.flags = 0; |
| 88 |
msg.len = len; |
| 89 |
msg.buf = data; |
| 90 |
ret = i2c_transfer(client->adapter, &msg, 1); |
| 91 |
|
| 92 |
return ret == num_msg ? 0 : -EIO; |
| 93 |
} |
| 94 |
|
| 95 |
static int gc2235_write_reg(struct i2c_client *client, u16 data_length, |
| 96 |
u8 reg, u8 val) |
| 97 |
{ |
| 98 |
int ret; |
| 99 |
unsigned char data[4] = {0}; |
| 100 |
const u16 len = data_length + sizeof(u8); /* 16-bit address + data */ |
| 101 |
|
| 102 |
if (data_length != GC2235_8BIT) { |
| 103 |
dev_err(&client->dev, |
| 104 |
"%s error, invalid data_length\n", __func__); |
| 105 |
return -EINVAL; |
| 106 |
} |
| 107 |
|
| 108 |
/* high byte goes out first */ |
| 109 |
data[0] = reg; |
| 110 |
data[1] = val; |
| 111 |
|
| 112 |
ret = gc2235_i2c_write(client, len, data); |
| 113 |
if (ret) |
| 114 |
dev_err(&client->dev, |
| 115 |
"write error: wrote 0x%x to offset 0x%x error %d", |
| 116 |
val, reg, ret); |
| 117 |
|
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
static int __gc2235_flush_reg_array(struct i2c_client *client, |
| 122 |
struct gc2235_write_ctrl *ctrl) |
| 123 |
{ |
| 124 |
u16 size; |
| 125 |
|
| 126 |
if (ctrl->index == 0) |
| 127 |
return 0; |
| 128 |
|
| 129 |
size = sizeof(u8) + ctrl->index; /* 8-bit address + data */ |
| 130 |
ctrl->index = 0; |
| 131 |
|
| 132 |
return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer); |
| 133 |
} |
| 134 |
|
| 135 |
static int __gc2235_buf_reg_array(struct i2c_client *client, |
| 136 |
struct gc2235_write_ctrl *ctrl, |
| 137 |
const struct gc2235_reg *next) |
| 138 |
{ |
| 139 |
int size; |
| 140 |
|
| 141 |
if (next->type != GC2235_8BIT) |
| 142 |
return -EINVAL; |
| 143 |
|
| 144 |
size = 1; |
| 145 |
ctrl->buffer.data[ctrl->index] = (u8)next->val; |
| 146 |
|
| 147 |
/* When first item is added, we need to store its starting address */ |
| 148 |
if (ctrl->index == 0) |
| 149 |
ctrl->buffer.addr = next->reg; |
| 150 |
|
| 151 |
ctrl->index += size; |
| 152 |
|
| 153 |
/* |
| 154 |
* Buffer cannot guarantee free space for u32? Better flush it to avoid |
| 155 |
* possible lack of memory for next item. |
| 156 |
*/ |
| 157 |
if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE) |
| 158 |
return __gc2235_flush_reg_array(client, ctrl); |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static int __gc2235_write_reg_is_consecutive(struct i2c_client *client, |
| 164 |
struct gc2235_write_ctrl *ctrl, |
| 165 |
const struct gc2235_reg *next) |
| 166 |
{ |
| 167 |
if (ctrl->index == 0) |
| 168 |
return 1; |
| 169 |
|
| 170 |
return ctrl->buffer.addr + ctrl->index == next->reg; |
| 171 |
} |
| 172 |
|
| 173 |
static int gc2235_write_reg_array(struct i2c_client *client, |
| 174 |
const struct gc2235_reg *reglist) |
| 175 |
{ |
| 176 |
const struct gc2235_reg *next = reglist; |
| 177 |
struct gc2235_write_ctrl ctrl; |
| 178 |
int err; |
| 179 |
|
| 180 |
ctrl.index = 0; |
| 181 |
for (; next->type != GC2235_TOK_TERM; next++) { |
| 182 |
switch (next->type & GC2235_TOK_MASK) { |
| 183 |
case GC2235_TOK_DELAY: |
| 184 |
err = __gc2235_flush_reg_array(client, &ctrl); |
| 185 |
if (err) |
| 186 |
return err; |
| 187 |
msleep(next->val); |
| 188 |
break; |
| 189 |
default: |
| 190 |
/* |
| 191 |
* If next address is not consecutive, data needs to be |
| 192 |
* flushed before proceed. |
| 193 |
*/ |
| 194 |
if (!__gc2235_write_reg_is_consecutive(client, &ctrl, |
| 195 |
next)) { |
| 196 |
err = __gc2235_flush_reg_array(client, &ctrl); |
| 197 |
if (err) |
| 198 |
return err; |
| 199 |
} |
| 200 |
err = __gc2235_buf_reg_array(client, &ctrl, next); |
| 201 |
if (err) { |
| 202 |
dev_err(&client->dev, "%s: write error, aborted\n", |
| 203 |
__func__); |
| 204 |
return err; |
| 205 |
} |
| 206 |
break; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return __gc2235_flush_reg_array(client, &ctrl); |
| 211 |
} |
| 212 |
|
| 213 |
static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg, |
| 214 |
int gain, int digitgain) |
| 215 |
|
| 216 |
{ |
| 217 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 218 |
u16 coarse_integration = (u16)coarse_itg; |
| 219 |
int ret = 0; |
| 220 |
u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0; |
| 221 |
|
| 222 |
expo_coarse_h = coarse_integration >> 8; |
| 223 |
expo_coarse_l = coarse_integration & 0xff; |
| 224 |
|
| 225 |
ret = gc2235_write_reg(client, GC2235_8BIT, |
| 226 |
GC2235_EXPOSURE_H, expo_coarse_h); |
| 227 |
ret = gc2235_write_reg(client, GC2235_8BIT, |
| 228 |
GC2235_EXPOSURE_L, expo_coarse_l); |
| 229 |
|
| 230 |
if (gain <= 0x58) { |
| 231 |
gain_val = 0x40; |
| 232 |
gain_val2 = 0x58; |
| 233 |
} else if (gain < 256) { |
| 234 |
gain_val = 0x40; |
| 235 |
gain_val2 = gain; |
| 236 |
} else { |
| 237 |
gain_val2 = 64 * gain / 256; |
| 238 |
gain_val = 0xff; |
| 239 |
} |
| 240 |
|
| 241 |
ret = gc2235_write_reg(client, GC2235_8BIT, |
| 242 |
GC2235_GLOBAL_GAIN, (u8)gain_val); |
| 243 |
ret = gc2235_write_reg(client, GC2235_8BIT, |
| 244 |
GC2235_PRE_GAIN, (u8)gain_val2); |
| 245 |
|
| 246 |
return ret; |
| 247 |
} |
| 248 |
|
| 249 |
static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure, |
| 250 |
int gain, int digitgain) |
| 251 |
{ |
| 252 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 253 |
int ret; |
| 254 |
|
| 255 |
mutex_lock(&dev->input_lock); |
| 256 |
ret = __gc2235_set_exposure(sd, exposure, gain, digitgain); |
| 257 |
mutex_unlock(&dev->input_lock); |
| 258 |
|
| 259 |
return ret; |
| 260 |
} |
| 261 |
|
| 262 |
static long gc2235_s_exposure(struct v4l2_subdev *sd, |
| 263 |
struct atomisp_exposure *exposure) |
| 264 |
{ |
| 265 |
int exp = exposure->integration_time[0]; |
| 266 |
int gain = exposure->gain[0]; |
| 267 |
int digitgain = exposure->gain[1]; |
| 268 |
|
| 269 |
/* we should not accept the invalid value below. */ |
| 270 |
if (gain == 0) { |
| 271 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 272 |
|
| 273 |
v4l2_err(client, "%s: invalid value\n", __func__); |
| 274 |
return -EINVAL; |
| 275 |
} |
| 276 |
|
| 277 |
return gc2235_set_exposure(sd, exp, gain, digitgain); |
| 278 |
} |
| 279 |
|
| 280 |
static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) |
| 281 |
{ |
| 282 |
switch (cmd) { |
| 283 |
case ATOMISP_IOC_S_EXPOSURE: |
| 284 |
return gc2235_s_exposure(sd, arg); |
| 285 |
default: |
| 286 |
return -EINVAL; |
| 287 |
} |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
/* |
| 292 |
* This returns the exposure time being used. This should only be used |
| 293 |
* for filling in EXIF data, not for actual image processing. |
| 294 |
*/ |
| 295 |
static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value) |
| 296 |
{ |
| 297 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 298 |
u16 reg_v, reg_v2; |
| 299 |
int ret; |
| 300 |
|
| 301 |
/* get exposure */ |
| 302 |
ret = gc2235_read_reg(client, GC2235_8BIT, |
| 303 |
GC2235_EXPOSURE_L, |
| 304 |
®_v); |
| 305 |
if (ret) |
| 306 |
goto err; |
| 307 |
|
| 308 |
ret = gc2235_read_reg(client, GC2235_8BIT, |
| 309 |
GC2235_EXPOSURE_H, |
| 310 |
®_v2); |
| 311 |
if (ret) |
| 312 |
goto err; |
| 313 |
|
| 314 |
reg_v += reg_v2 << 8; |
| 315 |
|
| 316 |
*value = reg_v; |
| 317 |
err: |
| 318 |
return ret; |
| 319 |
} |
| 320 |
|
| 321 |
static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl) |
| 322 |
{ |
| 323 |
struct gc2235_device *dev = |
| 324 |
container_of(ctrl->handler, struct gc2235_device, ctrl_handler); |
| 325 |
int ret = 0; |
| 326 |
|
| 327 |
switch (ctrl->id) { |
| 328 |
case V4L2_CID_EXPOSURE_ABSOLUTE: |
| 329 |
ret = gc2235_q_exposure(&dev->sd, &ctrl->val); |
| 330 |
break; |
| 331 |
default: |
| 332 |
ret = -EINVAL; |
| 333 |
} |
| 334 |
|
| 335 |
return ret; |
| 336 |
} |
| 337 |
|
| 338 |
static const struct v4l2_ctrl_ops ctrl_ops = { |
| 339 |
.g_volatile_ctrl = gc2235_g_volatile_ctrl |
| 340 |
}; |
| 341 |
|
| 342 |
static struct v4l2_ctrl_config gc2235_controls[] = { |
| 343 |
{ |
| 344 |
.ops = &ctrl_ops, |
| 345 |
.id = V4L2_CID_EXPOSURE_ABSOLUTE, |
| 346 |
.type = V4L2_CTRL_TYPE_INTEGER, |
| 347 |
.name = "exposure", |
| 348 |
.min = 0x0, |
| 349 |
.max = 0xffff, |
| 350 |
.step = 0x01, |
| 351 |
.def = 0x00, |
| 352 |
.flags = 0, |
| 353 |
}, |
| 354 |
}; |
| 355 |
|
| 356 |
static int __gc2235_init(struct v4l2_subdev *sd) |
| 357 |
{ |
| 358 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 359 |
|
| 360 |
/* restore settings */ |
| 361 |
gc2235_res = gc2235_res_preview; |
| 362 |
N_RES = N_RES_PREVIEW; |
| 363 |
|
| 364 |
return gc2235_write_reg_array(client, gc2235_init_settings); |
| 365 |
} |
| 366 |
|
| 367 |
static int is_init; |
| 368 |
|
| 369 |
static int power_ctrl(struct v4l2_subdev *sd, bool flag) |
| 370 |
{ |
| 371 |
int ret = -1; |
| 372 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 373 |
|
| 374 |
if (!dev || !dev->platform_data) |
| 375 |
return -ENODEV; |
| 376 |
|
| 377 |
if (flag) { |
| 378 |
ret = dev->platform_data->v1p8_ctrl(sd, 1); |
| 379 |
usleep_range(60, 90); |
| 380 |
if (ret == 0) |
| 381 |
ret |= dev->platform_data->v2p8_ctrl(sd, 1); |
| 382 |
} else { |
| 383 |
ret = dev->platform_data->v1p8_ctrl(sd, 0); |
| 384 |
ret |= dev->platform_data->v2p8_ctrl(sd, 0); |
| 385 |
} |
| 386 |
return ret; |
| 387 |
} |
| 388 |
|
| 389 |
static int gpio_ctrl(struct v4l2_subdev *sd, bool flag) |
| 390 |
{ |
| 391 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 392 |
int ret; |
| 393 |
|
| 394 |
if (!dev || !dev->platform_data) |
| 395 |
return -ENODEV; |
| 396 |
|
| 397 |
ret = dev->platform_data->gpio1_ctrl(sd, !flag); |
| 398 |
usleep_range(60, 90); |
| 399 |
ret |= dev->platform_data->gpio0_ctrl(sd, flag); |
| 400 |
|
| 401 |
return ret; |
| 402 |
} |
| 403 |
|
| 404 |
static int power_up(struct v4l2_subdev *sd) |
| 405 |
{ |
| 406 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 407 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 408 |
int ret; |
| 409 |
|
| 410 |
if (!dev->platform_data) { |
| 411 |
dev_err(&client->dev, |
| 412 |
"no camera_sensor_platform_data"); |
| 413 |
return -ENODEV; |
| 414 |
} |
| 415 |
/* power control */ |
| 416 |
ret = power_ctrl(sd, 1); |
| 417 |
if (ret) |
| 418 |
goto fail_power; |
| 419 |
|
| 420 |
/* according to DS, at least 5ms is needed between DOVDD and PWDN */ |
| 421 |
usleep_range(5000, 6000); |
| 422 |
|
| 423 |
ret = dev->platform_data->flisclk_ctrl(sd, 1); |
| 424 |
if (ret) |
| 425 |
goto fail_clk; |
| 426 |
usleep_range(5000, 6000); |
| 427 |
|
| 428 |
/* gpio ctrl */ |
| 429 |
ret = gpio_ctrl(sd, 1); |
| 430 |
if (ret) { |
| 431 |
ret = gpio_ctrl(sd, 1); |
| 432 |
if (ret) |
| 433 |
goto fail_power; |
| 434 |
} |
| 435 |
|
| 436 |
msleep(5); |
| 437 |
return 0; |
| 438 |
|
| 439 |
fail_clk: |
| 440 |
gpio_ctrl(sd, 0); |
| 441 |
fail_power: |
| 442 |
power_ctrl(sd, 0); |
| 443 |
dev_err(&client->dev, "sensor power-up failed\n"); |
| 444 |
|
| 445 |
return ret; |
| 446 |
} |
| 447 |
|
| 448 |
static int power_down(struct v4l2_subdev *sd) |
| 449 |
{ |
| 450 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 451 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 452 |
int ret = 0; |
| 453 |
|
| 454 |
if (!dev->platform_data) { |
| 455 |
dev_err(&client->dev, |
| 456 |
"no camera_sensor_platform_data"); |
| 457 |
return -ENODEV; |
| 458 |
} |
| 459 |
/* gpio ctrl */ |
| 460 |
ret = gpio_ctrl(sd, 0); |
| 461 |
if (ret) { |
| 462 |
ret = gpio_ctrl(sd, 0); |
| 463 |
if (ret) |
| 464 |
dev_err(&client->dev, "gpio failed 2\n"); |
| 465 |
} |
| 466 |
|
| 467 |
ret = dev->platform_data->flisclk_ctrl(sd, 0); |
| 468 |
if (ret) |
| 469 |
dev_err(&client->dev, "flisclk failed\n"); |
| 470 |
|
| 471 |
/* power control */ |
| 472 |
ret = power_ctrl(sd, 0); |
| 473 |
if (ret) |
| 474 |
dev_err(&client->dev, "vprog failed.\n"); |
| 475 |
|
| 476 |
return ret; |
| 477 |
} |
| 478 |
|
| 479 |
static int gc2235_s_power(struct v4l2_subdev *sd, int on) |
| 480 |
{ |
| 481 |
int ret; |
| 482 |
|
| 483 |
if (on == 0) { |
| 484 |
ret = power_down(sd); |
| 485 |
} else { |
| 486 |
ret = power_up(sd); |
| 487 |
if (!ret) |
| 488 |
ret = __gc2235_init(sd); |
| 489 |
is_init = 1; |
| 490 |
} |
| 491 |
return ret; |
| 492 |
} |
| 493 |
|
| 494 |
static int gc2235_startup(struct v4l2_subdev *sd) |
| 495 |
{ |
| 496 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 497 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 498 |
int ret = 0; |
| 499 |
|
| 500 |
if (is_init == 0) { |
| 501 |
/* |
| 502 |
* force gc2235 to do a reset in res change, otherwise it |
| 503 |
* can not output normal after switching res. and it is not |
| 504 |
* necessary for first time run up after power on, for the sack |
| 505 |
* of performance |
| 506 |
*/ |
| 507 |
power_down(sd); |
| 508 |
power_up(sd); |
| 509 |
gc2235_write_reg_array(client, gc2235_init_settings); |
| 510 |
} |
| 511 |
|
| 512 |
ret = gc2235_write_reg_array(client, dev->res->regs); |
| 513 |
if (ret) { |
| 514 |
dev_err(&client->dev, "gc2235 write register err.\n"); |
| 515 |
return ret; |
| 516 |
} |
| 517 |
is_init = 0; |
| 518 |
|
| 519 |
return ret; |
| 520 |
} |
| 521 |
|
| 522 |
static int gc2235_set_fmt(struct v4l2_subdev *sd, |
| 523 |
struct v4l2_subdev_state *sd_state, |
| 524 |
struct v4l2_subdev_format *format) |
| 525 |
{ |
| 526 |
struct v4l2_mbus_framefmt *fmt = &format->format; |
| 527 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 528 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 529 |
struct camera_mipi_info *gc2235_info = NULL; |
| 530 |
struct gc2235_resolution *res; |
| 531 |
int ret = 0; |
| 532 |
|
| 533 |
gc2235_info = v4l2_get_subdev_hostdata(sd); |
| 534 |
if (!gc2235_info) |
| 535 |
return -EINVAL; |
| 536 |
if (format->pad) |
| 537 |
return -EINVAL; |
| 538 |
if (!fmt) |
| 539 |
return -EINVAL; |
| 540 |
|
| 541 |
mutex_lock(&dev->input_lock); |
| 542 |
res = v4l2_find_nearest_size(gc2235_res_preview, |
| 543 |
ARRAY_SIZE(gc2235_res_preview), width, |
| 544 |
height, fmt->width, fmt->height); |
| 545 |
if (!res) |
| 546 |
res = &gc2235_res_preview[N_RES - 1]; |
| 547 |
|
| 548 |
fmt->width = res->width; |
| 549 |
fmt->height = res->height; |
| 550 |
dev->res = res; |
| 551 |
|
| 552 |
fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; |
| 553 |
if (format->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 554 |
*v4l2_subdev_state_get_format(sd_state, 0) = *fmt; |
| 555 |
mutex_unlock(&dev->input_lock); |
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
ret = gc2235_startup(sd); |
| 560 |
if (ret) { |
| 561 |
dev_err(&client->dev, "gc2235 startup err\n"); |
| 562 |
goto err; |
| 563 |
} |
| 564 |
|
| 565 |
err: |
| 566 |
mutex_unlock(&dev->input_lock); |
| 567 |
return ret; |
| 568 |
} |
| 569 |
|
| 570 |
static int gc2235_get_fmt(struct v4l2_subdev *sd, |
| 571 |
struct v4l2_subdev_state *sd_state, |
| 572 |
struct v4l2_subdev_format *format) |
| 573 |
{ |
| 574 |
struct v4l2_mbus_framefmt *fmt = &format->format; |
| 575 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 576 |
|
| 577 |
if (format->pad) |
| 578 |
return -EINVAL; |
| 579 |
|
| 580 |
if (!fmt) |
| 581 |
return -EINVAL; |
| 582 |
|
| 583 |
fmt->width = dev->res->width; |
| 584 |
fmt->height = dev->res->height; |
| 585 |
fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10; |
| 586 |
|
| 587 |
return 0; |
| 588 |
} |
| 589 |
|
| 590 |
static int gc2235_detect(struct i2c_client *client) |
| 591 |
{ |
| 592 |
struct i2c_adapter *adapter = client->adapter; |
| 593 |
u16 high = 0, low = 0; |
| 594 |
u16 id; |
| 595 |
|
| 596 |
if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) |
| 597 |
return -ENODEV; |
| 598 |
|
| 599 |
gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_H, &high); |
| 600 |
gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_L, &low); |
| 601 |
id = ((high << 8) | low); |
| 602 |
|
| 603 |
if (id != GC2235_ID) { |
| 604 |
dev_err(&client->dev, "sensor ID error, 0x%x\n", id); |
| 605 |
return -ENODEV; |
| 606 |
} |
| 607 |
|
| 608 |
dev_info(&client->dev, "detect gc2235 success\n"); |
| 609 |
return 0; |
| 610 |
} |
| 611 |
|
| 612 |
static int gc2235_s_stream(struct v4l2_subdev *sd, int enable) |
| 613 |
{ |
| 614 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 615 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 616 |
int ret; |
| 617 |
|
| 618 |
mutex_lock(&dev->input_lock); |
| 619 |
|
| 620 |
if (enable) |
| 621 |
ret = gc2235_write_reg_array(client, gc2235_stream_on); |
| 622 |
else |
| 623 |
ret = gc2235_write_reg_array(client, gc2235_stream_off); |
| 624 |
|
| 625 |
mutex_unlock(&dev->input_lock); |
| 626 |
return ret; |
| 627 |
} |
| 628 |
|
| 629 |
static int gc2235_s_config(struct v4l2_subdev *sd, |
| 630 |
int irq, void *platform_data) |
| 631 |
{ |
| 632 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 633 |
struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 634 |
int ret = 0; |
| 635 |
|
| 636 |
if (!platform_data) |
| 637 |
return -ENODEV; |
| 638 |
|
| 639 |
dev->platform_data = |
| 640 |
(struct camera_sensor_platform_data *)platform_data; |
| 641 |
|
| 642 |
mutex_lock(&dev->input_lock); |
| 643 |
/* |
| 644 |
* power off the module, then power on it in future |
| 645 |
* as first power on by board may not fulfill the |
| 646 |
* power on sequqence needed by the module |
| 647 |
*/ |
| 648 |
ret = power_down(sd); |
| 649 |
if (ret) { |
| 650 |
dev_err(&client->dev, "gc2235 power-off err.\n"); |
| 651 |
goto fail_power_off; |
| 652 |
} |
| 653 |
|
| 654 |
ret = power_up(sd); |
| 655 |
if (ret) { |
| 656 |
dev_err(&client->dev, "gc2235 power-up err.\n"); |
| 657 |
goto fail_power_on; |
| 658 |
} |
| 659 |
|
| 660 |
ret = dev->platform_data->csi_cfg(sd, 1); |
| 661 |
if (ret) |
| 662 |
goto fail_csi_cfg; |
| 663 |
|
| 664 |
/* config & detect sensor */ |
| 665 |
ret = gc2235_detect(client); |
| 666 |
if (ret) { |
| 667 |
dev_err(&client->dev, "gc2235_detect err s_config.\n"); |
| 668 |
goto fail_csi_cfg; |
| 669 |
} |
| 670 |
|
| 671 |
/* turn off sensor, after probed */ |
| 672 |
ret = power_down(sd); |
| 673 |
if (ret) { |
| 674 |
dev_err(&client->dev, "gc2235 power-off err.\n"); |
| 675 |
goto fail_csi_cfg; |
| 676 |
} |
| 677 |
mutex_unlock(&dev->input_lock); |
| 678 |
|
| 679 |
return 0; |
| 680 |
|
| 681 |
fail_csi_cfg: |
| 682 |
dev->platform_data->csi_cfg(sd, 0); |
| 683 |
fail_power_on: |
| 684 |
power_down(sd); |
| 685 |
dev_err(&client->dev, "sensor power-gating failed\n"); |
| 686 |
fail_power_off: |
| 687 |
mutex_unlock(&dev->input_lock); |
| 688 |
return ret; |
| 689 |
} |
| 690 |
|
| 691 |
static int gc2235_get_frame_interval(struct v4l2_subdev *sd, |
| 692 |
struct v4l2_subdev_state *sd_state, |
| 693 |
struct v4l2_subdev_frame_interval *interval) |
| 694 |
{ |
| 695 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 696 |
|
| 697 |
/* |
| 698 |
* FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2 |
| 699 |
* subdev active state API. |
| 700 |
*/ |
| 701 |
if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 702 |
return -EINVAL; |
| 703 |
|
| 704 |
interval->interval.numerator = 1; |
| 705 |
interval->interval.denominator = dev->res->fps; |
| 706 |
|
| 707 |
return 0; |
| 708 |
} |
| 709 |
|
| 710 |
static int gc2235_enum_mbus_code(struct v4l2_subdev *sd, |
| 711 |
struct v4l2_subdev_state *sd_state, |
| 712 |
struct v4l2_subdev_mbus_code_enum *code) |
| 713 |
{ |
| 714 |
if (code->index >= MAX_FMTS) |
| 715 |
return -EINVAL; |
| 716 |
|
| 717 |
code->code = MEDIA_BUS_FMT_SBGGR10_1X10; |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|
| 721 |
static int gc2235_enum_frame_size(struct v4l2_subdev *sd, |
| 722 |
struct v4l2_subdev_state *sd_state, |
| 723 |
struct v4l2_subdev_frame_size_enum *fse) |
| 724 |
{ |
| 725 |
int index = fse->index; |
| 726 |
|
| 727 |
if (index >= N_RES) |
| 728 |
return -EINVAL; |
| 729 |
|
| 730 |
fse->min_width = gc2235_res[index].width; |
| 731 |
fse->min_height = gc2235_res[index].height; |
| 732 |
fse->max_width = gc2235_res[index].width; |
| 733 |
fse->max_height = gc2235_res[index].height; |
| 734 |
|
| 735 |
return 0; |
| 736 |
} |
| 737 |
|
| 738 |
static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames) |
| 739 |
{ |
| 740 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 741 |
|
| 742 |
mutex_lock(&dev->input_lock); |
| 743 |
*frames = dev->res->skip_frames; |
| 744 |
mutex_unlock(&dev->input_lock); |
| 745 |
|
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = { |
| 750 |
.g_skip_frames = gc2235_g_skip_frames, |
| 751 |
}; |
| 752 |
|
| 753 |
static const struct v4l2_subdev_video_ops gc2235_video_ops = { |
| 754 |
.s_stream = gc2235_s_stream, |
| 755 |
}; |
| 756 |
|
| 757 |
static const struct v4l2_subdev_core_ops gc2235_core_ops = { |
| 758 |
.s_power = gc2235_s_power, |
| 759 |
.ioctl = gc2235_ioctl, |
| 760 |
}; |
| 761 |
|
| 762 |
static const struct v4l2_subdev_pad_ops gc2235_pad_ops = { |
| 763 |
.enum_mbus_code = gc2235_enum_mbus_code, |
| 764 |
.enum_frame_size = gc2235_enum_frame_size, |
| 765 |
.get_fmt = gc2235_get_fmt, |
| 766 |
.set_fmt = gc2235_set_fmt, |
| 767 |
.get_frame_interval = gc2235_get_frame_interval, |
| 768 |
}; |
| 769 |
|
| 770 |
static const struct v4l2_subdev_ops gc2235_ops = { |
| 771 |
.core = &gc2235_core_ops, |
| 772 |
.video = &gc2235_video_ops, |
| 773 |
.pad = &gc2235_pad_ops, |
| 774 |
.sensor = &gc2235_sensor_ops, |
| 775 |
}; |
| 776 |
|
| 777 |
static void gc2235_remove(struct i2c_client *client) |
| 778 |
{ |
| 779 |
struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 780 |
struct gc2235_device *dev = to_gc2235_sensor(sd); |
| 781 |
|
| 782 |
dev->platform_data->csi_cfg(sd, 0); |
| 783 |
|
| 784 |
v4l2_device_unregister_subdev(sd); |
| 785 |
media_entity_cleanup(&dev->sd.entity); |
| 786 |
v4l2_ctrl_handler_free(&dev->ctrl_handler); |
| 787 |
kfree(dev); |
| 788 |
} |
| 789 |
|
| 790 |
static int gc2235_probe(struct i2c_client *client) |
| 791 |
{ |
| 792 |
struct gc2235_device *dev; |
| 793 |
void *gcpdev; |
| 794 |
int ret; |
| 795 |
unsigned int i; |
| 796 |
|
| 797 |
dev = kzalloc_obj(*dev); |
| 798 |
if (!dev) |
| 799 |
return -ENOMEM; |
| 800 |
|
| 801 |
mutex_init(&dev->input_lock); |
| 802 |
|
| 803 |
dev->res = &gc2235_res_preview[0]; |
| 804 |
v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops); |
| 805 |
|
| 806 |
gcpdev = gmin_camera_platform_data(&dev->sd, |
| 807 |
ATOMISP_INPUT_FORMAT_RAW_10, |
| 808 |
atomisp_bayer_order_grbg); |
| 809 |
|
| 810 |
ret = gc2235_s_config(&dev->sd, client->irq, gcpdev); |
| 811 |
if (ret) |
| 812 |
goto out_free; |
| 813 |
|
| 814 |
dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 815 |
dev->pad.flags = MEDIA_PAD_FL_SOURCE; |
| 816 |
dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10; |
| 817 |
dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; |
| 818 |
ret = |
| 819 |
v4l2_ctrl_handler_init(&dev->ctrl_handler, |
| 820 |
ARRAY_SIZE(gc2235_controls)); |
| 821 |
if (ret) { |
| 822 |
gc2235_remove(client); |
| 823 |
return ret; |
| 824 |
} |
| 825 |
|
| 826 |
for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++) |
| 827 |
v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i], |
| 828 |
NULL); |
| 829 |
|
| 830 |
if (dev->ctrl_handler.error) { |
| 831 |
gc2235_remove(client); |
| 832 |
return dev->ctrl_handler.error; |
| 833 |
} |
| 834 |
|
| 835 |
/* Use same lock for controls as for everything else. */ |
| 836 |
dev->ctrl_handler.lock = &dev->input_lock; |
| 837 |
dev->sd.ctrl_handler = &dev->ctrl_handler; |
| 838 |
|
| 839 |
ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad); |
| 840 |
if (ret) |
| 841 |
gc2235_remove(client); |
| 842 |
|
| 843 |
return atomisp_register_i2c_module(&dev->sd, gcpdev); |
| 844 |
|
| 845 |
out_free: |
| 846 |
v4l2_device_unregister_subdev(&dev->sd); |
| 847 |
kfree(dev); |
| 848 |
|
| 849 |
return ret; |
| 850 |
} |
| 851 |
|
| 852 |
static const struct acpi_device_id gc2235_acpi_match[] = { |
| 853 |
{ "INT33F8" }, |
| 854 |
{}, |
| 855 |
}; |
| 856 |
MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match); |
| 857 |
|
| 858 |
static struct i2c_driver gc2235_driver = { |
| 859 |
.driver = { |
| 860 |
.name = "gc2235", |
| 861 |
.acpi_match_table = gc2235_acpi_match, |
| 862 |
}, |
| 863 |
.probe = gc2235_probe, |
| 864 |
.remove = gc2235_remove, |
| 865 |
}; |
| 866 |
module_i2c_driver(gc2235_driver); |
| 867 |
|
| 868 |
MODULE_AUTHOR("Shuguang Gong <[email protected]>"); |
| 869 |
MODULE_DESCRIPTION("A low-level driver for GC2235 sensors"); |
| 870 |
MODULE_LICENSE("GPL"); |