root/drivers/staging/media/soc_camera/mt9t031.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. to_mt9t031
  2. reg_read
  3. reg_write
  4. reg_set
  5. reg_clear
  6. set_shutter
  7. get_shutter
  8. mt9t031_idle
  9. mt9t031_s_stream
  10. mt9t031_skip
  11. mt9t031_set_params
  12. mt9t031_set_selection
  13. mt9t031_get_selection
  14. mt9t031_get_fmt
  15. mt9t031_set_fmt
  16. mt9t031_g_register
  17. mt9t031_s_register
  18. mt9t031_g_volatile_ctrl
  19. mt9t031_s_ctrl
  20. mt9t031_runtime_suspend
  21. mt9t031_runtime_resume
  22. mt9t031_s_power
  23. mt9t031_video_probe
  24. mt9t031_g_skip_top_lines
  25. mt9t031_enum_mbus_code
  26. mt9t031_g_mbus_config
  27. mt9t031_s_mbus_config
  28. mt9t031_probe
  29. mt9t031_remove

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * Driver for MT9T031 CMOS Image Sensor from Micron
   4  *
   5  * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.de>
   6  */
   7 #include <linux/device.h>
   8 #include <linux/i2c.h>
   9 #include <linux/log2.h>
  10 #include <linux/pm.h>
  11 #include <linux/slab.h>
  12 #include <linux/v4l2-mediabus.h>
  13 #include <linux/videodev2.h>
  14 #include <linux/module.h>
  15 
  16 #include <media/soc_camera.h>
  17 #include <media/v4l2-clk.h>
  18 #include <media/v4l2-subdev.h>
  19 #include <media/v4l2-ctrls.h>
  20 
  21 /*
  22  * ATTENTION: this driver still cannot be used outside of the soc-camera
  23  * framework because of its PM implementation, using the video_device node.
  24  * If hardware becomes available for testing, alternative PM approaches shall
  25  * be considered and tested.
  26  */
  27 
  28 /*
  29  * mt9t031 i2c address 0x5d
  30  * The platform has to define struct i2c_board_info objects and link to them
  31  * from struct soc_camera_host_desc
  32  */
  33 
  34 /* mt9t031 selected register addresses */
  35 #define MT9T031_CHIP_VERSION            0x00
  36 #define MT9T031_ROW_START               0x01
  37 #define MT9T031_COLUMN_START            0x02
  38 #define MT9T031_WINDOW_HEIGHT           0x03
  39 #define MT9T031_WINDOW_WIDTH            0x04
  40 #define MT9T031_HORIZONTAL_BLANKING     0x05
  41 #define MT9T031_VERTICAL_BLANKING       0x06
  42 #define MT9T031_OUTPUT_CONTROL          0x07
  43 #define MT9T031_SHUTTER_WIDTH_UPPER     0x08
  44 #define MT9T031_SHUTTER_WIDTH           0x09
  45 #define MT9T031_PIXEL_CLOCK_CONTROL     0x0a
  46 #define MT9T031_FRAME_RESTART           0x0b
  47 #define MT9T031_SHUTTER_DELAY           0x0c
  48 #define MT9T031_RESET                   0x0d
  49 #define MT9T031_READ_MODE_1             0x1e
  50 #define MT9T031_READ_MODE_2             0x20
  51 #define MT9T031_READ_MODE_3             0x21
  52 #define MT9T031_ROW_ADDRESS_MODE        0x22
  53 #define MT9T031_COLUMN_ADDRESS_MODE     0x23
  54 #define MT9T031_GLOBAL_GAIN             0x35
  55 #define MT9T031_CHIP_ENABLE             0xF8
  56 
  57 #define MT9T031_MAX_HEIGHT              1536
  58 #define MT9T031_MAX_WIDTH               2048
  59 #define MT9T031_MIN_HEIGHT              2
  60 #define MT9T031_MIN_WIDTH               18
  61 #define MT9T031_HORIZONTAL_BLANK        142
  62 #define MT9T031_VERTICAL_BLANK          25
  63 #define MT9T031_COLUMN_SKIP             32
  64 #define MT9T031_ROW_SKIP                20
  65 
  66 struct mt9t031 {
  67         struct v4l2_subdev subdev;
  68         struct v4l2_ctrl_handler hdl;
  69         struct {
  70                 /* exposure/auto-exposure cluster */
  71                 struct v4l2_ctrl *autoexposure;
  72                 struct v4l2_ctrl *exposure;
  73         };
  74         struct v4l2_rect rect;  /* Sensor window */
  75         struct v4l2_clk *clk;
  76         u16 xskip;
  77         u16 yskip;
  78         unsigned int total_h;
  79         unsigned short y_skip_top;      /* Lines to skip at the top */
  80 };
  81 
  82 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
  83 {
  84         return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
  85 }
  86 
  87 static int reg_read(struct i2c_client *client, const u8 reg)
  88 {
  89         return i2c_smbus_read_word_swapped(client, reg);
  90 }
  91 
  92 static int reg_write(struct i2c_client *client, const u8 reg,
  93                      const u16 data)
  94 {
  95         return i2c_smbus_write_word_swapped(client, reg, data);
  96 }
  97 
  98 static int reg_set(struct i2c_client *client, const u8 reg,
  99                    const u16 data)
 100 {
 101         int ret;
 102 
 103         ret = reg_read(client, reg);
 104         if (ret < 0)
 105                 return ret;
 106         return reg_write(client, reg, ret | data);
 107 }
 108 
 109 static int reg_clear(struct i2c_client *client, const u8 reg,
 110                      const u16 data)
 111 {
 112         int ret;
 113 
 114         ret = reg_read(client, reg);
 115         if (ret < 0)
 116                 return ret;
 117         return reg_write(client, reg, ret & ~data);
 118 }
 119 
 120 static int set_shutter(struct i2c_client *client, const u32 data)
 121 {
 122         int ret;
 123 
 124         ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
 125 
 126         if (ret >= 0)
 127                 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
 128 
 129         return ret;
 130 }
 131 
 132 static int get_shutter(struct i2c_client *client, u32 *data)
 133 {
 134         int ret;
 135 
 136         ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
 137         *data = ret << 16;
 138 
 139         if (ret >= 0)
 140                 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
 141         *data |= ret & 0xffff;
 142 
 143         return ret < 0 ? ret : 0;
 144 }
 145 
 146 static int mt9t031_idle(struct i2c_client *client)
 147 {
 148         int ret;
 149 
 150         /* Disable chip output, synchronous option update */
 151         ret = reg_write(client, MT9T031_RESET, 1);
 152         if (ret >= 0)
 153                 ret = reg_write(client, MT9T031_RESET, 0);
 154         if (ret >= 0)
 155                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
 156 
 157         return ret >= 0 ? 0 : -EIO;
 158 }
 159 
 160 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
 161 {
 162         struct i2c_client *client = v4l2_get_subdevdata(sd);
 163         int ret;
 164 
 165         if (enable)
 166                 /* Switch to master "normal" mode */
 167                 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
 168         else
 169                 /* Stop sensor readout */
 170                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
 171 
 172         if (ret < 0)
 173                 return -EIO;
 174 
 175         return 0;
 176 }
 177 
 178 /* target must be _even_ */
 179 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
 180 {
 181         unsigned int skip;
 182 
 183         if (*source < target + target / 2) {
 184                 *source = target;
 185                 return 1;
 186         }
 187 
 188         skip = min(max, *source + target / 2) / target;
 189         if (skip > 8)
 190                 skip = 8;
 191         *source = target * skip;
 192 
 193         return skip;
 194 }
 195 
 196 /* rect is the sensor rectangle, the caller guarantees parameter validity */
 197 static int mt9t031_set_params(struct i2c_client *client,
 198                               struct v4l2_rect *rect, u16 xskip, u16 yskip)
 199 {
 200         struct mt9t031 *mt9t031 = to_mt9t031(client);
 201         int ret;
 202         u16 xbin, ybin;
 203         const u16 hblank = MT9T031_HORIZONTAL_BLANK,
 204                 vblank = MT9T031_VERTICAL_BLANK;
 205 
 206         xbin = min(xskip, (u16)3);
 207         ybin = min(yskip, (u16)3);
 208 
 209         /*
 210          * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
 211          * There is always a valid suitably aligned value. The worst case is
 212          * xbin = 3, width = 2048. Then we will start at 36, the last read out
 213          * pixel will be 2083, which is < 2085 - first black pixel.
 214          *
 215          * MT9T031 datasheet imposes window left border alignment, depending on
 216          * the selected xskip. Failing to conform to this requirement produces
 217          * dark horizontal stripes in the image. However, even obeying to this
 218          * requirement doesn't eliminate the stripes in all configurations. They
 219          * appear "locally reproducibly," but can differ between tests under
 220          * different lighting conditions.
 221          */
 222         switch (xbin) {
 223         case 1:
 224                 rect->left &= ~1;
 225                 break;
 226         case 2:
 227                 rect->left &= ~3;
 228                 break;
 229         case 3:
 230                 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
 231                         (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
 232         }
 233 
 234         rect->top &= ~1;
 235 
 236         dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
 237                 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
 238 
 239         /* Disable register update, reconfigure atomically */
 240         ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
 241         if (ret < 0)
 242                 return ret;
 243 
 244         /* Blanking and start values - default... */
 245         ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
 246         if (ret >= 0)
 247                 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
 248 
 249         if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
 250                 /* Binning, skipping */
 251                 if (ret >= 0)
 252                         ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
 253                                         ((xbin - 1) << 4) | (xskip - 1));
 254                 if (ret >= 0)
 255                         ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
 256                                         ((ybin - 1) << 4) | (yskip - 1));
 257         }
 258         dev_dbg(&client->dev, "new physical left %u, top %u\n",
 259                 rect->left, rect->top);
 260 
 261         /*
 262          * The caller provides a supported format, as guaranteed by
 263          * .set_fmt(FORMAT_TRY), soc_camera_s_selection() and soc_camera_cropcap()
 264          */
 265         if (ret >= 0)
 266                 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
 267         if (ret >= 0)
 268                 ret = reg_write(client, MT9T031_ROW_START, rect->top);
 269         if (ret >= 0)
 270                 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
 271         if (ret >= 0)
 272                 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
 273                                 rect->height + mt9t031->y_skip_top - 1);
 274         if (ret >= 0 && v4l2_ctrl_g_ctrl(mt9t031->autoexposure) == V4L2_EXPOSURE_AUTO) {
 275                 mt9t031->total_h = rect->height + mt9t031->y_skip_top + vblank;
 276 
 277                 ret = set_shutter(client, mt9t031->total_h);
 278         }
 279 
 280         /* Re-enable register update, commit all changes */
 281         if (ret >= 0)
 282                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
 283 
 284         if (ret >= 0) {
 285                 mt9t031->rect = *rect;
 286                 mt9t031->xskip = xskip;
 287                 mt9t031->yskip = yskip;
 288         }
 289 
 290         return ret < 0 ? ret : 0;
 291 }
 292 
 293 static int mt9t031_set_selection(struct v4l2_subdev *sd,
 294                 struct v4l2_subdev_pad_config *cfg,
 295                 struct v4l2_subdev_selection *sel)
 296 {
 297         struct i2c_client *client = v4l2_get_subdevdata(sd);
 298         struct mt9t031 *mt9t031 = to_mt9t031(client);
 299         struct v4l2_rect rect = sel->r;
 300 
 301         if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
 302             sel->target != V4L2_SEL_TGT_CROP)
 303                 return -EINVAL;
 304 
 305         rect.width = ALIGN(rect.width, 2);
 306         rect.height = ALIGN(rect.height, 2);
 307 
 308         soc_camera_limit_side(&rect.left, &rect.width,
 309                      MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
 310 
 311         soc_camera_limit_side(&rect.top, &rect.height,
 312                      MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
 313 
 314         return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
 315 }
 316 
 317 static int mt9t031_get_selection(struct v4l2_subdev *sd,
 318                 struct v4l2_subdev_pad_config *cfg,
 319                 struct v4l2_subdev_selection *sel)
 320 {
 321         struct i2c_client *client = v4l2_get_subdevdata(sd);
 322         struct mt9t031 *mt9t031 = to_mt9t031(client);
 323 
 324         if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
 325                 return -EINVAL;
 326 
 327         switch (sel->target) {
 328         case V4L2_SEL_TGT_CROP_BOUNDS:
 329                 sel->r.left = MT9T031_COLUMN_SKIP;
 330                 sel->r.top = MT9T031_ROW_SKIP;
 331                 sel->r.width = MT9T031_MAX_WIDTH;
 332                 sel->r.height = MT9T031_MAX_HEIGHT;
 333                 return 0;
 334         case V4L2_SEL_TGT_CROP:
 335                 sel->r = mt9t031->rect;
 336                 return 0;
 337         default:
 338                 return -EINVAL;
 339         }
 340 }
 341 
 342 static int mt9t031_get_fmt(struct v4l2_subdev *sd,
 343                 struct v4l2_subdev_pad_config *cfg,
 344                 struct v4l2_subdev_format *format)
 345 {
 346         struct v4l2_mbus_framefmt *mf = &format->format;
 347         struct i2c_client *client = v4l2_get_subdevdata(sd);
 348         struct mt9t031 *mt9t031 = to_mt9t031(client);
 349 
 350         if (format->pad)
 351                 return -EINVAL;
 352 
 353         mf->width       = mt9t031->rect.width / mt9t031->xskip;
 354         mf->height      = mt9t031->rect.height / mt9t031->yskip;
 355         mf->code        = MEDIA_BUS_FMT_SBGGR10_1X10;
 356         mf->colorspace  = V4L2_COLORSPACE_SRGB;
 357         mf->field       = V4L2_FIELD_NONE;
 358 
 359         return 0;
 360 }
 361 
 362 /*
 363  * If a user window larger than sensor window is requested, we'll increase the
 364  * sensor window.
 365  */
 366 static int mt9t031_set_fmt(struct v4l2_subdev *sd,
 367                 struct v4l2_subdev_pad_config *cfg,
 368                 struct v4l2_subdev_format *format)
 369 {
 370         struct v4l2_mbus_framefmt *mf = &format->format;
 371         struct i2c_client *client = v4l2_get_subdevdata(sd);
 372         struct mt9t031 *mt9t031 = to_mt9t031(client);
 373         u16 xskip, yskip;
 374         struct v4l2_rect rect = mt9t031->rect;
 375 
 376         if (format->pad)
 377                 return -EINVAL;
 378 
 379         mf->code        = MEDIA_BUS_FMT_SBGGR10_1X10;
 380         mf->colorspace  = V4L2_COLORSPACE_SRGB;
 381         v4l_bound_align_image(
 382                         &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
 383                         &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
 384 
 385         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
 386                 cfg->try_fmt = *mf;
 387                 return 0;
 388         }
 389 
 390         /*
 391          * Width and height are within limits.
 392          * S_FMT: use binning and skipping for scaling
 393          */
 394         xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
 395         yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
 396 
 397         mf->code        = MEDIA_BUS_FMT_SBGGR10_1X10;
 398         mf->colorspace  = V4L2_COLORSPACE_SRGB;
 399 
 400         /* mt9t031_set_params() doesn't change width and height */
 401         return mt9t031_set_params(client, &rect, xskip, yskip);
 402 }
 403 
 404 #ifdef CONFIG_VIDEO_ADV_DEBUG
 405 static int mt9t031_g_register(struct v4l2_subdev *sd,
 406                               struct v4l2_dbg_register *reg)
 407 {
 408         struct i2c_client *client = v4l2_get_subdevdata(sd);
 409 
 410         if (reg->reg > 0xff)
 411                 return -EINVAL;
 412 
 413         reg->size = 1;
 414         reg->val = reg_read(client, reg->reg);
 415 
 416         if (reg->val > 0xffff)
 417                 return -EIO;
 418 
 419         return 0;
 420 }
 421 
 422 static int mt9t031_s_register(struct v4l2_subdev *sd,
 423                               const struct v4l2_dbg_register *reg)
 424 {
 425         struct i2c_client *client = v4l2_get_subdevdata(sd);
 426 
 427         if (reg->reg > 0xff)
 428                 return -EINVAL;
 429 
 430         if (reg_write(client, reg->reg, reg->val) < 0)
 431                 return -EIO;
 432 
 433         return 0;
 434 }
 435 #endif
 436 
 437 static int mt9t031_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 438 {
 439         struct mt9t031 *mt9t031 = container_of(ctrl->handler,
 440                                                struct mt9t031, hdl);
 441         const u32 shutter_max = MT9T031_MAX_HEIGHT + MT9T031_VERTICAL_BLANK;
 442         s32 min, max;
 443 
 444         switch (ctrl->id) {
 445         case V4L2_CID_EXPOSURE_AUTO:
 446                 min = mt9t031->exposure->minimum;
 447                 max = mt9t031->exposure->maximum;
 448                 mt9t031->exposure->val =
 449                         (shutter_max / 2 + (mt9t031->total_h - 1) * (max - min))
 450                                 / shutter_max + min;
 451                 break;
 452         }
 453         return 0;
 454 }
 455 
 456 static int mt9t031_s_ctrl(struct v4l2_ctrl *ctrl)
 457 {
 458         struct mt9t031 *mt9t031 = container_of(ctrl->handler,
 459                                                struct mt9t031, hdl);
 460         struct v4l2_subdev *sd = &mt9t031->subdev;
 461         struct i2c_client *client = v4l2_get_subdevdata(sd);
 462         struct v4l2_ctrl *exp = mt9t031->exposure;
 463         int data;
 464 
 465         switch (ctrl->id) {
 466         case V4L2_CID_VFLIP:
 467                 if (ctrl->val)
 468                         data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
 469                 else
 470                         data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
 471                 if (data < 0)
 472                         return -EIO;
 473                 return 0;
 474         case V4L2_CID_HFLIP:
 475                 if (ctrl->val)
 476                         data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
 477                 else
 478                         data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
 479                 if (data < 0)
 480                         return -EIO;
 481                 return 0;
 482         case V4L2_CID_GAIN:
 483                 /* See Datasheet Table 7, Gain settings. */
 484                 if (ctrl->val <= ctrl->default_value) {
 485                         /* Pack it into 0..1 step 0.125, register values 0..8 */
 486                         unsigned long range = ctrl->default_value - ctrl->minimum;
 487                         data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range;
 488 
 489                         dev_dbg(&client->dev, "Setting gain %d\n", data);
 490                         data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
 491                         if (data < 0)
 492                                 return -EIO;
 493                 } else {
 494                         /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
 495                         /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
 496                         unsigned long range = ctrl->maximum - ctrl->default_value - 1;
 497                         /* calculated gain: map 65..127 to 9..1024 step 0.125 */
 498                         unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) *
 499                                                1015 + range / 2) / range + 9;
 500 
 501                         if (gain <= 32)         /* calculated gain 9..32 -> 9..32 */
 502                                 data = gain;
 503                         else if (gain <= 64)    /* calculated gain 33..64 -> 0x51..0x60 */
 504                                 data = ((gain - 32) * 16 + 16) / 32 + 80;
 505                         else
 506                                 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
 507                                 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
 508 
 509                         dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
 510                                 reg_read(client, MT9T031_GLOBAL_GAIN), data);
 511                         data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
 512                         if (data < 0)
 513                                 return -EIO;
 514                 }
 515                 return 0;
 516 
 517         case V4L2_CID_EXPOSURE_AUTO:
 518                 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
 519                         unsigned int range = exp->maximum - exp->minimum;
 520                         unsigned int shutter = ((exp->val - (s32)exp->minimum) * 1048 +
 521                                                  range / 2) / range + 1;
 522                         u32 old;
 523 
 524                         get_shutter(client, &old);
 525                         dev_dbg(&client->dev, "Set shutter from %u to %u\n",
 526                                 old, shutter);
 527                         if (set_shutter(client, shutter) < 0)
 528                                 return -EIO;
 529                 } else {
 530                         const u16 vblank = MT9T031_VERTICAL_BLANK;
 531                         mt9t031->total_h = mt9t031->rect.height +
 532                                 mt9t031->y_skip_top + vblank;
 533 
 534                         if (set_shutter(client, mt9t031->total_h) < 0)
 535                                 return -EIO;
 536                 }
 537                 return 0;
 538         default:
 539                 return -EINVAL;
 540         }
 541         return 0;
 542 }
 543 
 544 /*
 545  * Power Management:
 546  * This function does nothing for now but must be present for pm to work
 547  */
 548 static int mt9t031_runtime_suspend(struct device *dev)
 549 {
 550         return 0;
 551 }
 552 
 553 /*
 554  * Power Management:
 555  * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
 556  * they are however changed at reset if the platform hook is present
 557  * thus we rewrite them with the values stored by the driver
 558  */
 559 static int mt9t031_runtime_resume(struct device *dev)
 560 {
 561         struct video_device *vdev = to_video_device(dev);
 562         struct v4l2_subdev *sd = soc_camera_vdev_to_subdev(vdev);
 563         struct i2c_client *client = v4l2_get_subdevdata(sd);
 564         struct mt9t031 *mt9t031 = to_mt9t031(client);
 565 
 566         int ret;
 567         u16 xbin, ybin;
 568 
 569         xbin = min(mt9t031->xskip, (u16)3);
 570         ybin = min(mt9t031->yskip, (u16)3);
 571 
 572         ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
 573                 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
 574         if (ret < 0)
 575                 return ret;
 576 
 577         ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
 578                 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
 579         if (ret < 0)
 580                 return ret;
 581 
 582         return 0;
 583 }
 584 
 585 static const struct dev_pm_ops mt9t031_dev_pm_ops = {
 586         .runtime_suspend        = mt9t031_runtime_suspend,
 587         .runtime_resume         = mt9t031_runtime_resume,
 588 };
 589 
 590 static const struct device_type mt9t031_dev_type = {
 591         .name   = "MT9T031",
 592         .pm     = &mt9t031_dev_pm_ops,
 593 };
 594 
 595 static int mt9t031_s_power(struct v4l2_subdev *sd, int on)
 596 {
 597         struct i2c_client *client = v4l2_get_subdevdata(sd);
 598         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
 599         struct video_device *vdev = soc_camera_i2c_to_vdev(client);
 600         struct mt9t031 *mt9t031 = to_mt9t031(client);
 601         int ret;
 602 
 603         if (on) {
 604                 ret = soc_camera_power_on(&client->dev, ssdd, mt9t031->clk);
 605                 if (ret < 0)
 606                         return ret;
 607                 if (vdev)
 608                         /* Not needed during probing, when vdev isn't available yet */
 609                         vdev->dev.type = &mt9t031_dev_type;
 610         } else {
 611                 if (vdev)
 612                         vdev->dev.type = NULL;
 613                 soc_camera_power_off(&client->dev, ssdd, mt9t031->clk);
 614         }
 615 
 616         return 0;
 617 }
 618 
 619 /*
 620  * Interface active, can use i2c. If it fails, it can indeed mean, that
 621  * this wasn't our capture interface, so, we wait for the right one
 622  */
 623 static int mt9t031_video_probe(struct i2c_client *client)
 624 {
 625         struct mt9t031 *mt9t031 = to_mt9t031(client);
 626         s32 data;
 627         int ret;
 628 
 629         ret = mt9t031_s_power(&mt9t031->subdev, 1);
 630         if (ret < 0)
 631                 return ret;
 632 
 633         ret = mt9t031_idle(client);
 634         if (ret < 0) {
 635                 dev_err(&client->dev, "Failed to initialise the camera\n");
 636                 goto done;
 637         }
 638 
 639         /* Read out the chip version register */
 640         data = reg_read(client, MT9T031_CHIP_VERSION);
 641 
 642         switch (data) {
 643         case 0x1621:
 644                 break;
 645         default:
 646                 dev_err(&client->dev,
 647                         "No MT9T031 chip detected, register read %x\n", data);
 648                 ret = -ENODEV;
 649                 goto done;
 650         }
 651 
 652         dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
 653 
 654         ret = v4l2_ctrl_handler_setup(&mt9t031->hdl);
 655 
 656 done:
 657         mt9t031_s_power(&mt9t031->subdev, 0);
 658 
 659         return ret;
 660 }
 661 
 662 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
 663 {
 664         struct i2c_client *client = v4l2_get_subdevdata(sd);
 665         struct mt9t031 *mt9t031 = to_mt9t031(client);
 666 
 667         *lines = mt9t031->y_skip_top;
 668 
 669         return 0;
 670 }
 671 
 672 static const struct v4l2_ctrl_ops mt9t031_ctrl_ops = {
 673         .g_volatile_ctrl = mt9t031_g_volatile_ctrl,
 674         .s_ctrl = mt9t031_s_ctrl,
 675 };
 676 
 677 static const struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
 678         .s_power        = mt9t031_s_power,
 679 #ifdef CONFIG_VIDEO_ADV_DEBUG
 680         .g_register     = mt9t031_g_register,
 681         .s_register     = mt9t031_s_register,
 682 #endif
 683 };
 684 
 685 static int mt9t031_enum_mbus_code(struct v4l2_subdev *sd,
 686                 struct v4l2_subdev_pad_config *cfg,
 687                 struct v4l2_subdev_mbus_code_enum *code)
 688 {
 689         if (code->pad || code->index)
 690                 return -EINVAL;
 691 
 692         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
 693         return 0;
 694 }
 695 
 696 static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
 697                                 struct v4l2_mbus_config *cfg)
 698 {
 699         struct i2c_client *client = v4l2_get_subdevdata(sd);
 700         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
 701 
 702         cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
 703                 V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
 704                 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
 705         cfg->type = V4L2_MBUS_PARALLEL;
 706         cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
 707 
 708         return 0;
 709 }
 710 
 711 static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
 712                                 const struct v4l2_mbus_config *cfg)
 713 {
 714         struct i2c_client *client = v4l2_get_subdevdata(sd);
 715         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
 716 
 717         if (soc_camera_apply_board_flags(ssdd, cfg) &
 718             V4L2_MBUS_PCLK_SAMPLE_FALLING)
 719                 return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
 720         else
 721                 return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
 722 }
 723 
 724 static const struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
 725         .s_stream       = mt9t031_s_stream,
 726         .g_mbus_config  = mt9t031_g_mbus_config,
 727         .s_mbus_config  = mt9t031_s_mbus_config,
 728 };
 729 
 730 static const struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
 731         .g_skip_top_lines       = mt9t031_g_skip_top_lines,
 732 };
 733 
 734 static const struct v4l2_subdev_pad_ops mt9t031_subdev_pad_ops = {
 735         .enum_mbus_code = mt9t031_enum_mbus_code,
 736         .get_selection  = mt9t031_get_selection,
 737         .set_selection  = mt9t031_set_selection,
 738         .get_fmt        = mt9t031_get_fmt,
 739         .set_fmt        = mt9t031_set_fmt,
 740 };
 741 
 742 static const struct v4l2_subdev_ops mt9t031_subdev_ops = {
 743         .core   = &mt9t031_subdev_core_ops,
 744         .video  = &mt9t031_subdev_video_ops,
 745         .sensor = &mt9t031_subdev_sensor_ops,
 746         .pad    = &mt9t031_subdev_pad_ops,
 747 };
 748 
 749 static int mt9t031_probe(struct i2c_client *client,
 750                          const struct i2c_device_id *did)
 751 {
 752         struct mt9t031 *mt9t031;
 753         struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
 754         struct i2c_adapter *adapter = client->adapter;
 755         int ret;
 756 
 757         if (!ssdd) {
 758                 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
 759                 return -EINVAL;
 760         }
 761 
 762         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
 763                 dev_warn(&adapter->dev,
 764                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
 765                 return -EIO;
 766         }
 767 
 768         mt9t031 = devm_kzalloc(&client->dev, sizeof(struct mt9t031), GFP_KERNEL);
 769         if (!mt9t031)
 770                 return -ENOMEM;
 771 
 772         v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
 773         v4l2_ctrl_handler_init(&mt9t031->hdl, 5);
 774         v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
 775                         V4L2_CID_VFLIP, 0, 1, 1, 0);
 776         v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
 777                         V4L2_CID_HFLIP, 0, 1, 1, 0);
 778         v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
 779                         V4L2_CID_GAIN, 0, 127, 1, 64);
 780 
 781         /*
 782          * Simulated autoexposure. If enabled, we calculate shutter width
 783          * ourselves in the driver based on vertical blanking and frame width
 784          */
 785         mt9t031->autoexposure = v4l2_ctrl_new_std_menu(&mt9t031->hdl,
 786                         &mt9t031_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
 787                         V4L2_EXPOSURE_AUTO);
 788         mt9t031->exposure = v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
 789                         V4L2_CID_EXPOSURE, 1, 255, 1, 255);
 790 
 791         mt9t031->subdev.ctrl_handler = &mt9t031->hdl;
 792         if (mt9t031->hdl.error)
 793                 return mt9t031->hdl.error;
 794 
 795         v4l2_ctrl_auto_cluster(2, &mt9t031->autoexposure,
 796                                 V4L2_EXPOSURE_MANUAL, true);
 797 
 798         mt9t031->y_skip_top     = 0;
 799         mt9t031->rect.left      = MT9T031_COLUMN_SKIP;
 800         mt9t031->rect.top       = MT9T031_ROW_SKIP;
 801         mt9t031->rect.width     = MT9T031_MAX_WIDTH;
 802         mt9t031->rect.height    = MT9T031_MAX_HEIGHT;
 803 
 804         mt9t031->xskip = 1;
 805         mt9t031->yskip = 1;
 806 
 807         mt9t031->clk = v4l2_clk_get(&client->dev, "mclk");
 808         if (IS_ERR(mt9t031->clk)) {
 809                 ret = PTR_ERR(mt9t031->clk);
 810                 goto eclkget;
 811         }
 812 
 813         ret = mt9t031_video_probe(client);
 814         if (ret) {
 815                 v4l2_clk_put(mt9t031->clk);
 816 eclkget:
 817                 v4l2_ctrl_handler_free(&mt9t031->hdl);
 818         }
 819 
 820         return ret;
 821 }
 822 
 823 static int mt9t031_remove(struct i2c_client *client)
 824 {
 825         struct mt9t031 *mt9t031 = to_mt9t031(client);
 826 
 827         v4l2_clk_put(mt9t031->clk);
 828         v4l2_device_unregister_subdev(&mt9t031->subdev);
 829         v4l2_ctrl_handler_free(&mt9t031->hdl);
 830 
 831         return 0;
 832 }
 833 
 834 static const struct i2c_device_id mt9t031_id[] = {
 835         { "mt9t031", 0 },
 836         { }
 837 };
 838 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
 839 
 840 static struct i2c_driver mt9t031_i2c_driver = {
 841         .driver = {
 842                 .name = "mt9t031",
 843         },
 844         .probe          = mt9t031_probe,
 845         .remove         = mt9t031_remove,
 846         .id_table       = mt9t031_id,
 847 };
 848 
 849 module_i2c_driver(mt9t031_i2c_driver);
 850 
 851 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
 852 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
 853 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */