linux/drivers/media/i2c/soc_camera/mt9v022.c
<<
>>
Prefs
   1/*
   2 * Driver for MT9V022 CMOS Image Sensor from Micron
   3 *
   4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 */
  10
  11#include <linux/videodev2.h>
  12#include <linux/slab.h>
  13#include <linux/i2c.h>
  14#include <linux/delay.h>
  15#include <linux/log2.h>
  16#include <linux/module.h>
  17
  18#include <media/mt9v022.h>
  19#include <media/soc_camera.h>
  20#include <media/soc_mediabus.h>
  21#include <media/v4l2-subdev.h>
  22#include <media/v4l2-chip-ident.h>
  23#include <media/v4l2-ctrls.h>
  24
  25/*
  26 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
  27 * The platform has to define struct i2c_board_info objects and link to them
  28 * from struct soc_camera_link
  29 */
  30
  31static char *sensor_type;
  32module_param(sensor_type, charp, S_IRUGO);
  33MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
  34
  35/* mt9v022 selected register addresses */
  36#define MT9V022_CHIP_VERSION            0x00
  37#define MT9V022_COLUMN_START            0x01
  38#define MT9V022_ROW_START               0x02
  39#define MT9V022_WINDOW_HEIGHT           0x03
  40#define MT9V022_WINDOW_WIDTH            0x04
  41#define MT9V022_HORIZONTAL_BLANKING     0x05
  42#define MT9V022_VERTICAL_BLANKING       0x06
  43#define MT9V022_CHIP_CONTROL            0x07
  44#define MT9V022_SHUTTER_WIDTH1          0x08
  45#define MT9V022_SHUTTER_WIDTH2          0x09
  46#define MT9V022_SHUTTER_WIDTH_CTRL      0x0a
  47#define MT9V022_TOTAL_SHUTTER_WIDTH     0x0b
  48#define MT9V022_RESET                   0x0c
  49#define MT9V022_READ_MODE               0x0d
  50#define MT9V022_MONITOR_MODE            0x0e
  51#define MT9V022_PIXEL_OPERATION_MODE    0x0f
  52#define MT9V022_LED_OUT_CONTROL         0x1b
  53#define MT9V022_ADC_MODE_CONTROL        0x1c
  54#define MT9V022_REG32                   0x20
  55#define MT9V022_ANALOG_GAIN             0x35
  56#define MT9V022_BLACK_LEVEL_CALIB_CTRL  0x47
  57#define MT9V022_PIXCLK_FV_LV            0x74
  58#define MT9V022_DIGITAL_TEST_PATTERN    0x7f
  59#define MT9V022_AEC_AGC_ENABLE          0xAF
  60#define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
  61
  62/* mt9v024 partial list register addresses changes with respect to mt9v022 */
  63#define MT9V024_PIXCLK_FV_LV            0x72
  64#define MT9V024_MAX_TOTAL_SHUTTER_WIDTH 0xAD
  65
  66/* Progressive scan, master, defaults */
  67#define MT9V022_CHIP_CONTROL_DEFAULT    0x188
  68
  69#define MT9V022_MAX_WIDTH               752
  70#define MT9V022_MAX_HEIGHT              480
  71#define MT9V022_MIN_WIDTH               48
  72#define MT9V022_MIN_HEIGHT              32
  73#define MT9V022_COLUMN_SKIP             1
  74#define MT9V022_ROW_SKIP                4
  75
  76#define MT9V022_HORIZONTAL_BLANKING_MIN 43
  77#define MT9V022_HORIZONTAL_BLANKING_MAX 1023
  78#define MT9V022_HORIZONTAL_BLANKING_DEF 94
  79#define MT9V022_VERTICAL_BLANKING_MIN   2
  80#define MT9V022_VERTICAL_BLANKING_MAX   3000
  81#define MT9V022_VERTICAL_BLANKING_DEF   45
  82
  83#define is_mt9v022_rev3(id)     (id == 0x1313)
  84#define is_mt9v024(id)          (id == 0x1324)
  85
  86/* MT9V022 has only one fixed colorspace per pixelcode */
  87struct mt9v022_datafmt {
  88        enum v4l2_mbus_pixelcode        code;
  89        enum v4l2_colorspace            colorspace;
  90};
  91
  92/* Find a data format by a pixel code in an array */
  93static const struct mt9v022_datafmt *mt9v022_find_datafmt(
  94        enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
  95        int n)
  96{
  97        int i;
  98        for (i = 0; i < n; i++)
  99                if (fmt[i].code == code)
 100                        return fmt + i;
 101
 102        return NULL;
 103}
 104
 105static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
 106        /*
 107         * Order important: first natively supported,
 108         * second supported with a GPIO extender
 109         */
 110        {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
 111        {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
 112};
 113
 114static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
 115        /* Order important - see above */
 116        {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
 117        {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
 118};
 119
 120/* only registers with different addresses on different mt9v02x sensors */
 121struct mt9v02x_register {
 122        u8      max_total_shutter_width;
 123        u8      pixclk_fv_lv;
 124};
 125
 126static const struct mt9v02x_register mt9v022_register = {
 127        .max_total_shutter_width        = MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
 128        .pixclk_fv_lv                   = MT9V022_PIXCLK_FV_LV,
 129};
 130
 131static const struct mt9v02x_register mt9v024_register = {
 132        .max_total_shutter_width        = MT9V024_MAX_TOTAL_SHUTTER_WIDTH,
 133        .pixclk_fv_lv                   = MT9V024_PIXCLK_FV_LV,
 134};
 135
 136struct mt9v022 {
 137        struct v4l2_subdev subdev;
 138        struct v4l2_ctrl_handler hdl;
 139        struct {
 140                /* exposure/auto-exposure cluster */
 141                struct v4l2_ctrl *autoexposure;
 142                struct v4l2_ctrl *exposure;
 143        };
 144        struct {
 145                /* gain/auto-gain cluster */
 146                struct v4l2_ctrl *autogain;
 147                struct v4l2_ctrl *gain;
 148        };
 149        struct v4l2_ctrl *hblank;
 150        struct v4l2_ctrl *vblank;
 151        struct v4l2_rect rect;  /* Sensor window */
 152        const struct mt9v022_datafmt *fmt;
 153        const struct mt9v022_datafmt *fmts;
 154        const struct mt9v02x_register *reg;
 155        int num_fmts;
 156        int model;      /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
 157        u16 chip_control;
 158        u16 chip_version;
 159        unsigned short y_skip_top;      /* Lines to skip at the top */
 160};
 161
 162static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
 163{
 164        return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
 165}
 166
 167static int reg_read(struct i2c_client *client, const u8 reg)
 168{
 169        return i2c_smbus_read_word_swapped(client, reg);
 170}
 171
 172static int reg_write(struct i2c_client *client, const u8 reg,
 173                     const u16 data)
 174{
 175        return i2c_smbus_write_word_swapped(client, reg, data);
 176}
 177
 178static int reg_set(struct i2c_client *client, const u8 reg,
 179                   const u16 data)
 180{
 181        int ret;
 182
 183        ret = reg_read(client, reg);
 184        if (ret < 0)
 185                return ret;
 186        return reg_write(client, reg, ret | data);
 187}
 188
 189static int reg_clear(struct i2c_client *client, const u8 reg,
 190                     const u16 data)
 191{
 192        int ret;
 193
 194        ret = reg_read(client, reg);
 195        if (ret < 0)
 196                return ret;
 197        return reg_write(client, reg, ret & ~data);
 198}
 199
 200static int mt9v022_init(struct i2c_client *client)
 201{
 202        struct mt9v022 *mt9v022 = to_mt9v022(client);
 203        int ret;
 204
 205        /*
 206         * Almost the default mode: master, parallel, simultaneous, and an
 207         * undocumented bit 0x200, which is present in table 7, but not in 8,
 208         * plus snapshot mode to disable scan for now
 209         */
 210        mt9v022->chip_control |= 0x10;
 211        ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
 212        if (!ret)
 213                ret = reg_write(client, MT9V022_READ_MODE, 0x300);
 214
 215        /* All defaults */
 216        if (!ret)
 217                /* AEC, AGC on */
 218                ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
 219        if (!ret)
 220                ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
 221        if (!ret)
 222                ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
 223        if (!ret)
 224                ret = reg_write(client, mt9v022->reg->max_total_shutter_width, 480);
 225        if (!ret)
 226                /* default - auto */
 227                ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
 228        if (!ret)
 229                ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
 230        if (!ret)
 231                return v4l2_ctrl_handler_setup(&mt9v022->hdl);
 232
 233        return ret;
 234}
 235
 236static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
 237{
 238        struct i2c_client *client = v4l2_get_subdevdata(sd);
 239        struct mt9v022 *mt9v022 = to_mt9v022(client);
 240
 241        if (enable) {
 242                /* Switch to master "normal" mode */
 243                mt9v022->chip_control &= ~0x10;
 244                if (is_mt9v022_rev3(mt9v022->chip_version) ||
 245                    is_mt9v024(mt9v022->chip_version)) {
 246                        /*
 247                         * Unset snapshot mode specific settings: clear bit 9
 248                         * and bit 2 in reg. 0x20 when in normal mode.
 249                         */
 250                        if (reg_clear(client, MT9V022_REG32, 0x204))
 251                                return -EIO;
 252                }
 253        } else {
 254                /* Switch to snapshot mode */
 255                mt9v022->chip_control |= 0x10;
 256                if (is_mt9v022_rev3(mt9v022->chip_version) ||
 257                    is_mt9v024(mt9v022->chip_version)) {
 258                        /*
 259                         * Required settings for snapshot mode: set bit 9
 260                         * (RST enable) and bit 2 (CR enable) in reg. 0x20
 261                         * See TechNote TN0960 or TN-09-225.
 262                         */
 263                        if (reg_set(client, MT9V022_REG32, 0x204))
 264                                return -EIO;
 265                }
 266        }
 267
 268        if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
 269                return -EIO;
 270        return 0;
 271}
 272
 273static int mt9v022_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
 274{
 275        struct i2c_client *client = v4l2_get_subdevdata(sd);
 276        struct mt9v022 *mt9v022 = to_mt9v022(client);
 277        struct v4l2_rect rect = a->c;
 278        int ret;
 279
 280        /* Bayer format - even size lengths */
 281        if (mt9v022->fmts == mt9v022_colour_fmts) {
 282                rect.width      = ALIGN(rect.width, 2);
 283                rect.height     = ALIGN(rect.height, 2);
 284                /* Let the user play with the starting pixel */
 285        }
 286
 287        soc_camera_limit_side(&rect.left, &rect.width,
 288                     MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
 289
 290        soc_camera_limit_side(&rect.top, &rect.height,
 291                     MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
 292
 293        /* Like in example app. Contradicts the datasheet though */
 294        ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
 295        if (ret >= 0) {
 296                if (ret & 1) /* Autoexposure */
 297                        ret = reg_write(client, mt9v022->reg->max_total_shutter_width,
 298                                        rect.height + mt9v022->y_skip_top + 43);
 299                /*
 300                 * If autoexposure is off, there is no need to set
 301                 * MT9V022_TOTAL_SHUTTER_WIDTH here. Autoexposure can be off
 302                 * only if the user has set exposure manually, using the
 303                 * V4L2_CID_EXPOSURE_AUTO with the value V4L2_EXPOSURE_MANUAL.
 304                 * In this case the register MT9V022_TOTAL_SHUTTER_WIDTH
 305                 * already contains the correct value.
 306                 */
 307        }
 308        /* Setup frame format: defaults apart from width and height */
 309        if (!ret)
 310                ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
 311        if (!ret)
 312                ret = reg_write(client, MT9V022_ROW_START, rect.top);
 313        if (!ret)
 314                /*
 315                 * Default 94, Phytec driver says:
 316                 * "width + horizontal blank >= 660"
 317                 */
 318                ret = v4l2_ctrl_s_ctrl(mt9v022->hblank,
 319                                rect.width > 660 - 43 ? 43 : 660 - rect.width);
 320        if (!ret)
 321                ret = v4l2_ctrl_s_ctrl(mt9v022->vblank, 45);
 322        if (!ret)
 323                ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
 324        if (!ret)
 325                ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
 326                                rect.height + mt9v022->y_skip_top);
 327
 328        if (ret < 0)
 329                return ret;
 330
 331        dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
 332
 333        mt9v022->rect = rect;
 334
 335        return 0;
 336}
 337
 338static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
 339{
 340        struct i2c_client *client = v4l2_get_subdevdata(sd);
 341        struct mt9v022 *mt9v022 = to_mt9v022(client);
 342
 343        a->c    = mt9v022->rect;
 344        a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 345
 346        return 0;
 347}
 348
 349static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
 350{
 351        a->bounds.left                  = MT9V022_COLUMN_SKIP;
 352        a->bounds.top                   = MT9V022_ROW_SKIP;
 353        a->bounds.width                 = MT9V022_MAX_WIDTH;
 354        a->bounds.height                = MT9V022_MAX_HEIGHT;
 355        a->defrect                      = a->bounds;
 356        a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 357        a->pixelaspect.numerator        = 1;
 358        a->pixelaspect.denominator      = 1;
 359
 360        return 0;
 361}
 362
 363static int mt9v022_g_fmt(struct v4l2_subdev *sd,
 364                         struct v4l2_mbus_framefmt *mf)
 365{
 366        struct i2c_client *client = v4l2_get_subdevdata(sd);
 367        struct mt9v022 *mt9v022 = to_mt9v022(client);
 368
 369        mf->width       = mt9v022->rect.width;
 370        mf->height      = mt9v022->rect.height;
 371        mf->code        = mt9v022->fmt->code;
 372        mf->colorspace  = mt9v022->fmt->colorspace;
 373        mf->field       = V4L2_FIELD_NONE;
 374
 375        return 0;
 376}
 377
 378static int mt9v022_s_fmt(struct v4l2_subdev *sd,
 379                         struct v4l2_mbus_framefmt *mf)
 380{
 381        struct i2c_client *client = v4l2_get_subdevdata(sd);
 382        struct mt9v022 *mt9v022 = to_mt9v022(client);
 383        struct v4l2_crop a = {
 384                .c = {
 385                        .left   = mt9v022->rect.left,
 386                        .top    = mt9v022->rect.top,
 387                        .width  = mf->width,
 388                        .height = mf->height,
 389                },
 390        };
 391        int ret;
 392
 393        /*
 394         * The caller provides a supported format, as verified per call to
 395         * .try_mbus_fmt(), datawidth is from our supported format list
 396         */
 397        switch (mf->code) {
 398        case V4L2_MBUS_FMT_Y8_1X8:
 399        case V4L2_MBUS_FMT_Y10_1X10:
 400                if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
 401                        return -EINVAL;
 402                break;
 403        case V4L2_MBUS_FMT_SBGGR8_1X8:
 404        case V4L2_MBUS_FMT_SBGGR10_1X10:
 405                if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
 406                        return -EINVAL;
 407                break;
 408        default:
 409                return -EINVAL;
 410        }
 411
 412        /* No support for scaling on this camera, just crop. */
 413        ret = mt9v022_s_crop(sd, &a);
 414        if (!ret) {
 415                mf->width       = mt9v022->rect.width;
 416                mf->height      = mt9v022->rect.height;
 417                mt9v022->fmt    = mt9v022_find_datafmt(mf->code,
 418                                        mt9v022->fmts, mt9v022->num_fmts);
 419                mf->colorspace  = mt9v022->fmt->colorspace;
 420        }
 421
 422        return ret;
 423}
 424
 425static int mt9v022_try_fmt(struct v4l2_subdev *sd,
 426                           struct v4l2_mbus_framefmt *mf)
 427{
 428        struct i2c_client *client = v4l2_get_subdevdata(sd);
 429        struct mt9v022 *mt9v022 = to_mt9v022(client);
 430        const struct mt9v022_datafmt *fmt;
 431        int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
 432                mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
 433
 434        v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
 435                MT9V022_MAX_WIDTH, align,
 436                &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
 437                MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
 438
 439        fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
 440                                   mt9v022->num_fmts);
 441        if (!fmt) {
 442                fmt = mt9v022->fmt;
 443                mf->code = fmt->code;
 444        }
 445
 446        mf->colorspace  = fmt->colorspace;
 447
 448        return 0;
 449}
 450
 451static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
 452                                struct v4l2_dbg_chip_ident *id)
 453{
 454        struct i2c_client *client = v4l2_get_subdevdata(sd);
 455        struct mt9v022 *mt9v022 = to_mt9v022(client);
 456
 457        if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
 458                return -EINVAL;
 459
 460        if (id->match.addr != client->addr)
 461                return -ENODEV;
 462
 463        id->ident       = mt9v022->model;
 464        id->revision    = 0;
 465
 466        return 0;
 467}
 468
 469#ifdef CONFIG_VIDEO_ADV_DEBUG
 470static int mt9v022_g_register(struct v4l2_subdev *sd,
 471                              struct v4l2_dbg_register *reg)
 472{
 473        struct i2c_client *client = v4l2_get_subdevdata(sd);
 474
 475        if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
 476                return -EINVAL;
 477
 478        if (reg->match.addr != client->addr)
 479                return -ENODEV;
 480
 481        reg->size = 2;
 482        reg->val = reg_read(client, reg->reg);
 483
 484        if (reg->val > 0xffff)
 485                return -EIO;
 486
 487        return 0;
 488}
 489
 490static int mt9v022_s_register(struct v4l2_subdev *sd,
 491                              struct v4l2_dbg_register *reg)
 492{
 493        struct i2c_client *client = v4l2_get_subdevdata(sd);
 494
 495        if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
 496                return -EINVAL;
 497
 498        if (reg->match.addr != client->addr)
 499                return -ENODEV;
 500
 501        if (reg_write(client, reg->reg, reg->val) < 0)
 502                return -EIO;
 503
 504        return 0;
 505}
 506#endif
 507
 508static int mt9v022_s_power(struct v4l2_subdev *sd, int on)
 509{
 510        struct i2c_client *client = v4l2_get_subdevdata(sd);
 511        struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
 512
 513        return soc_camera_set_power(&client->dev, icl, on);
 514}
 515
 516static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 517{
 518        struct mt9v022 *mt9v022 = container_of(ctrl->handler,
 519                                               struct mt9v022, hdl);
 520        struct v4l2_subdev *sd = &mt9v022->subdev;
 521        struct i2c_client *client = v4l2_get_subdevdata(sd);
 522        struct v4l2_ctrl *gain = mt9v022->gain;
 523        struct v4l2_ctrl *exp = mt9v022->exposure;
 524        unsigned long range;
 525        int data;
 526
 527        switch (ctrl->id) {
 528        case V4L2_CID_AUTOGAIN:
 529                data = reg_read(client, MT9V022_ANALOG_GAIN);
 530                if (data < 0)
 531                        return -EIO;
 532
 533                range = gain->maximum - gain->minimum;
 534                gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
 535                return 0;
 536        case V4L2_CID_EXPOSURE_AUTO:
 537                data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
 538                if (data < 0)
 539                        return -EIO;
 540
 541                range = exp->maximum - exp->minimum;
 542                exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
 543                return 0;
 544        case V4L2_CID_HBLANK:
 545                data = reg_read(client, MT9V022_HORIZONTAL_BLANKING);
 546                if (data < 0)
 547                        return -EIO;
 548                ctrl->val = data;
 549                return 0;
 550        case V4L2_CID_VBLANK:
 551                data = reg_read(client, MT9V022_VERTICAL_BLANKING);
 552                if (data < 0)
 553                        return -EIO;
 554                ctrl->val = data;
 555                return 0;
 556        }
 557        return -EINVAL;
 558}
 559
 560static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
 561{
 562        struct mt9v022 *mt9v022 = container_of(ctrl->handler,
 563                                               struct mt9v022, hdl);
 564        struct v4l2_subdev *sd = &mt9v022->subdev;
 565        struct i2c_client *client = v4l2_get_subdevdata(sd);
 566        int data;
 567
 568        switch (ctrl->id) {
 569        case V4L2_CID_VFLIP:
 570                if (ctrl->val)
 571                        data = reg_set(client, MT9V022_READ_MODE, 0x10);
 572                else
 573                        data = reg_clear(client, MT9V022_READ_MODE, 0x10);
 574                if (data < 0)
 575                        return -EIO;
 576                return 0;
 577        case V4L2_CID_HFLIP:
 578                if (ctrl->val)
 579                        data = reg_set(client, MT9V022_READ_MODE, 0x20);
 580                else
 581                        data = reg_clear(client, MT9V022_READ_MODE, 0x20);
 582                if (data < 0)
 583                        return -EIO;
 584                return 0;
 585        case V4L2_CID_AUTOGAIN:
 586                if (ctrl->val) {
 587                        if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
 588                                return -EIO;
 589                } else {
 590                        struct v4l2_ctrl *gain = mt9v022->gain;
 591                        /* mt9v022 has minimum == default */
 592                        unsigned long range = gain->maximum - gain->minimum;
 593                        /* Valid values 16 to 64, 32 to 64 must be even. */
 594                        unsigned long gain_val = ((gain->val - gain->minimum) *
 595                                              48 + range / 2) / range + 16;
 596
 597                        if (gain_val >= 32)
 598                                gain_val &= ~1;
 599
 600                        /*
 601                         * The user wants to set gain manually, hope, she
 602                         * knows, what she's doing... Switch AGC off.
 603                         */
 604                        if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
 605                                return -EIO;
 606
 607                        dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
 608                                reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
 609                        if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
 610                                return -EIO;
 611                }
 612                return 0;
 613        case V4L2_CID_EXPOSURE_AUTO:
 614                if (ctrl->val == V4L2_EXPOSURE_AUTO) {
 615                        data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
 616                } else {
 617                        struct v4l2_ctrl *exp = mt9v022->exposure;
 618                        unsigned long range = exp->maximum - exp->minimum;
 619                        unsigned long shutter = ((exp->val - exp->minimum) *
 620                                        479 + range / 2) / range + 1;
 621
 622                        /*
 623                         * The user wants to set shutter width manually, hope,
 624                         * she knows, what she's doing... Switch AEC off.
 625                         */
 626                        data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
 627                        if (data < 0)
 628                                return -EIO;
 629                        dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
 630                                        reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
 631                                        shutter);
 632                        if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
 633                                                shutter) < 0)
 634                                return -EIO;
 635                }
 636                return 0;
 637        case V4L2_CID_HBLANK:
 638                if (reg_write(client, MT9V022_HORIZONTAL_BLANKING,
 639                                ctrl->val) < 0)
 640                        return -EIO;
 641                return 0;
 642        case V4L2_CID_VBLANK:
 643                if (reg_write(client, MT9V022_VERTICAL_BLANKING,
 644                                ctrl->val) < 0)
 645                        return -EIO;
 646                return 0;
 647        }
 648        return -EINVAL;
 649}
 650
 651/*
 652 * Interface active, can use i2c. If it fails, it can indeed mean, that
 653 * this wasn't our capture interface, so, we wait for the right one
 654 */
 655static int mt9v022_video_probe(struct i2c_client *client)
 656{
 657        struct mt9v022 *mt9v022 = to_mt9v022(client);
 658        struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
 659        s32 data;
 660        int ret;
 661        unsigned long flags;
 662
 663        ret = mt9v022_s_power(&mt9v022->subdev, 1);
 664        if (ret < 0)
 665                return ret;
 666
 667        /* Read out the chip version register */
 668        data = reg_read(client, MT9V022_CHIP_VERSION);
 669
 670        /* must be 0x1311, 0x1313 or 0x1324 */
 671        if (data != 0x1311 && data != 0x1313 && data != 0x1324) {
 672                ret = -ENODEV;
 673                dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
 674                         data);
 675                goto ei2c;
 676        }
 677
 678        mt9v022->chip_version = data;
 679
 680        mt9v022->reg = is_mt9v024(data) ? &mt9v024_register :
 681                        &mt9v022_register;
 682
 683        /* Soft reset */
 684        ret = reg_write(client, MT9V022_RESET, 1);
 685        if (ret < 0)
 686                goto ei2c;
 687        /* 15 clock cycles */
 688        udelay(200);
 689        if (reg_read(client, MT9V022_RESET)) {
 690                dev_err(&client->dev, "Resetting MT9V022 failed!\n");
 691                if (ret > 0)
 692                        ret = -EIO;
 693                goto ei2c;
 694        }
 695
 696        /* Set monochrome or colour sensor type */
 697        if (sensor_type && (!strcmp("colour", sensor_type) ||
 698                            !strcmp("color", sensor_type))) {
 699                ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
 700                mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
 701                mt9v022->fmts = mt9v022_colour_fmts;
 702        } else {
 703                ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
 704                mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
 705                mt9v022->fmts = mt9v022_monochrome_fmts;
 706        }
 707
 708        if (ret < 0)
 709                goto ei2c;
 710
 711        mt9v022->num_fmts = 0;
 712
 713        /*
 714         * This is a 10bit sensor, so by default we only allow 10bit.
 715         * The platform may support different bus widths due to
 716         * different routing of the data lines.
 717         */
 718        if (icl->query_bus_param)
 719                flags = icl->query_bus_param(icl);
 720        else
 721                flags = SOCAM_DATAWIDTH_10;
 722
 723        if (flags & SOCAM_DATAWIDTH_10)
 724                mt9v022->num_fmts++;
 725        else
 726                mt9v022->fmts++;
 727
 728        if (flags & SOCAM_DATAWIDTH_8)
 729                mt9v022->num_fmts++;
 730
 731        mt9v022->fmt = &mt9v022->fmts[0];
 732
 733        dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
 734                 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
 735                 "monochrome" : "colour");
 736
 737        ret = mt9v022_init(client);
 738        if (ret < 0)
 739                dev_err(&client->dev, "Failed to initialise the camera\n");
 740
 741ei2c:
 742        mt9v022_s_power(&mt9v022->subdev, 0);
 743        return ret;
 744}
 745
 746static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
 747{
 748        struct i2c_client *client = v4l2_get_subdevdata(sd);
 749        struct mt9v022 *mt9v022 = to_mt9v022(client);
 750
 751        *lines = mt9v022->y_skip_top;
 752
 753        return 0;
 754}
 755
 756static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
 757        .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
 758        .s_ctrl = mt9v022_s_ctrl,
 759};
 760
 761static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
 762        .g_chip_ident   = mt9v022_g_chip_ident,
 763#ifdef CONFIG_VIDEO_ADV_DEBUG
 764        .g_register     = mt9v022_g_register,
 765        .s_register     = mt9v022_s_register,
 766#endif
 767        .s_power        = mt9v022_s_power,
 768};
 769
 770static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
 771                            enum v4l2_mbus_pixelcode *code)
 772{
 773        struct i2c_client *client = v4l2_get_subdevdata(sd);
 774        struct mt9v022 *mt9v022 = to_mt9v022(client);
 775
 776        if (index >= mt9v022->num_fmts)
 777                return -EINVAL;
 778
 779        *code = mt9v022->fmts[index].code;
 780        return 0;
 781}
 782
 783static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
 784                                struct v4l2_mbus_config *cfg)
 785{
 786        struct i2c_client *client = v4l2_get_subdevdata(sd);
 787        struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
 788
 789        cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
 790                V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
 791                V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
 792                V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
 793                V4L2_MBUS_DATA_ACTIVE_HIGH;
 794        cfg->type = V4L2_MBUS_PARALLEL;
 795        cfg->flags = soc_camera_apply_board_flags(icl, cfg);
 796
 797        return 0;
 798}
 799
 800static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
 801                                 const struct v4l2_mbus_config *cfg)
 802{
 803        struct i2c_client *client = v4l2_get_subdevdata(sd);
 804        struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
 805        struct mt9v022 *mt9v022 = to_mt9v022(client);
 806        unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
 807        unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
 808        int ret;
 809        u16 pixclk = 0;
 810
 811        if (icl->set_bus_param) {
 812                ret = icl->set_bus_param(icl, 1 << (bps - 1));
 813                if (ret)
 814                        return ret;
 815        } else if (bps != 10) {
 816                /*
 817                 * Without board specific bus width settings we only support the
 818                 * sensors native bus width
 819                 */
 820                return -EINVAL;
 821        }
 822
 823        if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
 824                pixclk |= 0x10;
 825
 826        if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
 827                pixclk |= 0x1;
 828
 829        if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
 830                pixclk |= 0x2;
 831
 832        ret = reg_write(client, mt9v022->reg->pixclk_fv_lv, pixclk);
 833        if (ret < 0)
 834                return ret;
 835
 836        if (!(flags & V4L2_MBUS_MASTER))
 837                mt9v022->chip_control &= ~0x8;
 838
 839        ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
 840        if (ret < 0)
 841                return ret;
 842
 843        dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
 844                pixclk, mt9v022->chip_control);
 845
 846        return 0;
 847}
 848
 849static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
 850        .s_stream       = mt9v022_s_stream,
 851        .s_mbus_fmt     = mt9v022_s_fmt,
 852        .g_mbus_fmt     = mt9v022_g_fmt,
 853        .try_mbus_fmt   = mt9v022_try_fmt,
 854        .s_crop         = mt9v022_s_crop,
 855        .g_crop         = mt9v022_g_crop,
 856        .cropcap        = mt9v022_cropcap,
 857        .enum_mbus_fmt  = mt9v022_enum_fmt,
 858        .g_mbus_config  = mt9v022_g_mbus_config,
 859        .s_mbus_config  = mt9v022_s_mbus_config,
 860};
 861
 862static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
 863        .g_skip_top_lines       = mt9v022_g_skip_top_lines,
 864};
 865
 866static struct v4l2_subdev_ops mt9v022_subdev_ops = {
 867        .core   = &mt9v022_subdev_core_ops,
 868        .video  = &mt9v022_subdev_video_ops,
 869        .sensor = &mt9v022_subdev_sensor_ops,
 870};
 871
 872static int mt9v022_probe(struct i2c_client *client,
 873                         const struct i2c_device_id *did)
 874{
 875        struct mt9v022 *mt9v022;
 876        struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
 877        struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
 878        struct mt9v022_platform_data *pdata = icl->priv;
 879        int ret;
 880
 881        if (!icl) {
 882                dev_err(&client->dev, "MT9V022 driver needs platform data\n");
 883                return -EINVAL;
 884        }
 885
 886        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
 887                dev_warn(&adapter->dev,
 888                         "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
 889                return -EIO;
 890        }
 891
 892        mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
 893        if (!mt9v022)
 894                return -ENOMEM;
 895
 896        v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
 897        v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
 898        v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
 899                        V4L2_CID_VFLIP, 0, 1, 1, 0);
 900        v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
 901                        V4L2_CID_HFLIP, 0, 1, 1, 0);
 902        mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
 903                        V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
 904        mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
 905                        V4L2_CID_GAIN, 0, 127, 1, 64);
 906
 907        /*
 908         * Simulated autoexposure. If enabled, we calculate shutter width
 909         * ourselves in the driver based on vertical blanking and frame width
 910         */
 911        mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
 912                        &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
 913                        V4L2_EXPOSURE_AUTO);
 914        mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
 915                        V4L2_CID_EXPOSURE, 1, 255, 1, 255);
 916
 917        mt9v022->hblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
 918                        V4L2_CID_HBLANK, MT9V022_HORIZONTAL_BLANKING_MIN,
 919                        MT9V022_HORIZONTAL_BLANKING_MAX, 1,
 920                        MT9V022_HORIZONTAL_BLANKING_DEF);
 921
 922        mt9v022->vblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
 923                        V4L2_CID_VBLANK, MT9V022_VERTICAL_BLANKING_MIN,
 924                        MT9V022_VERTICAL_BLANKING_MAX, 1,
 925                        MT9V022_VERTICAL_BLANKING_DEF);
 926
 927        mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
 928        if (mt9v022->hdl.error) {
 929                int err = mt9v022->hdl.error;
 930
 931                dev_err(&client->dev, "control initialisation err %d\n", err);
 932                kfree(mt9v022);
 933                return err;
 934        }
 935        v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
 936                                V4L2_EXPOSURE_MANUAL, true);
 937        v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
 938
 939        mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
 940
 941        /*
 942         * On some platforms the first read out line is corrupted.
 943         * Workaround it by skipping if indicated by platform data.
 944         */
 945        mt9v022->y_skip_top     = pdata ? pdata->y_skip_top : 0;
 946        mt9v022->rect.left      = MT9V022_COLUMN_SKIP;
 947        mt9v022->rect.top       = MT9V022_ROW_SKIP;
 948        mt9v022->rect.width     = MT9V022_MAX_WIDTH;
 949        mt9v022->rect.height    = MT9V022_MAX_HEIGHT;
 950
 951        ret = mt9v022_video_probe(client);
 952        if (ret) {
 953                v4l2_ctrl_handler_free(&mt9v022->hdl);
 954                kfree(mt9v022);
 955        }
 956
 957        return ret;
 958}
 959
 960static int mt9v022_remove(struct i2c_client *client)
 961{
 962        struct mt9v022 *mt9v022 = to_mt9v022(client);
 963        struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
 964
 965        v4l2_device_unregister_subdev(&mt9v022->subdev);
 966        if (icl->free_bus)
 967                icl->free_bus(icl);
 968        v4l2_ctrl_handler_free(&mt9v022->hdl);
 969        kfree(mt9v022);
 970
 971        return 0;
 972}
 973static const struct i2c_device_id mt9v022_id[] = {
 974        { "mt9v022", 0 },
 975        { }
 976};
 977MODULE_DEVICE_TABLE(i2c, mt9v022_id);
 978
 979static struct i2c_driver mt9v022_i2c_driver = {
 980        .driver = {
 981                .name = "mt9v022",
 982        },
 983        .probe          = mt9v022_probe,
 984        .remove         = mt9v022_remove,
 985        .id_table       = mt9v022_id,
 986};
 987
 988module_i2c_driver(mt9v022_i2c_driver);
 989
 990MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
 991MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
 992MODULE_LICENSE("GPL");
 993