linux/drivers/staging/media/atomisp/i2c/gc0310.c
<<
>>
Prefs
   1/*
   2 * Support for GalaxyCore GC0310 VGA camera sensor.
   3 *
   4 * Copyright (c) 2013 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 <linux/io.h>
  33#include "../include/linux/atomisp_gmin_platform.h"
  34
  35#include "gc0310.h"
  36
  37/* i2c read/write stuff */
  38static int gc0310_read_reg(struct i2c_client *client,
  39                           u16 data_length, u8 reg, u8 *val)
  40{
  41        int err;
  42        struct i2c_msg msg[2];
  43        unsigned char data[1];
  44
  45        if (!client->adapter) {
  46                dev_err(&client->dev, "%s error, no client->adapter\n",
  47                        __func__);
  48                return -ENODEV;
  49        }
  50
  51        if (data_length != GC0310_8BIT) {
  52                dev_err(&client->dev, "%s error, invalid data length\n",
  53                        __func__);
  54                return -EINVAL;
  55        }
  56
  57        memset(msg, 0, sizeof(msg));
  58
  59        msg[0].addr = client->addr;
  60        msg[0].flags = 0;
  61        msg[0].len = I2C_MSG_LENGTH;
  62        msg[0].buf = data;
  63
  64        /* high byte goes out first */
  65        data[0] = (u8)(reg & 0xff);
  66
  67        msg[1].addr = client->addr;
  68        msg[1].len = data_length;
  69        msg[1].flags = I2C_M_RD;
  70        msg[1].buf = data;
  71
  72        err = i2c_transfer(client->adapter, msg, 2);
  73        if (err != 2) {
  74                if (err >= 0)
  75                        err = -EIO;
  76                dev_err(&client->dev,
  77                        "read from offset 0x%x error %d", reg, err);
  78                return err;
  79        }
  80
  81        *val = 0;
  82        /* high byte comes first */
  83        if (data_length == GC0310_8BIT)
  84                *val = (u8)data[0];
  85
  86        return 0;
  87}
  88
  89static int gc0310_i2c_write(struct i2c_client *client, u16 len, u8 *data)
  90{
  91        struct i2c_msg msg;
  92        const int num_msg = 1;
  93        int ret;
  94
  95        msg.addr = client->addr;
  96        msg.flags = 0;
  97        msg.len = len;
  98        msg.buf = data;
  99        ret = i2c_transfer(client->adapter, &msg, 1);
 100
 101        return ret == num_msg ? 0 : -EIO;
 102}
 103
 104static int gc0310_write_reg(struct i2c_client *client, u16 data_length,
 105                                                        u8 reg, u8 val)
 106{
 107        int ret;
 108        unsigned char data[2] = {0};
 109        u8 *wreg = (u8 *)data;
 110        const u16 len = data_length + sizeof(u8); /* 8-bit address + data */
 111
 112        if (data_length != GC0310_8BIT) {
 113                dev_err(&client->dev,
 114                        "%s error, invalid data_length\n", __func__);
 115                return -EINVAL;
 116        }
 117
 118        /* high byte goes out first */
 119        *wreg = (u8)(reg & 0xff);
 120
 121        if (data_length == GC0310_8BIT)
 122                data[1] = (u8)(val);
 123
 124        ret = gc0310_i2c_write(client, len, data);
 125        if (ret)
 126                dev_err(&client->dev,
 127                        "write error: wrote 0x%x to offset 0x%x error %d",
 128                        val, reg, ret);
 129
 130        return ret;
 131}
 132
 133/*
 134 * gc0310_write_reg_array - Initializes a list of GC0310 registers
 135 * @client: i2c driver client structure
 136 * @reglist: list of registers to be written
 137 *
 138 * This function initializes a list of registers. When consecutive addresses
 139 * are found in a row on the list, this function creates a buffer and sends
 140 * consecutive data in a single i2c_transfer().
 141 *
 142 * __gc0310_flush_reg_array, __gc0310_buf_reg_array() and
 143 * __gc0310_write_reg_is_consecutive() are internal functions to
 144 * gc0310_write_reg_array_fast() and should be not used anywhere else.
 145 *
 146 */
 147
 148static int __gc0310_flush_reg_array(struct i2c_client *client,
 149                                    struct gc0310_write_ctrl *ctrl)
 150{
 151        u16 size;
 152
 153        if (ctrl->index == 0)
 154                return 0;
 155
 156        size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
 157        ctrl->buffer.addr = (u8)(ctrl->buffer.addr);
 158        ctrl->index = 0;
 159
 160        return gc0310_i2c_write(client, size, (u8 *)&ctrl->buffer);
 161}
 162
 163static int __gc0310_buf_reg_array(struct i2c_client *client,
 164                                  struct gc0310_write_ctrl *ctrl,
 165                                  const struct gc0310_reg *next)
 166{
 167        int size;
 168
 169        switch (next->type) {
 170        case GC0310_8BIT:
 171                size = 1;
 172                ctrl->buffer.data[ctrl->index] = (u8)next->val;
 173                break;
 174        default:
 175                return -EINVAL;
 176        }
 177
 178        /* When first item is added, we need to store its starting address */
 179        if (ctrl->index == 0)
 180                ctrl->buffer.addr = next->reg;
 181
 182        ctrl->index += size;
 183
 184        /*
 185         * Buffer cannot guarantee free space for u32? Better flush it to avoid
 186         * possible lack of memory for next item.
 187         */
 188        if (ctrl->index + sizeof(u8) >= GC0310_MAX_WRITE_BUF_SIZE)
 189                return __gc0310_flush_reg_array(client, ctrl);
 190
 191        return 0;
 192}
 193
 194static int __gc0310_write_reg_is_consecutive(struct i2c_client *client,
 195                                             struct gc0310_write_ctrl *ctrl,
 196                                             const struct gc0310_reg *next)
 197{
 198        if (ctrl->index == 0)
 199                return 1;
 200
 201        return ctrl->buffer.addr + ctrl->index == next->reg;
 202}
 203
 204static int gc0310_write_reg_array(struct i2c_client *client,
 205                                  const struct gc0310_reg *reglist)
 206{
 207        const struct gc0310_reg *next = reglist;
 208        struct gc0310_write_ctrl ctrl;
 209        int err;
 210
 211        ctrl.index = 0;
 212        for (; next->type != GC0310_TOK_TERM; next++) {
 213                switch (next->type & GC0310_TOK_MASK) {
 214                case GC0310_TOK_DELAY:
 215                        err = __gc0310_flush_reg_array(client, &ctrl);
 216                        if (err)
 217                                return err;
 218                        msleep(next->val);
 219                        break;
 220                default:
 221                        /*
 222                         * If next address is not consecutive, data needs to be
 223                         * flushed before proceed.
 224                         */
 225                        if (!__gc0310_write_reg_is_consecutive(client, &ctrl,
 226                                                                next)) {
 227                                err = __gc0310_flush_reg_array(client, &ctrl);
 228                                if (err)
 229                                        return err;
 230                        }
 231                        err = __gc0310_buf_reg_array(client, &ctrl, next);
 232                        if (err) {
 233                                dev_err(&client->dev, "%s: write error, aborted\n",
 234                                         __func__);
 235                                return err;
 236                        }
 237                        break;
 238                }
 239        }
 240
 241        return __gc0310_flush_reg_array(client, &ctrl);
 242}
 243static int gc0310_g_focal(struct v4l2_subdev *sd, s32 *val)
 244{
 245        *val = (GC0310_FOCAL_LENGTH_NUM << 16) | GC0310_FOCAL_LENGTH_DEM;
 246        return 0;
 247}
 248
 249static int gc0310_g_fnumber(struct v4l2_subdev *sd, s32 *val)
 250{
 251        /*const f number for imx*/
 252        *val = (GC0310_F_NUMBER_DEFAULT_NUM << 16) | GC0310_F_NUMBER_DEM;
 253        return 0;
 254}
 255
 256static int gc0310_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
 257{
 258        *val = (GC0310_F_NUMBER_DEFAULT_NUM << 24) |
 259                (GC0310_F_NUMBER_DEM << 16) |
 260                (GC0310_F_NUMBER_DEFAULT_NUM << 8) | GC0310_F_NUMBER_DEM;
 261        return 0;
 262}
 263
 264static int gc0310_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
 265{
 266        struct gc0310_device *dev = to_gc0310_sensor(sd);
 267
 268        *val = gc0310_res[dev->fmt_idx].bin_factor_x;
 269
 270        return 0;
 271}
 272
 273static int gc0310_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
 274{
 275        struct gc0310_device *dev = to_gc0310_sensor(sd);
 276
 277        *val = gc0310_res[dev->fmt_idx].bin_factor_y;
 278
 279        return 0;
 280}
 281
 282static int gc0310_get_intg_factor(struct i2c_client *client,
 283                                struct camera_mipi_info *info,
 284                                const struct gc0310_resolution *res)
 285{
 286        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 287        struct gc0310_device *dev = to_gc0310_sensor(sd);
 288        struct atomisp_sensor_mode_data *buf = &info->data;
 289        u16 val;
 290        u8 reg_val;
 291        int ret;
 292        unsigned int hori_blanking;
 293        unsigned int vert_blanking;
 294        unsigned int sh_delay;
 295
 296        if (!info)
 297                return -EINVAL;
 298
 299        /* pixel clock calculattion */
 300        dev->vt_pix_clk_freq_mhz = 14400000; // 16.8MHz
 301        buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz;
 302        pr_info("vt_pix_clk_freq_mhz=%d\n", buf->vt_pix_clk_freq_mhz);
 303
 304        /* get integration time */
 305        buf->coarse_integration_time_min = GC0310_COARSE_INTG_TIME_MIN;
 306        buf->coarse_integration_time_max_margin =
 307                                        GC0310_COARSE_INTG_TIME_MAX_MARGIN;
 308
 309        buf->fine_integration_time_min = GC0310_FINE_INTG_TIME_MIN;
 310        buf->fine_integration_time_max_margin =
 311                                        GC0310_FINE_INTG_TIME_MAX_MARGIN;
 312
 313        buf->fine_integration_time_def = GC0310_FINE_INTG_TIME_MIN;
 314        buf->read_mode = res->bin_mode;
 315
 316        /* get the cropping and output resolution to ISP for this mode. */
 317        /* Getting crop_horizontal_start */
 318        ret =  gc0310_read_reg(client, GC0310_8BIT,
 319                                        GC0310_H_CROP_START_H, &reg_val);
 320        if (ret)
 321                return ret;
 322        val = (reg_val & 0xFF) << 8;
 323        ret =  gc0310_read_reg(client, GC0310_8BIT,
 324                                        GC0310_H_CROP_START_L, &reg_val);
 325        if (ret)
 326                return ret;
 327        buf->crop_horizontal_start = val | (reg_val & 0xFF);
 328        pr_info("crop_horizontal_start=%d\n", buf->crop_horizontal_start);
 329
 330        /* Getting crop_vertical_start */
 331        ret =  gc0310_read_reg(client, GC0310_8BIT,
 332                                        GC0310_V_CROP_START_H, &reg_val);
 333        if (ret)
 334                return ret;
 335        val = (reg_val & 0xFF) << 8;
 336        ret =  gc0310_read_reg(client, GC0310_8BIT,
 337                                        GC0310_V_CROP_START_L, &reg_val);
 338        if (ret)
 339                return ret;
 340        buf->crop_vertical_start = val | (reg_val & 0xFF);
 341        pr_info("crop_vertical_start=%d\n", buf->crop_vertical_start);
 342
 343        /* Getting output_width */
 344        ret = gc0310_read_reg(client, GC0310_8BIT,
 345                                        GC0310_H_OUTSIZE_H, &reg_val);
 346        if (ret)
 347                return ret;
 348        val = (reg_val & 0xFF) << 8;
 349        ret = gc0310_read_reg(client, GC0310_8BIT,
 350                                        GC0310_H_OUTSIZE_L, &reg_val);
 351        if (ret)
 352                return ret;
 353        buf->output_width = val | (reg_val & 0xFF);
 354        pr_info("output_width=%d\n", buf->output_width);
 355
 356        /* Getting output_height */
 357        ret = gc0310_read_reg(client, GC0310_8BIT,
 358                                        GC0310_V_OUTSIZE_H, &reg_val);
 359        if (ret)
 360                return ret;
 361        val = (reg_val & 0xFF) << 8;
 362        ret = gc0310_read_reg(client, GC0310_8BIT,
 363                                        GC0310_V_OUTSIZE_L, &reg_val);
 364        if (ret)
 365                return ret;
 366        buf->output_height = val | (reg_val & 0xFF);
 367        pr_info("output_height=%d\n", buf->output_height);
 368
 369        buf->crop_horizontal_end = buf->crop_horizontal_start + buf->output_width - 1;
 370        buf->crop_vertical_end = buf->crop_vertical_start + buf->output_height - 1;
 371        pr_info("crop_horizontal_end=%d\n", buf->crop_horizontal_end);
 372        pr_info("crop_vertical_end=%d\n", buf->crop_vertical_end);
 373
 374        /* Getting line_length_pck */
 375        ret = gc0310_read_reg(client, GC0310_8BIT,
 376                                        GC0310_H_BLANKING_H, &reg_val);
 377        if (ret)
 378                return ret;
 379        val = (reg_val & 0xFF) << 8;
 380        ret = gc0310_read_reg(client, GC0310_8BIT,
 381                                        GC0310_H_BLANKING_L, &reg_val);
 382        if (ret)
 383                return ret;
 384        hori_blanking = val | (reg_val & 0xFF);
 385        ret = gc0310_read_reg(client, GC0310_8BIT,
 386                                        GC0310_SH_DELAY, &reg_val);
 387        if (ret)
 388                return ret;
 389        sh_delay = reg_val;
 390        buf->line_length_pck = buf->output_width + hori_blanking + sh_delay + 4;
 391        pr_info("hori_blanking=%d sh_delay=%d line_length_pck=%d\n", hori_blanking, sh_delay, buf->line_length_pck);
 392
 393        /* Getting frame_length_lines */
 394        ret = gc0310_read_reg(client, GC0310_8BIT,
 395                                        GC0310_V_BLANKING_H, &reg_val);
 396        if (ret)
 397                return ret;
 398        val = (reg_val & 0xFF) << 8;
 399        ret = gc0310_read_reg(client, GC0310_8BIT,
 400                                        GC0310_V_BLANKING_L, &reg_val);
 401        if (ret)
 402                return ret;
 403        vert_blanking = val | (reg_val & 0xFF);
 404        buf->frame_length_lines = buf->output_height + vert_blanking;
 405        pr_info("vert_blanking=%d frame_length_lines=%d\n", vert_blanking, buf->frame_length_lines);
 406
 407        buf->binning_factor_x = res->bin_factor_x ?
 408                                        res->bin_factor_x : 1;
 409        buf->binning_factor_y = res->bin_factor_y ?
 410                                        res->bin_factor_y : 1;
 411        return 0;
 412}
 413
 414static int gc0310_set_gain(struct v4l2_subdev *sd, int gain)
 415
 416{
 417        struct i2c_client *client = v4l2_get_subdevdata(sd);
 418        int ret;
 419        u8 again, dgain;
 420
 421        if (gain < 0x20)
 422                gain = 0x20;
 423        if (gain > 0x80)
 424                gain = 0x80;
 425
 426        if (gain >= 0x20 && gain < 0x40) {
 427                again = 0x0; /* sqrt(2) */
 428                dgain = gain;
 429        } else {
 430                again = 0x2; /* 2 * sqrt(2) */
 431                dgain = gain / 2;
 432        }
 433
 434        pr_info("gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain);
 435
 436        /* set analog gain */
 437        ret = gc0310_write_reg(client, GC0310_8BIT,
 438                                        GC0310_AGC_ADJ, again);
 439        if (ret)
 440                return ret;
 441
 442        /* set digital gain */
 443        ret = gc0310_write_reg(client, GC0310_8BIT,
 444                                        GC0310_DGC_ADJ, dgain);
 445        if (ret)
 446                return ret;
 447
 448        return 0;
 449}
 450
 451static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
 452                                 int gain, int digitgain)
 453
 454{
 455        struct i2c_client *client = v4l2_get_subdevdata(sd);
 456        int ret;
 457
 458        pr_info("coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain);
 459
 460        /* set exposure */
 461        ret = gc0310_write_reg(client, GC0310_8BIT,
 462                                        GC0310_AEC_PK_EXPO_L,
 463                                        coarse_itg & 0xff);
 464        if (ret)
 465                return ret;
 466
 467        ret = gc0310_write_reg(client, GC0310_8BIT,
 468                                        GC0310_AEC_PK_EXPO_H,
 469                                        (coarse_itg >> 8) & 0x0f);
 470        if (ret)
 471                return ret;
 472
 473        ret = gc0310_set_gain(sd, gain);
 474        if (ret)
 475                return ret;
 476
 477        return ret;
 478}
 479
 480static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure,
 481        int gain, int digitgain)
 482{
 483        struct gc0310_device *dev = to_gc0310_sensor(sd);
 484        int ret;
 485
 486        mutex_lock(&dev->input_lock);
 487        ret = __gc0310_set_exposure(sd, exposure, gain, digitgain);
 488        mutex_unlock(&dev->input_lock);
 489
 490        return ret;
 491}
 492
 493static long gc0310_s_exposure(struct v4l2_subdev *sd,
 494                               struct atomisp_exposure *exposure)
 495{
 496        int exp = exposure->integration_time[0];
 497        int gain = exposure->gain[0];
 498        int digitgain = exposure->gain[1];
 499
 500        /* we should not accept the invalid value below. */
 501        if (gain == 0) {
 502                struct i2c_client *client = v4l2_get_subdevdata(sd);
 503                v4l2_err(client, "%s: invalid value\n", __func__);
 504                return -EINVAL;
 505        }
 506
 507        return gc0310_set_exposure(sd, exp, gain, digitgain);
 508}
 509
 510/* TO DO */
 511static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value)
 512{
 513        return 0;
 514}
 515
 516/* TO DO */
 517static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value)
 518{
 519        return 0;
 520}
 521
 522static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
 523{
 524
 525        switch (cmd) {
 526        case ATOMISP_IOC_S_EXPOSURE:
 527                return gc0310_s_exposure(sd, arg);
 528        default:
 529                return -EINVAL;
 530        }
 531        return 0;
 532}
 533
 534/* This returns the exposure time being used. This should only be used
 535 * for filling in EXIF data, not for actual image processing.
 536 */
 537static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value)
 538{
 539        struct i2c_client *client = v4l2_get_subdevdata(sd);
 540        u8 reg_v;
 541        int ret;
 542
 543        /* get exposure */
 544        ret = gc0310_read_reg(client, GC0310_8BIT,
 545                                        GC0310_AEC_PK_EXPO_L,
 546                                        &reg_v);
 547        if (ret)
 548                goto err;
 549
 550        *value = reg_v;
 551        ret = gc0310_read_reg(client, GC0310_8BIT,
 552                                        GC0310_AEC_PK_EXPO_H,
 553                                        &reg_v);
 554        if (ret)
 555                goto err;
 556
 557        *value = *value + (reg_v << 8);
 558err:
 559        return ret;
 560}
 561
 562static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl)
 563{
 564        struct gc0310_device *dev =
 565            container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
 566        struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
 567        int ret = 0;
 568
 569        switch (ctrl->id) {
 570        case V4L2_CID_VFLIP:
 571                dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
 572                        __func__, ctrl->val);
 573                ret = gc0310_v_flip(&dev->sd, ctrl->val);
 574                break;
 575        case V4L2_CID_HFLIP:
 576                dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
 577                        __func__, ctrl->val);
 578                ret = gc0310_h_flip(&dev->sd, ctrl->val);
 579                break;
 580        default:
 581                ret = -EINVAL;
 582        }
 583        return ret;
 584}
 585
 586static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 587{
 588        struct gc0310_device *dev =
 589            container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
 590        int ret = 0;
 591
 592        switch (ctrl->id) {
 593        case V4L2_CID_EXPOSURE_ABSOLUTE:
 594                ret = gc0310_q_exposure(&dev->sd, &ctrl->val);
 595                break;
 596        case V4L2_CID_FOCAL_ABSOLUTE:
 597                ret = gc0310_g_focal(&dev->sd, &ctrl->val);
 598                break;
 599        case V4L2_CID_FNUMBER_ABSOLUTE:
 600                ret = gc0310_g_fnumber(&dev->sd, &ctrl->val);
 601                break;
 602        case V4L2_CID_FNUMBER_RANGE:
 603                ret = gc0310_g_fnumber_range(&dev->sd, &ctrl->val);
 604                break;
 605        case V4L2_CID_BIN_FACTOR_HORZ:
 606                ret = gc0310_g_bin_factor_x(&dev->sd, &ctrl->val);
 607                break;
 608        case V4L2_CID_BIN_FACTOR_VERT:
 609                ret = gc0310_g_bin_factor_y(&dev->sd, &ctrl->val);
 610                break;
 611        default:
 612                ret = -EINVAL;
 613        }
 614
 615        return ret;
 616}
 617
 618static const struct v4l2_ctrl_ops ctrl_ops = {
 619        .s_ctrl = gc0310_s_ctrl,
 620        .g_volatile_ctrl = gc0310_g_volatile_ctrl
 621};
 622
 623struct v4l2_ctrl_config gc0310_controls[] = {
 624        {
 625         .ops = &ctrl_ops,
 626         .id = V4L2_CID_EXPOSURE_ABSOLUTE,
 627         .type = V4L2_CTRL_TYPE_INTEGER,
 628         .name = "exposure",
 629         .min = 0x0,
 630         .max = 0xffff,
 631         .step = 0x01,
 632         .def = 0x00,
 633         .flags = 0,
 634         },
 635        {
 636         .ops = &ctrl_ops,
 637         .id = V4L2_CID_VFLIP,
 638         .type = V4L2_CTRL_TYPE_BOOLEAN,
 639         .name = "Flip",
 640         .min = 0,
 641         .max = 1,
 642         .step = 1,
 643         .def = 0,
 644         },
 645        {
 646         .ops = &ctrl_ops,
 647         .id = V4L2_CID_HFLIP,
 648         .type = V4L2_CTRL_TYPE_BOOLEAN,
 649         .name = "Mirror",
 650         .min = 0,
 651         .max = 1,
 652         .step = 1,
 653         .def = 0,
 654         },
 655        {
 656         .ops = &ctrl_ops,
 657         .id = V4L2_CID_FOCAL_ABSOLUTE,
 658         .type = V4L2_CTRL_TYPE_INTEGER,
 659         .name = "focal length",
 660         .min = GC0310_FOCAL_LENGTH_DEFAULT,
 661         .max = GC0310_FOCAL_LENGTH_DEFAULT,
 662         .step = 0x01,
 663         .def = GC0310_FOCAL_LENGTH_DEFAULT,
 664         .flags = 0,
 665         },
 666        {
 667         .ops = &ctrl_ops,
 668         .id = V4L2_CID_FNUMBER_ABSOLUTE,
 669         .type = V4L2_CTRL_TYPE_INTEGER,
 670         .name = "f-number",
 671         .min = GC0310_F_NUMBER_DEFAULT,
 672         .max = GC0310_F_NUMBER_DEFAULT,
 673         .step = 0x01,
 674         .def = GC0310_F_NUMBER_DEFAULT,
 675         .flags = 0,
 676         },
 677        {
 678         .ops = &ctrl_ops,
 679         .id = V4L2_CID_FNUMBER_RANGE,
 680         .type = V4L2_CTRL_TYPE_INTEGER,
 681         .name = "f-number range",
 682         .min = GC0310_F_NUMBER_RANGE,
 683         .max = GC0310_F_NUMBER_RANGE,
 684         .step = 0x01,
 685         .def = GC0310_F_NUMBER_RANGE,
 686         .flags = 0,
 687         },
 688        {
 689         .ops = &ctrl_ops,
 690         .id = V4L2_CID_BIN_FACTOR_HORZ,
 691         .type = V4L2_CTRL_TYPE_INTEGER,
 692         .name = "horizontal binning factor",
 693         .min = 0,
 694         .max = GC0310_BIN_FACTOR_MAX,
 695         .step = 1,
 696         .def = 0,
 697         .flags = 0,
 698         },
 699        {
 700         .ops = &ctrl_ops,
 701         .id = V4L2_CID_BIN_FACTOR_VERT,
 702         .type = V4L2_CTRL_TYPE_INTEGER,
 703         .name = "vertical binning factor",
 704         .min = 0,
 705         .max = GC0310_BIN_FACTOR_MAX,
 706         .step = 1,
 707         .def = 0,
 708         .flags = 0,
 709         },
 710};
 711
 712static int gc0310_init(struct v4l2_subdev *sd)
 713{
 714        int ret;
 715        struct i2c_client *client = v4l2_get_subdevdata(sd);
 716        struct gc0310_device *dev = to_gc0310_sensor(sd);
 717
 718        pr_info("%s S\n", __func__);
 719        mutex_lock(&dev->input_lock);
 720
 721        /* set inital registers */
 722        ret  = gc0310_write_reg_array(client, gc0310_reset_register);
 723
 724        /* restore settings */
 725        gc0310_res = gc0310_res_preview;
 726        N_RES = N_RES_PREVIEW;
 727
 728        mutex_unlock(&dev->input_lock);
 729
 730        pr_info("%s E\n", __func__);
 731        return 0;
 732}
 733
 734static int power_ctrl(struct v4l2_subdev *sd, bool flag)
 735{
 736        int ret = 0;
 737        struct gc0310_device *dev = to_gc0310_sensor(sd);
 738        if (!dev || !dev->platform_data)
 739                return -ENODEV;
 740
 741        /* Non-gmin platforms use the legacy callback */
 742        if (dev->platform_data->power_ctrl)
 743                return dev->platform_data->power_ctrl(sd, flag);
 744
 745        if (flag) {
 746                /* The upstream module driver (written to Crystal
 747                 * Cove) had this logic to pulse the rails low first.
 748                 * This appears to break things on the MRD7 with the
 749                 * X-Powers PMIC...
 750                 *
 751                 *     ret = dev->platform_data->v1p8_ctrl(sd, 0);
 752                 *     ret |= dev->platform_data->v2p8_ctrl(sd, 0);
 753                 *     mdelay(50);
 754                 */
 755                ret |= dev->platform_data->v1p8_ctrl(sd, 1);
 756                ret |= dev->platform_data->v2p8_ctrl(sd, 1);
 757                usleep_range(10000, 15000);
 758        }
 759
 760        if (!flag || ret) {
 761                ret |= dev->platform_data->v1p8_ctrl(sd, 0);
 762                ret |= dev->platform_data->v2p8_ctrl(sd, 0);
 763        }
 764        return ret;
 765}
 766
 767static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
 768{
 769        int ret;
 770        struct gc0310_device *dev = to_gc0310_sensor(sd);
 771
 772        if (!dev || !dev->platform_data)
 773                return -ENODEV;
 774
 775        /* Non-gmin platforms use the legacy callback */
 776        if (dev->platform_data->gpio_ctrl)
 777                return dev->platform_data->gpio_ctrl(sd, flag);
 778
 779        /* GPIO0 == "reset" (active low), GPIO1 == "power down" */
 780        if (flag) {
 781                /* Pulse reset, then release power down */
 782                ret = dev->platform_data->gpio0_ctrl(sd, 0);
 783                usleep_range(5000, 10000);
 784                ret |= dev->platform_data->gpio0_ctrl(sd, 1);
 785                usleep_range(10000, 15000);
 786                ret |= dev->platform_data->gpio1_ctrl(sd, 0);
 787                usleep_range(10000, 15000);
 788        } else {
 789                ret = dev->platform_data->gpio1_ctrl(sd, 1);
 790                ret |= dev->platform_data->gpio0_ctrl(sd, 0);
 791        }
 792        return ret;
 793}
 794
 795
 796static int power_down(struct v4l2_subdev *sd);
 797
 798static int power_up(struct v4l2_subdev *sd)
 799{
 800        struct gc0310_device *dev = to_gc0310_sensor(sd);
 801        struct i2c_client *client = v4l2_get_subdevdata(sd);
 802        int ret;
 803
 804        pr_info("%s S\n", __func__);
 805        if (!dev->platform_data) {
 806                dev_err(&client->dev,
 807                        "no camera_sensor_platform_data");
 808                return -ENODEV;
 809        }
 810
 811        /* power control */
 812        ret = power_ctrl(sd, 1);
 813        if (ret)
 814                goto fail_power;
 815
 816        /* flis clock control */
 817        ret = dev->platform_data->flisclk_ctrl(sd, 1);
 818        if (ret)
 819                goto fail_clk;
 820
 821        /* gpio ctrl */
 822        ret = gpio_ctrl(sd, 1);
 823        if (ret) {
 824                ret = gpio_ctrl(sd, 1);
 825                if (ret)
 826                        goto fail_gpio;
 827        }
 828
 829        msleep(100);
 830
 831        pr_info("%s E\n", __func__);
 832        return 0;
 833
 834fail_gpio:
 835        dev->platform_data->flisclk_ctrl(sd, 0);
 836fail_clk:
 837        power_ctrl(sd, 0);
 838fail_power:
 839        dev_err(&client->dev, "sensor power-up failed\n");
 840
 841        return ret;
 842}
 843
 844static int power_down(struct v4l2_subdev *sd)
 845{
 846        struct gc0310_device *dev = to_gc0310_sensor(sd);
 847        struct i2c_client *client = v4l2_get_subdevdata(sd);
 848        int ret = 0;
 849
 850        if (!dev->platform_data) {
 851                dev_err(&client->dev,
 852                        "no camera_sensor_platform_data");
 853                return -ENODEV;
 854        }
 855
 856        /* gpio ctrl */
 857        ret = gpio_ctrl(sd, 0);
 858        if (ret) {
 859                ret = gpio_ctrl(sd, 0);
 860                if (ret)
 861                        dev_err(&client->dev, "gpio failed 2\n");
 862        }
 863
 864        ret = dev->platform_data->flisclk_ctrl(sd, 0);
 865        if (ret)
 866                dev_err(&client->dev, "flisclk failed\n");
 867
 868        /* power control */
 869        ret = power_ctrl(sd, 0);
 870        if (ret)
 871                dev_err(&client->dev, "vprog failed.\n");
 872
 873        return ret;
 874}
 875
 876static int gc0310_s_power(struct v4l2_subdev *sd, int on)
 877{
 878        int ret;
 879        if (on == 0)
 880                return power_down(sd);
 881        else {
 882                ret = power_up(sd);
 883                if (!ret)
 884                        return gc0310_init(sd);
 885        }
 886        return ret;
 887}
 888
 889/*
 890 * distance - calculate the distance
 891 * @res: resolution
 892 * @w: width
 893 * @h: height
 894 *
 895 * Get the gap between resolution and w/h.
 896 * res->width/height smaller than w/h wouldn't be considered.
 897 * Returns the value of gap or -1 if fail.
 898 */
 899#define LARGEST_ALLOWED_RATIO_MISMATCH 800
 900static int distance(struct gc0310_resolution *res, u32 w, u32 h)
 901{
 902        unsigned int w_ratio = (res->width << 13) / w;
 903        unsigned int h_ratio;
 904        int match;
 905
 906        if (h == 0)
 907                return -1;
 908        h_ratio = (res->height << 13) / h;
 909        if (h_ratio == 0)
 910                return -1;
 911        match   = abs(((w_ratio << 13) / h_ratio) - ((int)8192));
 912
 913        if ((w_ratio < (int)8192) || (h_ratio < (int)8192)  ||
 914                (match > LARGEST_ALLOWED_RATIO_MISMATCH))
 915                return -1;
 916
 917        return w_ratio + h_ratio;
 918}
 919
 920/* Return the nearest higher resolution index */
 921static int nearest_resolution_index(int w, int h)
 922{
 923        int i;
 924        int idx = -1;
 925        int dist;
 926        int min_dist = INT_MAX;
 927        struct gc0310_resolution *tmp_res = NULL;
 928
 929        for (i = 0; i < N_RES; i++) {
 930                tmp_res = &gc0310_res[i];
 931                dist = distance(tmp_res, w, h);
 932                if (dist == -1)
 933                        continue;
 934                if (dist < min_dist) {
 935                        min_dist = dist;
 936                        idx = i;
 937                }
 938        }
 939
 940        return idx;
 941}
 942
 943static int get_resolution_index(int w, int h)
 944{
 945        int i;
 946
 947        for (i = 0; i < N_RES; i++) {
 948                if (w != gc0310_res[i].width)
 949                        continue;
 950                if (h != gc0310_res[i].height)
 951                        continue;
 952
 953                return i;
 954        }
 955
 956        return -1;
 957}
 958
 959
 960/* TODO: remove it. */
 961static int startup(struct v4l2_subdev *sd)
 962{
 963        struct gc0310_device *dev = to_gc0310_sensor(sd);
 964        struct i2c_client *client = v4l2_get_subdevdata(sd);
 965        int ret = 0;
 966
 967        pr_info("%s S\n", __func__);
 968
 969        ret = gc0310_write_reg_array(client, gc0310_res[dev->fmt_idx].regs);
 970        if (ret) {
 971                dev_err(&client->dev, "gc0310 write register err.\n");
 972                return ret;
 973        }
 974
 975        pr_info("%s E\n", __func__);
 976        return ret;
 977}
 978
 979static int gc0310_set_fmt(struct v4l2_subdev *sd,
 980                          struct v4l2_subdev_pad_config *cfg,
 981                          struct v4l2_subdev_format *format)
 982{
 983        struct v4l2_mbus_framefmt *fmt = &format->format;
 984        struct gc0310_device *dev = to_gc0310_sensor(sd);
 985        struct i2c_client *client = v4l2_get_subdevdata(sd);
 986        struct camera_mipi_info *gc0310_info = NULL;
 987        int ret = 0;
 988        int idx = 0;
 989        pr_info("%s S\n", __func__);
 990
 991        if (format->pad)
 992                return -EINVAL;
 993
 994        if (!fmt)
 995                return -EINVAL;
 996
 997        gc0310_info = v4l2_get_subdev_hostdata(sd);
 998        if (!gc0310_info)
 999                return -EINVAL;
1000
1001        mutex_lock(&dev->input_lock);
1002
1003        idx = nearest_resolution_index(fmt->width, fmt->height);
1004        if (idx == -1) {
1005                /* return the largest resolution */
1006                fmt->width = gc0310_res[N_RES - 1].width;
1007                fmt->height = gc0310_res[N_RES - 1].height;
1008        } else {
1009                fmt->width = gc0310_res[idx].width;
1010                fmt->height = gc0310_res[idx].height;
1011        }
1012        fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1013
1014        if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1015                cfg->try_fmt = *fmt;
1016                mutex_unlock(&dev->input_lock);
1017                return 0;
1018        }
1019
1020        dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1021        if (dev->fmt_idx == -1) {
1022                dev_err(&client->dev, "get resolution fail\n");
1023                mutex_unlock(&dev->input_lock);
1024                return -EINVAL;
1025        }
1026
1027        printk("%s: before gc0310_write_reg_array %s\n", __FUNCTION__,
1028               gc0310_res[dev->fmt_idx].desc);
1029        ret = startup(sd);
1030        if (ret) {
1031                dev_err(&client->dev, "gc0310 startup err\n");
1032                goto err;
1033        }
1034
1035        ret = gc0310_get_intg_factor(client, gc0310_info,
1036                                     &gc0310_res[dev->fmt_idx]);
1037        if (ret) {
1038                dev_err(&client->dev, "failed to get integration_factor\n");
1039                goto err;
1040        }
1041
1042        pr_info("%s E\n", __func__);
1043err:
1044        mutex_unlock(&dev->input_lock);
1045        return ret;
1046}
1047
1048static int gc0310_get_fmt(struct v4l2_subdev *sd,
1049                          struct v4l2_subdev_pad_config *cfg,
1050                          struct v4l2_subdev_format *format)
1051{
1052        struct v4l2_mbus_framefmt *fmt = &format->format;
1053        struct gc0310_device *dev = to_gc0310_sensor(sd);
1054
1055        if (format->pad)
1056                return -EINVAL;
1057
1058        if (!fmt)
1059                return -EINVAL;
1060
1061        fmt->width = gc0310_res[dev->fmt_idx].width;
1062        fmt->height = gc0310_res[dev->fmt_idx].height;
1063        fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1064
1065        return 0;
1066}
1067
1068static int gc0310_detect(struct i2c_client *client)
1069{
1070        struct i2c_adapter *adapter = client->adapter;
1071        u8 high, low;
1072        int ret;
1073        u16 id;
1074
1075        pr_info("%s S\n", __func__);
1076        if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1077                return -ENODEV;
1078
1079        ret = gc0310_read_reg(client, GC0310_8BIT,
1080                                        GC0310_SC_CMMN_CHIP_ID_H, &high);
1081        if (ret) {
1082                dev_err(&client->dev, "read sensor_id_high failed\n");
1083                return -ENODEV;
1084        }
1085        ret = gc0310_read_reg(client, GC0310_8BIT,
1086                                        GC0310_SC_CMMN_CHIP_ID_L, &low);
1087        if (ret) {
1088                dev_err(&client->dev, "read sensor_id_low failed\n");
1089                return -ENODEV;
1090        }
1091        id = ((((u16) high) << 8) | (u16) low);
1092        pr_info("sensor ID = 0x%x\n", id);
1093
1094        if (id != GC0310_ID) {
1095                dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id, GC0310_ID);
1096                return -ENODEV;
1097        }
1098
1099        dev_dbg(&client->dev, "detect gc0310 success\n");
1100
1101        pr_info("%s E\n", __func__);
1102
1103        return 0;
1104}
1105
1106static int gc0310_s_stream(struct v4l2_subdev *sd, int enable)
1107{
1108        struct gc0310_device *dev = to_gc0310_sensor(sd);
1109        struct i2c_client *client = v4l2_get_subdevdata(sd);
1110        int ret;
1111
1112        pr_info("%s S enable=%d\n", __func__, enable);
1113        mutex_lock(&dev->input_lock);
1114
1115        if (enable) {
1116                /* enable per frame MIPI and sensor ctrl reset  */
1117                ret = gc0310_write_reg(client, GC0310_8BIT,
1118                                                0xFE, 0x30);
1119                if (ret) {
1120                        mutex_unlock(&dev->input_lock);
1121                        return ret;
1122                }
1123        }
1124
1125        ret = gc0310_write_reg(client, GC0310_8BIT,
1126                                GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3);
1127        if (ret) {
1128                mutex_unlock(&dev->input_lock);
1129                return ret;
1130        }
1131
1132        ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM,
1133                                enable ? GC0310_START_STREAMING :
1134                                GC0310_STOP_STREAMING);
1135        if (ret) {
1136                mutex_unlock(&dev->input_lock);
1137                return ret;
1138        }
1139
1140        ret = gc0310_write_reg(client, GC0310_8BIT,
1141                                GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0);
1142        if (ret) {
1143                mutex_unlock(&dev->input_lock);
1144                return ret;
1145        }
1146
1147        mutex_unlock(&dev->input_lock);
1148        pr_info("%s E\n", __func__);
1149        return ret;
1150}
1151
1152
1153static int gc0310_s_config(struct v4l2_subdev *sd,
1154                           int irq, void *platform_data)
1155{
1156        struct gc0310_device *dev = to_gc0310_sensor(sd);
1157        struct i2c_client *client = v4l2_get_subdevdata(sd);
1158        int ret = 0;
1159
1160        pr_info("%s S\n", __func__);
1161        if (!platform_data)
1162                return -ENODEV;
1163
1164        dev->platform_data =
1165                (struct camera_sensor_platform_data *)platform_data;
1166
1167        mutex_lock(&dev->input_lock);
1168        if (dev->platform_data->platform_init) {
1169                ret = dev->platform_data->platform_init(client);
1170                if (ret) {
1171                        dev_err(&client->dev, "platform init err\n");
1172                        goto platform_init_failed;
1173                }
1174        }
1175        /* power off the module, then power on it in future
1176         * as first power on by board may not fulfill the
1177         * power on sequqence needed by the module
1178         */
1179        ret = power_down(sd);
1180        if (ret) {
1181                dev_err(&client->dev, "gc0310 power-off err.\n");
1182                goto fail_power_off;
1183        }
1184
1185        ret = power_up(sd);
1186        if (ret) {
1187                dev_err(&client->dev, "gc0310 power-up err.\n");
1188                goto fail_power_on;
1189        }
1190
1191        ret = dev->platform_data->csi_cfg(sd, 1);
1192        if (ret)
1193                goto fail_csi_cfg;
1194
1195        /* config & detect sensor */
1196        ret = gc0310_detect(client);
1197        if (ret) {
1198                dev_err(&client->dev, "gc0310_detect err s_config.\n");
1199                goto fail_csi_cfg;
1200        }
1201
1202        /* turn off sensor, after probed */
1203        ret = power_down(sd);
1204        if (ret) {
1205                dev_err(&client->dev, "gc0310 power-off err.\n");
1206                goto fail_csi_cfg;
1207        }
1208        mutex_unlock(&dev->input_lock);
1209
1210        pr_info("%s E\n", __func__);
1211        return 0;
1212
1213fail_csi_cfg:
1214        dev->platform_data->csi_cfg(sd, 0);
1215fail_power_on:
1216        power_down(sd);
1217        dev_err(&client->dev, "sensor power-gating failed\n");
1218fail_power_off:
1219        if (dev->platform_data->platform_deinit)
1220                dev->platform_data->platform_deinit();
1221platform_init_failed:
1222        mutex_unlock(&dev->input_lock);
1223        return ret;
1224}
1225
1226static int gc0310_g_parm(struct v4l2_subdev *sd,
1227                        struct v4l2_streamparm *param)
1228{
1229        struct gc0310_device *dev = to_gc0310_sensor(sd);
1230        struct i2c_client *client = v4l2_get_subdevdata(sd);
1231
1232        if (!param)
1233                return -EINVAL;
1234
1235        if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1236                dev_err(&client->dev,  "unsupported buffer type.\n");
1237                return -EINVAL;
1238        }
1239
1240        memset(param, 0, sizeof(*param));
1241        param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1242
1243        if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
1244                param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1245                param->parm.capture.timeperframe.numerator = 1;
1246                param->parm.capture.capturemode = dev->run_mode;
1247                param->parm.capture.timeperframe.denominator =
1248                        gc0310_res[dev->fmt_idx].fps;
1249        }
1250        return 0;
1251}
1252
1253static int gc0310_s_parm(struct v4l2_subdev *sd,
1254                        struct v4l2_streamparm *param)
1255{
1256        struct gc0310_device *dev = to_gc0310_sensor(sd);
1257        dev->run_mode = param->parm.capture.capturemode;
1258
1259        mutex_lock(&dev->input_lock);
1260        switch (dev->run_mode) {
1261        case CI_MODE_VIDEO:
1262                gc0310_res = gc0310_res_video;
1263                N_RES = N_RES_VIDEO;
1264                break;
1265        case CI_MODE_STILL_CAPTURE:
1266                gc0310_res = gc0310_res_still;
1267                N_RES = N_RES_STILL;
1268                break;
1269        default:
1270                gc0310_res = gc0310_res_preview;
1271                N_RES = N_RES_PREVIEW;
1272        }
1273        mutex_unlock(&dev->input_lock);
1274        return 0;
1275}
1276
1277static int gc0310_g_frame_interval(struct v4l2_subdev *sd,
1278                                   struct v4l2_subdev_frame_interval *interval)
1279{
1280        struct gc0310_device *dev = to_gc0310_sensor(sd);
1281
1282        interval->interval.numerator = 1;
1283        interval->interval.denominator = gc0310_res[dev->fmt_idx].fps;
1284
1285        return 0;
1286}
1287
1288static int gc0310_enum_mbus_code(struct v4l2_subdev *sd,
1289                                 struct v4l2_subdev_pad_config *cfg,
1290                                 struct v4l2_subdev_mbus_code_enum *code)
1291{
1292        if (code->index >= MAX_FMTS)
1293                return -EINVAL;
1294
1295        code->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1296        return 0;
1297}
1298
1299static int gc0310_enum_frame_size(struct v4l2_subdev *sd,
1300                                  struct v4l2_subdev_pad_config *cfg,
1301                                  struct v4l2_subdev_frame_size_enum *fse)
1302{
1303        int index = fse->index;
1304
1305        if (index >= N_RES)
1306                return -EINVAL;
1307
1308        fse->min_width = gc0310_res[index].width;
1309        fse->min_height = gc0310_res[index].height;
1310        fse->max_width = gc0310_res[index].width;
1311        fse->max_height = gc0310_res[index].height;
1312
1313        return 0;
1314
1315}
1316
1317
1318static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1319{
1320        struct gc0310_device *dev = to_gc0310_sensor(sd);
1321
1322        mutex_lock(&dev->input_lock);
1323        *frames = gc0310_res[dev->fmt_idx].skip_frames;
1324        mutex_unlock(&dev->input_lock);
1325
1326        return 0;
1327}
1328
1329static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = {
1330        .g_skip_frames  = gc0310_g_skip_frames,
1331};
1332
1333static const struct v4l2_subdev_video_ops gc0310_video_ops = {
1334        .s_stream = gc0310_s_stream,
1335        .g_parm = gc0310_g_parm,
1336        .s_parm = gc0310_s_parm,
1337        .g_frame_interval = gc0310_g_frame_interval,
1338};
1339
1340static const struct v4l2_subdev_core_ops gc0310_core_ops = {
1341        .s_power = gc0310_s_power,
1342        .ioctl = gc0310_ioctl,
1343};
1344
1345static const struct v4l2_subdev_pad_ops gc0310_pad_ops = {
1346        .enum_mbus_code = gc0310_enum_mbus_code,
1347        .enum_frame_size = gc0310_enum_frame_size,
1348        .get_fmt = gc0310_get_fmt,
1349        .set_fmt = gc0310_set_fmt,
1350};
1351
1352static const struct v4l2_subdev_ops gc0310_ops = {
1353        .core = &gc0310_core_ops,
1354        .video = &gc0310_video_ops,
1355        .pad = &gc0310_pad_ops,
1356        .sensor = &gc0310_sensor_ops,
1357};
1358
1359static int gc0310_remove(struct i2c_client *client)
1360{
1361        struct v4l2_subdev *sd = i2c_get_clientdata(client);
1362        struct gc0310_device *dev = to_gc0310_sensor(sd);
1363        dev_dbg(&client->dev, "gc0310_remove...\n");
1364
1365        if (dev->platform_data->platform_deinit)
1366                dev->platform_data->platform_deinit();
1367
1368        dev->platform_data->csi_cfg(sd, 0);
1369
1370        v4l2_device_unregister_subdev(sd);
1371        media_entity_cleanup(&dev->sd.entity);
1372        v4l2_ctrl_handler_free(&dev->ctrl_handler);
1373        kfree(dev);
1374
1375        return 0;
1376}
1377
1378static int gc0310_probe(struct i2c_client *client,
1379                        const struct i2c_device_id *id)
1380{
1381        struct gc0310_device *dev;
1382        int ret;
1383        void *pdata;
1384        unsigned int i;
1385
1386        pr_info("%s S\n", __func__);
1387        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1388        if (!dev) {
1389                dev_err(&client->dev, "out of memory\n");
1390                return -ENOMEM;
1391        }
1392
1393        mutex_init(&dev->input_lock);
1394
1395        dev->fmt_idx = 0;
1396        v4l2_i2c_subdev_init(&(dev->sd), client, &gc0310_ops);
1397
1398        if (ACPI_COMPANION(&client->dev))
1399                pdata = gmin_camera_platform_data(&dev->sd,
1400                                                  ATOMISP_INPUT_FORMAT_RAW_8,
1401                                                  atomisp_bayer_order_grbg);
1402        else
1403                pdata = client->dev.platform_data;
1404
1405        if (!pdata) {
1406                ret = -EINVAL;
1407                goto out_free;
1408        }
1409
1410        ret = gc0310_s_config(&dev->sd, client->irq, pdata);
1411        if (ret)
1412                goto out_free;
1413
1414        ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1415        if (ret)
1416                goto out_free;
1417
1418        dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1419        dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1420        dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8;
1421        dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1422        ret =
1423            v4l2_ctrl_handler_init(&dev->ctrl_handler,
1424                                   ARRAY_SIZE(gc0310_controls));
1425        if (ret) {
1426                gc0310_remove(client);
1427                return ret;
1428        }
1429
1430        for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++)
1431                v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i],
1432                                     NULL);
1433
1434        if (dev->ctrl_handler.error) {
1435                gc0310_remove(client);
1436                return dev->ctrl_handler.error;
1437        }
1438
1439        /* Use same lock for controls as for everything else. */
1440        dev->ctrl_handler.lock = &dev->input_lock;
1441        dev->sd.ctrl_handler = &dev->ctrl_handler;
1442
1443        ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1444        if (ret)
1445                gc0310_remove(client);
1446
1447        pr_info("%s E\n", __func__);
1448        return ret;
1449out_free:
1450        v4l2_device_unregister_subdev(&dev->sd);
1451        kfree(dev);
1452        return ret;
1453}
1454
1455static const struct acpi_device_id gc0310_acpi_match[] = {
1456        {"XXGC0310"},
1457        {"INT0310"},
1458        {},
1459};
1460
1461MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match);
1462
1463MODULE_DEVICE_TABLE(i2c, gc0310_id);
1464static struct i2c_driver gc0310_driver = {
1465        .driver = {
1466                .name = GC0310_NAME,
1467                .acpi_match_table = ACPI_PTR(gc0310_acpi_match),
1468        },
1469        .probe = gc0310_probe,
1470        .remove = gc0310_remove,
1471        .id_table = gc0310_id,
1472};
1473
1474static int init_gc0310(void)
1475{
1476        return i2c_add_driver(&gc0310_driver);
1477}
1478
1479static void exit_gc0310(void)
1480{
1481
1482        i2c_del_driver(&gc0310_driver);
1483}
1484
1485module_init(init_gc0310);
1486module_exit(exit_gc0310);
1487
1488MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
1489MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
1490MODULE_LICENSE("GPL");
1491