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