linux/drivers/staging/media/atomisp/i2c/gc2235.c
<<
>>
Prefs
   1/*
   2 * Support for GalaxyCore GC2235 2M camera sensor.
   3 *
   4 * Copyright (c) 2014 Intel Corporation. All Rights Reserved.
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License version
   8 * 2 as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 */
  16
  17#include <linux/module.h>
  18#include <linux/types.h>
  19#include <linux/kernel.h>
  20#include <linux/mm.h>
  21#include <linux/string.h>
  22#include <linux/errno.h>
  23#include <linux/init.h>
  24#include <linux/kmod.h>
  25#include <linux/device.h>
  26#include <linux/delay.h>
  27#include <linux/slab.h>
  28#include <linux/i2c.h>
  29#include <linux/gpio.h>
  30#include <linux/moduleparam.h>
  31#include <media/v4l2-device.h>
  32#include "../include/linux/atomisp_gmin_platform.h"
  33#include <linux/acpi.h>
  34#include <linux/io.h>
  35
  36#include "gc2235.h"
  37
  38/* i2c read/write stuff */
  39static int gc2235_read_reg(struct i2c_client *client,
  40                           u16 data_length, u16 reg, u16 *val)
  41{
  42        int err;
  43        struct i2c_msg msg[2];
  44        unsigned char data[6];
  45
  46        if (!client->adapter) {
  47                dev_err(&client->dev, "%s error, no client->adapter\n",
  48                        __func__);
  49                return -ENODEV;
  50        }
  51
  52        if (data_length != GC2235_8BIT) {
  53                dev_err(&client->dev, "%s error, invalid data length\n",
  54                        __func__);
  55                return -EINVAL;
  56        }
  57
  58        memset(msg, 0, sizeof(msg));
  59
  60        msg[0].addr = client->addr;
  61        msg[0].flags = 0;
  62        msg[0].len = 1;
  63        msg[0].buf = data;
  64
  65        /* high byte goes out first */
  66        data[0] = (u8)(reg & 0xff);
  67
  68        msg[1].addr = client->addr;
  69        msg[1].len = data_length;
  70        msg[1].flags = I2C_M_RD;
  71        msg[1].buf = data;
  72
  73        err = i2c_transfer(client->adapter, msg, 2);
  74        if (err != 2) {
  75                if (err >= 0)
  76                        err = -EIO;
  77                dev_err(&client->dev,
  78                        "read from offset 0x%x error %d", reg, err);
  79                return err;
  80        }
  81
  82        *val = 0;
  83        /* high byte comes first */
  84        if (data_length == GC2235_8BIT)
  85                *val = (u8)data[0];
  86
  87        return 0;
  88}
  89
  90static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data)
  91{
  92        struct i2c_msg msg;
  93        const int num_msg = 1;
  94        int ret;
  95
  96        msg.addr = client->addr;
  97        msg.flags = 0;
  98        msg.len = len;
  99        msg.buf = data;
 100        ret = i2c_transfer(client->adapter, &msg, 1);
 101
 102        return ret == num_msg ? 0 : -EIO;
 103}
 104
 105static int gc2235_write_reg(struct i2c_client *client, u16 data_length,
 106                                                        u8 reg, u8 val)
 107{
 108        int ret;
 109        unsigned char data[4] = {0};
 110        const u16 len = data_length + sizeof(u8); /* 16-bit address + data */
 111
 112        if (data_length != GC2235_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        data[0] = reg;
 120        data[1] = val;
 121
 122        ret = gc2235_i2c_write(client, len, data);
 123        if (ret)
 124                dev_err(&client->dev,
 125                        "write error: wrote 0x%x to offset 0x%x error %d",
 126                        val, reg, ret);
 127
 128        return ret;
 129}
 130
 131static int __gc2235_flush_reg_array(struct i2c_client *client,
 132                                    struct gc2235_write_ctrl *ctrl)
 133{
 134        u16 size;
 135
 136        if (ctrl->index == 0)
 137                return 0;
 138
 139        size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
 140        ctrl->index = 0;
 141
 142        return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer);
 143}
 144
 145static int __gc2235_buf_reg_array(struct i2c_client *client,
 146                                  struct gc2235_write_ctrl *ctrl,
 147                                  const struct gc2235_reg *next)
 148{
 149        int size;
 150
 151        if (next->type != GC2235_8BIT)
 152                return -EINVAL;
 153
 154        size = 1;
 155        ctrl->buffer.data[ctrl->index] = (u8)next->val;
 156
 157        /* When first item is added, we need to store its starting address */
 158        if (ctrl->index == 0)
 159                ctrl->buffer.addr = next->reg;
 160
 161        ctrl->index += size;
 162
 163        /*
 164         * Buffer cannot guarantee free space for u32? Better flush it to avoid
 165         * possible lack of memory for next item.
 166         */
 167        if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE)
 168                return __gc2235_flush_reg_array(client, ctrl);
 169
 170        return 0;
 171}
 172static int __gc2235_write_reg_is_consecutive(struct i2c_client *client,
 173                                             struct gc2235_write_ctrl *ctrl,
 174                                             const struct gc2235_reg *next)
 175{
 176        if (ctrl->index == 0)
 177                return 1;
 178
 179        return ctrl->buffer.addr + ctrl->index == next->reg;
 180}
 181static int gc2235_write_reg_array(struct i2c_client *client,
 182                                  const struct gc2235_reg *reglist)
 183{
 184        const struct gc2235_reg *next = reglist;
 185        struct gc2235_write_ctrl ctrl;
 186        int err;
 187
 188        ctrl.index = 0;
 189        for (; next->type != GC2235_TOK_TERM; next++) {
 190                switch (next->type & GC2235_TOK_MASK) {
 191                case GC2235_TOK_DELAY:
 192                        err = __gc2235_flush_reg_array(client, &ctrl);
 193                        if (err)
 194                                return err;
 195                        msleep(next->val);
 196                        break;
 197                default:
 198                        /*
 199                         * If next address is not consecutive, data needs to be
 200                         * flushed before proceed.
 201                         */
 202                        if (!__gc2235_write_reg_is_consecutive(client, &ctrl,
 203                                                                next)) {
 204                                err = __gc2235_flush_reg_array(client, &ctrl);
 205                                if (err)
 206                                        return err;
 207                        }
 208                        err = __gc2235_buf_reg_array(client, &ctrl, next);
 209                        if (err) {
 210                                dev_err(&client->dev, "%s: write error, aborted\n",
 211                                         __func__);
 212                                return err;
 213                        }
 214                        break;
 215                }
 216        }
 217
 218        return __gc2235_flush_reg_array(client, &ctrl);
 219}
 220
 221static int gc2235_g_focal(struct v4l2_subdev *sd, s32 *val)
 222{
 223        *val = (GC2235_FOCAL_LENGTH_NUM << 16) | GC2235_FOCAL_LENGTH_DEM;
 224        return 0;
 225}
 226
 227static int gc2235_g_fnumber(struct v4l2_subdev *sd, s32 *val)
 228{
 229        /*const f number for imx*/
 230        *val = (GC2235_F_NUMBER_DEFAULT_NUM << 16) | GC2235_F_NUMBER_DEM;
 231        return 0;
 232}
 233
 234static int gc2235_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
 235{
 236        *val = (GC2235_F_NUMBER_DEFAULT_NUM << 24) |
 237                (GC2235_F_NUMBER_DEM << 16) |
 238                (GC2235_F_NUMBER_DEFAULT_NUM << 8) | GC2235_F_NUMBER_DEM;
 239        return 0;
 240}
 241
 242
 243static int gc2235_get_intg_factor(struct i2c_client *client,
 244                                struct camera_mipi_info *info,
 245                                const struct gc2235_resolution *res)
 246{
 247        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 248        struct gc2235_device *dev = to_gc2235_sensor(sd);
 249        struct atomisp_sensor_mode_data *buf = &info->data;
 250        u16 reg_val, reg_val_h, dummy;
 251        int ret;
 252
 253        if (!info)
 254                return -EINVAL;
 255
 256        /* pixel clock calculattion */
 257        buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz = 30000000;
 258
 259        /* get integration time */
 260        buf->coarse_integration_time_min = GC2235_COARSE_INTG_TIME_MIN;
 261        buf->coarse_integration_time_max_margin =
 262                                        GC2235_COARSE_INTG_TIME_MAX_MARGIN;
 263
 264        buf->fine_integration_time_min = GC2235_FINE_INTG_TIME_MIN;
 265        buf->fine_integration_time_max_margin =
 266                                        GC2235_FINE_INTG_TIME_MAX_MARGIN;
 267
 268        buf->fine_integration_time_def = GC2235_FINE_INTG_TIME_MIN;
 269        buf->frame_length_lines = res->lines_per_frame;
 270        buf->line_length_pck = res->pixels_per_line;
 271        buf->read_mode = res->bin_mode;
 272
 273        /* get the cropping and output resolution to ISP for this mode. */
 274        ret =  gc2235_read_reg(client, GC2235_8BIT,
 275                                        GC2235_H_CROP_START_H, &reg_val_h);
 276        ret =  gc2235_read_reg(client, GC2235_8BIT,
 277                                        GC2235_H_CROP_START_L, &reg_val);
 278        if (ret)
 279                return ret;
 280
 281        buf->crop_horizontal_start = (reg_val_h << 8) | reg_val;
 282
 283        ret =  gc2235_read_reg(client, GC2235_8BIT,
 284                                        GC2235_V_CROP_START_H, &reg_val_h);
 285        ret =  gc2235_read_reg(client, GC2235_8BIT,
 286                                        GC2235_V_CROP_START_L, &reg_val);
 287        if (ret)
 288                return ret;
 289
 290        buf->crop_vertical_start = (reg_val_h << 8) | reg_val;
 291
 292        ret = gc2235_read_reg(client, GC2235_8BIT,
 293                                        GC2235_H_OUTSIZE_H, &reg_val_h);
 294        ret = gc2235_read_reg(client, GC2235_8BIT,
 295                                        GC2235_H_OUTSIZE_L, &reg_val);
 296        if (ret)
 297                return ret;
 298        buf->output_width = (reg_val_h << 8) | reg_val;
 299
 300        ret = gc2235_read_reg(client, GC2235_8BIT,
 301                                        GC2235_V_OUTSIZE_H, &reg_val_h);
 302        ret = gc2235_read_reg(client, GC2235_8BIT,
 303                                        GC2235_V_OUTSIZE_L, &reg_val);
 304        if (ret)
 305                return ret;
 306        buf->output_height = (reg_val_h << 8) | reg_val;
 307
 308        buf->crop_horizontal_end = buf->crop_horizontal_start +
 309                                                buf->output_width - 1;
 310        buf->crop_vertical_end = buf->crop_vertical_start +
 311                                                buf->output_height - 1;
 312
 313        ret = gc2235_read_reg(client, GC2235_8BIT,
 314                                        GC2235_HB_H, &reg_val_h);
 315        ret = gc2235_read_reg(client, GC2235_8BIT,
 316                                        GC2235_HB_L, &reg_val);
 317        if (ret)
 318                return ret;
 319
 320        dummy = (reg_val_h << 8) | reg_val;
 321
 322        ret = gc2235_read_reg(client, GC2235_8BIT,
 323                                        GC2235_SH_DELAY_H, &reg_val_h);
 324        ret = gc2235_read_reg(client, GC2235_8BIT,
 325                                        GC2235_SH_DELAY_L, &reg_val);
 326
 327#if 0
 328        buf->line_length_pck = buf->output_width + 16 + dummy +
 329                                (((u16)reg_val_h << 8) | (u16)reg_val) + 4;
 330#endif
 331        ret = gc2235_read_reg(client, GC2235_8BIT,
 332                                        GC2235_VB_H, &reg_val_h);
 333        ret = gc2235_read_reg(client, GC2235_8BIT,
 334                                        GC2235_VB_L, &reg_val);
 335        if (ret)
 336                return ret;
 337
 338#if 0
 339        buf->frame_length_lines = buf->output_height + 32 +
 340                                (((u16)reg_val_h << 8) | (u16)reg_val);
 341#endif
 342        buf->binning_factor_x = res->bin_factor_x ?
 343                                        res->bin_factor_x : 1;
 344        buf->binning_factor_y = res->bin_factor_y ?
 345                                        res->bin_factor_y : 1;
 346        return 0;
 347}
 348
 349static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
 350                                 int gain, int digitgain)
 351
 352{
 353        struct i2c_client *client = v4l2_get_subdevdata(sd);
 354        u16 coarse_integration = (u16)coarse_itg;
 355        int ret = 0;
 356        u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0;
 357        expo_coarse_h = coarse_integration >> 8;
 358        expo_coarse_l = coarse_integration & 0xff;
 359
 360        ret = gc2235_write_reg(client, GC2235_8BIT,
 361                                        GC2235_EXPOSURE_H, expo_coarse_h);
 362        ret = gc2235_write_reg(client, GC2235_8BIT,
 363                                        GC2235_EXPOSURE_L, expo_coarse_l);
 364
 365        if (gain <= 0x58) {
 366                gain_val = 0x40;
 367                gain_val2 = 0x58;
 368        } else if (gain < 256) {
 369                gain_val = 0x40;
 370                gain_val2 = gain;
 371        } else {
 372                gain_val2 = 64 * gain / 256;
 373                gain_val = 0xff;
 374        }
 375
 376        ret = gc2235_write_reg(client, GC2235_8BIT,
 377                                        GC2235_GLOBAL_GAIN, (u8)gain_val);
 378        ret = gc2235_write_reg(client, GC2235_8BIT,
 379                                        GC2235_PRE_GAIN, (u8)gain_val2);
 380
 381        return ret;
 382}
 383
 384
 385static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure,
 386        int gain, int digitgain)
 387{
 388        struct gc2235_device *dev = to_gc2235_sensor(sd);
 389        int ret;
 390
 391        mutex_lock(&dev->input_lock);
 392        ret = __gc2235_set_exposure(sd, exposure, gain, digitgain);
 393        mutex_unlock(&dev->input_lock);
 394
 395        return ret;
 396}
 397
 398static long gc2235_s_exposure(struct v4l2_subdev *sd,
 399                               struct atomisp_exposure *exposure)
 400{
 401        int exp = exposure->integration_time[0];
 402        int gain = exposure->gain[0];
 403        int digitgain = exposure->gain[1];
 404
 405        /* we should not accept the invalid value below. */
 406        if (gain == 0) {
 407                struct i2c_client *client = v4l2_get_subdevdata(sd);
 408                v4l2_err(client, "%s: invalid value\n", __func__);
 409                return -EINVAL;
 410        }
 411
 412        return gc2235_set_exposure(sd, exp, gain, digitgain);
 413}
 414static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
 415{
 416        switch (cmd) {
 417        case ATOMISP_IOC_S_EXPOSURE:
 418                return gc2235_s_exposure(sd, arg);
 419        default:
 420                return -EINVAL;
 421        }
 422        return 0;
 423}
 424/* This returns the exposure time being used. This should only be used
 425 * for filling in EXIF data, not for actual image processing.
 426 */
 427static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value)
 428{
 429        struct i2c_client *client = v4l2_get_subdevdata(sd);
 430        u16 reg_v, reg_v2;
 431        int ret;
 432
 433        /* get exposure */
 434        ret = gc2235_read_reg(client, GC2235_8BIT,
 435                                        GC2235_EXPOSURE_L,
 436                                        &reg_v);
 437        if (ret)
 438                goto err;
 439
 440        ret = gc2235_read_reg(client, GC2235_8BIT,
 441                                        GC2235_EXPOSURE_H,
 442                                        &reg_v2);
 443        if (ret)
 444                goto err;
 445
 446        reg_v += reg_v2 << 8;
 447
 448        *value = reg_v;
 449err:
 450        return ret;
 451}
 452
 453static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 454{
 455        struct gc2235_device *dev =
 456            container_of(ctrl->handler, struct gc2235_device, ctrl_handler);
 457        int ret = 0;
 458
 459        switch (ctrl->id) {
 460        case V4L2_CID_EXPOSURE_ABSOLUTE:
 461                ret = gc2235_q_exposure(&dev->sd, &ctrl->val);
 462                break;
 463        case V4L2_CID_FOCAL_ABSOLUTE:
 464                ret = gc2235_g_focal(&dev->sd, &ctrl->val);
 465                break;
 466        case V4L2_CID_FNUMBER_ABSOLUTE:
 467                ret = gc2235_g_fnumber(&dev->sd, &ctrl->val);
 468                break;
 469        case V4L2_CID_FNUMBER_RANGE:
 470                ret = gc2235_g_fnumber_range(&dev->sd, &ctrl->val);
 471                break;
 472        default:
 473                ret = -EINVAL;
 474        }
 475
 476        return ret;
 477}
 478
 479static const struct v4l2_ctrl_ops ctrl_ops = {
 480        .g_volatile_ctrl = gc2235_g_volatile_ctrl
 481};
 482
 483static struct v4l2_ctrl_config gc2235_controls[] = {
 484        {
 485         .ops = &ctrl_ops,
 486         .id = V4L2_CID_EXPOSURE_ABSOLUTE,
 487         .type = V4L2_CTRL_TYPE_INTEGER,
 488         .name = "exposure",
 489         .min = 0x0,
 490         .max = 0xffff,
 491         .step = 0x01,
 492         .def = 0x00,
 493         .flags = 0,
 494         },
 495        {
 496         .ops = &ctrl_ops,
 497         .id = V4L2_CID_FOCAL_ABSOLUTE,
 498         .type = V4L2_CTRL_TYPE_INTEGER,
 499         .name = "focal length",
 500         .min = GC2235_FOCAL_LENGTH_DEFAULT,
 501         .max = GC2235_FOCAL_LENGTH_DEFAULT,
 502         .step = 0x01,
 503         .def = GC2235_FOCAL_LENGTH_DEFAULT,
 504         .flags = 0,
 505         },
 506        {
 507         .ops = &ctrl_ops,
 508         .id = V4L2_CID_FNUMBER_ABSOLUTE,
 509         .type = V4L2_CTRL_TYPE_INTEGER,
 510         .name = "f-number",
 511         .min = GC2235_F_NUMBER_DEFAULT,
 512         .max = GC2235_F_NUMBER_DEFAULT,
 513         .step = 0x01,
 514         .def = GC2235_F_NUMBER_DEFAULT,
 515         .flags = 0,
 516         },
 517        {
 518         .ops = &ctrl_ops,
 519         .id = V4L2_CID_FNUMBER_RANGE,
 520         .type = V4L2_CTRL_TYPE_INTEGER,
 521         .name = "f-number range",
 522         .min = GC2235_F_NUMBER_RANGE,
 523         .max = GC2235_F_NUMBER_RANGE,
 524         .step = 0x01,
 525         .def = GC2235_F_NUMBER_RANGE,
 526         .flags = 0,
 527         },
 528};
 529
 530static int __gc2235_init(struct v4l2_subdev *sd)
 531{
 532        struct i2c_client *client = v4l2_get_subdevdata(sd);
 533
 534        /* restore settings */
 535        gc2235_res = gc2235_res_preview;
 536        N_RES = N_RES_PREVIEW;
 537
 538        return gc2235_write_reg_array(client, gc2235_init_settings);
 539}
 540
 541static int is_init;
 542
 543static int power_ctrl(struct v4l2_subdev *sd, bool flag)
 544{
 545        int ret = -1;
 546        struct gc2235_device *dev = to_gc2235_sensor(sd);
 547
 548        if (!dev || !dev->platform_data)
 549                return -ENODEV;
 550
 551        /* Non-gmin platforms use the legacy callback */
 552        if (dev->platform_data->power_ctrl)
 553                return dev->platform_data->power_ctrl(sd, flag);
 554
 555        if (flag) {
 556                ret = dev->platform_data->v1p8_ctrl(sd, 1);
 557                usleep_range(60, 90);
 558                if (ret == 0)
 559                        ret |= dev->platform_data->v2p8_ctrl(sd, 1);
 560        } else {
 561                ret = dev->platform_data->v1p8_ctrl(sd, 0);
 562                ret |= dev->platform_data->v2p8_ctrl(sd, 0);
 563        }
 564        return ret;
 565}
 566
 567static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
 568{
 569        struct gc2235_device *dev = to_gc2235_sensor(sd);
 570        int ret = -1;
 571
 572        if (!dev || !dev->platform_data)
 573                return -ENODEV;
 574
 575        /* Non-gmin platforms use the legacy callback */
 576        if (dev->platform_data->gpio_ctrl)
 577                return dev->platform_data->gpio_ctrl(sd, flag);
 578
 579        ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
 580        usleep_range(60, 90);
 581        return dev->platform_data->gpio0_ctrl(sd, flag);
 582}
 583
 584static int power_up(struct v4l2_subdev *sd)
 585{
 586        struct gc2235_device *dev = to_gc2235_sensor(sd);
 587        struct i2c_client *client = v4l2_get_subdevdata(sd);
 588        int ret;
 589
 590        if (!dev->platform_data) {
 591                dev_err(&client->dev,
 592                        "no camera_sensor_platform_data");
 593                return -ENODEV;
 594        }
 595        /* power control */
 596        ret = power_ctrl(sd, 1);
 597        if (ret)
 598                goto fail_power;
 599
 600        /* according to DS, at least 5ms is needed between DOVDD and PWDN */
 601        usleep_range(5000, 6000);
 602
 603        ret = dev->platform_data->flisclk_ctrl(sd, 1);
 604        if (ret)
 605                goto fail_clk;
 606        usleep_range(5000, 6000);
 607
 608        /* gpio ctrl */
 609        ret = gpio_ctrl(sd, 1);
 610        if (ret) {
 611                ret = gpio_ctrl(sd, 1);
 612                if (ret)
 613                        goto fail_power;
 614        }
 615
 616        msleep(5);
 617        return 0;
 618
 619fail_clk:
 620        gpio_ctrl(sd, 0);
 621fail_power:
 622        power_ctrl(sd, 0);
 623        dev_err(&client->dev, "sensor power-up failed\n");
 624
 625        return ret;
 626}
 627
 628static int power_down(struct v4l2_subdev *sd)
 629{
 630        struct gc2235_device *dev = to_gc2235_sensor(sd);
 631        struct i2c_client *client = v4l2_get_subdevdata(sd);
 632        int ret = 0;
 633
 634        if (!dev->platform_data) {
 635                dev_err(&client->dev,
 636                        "no camera_sensor_platform_data");
 637                return -ENODEV;
 638        }
 639        /* gpio ctrl */
 640        ret = gpio_ctrl(sd, 0);
 641        if (ret) {
 642                ret = gpio_ctrl(sd, 0);
 643                if (ret)
 644                        dev_err(&client->dev, "gpio failed 2\n");
 645        }
 646
 647        ret = dev->platform_data->flisclk_ctrl(sd, 0);
 648        if (ret)
 649                dev_err(&client->dev, "flisclk failed\n");
 650
 651        /* power control */
 652        ret = power_ctrl(sd, 0);
 653        if (ret)
 654                dev_err(&client->dev, "vprog failed.\n");
 655
 656        return ret;
 657}
 658
 659static int gc2235_s_power(struct v4l2_subdev *sd, int on)
 660{
 661        int ret;
 662
 663        if (on == 0)
 664                ret = power_down(sd);
 665        else {
 666                ret = power_up(sd);
 667                if (!ret)
 668                        ret = __gc2235_init(sd);
 669                is_init = 1;
 670        }
 671        return ret;
 672}
 673
 674/*
 675 * distance - calculate the distance
 676 * @res: resolution
 677 * @w: width
 678 * @h: height
 679 *
 680 * Get the gap between resolution and w/h.
 681 * res->width/height smaller than w/h wouldn't be considered.
 682 * Returns the value of gap or -1 if fail.
 683 */
 684#define LARGEST_ALLOWED_RATIO_MISMATCH 800
 685static int distance(struct gc2235_resolution *res, u32 w, u32 h)
 686{
 687        unsigned int w_ratio = (res->width << 13) / w;
 688        unsigned int h_ratio;
 689        int match;
 690
 691        if (h == 0)
 692                return -1;
 693        h_ratio = (res->height << 13) / h;
 694        if (h_ratio == 0)
 695                return -1;
 696        match   = abs(((w_ratio << 13) / h_ratio) - 8192);
 697
 698        if ((w_ratio < 8192) || (h_ratio < 8192) ||
 699            (match > LARGEST_ALLOWED_RATIO_MISMATCH))
 700                return -1;
 701
 702        return w_ratio + h_ratio;
 703}
 704
 705/* Return the nearest higher resolution index */
 706static int nearest_resolution_index(int w, int h)
 707{
 708        int i;
 709        int idx = -1;
 710        int dist;
 711        int min_dist = INT_MAX;
 712        struct gc2235_resolution *tmp_res = NULL;
 713
 714        for (i = 0; i < N_RES; i++) {
 715                tmp_res = &gc2235_res[i];
 716                dist = distance(tmp_res, w, h);
 717                if (dist == -1)
 718                        continue;
 719                if (dist < min_dist) {
 720                        min_dist = dist;
 721                        idx = i;
 722                }
 723        }
 724
 725        return idx;
 726}
 727
 728static int get_resolution_index(int w, int h)
 729{
 730        int i;
 731
 732        for (i = 0; i < N_RES; i++) {
 733                if (w != gc2235_res[i].width)
 734                        continue;
 735                if (h != gc2235_res[i].height)
 736                        continue;
 737
 738                return i;
 739        }
 740
 741        return -1;
 742}
 743
 744static int startup(struct v4l2_subdev *sd)
 745{
 746        struct gc2235_device *dev = to_gc2235_sensor(sd);
 747        struct i2c_client *client = v4l2_get_subdevdata(sd);
 748        int ret = 0;
 749        if (is_init == 0) {
 750                /* force gc2235 to do a reset in res change, otherwise it
 751                * can not output normal after switching res. and it is not
 752                * necessary for first time run up after power on, for the sack
 753                * of performance
 754                */
 755                power_down(sd);
 756                power_up(sd);
 757                gc2235_write_reg_array(client, gc2235_init_settings);
 758        }
 759
 760        ret = gc2235_write_reg_array(client, gc2235_res[dev->fmt_idx].regs);
 761        if (ret) {
 762                dev_err(&client->dev, "gc2235 write register err.\n");
 763                return ret;
 764        }
 765        is_init = 0;
 766
 767        return ret;
 768}
 769
 770static int gc2235_set_fmt(struct v4l2_subdev *sd,
 771                          struct v4l2_subdev_pad_config *cfg,
 772                          struct v4l2_subdev_format *format)
 773{
 774
 775        struct v4l2_mbus_framefmt *fmt = &format->format;
 776        struct gc2235_device *dev = to_gc2235_sensor(sd);
 777        struct i2c_client *client = v4l2_get_subdevdata(sd);
 778        struct camera_mipi_info *gc2235_info = NULL;
 779        int ret = 0;
 780        int idx;
 781
 782        gc2235_info = v4l2_get_subdev_hostdata(sd);
 783        if (!gc2235_info)
 784                return -EINVAL;
 785        if (format->pad)
 786                return -EINVAL;
 787        if (!fmt)
 788                return -EINVAL;
 789        mutex_lock(&dev->input_lock);
 790        idx = nearest_resolution_index(fmt->width, fmt->height);
 791        if (idx == -1) {
 792                /* return the largest resolution */
 793                fmt->width = gc2235_res[N_RES - 1].width;
 794                fmt->height = gc2235_res[N_RES - 1].height;
 795        } else {
 796                fmt->width = gc2235_res[idx].width;
 797                fmt->height = gc2235_res[idx].height;
 798        }
 799        fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
 800        if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
 801                cfg->try_fmt = *fmt;
 802                mutex_unlock(&dev->input_lock);
 803                return 0;
 804        }
 805
 806        dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
 807        if (dev->fmt_idx == -1) {
 808                dev_err(&client->dev, "get resolution fail\n");
 809                mutex_unlock(&dev->input_lock);
 810                return -EINVAL;
 811        }
 812
 813        ret = startup(sd);
 814        if (ret) {
 815                dev_err(&client->dev, "gc2235 startup err\n");
 816                goto err;
 817        }
 818
 819        ret = gc2235_get_intg_factor(client, gc2235_info,
 820                                     &gc2235_res[dev->fmt_idx]);
 821        if (ret)
 822                dev_err(&client->dev, "failed to get integration_factor\n");
 823
 824err:
 825        mutex_unlock(&dev->input_lock);
 826        return ret;
 827}
 828
 829static int gc2235_get_fmt(struct v4l2_subdev *sd,
 830                          struct v4l2_subdev_pad_config *cfg,
 831                          struct v4l2_subdev_format *format)
 832{
 833        struct v4l2_mbus_framefmt *fmt = &format->format;
 834        struct gc2235_device *dev = to_gc2235_sensor(sd);
 835
 836        if (format->pad)
 837                return -EINVAL;
 838
 839        if (!fmt)
 840                return -EINVAL;
 841
 842        fmt->width = gc2235_res[dev->fmt_idx].width;
 843        fmt->height = gc2235_res[dev->fmt_idx].height;
 844        fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
 845
 846        return 0;
 847}
 848
 849static int gc2235_detect(struct i2c_client *client)
 850{
 851        struct i2c_adapter *adapter = client->adapter;
 852        u16 high, low;
 853        int ret;
 854        u16 id;
 855
 856        if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
 857                return -ENODEV;
 858
 859        ret = gc2235_read_reg(client, GC2235_8BIT,
 860                                        GC2235_SENSOR_ID_H, &high);
 861        if (ret) {
 862                dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
 863                return -ENODEV;
 864        }
 865        ret = gc2235_read_reg(client, GC2235_8BIT,
 866                                        GC2235_SENSOR_ID_L, &low);
 867        id = ((high << 8) | low);
 868
 869        if (id != GC2235_ID) {
 870                dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
 871                return -ENODEV;
 872        }
 873
 874        dev_info(&client->dev, "detect gc2235 success\n");
 875        return 0;
 876}
 877
 878static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
 879{
 880        struct gc2235_device *dev = to_gc2235_sensor(sd);
 881        struct i2c_client *client = v4l2_get_subdevdata(sd);
 882        int ret;
 883        mutex_lock(&dev->input_lock);
 884
 885        if (enable)
 886                ret = gc2235_write_reg_array(client, gc2235_stream_on);
 887        else
 888                ret = gc2235_write_reg_array(client, gc2235_stream_off);
 889
 890        mutex_unlock(&dev->input_lock);
 891        return ret;
 892}
 893
 894
 895static int gc2235_s_config(struct v4l2_subdev *sd,
 896                           int irq, void *platform_data)
 897{
 898        struct gc2235_device *dev = to_gc2235_sensor(sd);
 899        struct i2c_client *client = v4l2_get_subdevdata(sd);
 900        int ret = 0;
 901
 902        if (!platform_data)
 903                return -ENODEV;
 904
 905        dev->platform_data =
 906                (struct camera_sensor_platform_data *)platform_data;
 907
 908        mutex_lock(&dev->input_lock);
 909        if (dev->platform_data->platform_init) {
 910                ret = dev->platform_data->platform_init(client);
 911                if (ret) {
 912                        dev_err(&client->dev, "platform init err\n");
 913                        goto platform_init_failed;
 914                }
 915        }
 916        /* power off the module, then power on it in future
 917         * as first power on by board may not fulfill the
 918         * power on sequqence needed by the module
 919         */
 920        ret = power_down(sd);
 921        if (ret) {
 922                dev_err(&client->dev, "gc2235 power-off err.\n");
 923                goto fail_power_off;
 924        }
 925
 926        ret = power_up(sd);
 927        if (ret) {
 928                dev_err(&client->dev, "gc2235 power-up err.\n");
 929                goto fail_power_on;
 930        }
 931
 932        ret = dev->platform_data->csi_cfg(sd, 1);
 933        if (ret)
 934                goto fail_csi_cfg;
 935
 936        /* config & detect sensor */
 937        ret = gc2235_detect(client);
 938        if (ret) {
 939                dev_err(&client->dev, "gc2235_detect err s_config.\n");
 940                goto fail_csi_cfg;
 941        }
 942
 943        /* turn off sensor, after probed */
 944        ret = power_down(sd);
 945        if (ret) {
 946                dev_err(&client->dev, "gc2235 power-off err.\n");
 947                goto fail_csi_cfg;
 948        }
 949        mutex_unlock(&dev->input_lock);
 950
 951        return 0;
 952
 953fail_csi_cfg:
 954        dev->platform_data->csi_cfg(sd, 0);
 955fail_power_on:
 956        power_down(sd);
 957        dev_err(&client->dev, "sensor power-gating failed\n");
 958fail_power_off:
 959        if (dev->platform_data->platform_deinit)
 960                dev->platform_data->platform_deinit();
 961platform_init_failed:
 962        mutex_unlock(&dev->input_lock);
 963        return ret;
 964}
 965
 966static int gc2235_g_parm(struct v4l2_subdev *sd,
 967                        struct v4l2_streamparm *param)
 968{
 969        struct gc2235_device *dev = to_gc2235_sensor(sd);
 970        struct i2c_client *client = v4l2_get_subdevdata(sd);
 971
 972        if (!param)
 973                return -EINVAL;
 974
 975        if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 976                dev_err(&client->dev,  "unsupported buffer type.\n");
 977                return -EINVAL;
 978        }
 979
 980        memset(param, 0, sizeof(*param));
 981        param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 982
 983        if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
 984                param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
 985                param->parm.capture.timeperframe.numerator = 1;
 986                param->parm.capture.capturemode = dev->run_mode;
 987                param->parm.capture.timeperframe.denominator =
 988                        gc2235_res[dev->fmt_idx].fps;
 989        }
 990        return 0;
 991}
 992
 993static int gc2235_s_parm(struct v4l2_subdev *sd,
 994                        struct v4l2_streamparm *param)
 995{
 996        struct gc2235_device *dev = to_gc2235_sensor(sd);
 997        dev->run_mode = param->parm.capture.capturemode;
 998
 999        mutex_lock(&dev->input_lock);
1000        switch (dev->run_mode) {
1001        case CI_MODE_VIDEO:
1002                gc2235_res = gc2235_res_video;
1003                N_RES = N_RES_VIDEO;
1004                break;
1005        case CI_MODE_STILL_CAPTURE:
1006                gc2235_res = gc2235_res_still;
1007                N_RES = N_RES_STILL;
1008                break;
1009        default:
1010                gc2235_res = gc2235_res_preview;
1011                N_RES = N_RES_PREVIEW;
1012        }
1013        mutex_unlock(&dev->input_lock);
1014        return 0;
1015}
1016
1017static int gc2235_g_frame_interval(struct v4l2_subdev *sd,
1018                                   struct v4l2_subdev_frame_interval *interval)
1019{
1020        struct gc2235_device *dev = to_gc2235_sensor(sd);
1021
1022        interval->interval.numerator = 1;
1023        interval->interval.denominator = gc2235_res[dev->fmt_idx].fps;
1024
1025        return 0;
1026}
1027
1028static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
1029                                struct v4l2_subdev_pad_config *cfg,
1030                                struct v4l2_subdev_mbus_code_enum *code)
1031{
1032        if (code->index >= MAX_FMTS)
1033                return -EINVAL;
1034
1035        code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1036        return 0;
1037}
1038
1039static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
1040                                  struct v4l2_subdev_pad_config *cfg,
1041                                  struct v4l2_subdev_frame_size_enum *fse)
1042{
1043        int index = fse->index;
1044
1045        if (index >= N_RES)
1046                return -EINVAL;
1047
1048        fse->min_width = gc2235_res[index].width;
1049        fse->min_height = gc2235_res[index].height;
1050        fse->max_width = gc2235_res[index].width;
1051        fse->max_height = gc2235_res[index].height;
1052
1053        return 0;
1054
1055}
1056
1057static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1058{
1059        struct gc2235_device *dev = to_gc2235_sensor(sd);
1060
1061        mutex_lock(&dev->input_lock);
1062        *frames = gc2235_res[dev->fmt_idx].skip_frames;
1063        mutex_unlock(&dev->input_lock);
1064
1065        return 0;
1066}
1067
1068static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
1069        .g_skip_frames  = gc2235_g_skip_frames,
1070};
1071
1072static const struct v4l2_subdev_video_ops gc2235_video_ops = {
1073        .s_stream = gc2235_s_stream,
1074        .g_parm = gc2235_g_parm,
1075        .s_parm = gc2235_s_parm,
1076        .g_frame_interval = gc2235_g_frame_interval,
1077};
1078
1079static const struct v4l2_subdev_core_ops gc2235_core_ops = {
1080        .s_power = gc2235_s_power,
1081        .ioctl = gc2235_ioctl,
1082};
1083
1084static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
1085        .enum_mbus_code = gc2235_enum_mbus_code,
1086        .enum_frame_size = gc2235_enum_frame_size,
1087        .get_fmt = gc2235_get_fmt,
1088        .set_fmt = gc2235_set_fmt,
1089};
1090
1091static const struct v4l2_subdev_ops gc2235_ops = {
1092        .core = &gc2235_core_ops,
1093        .video = &gc2235_video_ops,
1094        .pad = &gc2235_pad_ops,
1095        .sensor = &gc2235_sensor_ops,
1096};
1097
1098static int gc2235_remove(struct i2c_client *client)
1099{
1100        struct v4l2_subdev *sd = i2c_get_clientdata(client);
1101        struct gc2235_device *dev = to_gc2235_sensor(sd);
1102        dev_dbg(&client->dev, "gc2235_remove...\n");
1103
1104        if (dev->platform_data->platform_deinit)
1105                dev->platform_data->platform_deinit();
1106
1107        dev->platform_data->csi_cfg(sd, 0);
1108
1109        v4l2_device_unregister_subdev(sd);
1110        media_entity_cleanup(&dev->sd.entity);
1111        v4l2_ctrl_handler_free(&dev->ctrl_handler);
1112        kfree(dev);
1113
1114        return 0;
1115}
1116
1117static int gc2235_probe(struct i2c_client *client,
1118                        const struct i2c_device_id *id)
1119{
1120        struct gc2235_device *dev;
1121        void *gcpdev;
1122        int ret;
1123        unsigned int i;
1124
1125        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1126        if (!dev) {
1127                dev_err(&client->dev, "out of memory\n");
1128                return -ENOMEM;
1129        }
1130
1131        mutex_init(&dev->input_lock);
1132
1133        dev->fmt_idx = 0;
1134        v4l2_i2c_subdev_init(&(dev->sd), client, &gc2235_ops);
1135
1136        gcpdev = client->dev.platform_data;
1137        if (ACPI_COMPANION(&client->dev))
1138                gcpdev = gmin_camera_platform_data(&dev->sd,
1139                                   ATOMISP_INPUT_FORMAT_RAW_10,
1140                                   atomisp_bayer_order_grbg);
1141
1142        ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
1143        if (ret)
1144                goto out_free;
1145
1146        dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1147        dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1148        dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1149        dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1150        ret =
1151            v4l2_ctrl_handler_init(&dev->ctrl_handler,
1152                                   ARRAY_SIZE(gc2235_controls));
1153        if (ret) {
1154                gc2235_remove(client);
1155                return ret;
1156        }
1157
1158        for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
1159                v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
1160                                     NULL);
1161
1162        if (dev->ctrl_handler.error) {
1163                gc2235_remove(client);
1164                return dev->ctrl_handler.error;
1165        }
1166
1167        /* Use same lock for controls as for everything else. */
1168        dev->ctrl_handler.lock = &dev->input_lock;
1169        dev->sd.ctrl_handler = &dev->ctrl_handler;
1170
1171        ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1172        if (ret)
1173                gc2235_remove(client);
1174
1175        if (ACPI_HANDLE(&client->dev))
1176                ret = atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA);
1177
1178        return ret;
1179out_free:
1180        v4l2_device_unregister_subdev(&dev->sd);
1181        kfree(dev);
1182
1183        return ret;
1184}
1185
1186static const struct acpi_device_id gc2235_acpi_match[] = {
1187        { "INT33F8" },
1188        {},
1189};
1190
1191MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
1192MODULE_DEVICE_TABLE(i2c, gc2235_id);
1193static struct i2c_driver gc2235_driver = {
1194        .driver = {
1195                .name = GC2235_NAME,
1196                .acpi_match_table = ACPI_PTR(gc2235_acpi_match),
1197        },
1198        .probe = gc2235_probe,
1199        .remove = gc2235_remove,
1200        .id_table = gc2235_id,
1201};
1202
1203static int init_gc2235(void)
1204{
1205        return i2c_add_driver(&gc2235_driver);
1206}
1207
1208static void exit_gc2235(void)
1209{
1210
1211        i2c_del_driver(&gc2235_driver);
1212}
1213
1214module_init(init_gc2235);
1215module_exit(exit_gc2235);
1216
1217MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1218MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1219MODULE_LICENSE("GPL");
1220