1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for MT9M001 CMOS Image Sensor from Micron 4 * 5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> 6 */ 7 8 #include <linux/i2c.h> 9 #include <linux/log2.h> 10 #include <linux/module.h> 11 #include <linux/slab.h> 12 #include <linux/videodev2.h> 13 14 #include <media/drv-intf/soc_mediabus.h> 15 #include <media/soc_camera.h> 16 #include <media/v4l2-clk.h> 17 #include <media/v4l2-ctrls.h> 18 #include <media/v4l2-subdev.h> 19 20 /* 21 * mt9m001 i2c address 0x5d 22 * The platform has to define struct i2c_board_info objects and link to them 23 * from struct soc_camera_host_desc 24 */ 25 26 /* mt9m001 selected register addresses */ 27 #define MT9M001_CHIP_VERSION 0x00 28 #define MT9M001_ROW_START 0x01 29 #define MT9M001_COLUMN_START 0x02 30 #define MT9M001_WINDOW_HEIGHT 0x03 31 #define MT9M001_WINDOW_WIDTH 0x04 32 #define MT9M001_HORIZONTAL_BLANKING 0x05 33 #define MT9M001_VERTICAL_BLANKING 0x06 34 #define MT9M001_OUTPUT_CONTROL 0x07 35 #define MT9M001_SHUTTER_WIDTH 0x09 36 #define MT9M001_FRAME_RESTART 0x0b 37 #define MT9M001_SHUTTER_DELAY 0x0c 38 #define MT9M001_RESET 0x0d 39 #define MT9M001_READ_OPTIONS1 0x1e 40 #define MT9M001_READ_OPTIONS2 0x20 41 #define MT9M001_GLOBAL_GAIN 0x35 42 #define MT9M001_CHIP_ENABLE 0xF1 43 44 #define MT9M001_MAX_WIDTH 1280 45 #define MT9M001_MAX_HEIGHT 1024 46 #define MT9M001_MIN_WIDTH 48 47 #define MT9M001_MIN_HEIGHT 32 48 #define MT9M001_COLUMN_SKIP 20 49 #define MT9M001_ROW_SKIP 12 50 51 /* MT9M001 has only one fixed colorspace per pixelcode */ 52 struct mt9m001_datafmt { 53 u32 code; 54 enum v4l2_colorspace colorspace; 55 }; 56 57 /* Find a data format by a pixel code in an array */ 58 static const struct mt9m001_datafmt *mt9m001_find_datafmt( 59 u32 code, const struct mt9m001_datafmt *fmt, 60 int n) 61 { 62 int i; 63 for (i = 0; i < n; i++) 64 if (fmt[i].code == code) 65 return fmt + i; 66 67 return NULL; 68 } 69 70 static const struct mt9m001_datafmt mt9m001_colour_fmts[] = { 71 /* 72 * Order important: first natively supported, 73 * second supported with a GPIO extender 74 */ 75 {MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB}, 76 {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB}, 77 }; 78 79 static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = { 80 /* Order important - see above */ 81 {MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG}, 82 {MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG}, 83 }; 84 85 struct mt9m001 { 86 struct v4l2_subdev subdev; 87 struct v4l2_ctrl_handler hdl; 88 struct { 89 /* exposure/auto-exposure cluster */ 90 struct v4l2_ctrl *autoexposure; 91 struct v4l2_ctrl *exposure; 92 }; 93 struct v4l2_rect rect; /* Sensor window */ 94 struct v4l2_clk *clk; 95 const struct mt9m001_datafmt *fmt; 96 const struct mt9m001_datafmt *fmts; 97 int num_fmts; 98 unsigned int total_h; 99 unsigned short y_skip_top; /* Lines to skip at the top */ 100 }; 101 102 static struct mt9m001 *to_mt9m001(const struct i2c_client *client) 103 { 104 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev); 105 } 106 107 static int reg_read(struct i2c_client *client, const u8 reg) 108 { 109 return i2c_smbus_read_word_swapped(client, reg); 110 } 111 112 static int reg_write(struct i2c_client *client, const u8 reg, 113 const u16 data) 114 { 115 return i2c_smbus_write_word_swapped(client, reg, data); 116 } 117 118 static int reg_set(struct i2c_client *client, const u8 reg, 119 const u16 data) 120 { 121 int ret; 122 123 ret = reg_read(client, reg); 124 if (ret < 0) 125 return ret; 126 return reg_write(client, reg, ret | data); 127 } 128 129 static int reg_clear(struct i2c_client *client, const u8 reg, 130 const u16 data) 131 { 132 int ret; 133 134 ret = reg_read(client, reg); 135 if (ret < 0) 136 return ret; 137 return reg_write(client, reg, ret & ~data); 138 } 139 140 static int mt9m001_init(struct i2c_client *client) 141 { 142 int ret; 143 144 dev_dbg(&client->dev, "%s\n", __func__); 145 146 /* 147 * We don't know, whether platform provides reset, issue a soft reset 148 * too. This returns all registers to their default values. 149 */ 150 ret = reg_write(client, MT9M001_RESET, 1); 151 if (!ret) 152 ret = reg_write(client, MT9M001_RESET, 0); 153 154 /* Disable chip, synchronous option update */ 155 if (!ret) 156 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0); 157 158 return ret; 159 } 160 161 static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable) 162 { 163 struct i2c_client *client = v4l2_get_subdevdata(sd); 164 165 /* Switch to master "normal" mode or stop sensor readout */ 166 if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0) 167 return -EIO; 168 return 0; 169 } 170 171 static int mt9m001_set_selection(struct v4l2_subdev *sd, 172 struct v4l2_subdev_pad_config *cfg, 173 struct v4l2_subdev_selection *sel) 174 { 175 struct i2c_client *client = v4l2_get_subdevdata(sd); 176 struct mt9m001 *mt9m001 = to_mt9m001(client); 177 struct v4l2_rect rect = sel->r; 178 const u16 hblank = 9, vblank = 25; 179 int ret; 180 181 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE || 182 sel->target != V4L2_SEL_TGT_CROP) 183 return -EINVAL; 184 185 if (mt9m001->fmts == mt9m001_colour_fmts) 186 /* 187 * Bayer format - even number of rows for simplicity, 188 * but let the user play with the top row. 189 */ 190 rect.height = ALIGN(rect.height, 2); 191 192 /* Datasheet requirement: see register description */ 193 rect.width = ALIGN(rect.width, 2); 194 rect.left = ALIGN(rect.left, 2); 195 196 soc_camera_limit_side(&rect.left, &rect.width, 197 MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH); 198 199 soc_camera_limit_side(&rect.top, &rect.height, 200 MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT); 201 202 mt9m001->total_h = rect.height + mt9m001->y_skip_top + vblank; 203 204 /* Blanking and start values - default... */ 205 ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank); 206 if (!ret) 207 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank); 208 209 /* 210 * The caller provides a supported format, as verified per 211 * call to .set_fmt(FORMAT_TRY). 212 */ 213 if (!ret) 214 ret = reg_write(client, MT9M001_COLUMN_START, rect.left); 215 if (!ret) 216 ret = reg_write(client, MT9M001_ROW_START, rect.top); 217 if (!ret) 218 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1); 219 if (!ret) 220 ret = reg_write(client, MT9M001_WINDOW_HEIGHT, 221 rect.height + mt9m001->y_skip_top - 1); 222 if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO) 223 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h); 224 225 if (!ret) 226 mt9m001->rect = rect; 227 228 return ret; 229 } 230 231 static int mt9m001_get_selection(struct v4l2_subdev *sd, 232 struct v4l2_subdev_pad_config *cfg, 233 struct v4l2_subdev_selection *sel) 234 { 235 struct i2c_client *client = v4l2_get_subdevdata(sd); 236 struct mt9m001 *mt9m001 = to_mt9m001(client); 237 238 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) 239 return -EINVAL; 240 241 switch (sel->target) { 242 case V4L2_SEL_TGT_CROP_BOUNDS: 243 sel->r.left = MT9M001_COLUMN_SKIP; 244 sel->r.top = MT9M001_ROW_SKIP; 245 sel->r.width = MT9M001_MAX_WIDTH; 246 sel->r.height = MT9M001_MAX_HEIGHT; 247 return 0; 248 case V4L2_SEL_TGT_CROP: 249 sel->r = mt9m001->rect; 250 return 0; 251 default: 252 return -EINVAL; 253 } 254 } 255 256 static int mt9m001_get_fmt(struct v4l2_subdev *sd, 257 struct v4l2_subdev_pad_config *cfg, 258 struct v4l2_subdev_format *format) 259 { 260 struct i2c_client *client = v4l2_get_subdevdata(sd); 261 struct mt9m001 *mt9m001 = to_mt9m001(client); 262 struct v4l2_mbus_framefmt *mf = &format->format; 263 264 if (format->pad) 265 return -EINVAL; 266 267 mf->width = mt9m001->rect.width; 268 mf->height = mt9m001->rect.height; 269 mf->code = mt9m001->fmt->code; 270 mf->colorspace = mt9m001->fmt->colorspace; 271 mf->field = V4L2_FIELD_NONE; 272 273 return 0; 274 } 275 276 static int mt9m001_s_fmt(struct v4l2_subdev *sd, 277 const struct mt9m001_datafmt *fmt, 278 struct v4l2_mbus_framefmt *mf) 279 { 280 struct i2c_client *client = v4l2_get_subdevdata(sd); 281 struct mt9m001 *mt9m001 = to_mt9m001(client); 282 struct v4l2_subdev_selection sel = { 283 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 284 .target = V4L2_SEL_TGT_CROP, 285 .r.left = mt9m001->rect.left, 286 .r.top = mt9m001->rect.top, 287 .r.width = mf->width, 288 .r.height = mf->height, 289 }; 290 int ret; 291 292 /* No support for scaling so far, just crop. TODO: use skipping */ 293 ret = mt9m001_set_selection(sd, NULL, &sel); 294 if (!ret) { 295 mf->width = mt9m001->rect.width; 296 mf->height = mt9m001->rect.height; 297 mt9m001->fmt = fmt; 298 mf->colorspace = fmt->colorspace; 299 } 300 301 return ret; 302 } 303 304 static int mt9m001_set_fmt(struct v4l2_subdev *sd, 305 struct v4l2_subdev_pad_config *cfg, 306 struct v4l2_subdev_format *format) 307 { 308 struct v4l2_mbus_framefmt *mf = &format->format; 309 struct i2c_client *client = v4l2_get_subdevdata(sd); 310 struct mt9m001 *mt9m001 = to_mt9m001(client); 311 const struct mt9m001_datafmt *fmt; 312 313 if (format->pad) 314 return -EINVAL; 315 316 v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH, 317 MT9M001_MAX_WIDTH, 1, 318 &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top, 319 MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0); 320 321 if (mt9m001->fmts == mt9m001_colour_fmts) 322 mf->height = ALIGN(mf->height - 1, 2); 323 324 fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts, 325 mt9m001->num_fmts); 326 if (!fmt) { 327 fmt = mt9m001->fmt; 328 mf->code = fmt->code; 329 } 330 331 mf->colorspace = fmt->colorspace; 332 333 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) 334 return mt9m001_s_fmt(sd, fmt, mf); 335 cfg->try_fmt = *mf; 336 return 0; 337 } 338 339 #ifdef CONFIG_VIDEO_ADV_DEBUG 340 static int mt9m001_g_register(struct v4l2_subdev *sd, 341 struct v4l2_dbg_register *reg) 342 { 343 struct i2c_client *client = v4l2_get_subdevdata(sd); 344 345 if (reg->reg > 0xff) 346 return -EINVAL; 347 348 reg->size = 2; 349 reg->val = reg_read(client, reg->reg); 350 351 if (reg->val > 0xffff) 352 return -EIO; 353 354 return 0; 355 } 356 357 static int mt9m001_s_register(struct v4l2_subdev *sd, 358 const struct v4l2_dbg_register *reg) 359 { 360 struct i2c_client *client = v4l2_get_subdevdata(sd); 361 362 if (reg->reg > 0xff) 363 return -EINVAL; 364 365 if (reg_write(client, reg->reg, reg->val) < 0) 366 return -EIO; 367 368 return 0; 369 } 370 #endif 371 372 static int mt9m001_s_power(struct v4l2_subdev *sd, int on) 373 { 374 struct i2c_client *client = v4l2_get_subdevdata(sd); 375 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); 376 struct mt9m001 *mt9m001 = to_mt9m001(client); 377 378 return soc_camera_set_power(&client->dev, ssdd, mt9m001->clk, on); 379 } 380 381 static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 382 { 383 struct mt9m001 *mt9m001 = container_of(ctrl->handler, 384 struct mt9m001, hdl); 385 s32 min, max; 386 387 switch (ctrl->id) { 388 case V4L2_CID_EXPOSURE_AUTO: 389 min = mt9m001->exposure->minimum; 390 max = mt9m001->exposure->maximum; 391 mt9m001->exposure->val = 392 (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min; 393 break; 394 } 395 return 0; 396 } 397 398 static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl) 399 { 400 struct mt9m001 *mt9m001 = container_of(ctrl->handler, 401 struct mt9m001, hdl); 402 struct v4l2_subdev *sd = &mt9m001->subdev; 403 struct i2c_client *client = v4l2_get_subdevdata(sd); 404 struct v4l2_ctrl *exp = mt9m001->exposure; 405 int data; 406 407 switch (ctrl->id) { 408 case V4L2_CID_VFLIP: 409 if (ctrl->val) 410 data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000); 411 else 412 data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000); 413 if (data < 0) 414 return -EIO; 415 return 0; 416 417 case V4L2_CID_GAIN: 418 /* See Datasheet Table 7, Gain settings. */ 419 if (ctrl->val <= ctrl->default_value) { 420 /* Pack it into 0..1 step 0.125, register values 0..8 */ 421 unsigned long range = ctrl->default_value - ctrl->minimum; 422 data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range; 423 424 dev_dbg(&client->dev, "Setting gain %d\n", data); 425 data = reg_write(client, MT9M001_GLOBAL_GAIN, data); 426 if (data < 0) 427 return -EIO; 428 } else { 429 /* Pack it into 1.125..15 variable step, register values 9..67 */ 430 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ 431 unsigned long range = ctrl->maximum - ctrl->default_value - 1; 432 unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) * 433 111 + range / 2) / range + 9; 434 435 if (gain <= 32) 436 data = gain; 437 else if (gain <= 64) 438 data = ((gain - 32) * 16 + 16) / 32 + 80; 439 else 440 data = ((gain - 64) * 7 + 28) / 56 + 96; 441 442 dev_dbg(&client->dev, "Setting gain from %d to %d\n", 443 reg_read(client, MT9M001_GLOBAL_GAIN), data); 444 data = reg_write(client, MT9M001_GLOBAL_GAIN, data); 445 if (data < 0) 446 return -EIO; 447 } 448 return 0; 449 450 case V4L2_CID_EXPOSURE_AUTO: 451 if (ctrl->val == V4L2_EXPOSURE_MANUAL) { 452 unsigned long range = exp->maximum - exp->minimum; 453 unsigned long shutter = ((exp->val - (s32)exp->minimum) * 1048 + 454 range / 2) / range + 1; 455 456 dev_dbg(&client->dev, 457 "Setting shutter width from %d to %lu\n", 458 reg_read(client, MT9M001_SHUTTER_WIDTH), shutter); 459 if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0) 460 return -EIO; 461 } else { 462 const u16 vblank = 25; 463 464 mt9m001->total_h = mt9m001->rect.height + 465 mt9m001->y_skip_top + vblank; 466 if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0) 467 return -EIO; 468 } 469 return 0; 470 } 471 return -EINVAL; 472 } 473 474 /* 475 * Interface active, can use i2c. If it fails, it can indeed mean, that 476 * this wasn't our capture interface, so, we wait for the right one 477 */ 478 static int mt9m001_video_probe(struct soc_camera_subdev_desc *ssdd, 479 struct i2c_client *client) 480 { 481 struct mt9m001 *mt9m001 = to_mt9m001(client); 482 s32 data; 483 unsigned long flags; 484 int ret; 485 486 ret = mt9m001_s_power(&mt9m001->subdev, 1); 487 if (ret < 0) 488 return ret; 489 490 /* Enable the chip */ 491 data = reg_write(client, MT9M001_CHIP_ENABLE, 1); 492 dev_dbg(&client->dev, "write: %d\n", data); 493 494 /* Read out the chip version register */ 495 data = reg_read(client, MT9M001_CHIP_VERSION); 496 497 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ 498 switch (data) { 499 case 0x8411: 500 case 0x8421: 501 mt9m001->fmts = mt9m001_colour_fmts; 502 break; 503 case 0x8431: 504 mt9m001->fmts = mt9m001_monochrome_fmts; 505 break; 506 default: 507 dev_err(&client->dev, 508 "No MT9M001 chip detected, register read %x\n", data); 509 ret = -ENODEV; 510 goto done; 511 } 512 513 mt9m001->num_fmts = 0; 514 515 /* 516 * This is a 10bit sensor, so by default we only allow 10bit. 517 * The platform may support different bus widths due to 518 * different routing of the data lines. 519 */ 520 if (ssdd->query_bus_param) 521 flags = ssdd->query_bus_param(ssdd); 522 else 523 flags = SOCAM_DATAWIDTH_10; 524 525 if (flags & SOCAM_DATAWIDTH_10) 526 mt9m001->num_fmts++; 527 else 528 mt9m001->fmts++; 529 530 if (flags & SOCAM_DATAWIDTH_8) 531 mt9m001->num_fmts++; 532 533 mt9m001->fmt = &mt9m001->fmts[0]; 534 535 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, 536 data == 0x8431 ? "C12STM" : "C12ST"); 537 538 ret = mt9m001_init(client); 539 if (ret < 0) { 540 dev_err(&client->dev, "Failed to initialise the camera\n"); 541 goto done; 542 } 543 544 /* mt9m001_init() has reset the chip, returning registers to defaults */ 545 ret = v4l2_ctrl_handler_setup(&mt9m001->hdl); 546 547 done: 548 mt9m001_s_power(&mt9m001->subdev, 0); 549 return ret; 550 } 551 552 static void mt9m001_video_remove(struct soc_camera_subdev_desc *ssdd) 553 { 554 if (ssdd->free_bus) 555 ssdd->free_bus(ssdd); 556 } 557 558 static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines) 559 { 560 struct i2c_client *client = v4l2_get_subdevdata(sd); 561 struct mt9m001 *mt9m001 = to_mt9m001(client); 562 563 *lines = mt9m001->y_skip_top; 564 565 return 0; 566 } 567 568 static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = { 569 .g_volatile_ctrl = mt9m001_g_volatile_ctrl, 570 .s_ctrl = mt9m001_s_ctrl, 571 }; 572 573 static const struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = { 574 #ifdef CONFIG_VIDEO_ADV_DEBUG 575 .g_register = mt9m001_g_register, 576 .s_register = mt9m001_s_register, 577 #endif 578 .s_power = mt9m001_s_power, 579 }; 580 581 static int mt9m001_enum_mbus_code(struct v4l2_subdev *sd, 582 struct v4l2_subdev_pad_config *cfg, 583 struct v4l2_subdev_mbus_code_enum *code) 584 { 585 struct i2c_client *client = v4l2_get_subdevdata(sd); 586 struct mt9m001 *mt9m001 = to_mt9m001(client); 587 588 if (code->pad || code->index >= mt9m001->num_fmts) 589 return -EINVAL; 590 591 code->code = mt9m001->fmts[code->index].code; 592 return 0; 593 } 594 595 static int mt9m001_g_mbus_config(struct v4l2_subdev *sd, 596 struct v4l2_mbus_config *cfg) 597 { 598 struct i2c_client *client = v4l2_get_subdevdata(sd); 599 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); 600 601 /* MT9M001 has all capture_format parameters fixed */ 602 cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING | 603 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH | 604 V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER; 605 cfg->type = V4L2_MBUS_PARALLEL; 606 cfg->flags = soc_camera_apply_board_flags(ssdd, cfg); 607 608 return 0; 609 } 610 611 static int mt9m001_s_mbus_config(struct v4l2_subdev *sd, 612 const struct v4l2_mbus_config *cfg) 613 { 614 const struct i2c_client *client = v4l2_get_subdevdata(sd); 615 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); 616 struct mt9m001 *mt9m001 = to_mt9m001(client); 617 unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample; 618 619 if (ssdd->set_bus_param) 620 return ssdd->set_bus_param(ssdd, 1 << (bps - 1)); 621 622 /* 623 * Without board specific bus width settings we only support the 624 * sensors native bus width 625 */ 626 return bps == 10 ? 0 : -EINVAL; 627 } 628 629 static const struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = { 630 .s_stream = mt9m001_s_stream, 631 .g_mbus_config = mt9m001_g_mbus_config, 632 .s_mbus_config = mt9m001_s_mbus_config, 633 }; 634 635 static const struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = { 636 .g_skip_top_lines = mt9m001_g_skip_top_lines, 637 }; 638 639 static const struct v4l2_subdev_pad_ops mt9m001_subdev_pad_ops = { 640 .enum_mbus_code = mt9m001_enum_mbus_code, 641 .get_selection = mt9m001_get_selection, 642 .set_selection = mt9m001_set_selection, 643 .get_fmt = mt9m001_get_fmt, 644 .set_fmt = mt9m001_set_fmt, 645 }; 646 647 static const struct v4l2_subdev_ops mt9m001_subdev_ops = { 648 .core = &mt9m001_subdev_core_ops, 649 .video = &mt9m001_subdev_video_ops, 650 .sensor = &mt9m001_subdev_sensor_ops, 651 .pad = &mt9m001_subdev_pad_ops, 652 }; 653 654 static int mt9m001_probe(struct i2c_client *client, 655 const struct i2c_device_id *did) 656 { 657 struct mt9m001 *mt9m001; 658 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 659 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); 660 int ret; 661 662 if (!ssdd) { 663 dev_err(&client->dev, "MT9M001 driver needs platform data\n"); 664 return -EINVAL; 665 } 666 667 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { 668 dev_warn(&adapter->dev, 669 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); 670 return -EIO; 671 } 672 673 mt9m001 = devm_kzalloc(&client->dev, sizeof(struct mt9m001), GFP_KERNEL); 674 if (!mt9m001) 675 return -ENOMEM; 676 677 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops); 678 v4l2_ctrl_handler_init(&mt9m001->hdl, 4); 679 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops, 680 V4L2_CID_VFLIP, 0, 1, 1, 0); 681 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops, 682 V4L2_CID_GAIN, 0, 127, 1, 64); 683 mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops, 684 V4L2_CID_EXPOSURE, 1, 255, 1, 255); 685 /* 686 * Simulated autoexposure. If enabled, we calculate shutter width 687 * ourselves in the driver based on vertical blanking and frame width 688 */ 689 mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl, 690 &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0, 691 V4L2_EXPOSURE_AUTO); 692 mt9m001->subdev.ctrl_handler = &mt9m001->hdl; 693 if (mt9m001->hdl.error) 694 return mt9m001->hdl.error; 695 696 v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure, 697 V4L2_EXPOSURE_MANUAL, true); 698 699 /* Second stage probe - when a capture adapter is there */ 700 mt9m001->y_skip_top = 0; 701 mt9m001->rect.left = MT9M001_COLUMN_SKIP; 702 mt9m001->rect.top = MT9M001_ROW_SKIP; 703 mt9m001->rect.width = MT9M001_MAX_WIDTH; 704 mt9m001->rect.height = MT9M001_MAX_HEIGHT; 705 706 mt9m001->clk = v4l2_clk_get(&client->dev, "mclk"); 707 if (IS_ERR(mt9m001->clk)) { 708 ret = PTR_ERR(mt9m001->clk); 709 goto eclkget; 710 } 711 712 ret = mt9m001_video_probe(ssdd, client); 713 if (ret) { 714 v4l2_clk_put(mt9m001->clk); 715 eclkget: 716 v4l2_ctrl_handler_free(&mt9m001->hdl); 717 } 718 719 return ret; 720 } 721 722 static int mt9m001_remove(struct i2c_client *client) 723 { 724 struct mt9m001 *mt9m001 = to_mt9m001(client); 725 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client); 726 727 v4l2_clk_put(mt9m001->clk); 728 v4l2_device_unregister_subdev(&mt9m001->subdev); 729 v4l2_ctrl_handler_free(&mt9m001->hdl); 730 mt9m001_video_remove(ssdd); 731 732 return 0; 733 } 734 735 static const struct i2c_device_id mt9m001_id[] = { 736 { "mt9m001", 0 }, 737 { } 738 }; 739 MODULE_DEVICE_TABLE(i2c, mt9m001_id); 740 741 static const struct of_device_id mt9m001_of_match[] = { 742 { .compatible = "onnn,mt9m001", }, 743 { /* sentinel */ }, 744 }; 745 MODULE_DEVICE_TABLE(of, mt9m001_of_match); 746 747 static struct i2c_driver mt9m001_i2c_driver = { 748 .driver = { 749 .name = "mt9m001", 750 .of_match_table = mt9m001_of_match, 751 }, 752 .probe = mt9m001_probe, 753 .remove = mt9m001_remove, 754 .id_table = mt9m001_id, 755 }; 756 757 module_i2c_driver(mt9m001_i2c_driver); 758 759 MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); 760 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); 761 MODULE_LICENSE("GPL v2"); 762