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