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