0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/clk.h>
0009 #include <linux/delay.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/i2c.h>
0012 #include <linux/log2.h>
0013 #include <linux/module.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/slab.h>
0016 #include <linux/videodev2.h>
0017
0018 #include <media/v4l2-ctrls.h>
0019 #include <media/v4l2-device.h>
0020 #include <media/v4l2-event.h>
0021 #include <media/v4l2-subdev.h>
0022
0023
0024
0025
0026
0027
0028 #define MT9M001_CHIP_VERSION 0x00
0029 #define MT9M001_ROW_START 0x01
0030 #define MT9M001_COLUMN_START 0x02
0031 #define MT9M001_WINDOW_HEIGHT 0x03
0032 #define MT9M001_WINDOW_WIDTH 0x04
0033 #define MT9M001_HORIZONTAL_BLANKING 0x05
0034 #define MT9M001_VERTICAL_BLANKING 0x06
0035 #define MT9M001_OUTPUT_CONTROL 0x07
0036 #define MT9M001_SHUTTER_WIDTH 0x09
0037 #define MT9M001_FRAME_RESTART 0x0b
0038 #define MT9M001_SHUTTER_DELAY 0x0c
0039 #define MT9M001_RESET 0x0d
0040 #define MT9M001_READ_OPTIONS1 0x1e
0041 #define MT9M001_READ_OPTIONS2 0x20
0042 #define MT9M001_GLOBAL_GAIN 0x35
0043 #define MT9M001_CHIP_ENABLE 0xF1
0044
0045 #define MT9M001_MAX_WIDTH 1280
0046 #define MT9M001_MAX_HEIGHT 1024
0047 #define MT9M001_MIN_WIDTH 48
0048 #define MT9M001_MIN_HEIGHT 32
0049 #define MT9M001_COLUMN_SKIP 20
0050 #define MT9M001_ROW_SKIP 12
0051 #define MT9M001_DEFAULT_HBLANK 9
0052 #define MT9M001_DEFAULT_VBLANK 25
0053
0054
0055 struct mt9m001_datafmt {
0056 u32 code;
0057 enum v4l2_colorspace colorspace;
0058 };
0059
0060
0061 static const struct mt9m001_datafmt *mt9m001_find_datafmt(
0062 u32 code, const struct mt9m001_datafmt *fmt,
0063 int n)
0064 {
0065 int i;
0066 for (i = 0; i < n; i++)
0067 if (fmt[i].code == code)
0068 return fmt + i;
0069
0070 return NULL;
0071 }
0072
0073 static const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
0074
0075
0076
0077
0078 {MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
0079 {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
0080 };
0081
0082 static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
0083
0084 {MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
0085 {MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
0086 };
0087
0088 struct mt9m001 {
0089 struct v4l2_subdev subdev;
0090 struct v4l2_ctrl_handler hdl;
0091 struct {
0092
0093 struct v4l2_ctrl *autoexposure;
0094 struct v4l2_ctrl *exposure;
0095 };
0096 bool streaming;
0097 struct mutex mutex;
0098 struct v4l2_rect rect;
0099 struct clk *clk;
0100 struct gpio_desc *standby_gpio;
0101 struct gpio_desc *reset_gpio;
0102 const struct mt9m001_datafmt *fmt;
0103 const struct mt9m001_datafmt *fmts;
0104 int num_fmts;
0105 unsigned int total_h;
0106 unsigned short y_skip_top;
0107 struct media_pad pad;
0108 };
0109
0110 static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
0111 {
0112 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
0113 }
0114
0115 static int reg_read(struct i2c_client *client, const u8 reg)
0116 {
0117 return i2c_smbus_read_word_swapped(client, reg);
0118 }
0119
0120 static int reg_write(struct i2c_client *client, const u8 reg,
0121 const u16 data)
0122 {
0123 return i2c_smbus_write_word_swapped(client, reg, data);
0124 }
0125
0126 static int reg_set(struct i2c_client *client, const u8 reg,
0127 const u16 data)
0128 {
0129 int ret;
0130
0131 ret = reg_read(client, reg);
0132 if (ret < 0)
0133 return ret;
0134 return reg_write(client, reg, ret | data);
0135 }
0136
0137 static int reg_clear(struct i2c_client *client, const u8 reg,
0138 const u16 data)
0139 {
0140 int ret;
0141
0142 ret = reg_read(client, reg);
0143 if (ret < 0)
0144 return ret;
0145 return reg_write(client, reg, ret & ~data);
0146 }
0147
0148 struct mt9m001_reg {
0149 u8 reg;
0150 u16 data;
0151 };
0152
0153 static int multi_reg_write(struct i2c_client *client,
0154 const struct mt9m001_reg *regs, int num)
0155 {
0156 int i;
0157
0158 for (i = 0; i < num; i++) {
0159 int ret = reg_write(client, regs[i].reg, regs[i].data);
0160
0161 if (ret)
0162 return ret;
0163 }
0164
0165 return 0;
0166 }
0167
0168 static int mt9m001_init(struct i2c_client *client)
0169 {
0170 static const struct mt9m001_reg init_regs[] = {
0171
0172
0173
0174
0175 { MT9M001_RESET, 1 },
0176 { MT9M001_RESET, 0 },
0177
0178 { MT9M001_OUTPUT_CONTROL, 0 }
0179 };
0180
0181 dev_dbg(&client->dev, "%s\n", __func__);
0182
0183 return multi_reg_write(client, init_regs, ARRAY_SIZE(init_regs));
0184 }
0185
0186 static int mt9m001_apply_selection(struct v4l2_subdev *sd)
0187 {
0188 struct i2c_client *client = v4l2_get_subdevdata(sd);
0189 struct mt9m001 *mt9m001 = to_mt9m001(client);
0190 const struct mt9m001_reg regs[] = {
0191
0192 { MT9M001_HORIZONTAL_BLANKING, MT9M001_DEFAULT_HBLANK },
0193 { MT9M001_VERTICAL_BLANKING, MT9M001_DEFAULT_VBLANK },
0194
0195
0196
0197
0198 { MT9M001_COLUMN_START, mt9m001->rect.left },
0199 { MT9M001_ROW_START, mt9m001->rect.top },
0200 { MT9M001_WINDOW_WIDTH, mt9m001->rect.width - 1 },
0201 { MT9M001_WINDOW_HEIGHT,
0202 mt9m001->rect.height + mt9m001->y_skip_top - 1 },
0203 };
0204
0205 return multi_reg_write(client, regs, ARRAY_SIZE(regs));
0206 }
0207
0208 static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
0209 {
0210 struct i2c_client *client = v4l2_get_subdevdata(sd);
0211 struct mt9m001 *mt9m001 = to_mt9m001(client);
0212 int ret = 0;
0213
0214 mutex_lock(&mt9m001->mutex);
0215
0216 if (mt9m001->streaming == enable)
0217 goto done;
0218
0219 if (enable) {
0220 ret = pm_runtime_resume_and_get(&client->dev);
0221 if (ret < 0)
0222 goto unlock;
0223
0224 ret = mt9m001_apply_selection(sd);
0225 if (ret)
0226 goto put_unlock;
0227
0228 ret = __v4l2_ctrl_handler_setup(&mt9m001->hdl);
0229 if (ret)
0230 goto put_unlock;
0231
0232
0233 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 2);
0234 if (ret < 0)
0235 goto put_unlock;
0236 } else {
0237
0238 reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
0239 pm_runtime_put(&client->dev);
0240 }
0241
0242 mt9m001->streaming = enable;
0243 done:
0244 mutex_unlock(&mt9m001->mutex);
0245
0246 return 0;
0247
0248 put_unlock:
0249 pm_runtime_put(&client->dev);
0250 unlock:
0251 mutex_unlock(&mt9m001->mutex);
0252
0253 return ret;
0254 }
0255
0256 static int mt9m001_set_selection(struct v4l2_subdev *sd,
0257 struct v4l2_subdev_state *sd_state,
0258 struct v4l2_subdev_selection *sel)
0259 {
0260 struct i2c_client *client = v4l2_get_subdevdata(sd);
0261 struct mt9m001 *mt9m001 = to_mt9m001(client);
0262 struct v4l2_rect rect = sel->r;
0263
0264 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
0265 sel->target != V4L2_SEL_TGT_CROP)
0266 return -EINVAL;
0267
0268 if (mt9m001->fmts == mt9m001_colour_fmts)
0269
0270
0271
0272
0273 rect.height = ALIGN(rect.height, 2);
0274
0275
0276 rect.width = ALIGN(rect.width, 2);
0277 rect.left = ALIGN(rect.left, 2);
0278
0279 rect.width = clamp_t(u32, rect.width, MT9M001_MIN_WIDTH,
0280 MT9M001_MAX_WIDTH);
0281 rect.left = clamp_t(u32, rect.left, MT9M001_COLUMN_SKIP,
0282 MT9M001_COLUMN_SKIP + MT9M001_MAX_WIDTH - rect.width);
0283
0284 rect.height = clamp_t(u32, rect.height, MT9M001_MIN_HEIGHT,
0285 MT9M001_MAX_HEIGHT);
0286 rect.top = clamp_t(u32, rect.top, MT9M001_ROW_SKIP,
0287 MT9M001_ROW_SKIP + MT9M001_MAX_HEIGHT - rect.height);
0288
0289 mt9m001->total_h = rect.height + mt9m001->y_skip_top +
0290 MT9M001_DEFAULT_VBLANK;
0291
0292 mt9m001->rect = rect;
0293
0294 return 0;
0295 }
0296
0297 static int mt9m001_get_selection(struct v4l2_subdev *sd,
0298 struct v4l2_subdev_state *sd_state,
0299 struct v4l2_subdev_selection *sel)
0300 {
0301 struct i2c_client *client = v4l2_get_subdevdata(sd);
0302 struct mt9m001 *mt9m001 = to_mt9m001(client);
0303
0304 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
0305 return -EINVAL;
0306
0307 switch (sel->target) {
0308 case V4L2_SEL_TGT_CROP_BOUNDS:
0309 sel->r.left = MT9M001_COLUMN_SKIP;
0310 sel->r.top = MT9M001_ROW_SKIP;
0311 sel->r.width = MT9M001_MAX_WIDTH;
0312 sel->r.height = MT9M001_MAX_HEIGHT;
0313 return 0;
0314 case V4L2_SEL_TGT_CROP:
0315 sel->r = mt9m001->rect;
0316 return 0;
0317 default:
0318 return -EINVAL;
0319 }
0320 }
0321
0322 static int mt9m001_get_fmt(struct v4l2_subdev *sd,
0323 struct v4l2_subdev_state *sd_state,
0324 struct v4l2_subdev_format *format)
0325 {
0326 struct i2c_client *client = v4l2_get_subdevdata(sd);
0327 struct mt9m001 *mt9m001 = to_mt9m001(client);
0328 struct v4l2_mbus_framefmt *mf = &format->format;
0329
0330 if (format->pad)
0331 return -EINVAL;
0332
0333 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
0334 mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
0335 format->format = *mf;
0336 return 0;
0337 }
0338
0339 mf->width = mt9m001->rect.width;
0340 mf->height = mt9m001->rect.height;
0341 mf->code = mt9m001->fmt->code;
0342 mf->colorspace = mt9m001->fmt->colorspace;
0343 mf->field = V4L2_FIELD_NONE;
0344 mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
0345 mf->quantization = V4L2_QUANTIZATION_DEFAULT;
0346 mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
0347
0348 return 0;
0349 }
0350
0351 static int mt9m001_s_fmt(struct v4l2_subdev *sd,
0352 const struct mt9m001_datafmt *fmt,
0353 struct v4l2_mbus_framefmt *mf)
0354 {
0355 struct i2c_client *client = v4l2_get_subdevdata(sd);
0356 struct mt9m001 *mt9m001 = to_mt9m001(client);
0357 struct v4l2_subdev_selection sel = {
0358 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0359 .target = V4L2_SEL_TGT_CROP,
0360 .r.left = mt9m001->rect.left,
0361 .r.top = mt9m001->rect.top,
0362 .r.width = mf->width,
0363 .r.height = mf->height,
0364 };
0365 int ret;
0366
0367
0368 ret = mt9m001_set_selection(sd, NULL, &sel);
0369 if (!ret) {
0370 mf->width = mt9m001->rect.width;
0371 mf->height = mt9m001->rect.height;
0372 mt9m001->fmt = fmt;
0373 mf->colorspace = fmt->colorspace;
0374 }
0375
0376 return ret;
0377 }
0378
0379 static int mt9m001_set_fmt(struct v4l2_subdev *sd,
0380 struct v4l2_subdev_state *sd_state,
0381 struct v4l2_subdev_format *format)
0382 {
0383 struct v4l2_mbus_framefmt *mf = &format->format;
0384 struct i2c_client *client = v4l2_get_subdevdata(sd);
0385 struct mt9m001 *mt9m001 = to_mt9m001(client);
0386 const struct mt9m001_datafmt *fmt;
0387
0388 if (format->pad)
0389 return -EINVAL;
0390
0391 v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
0392 MT9M001_MAX_WIDTH, 1,
0393 &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
0394 MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
0395
0396 if (mt9m001->fmts == mt9m001_colour_fmts)
0397 mf->height = ALIGN(mf->height - 1, 2);
0398
0399 fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
0400 mt9m001->num_fmts);
0401 if (!fmt) {
0402 fmt = mt9m001->fmt;
0403 mf->code = fmt->code;
0404 }
0405
0406 mf->colorspace = fmt->colorspace;
0407 mf->field = V4L2_FIELD_NONE;
0408 mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
0409 mf->quantization = V4L2_QUANTIZATION_DEFAULT;
0410 mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
0411
0412 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
0413 return mt9m001_s_fmt(sd, fmt, mf);
0414 sd_state->pads->try_fmt = *mf;
0415 return 0;
0416 }
0417
0418 #ifdef CONFIG_VIDEO_ADV_DEBUG
0419 static int mt9m001_g_register(struct v4l2_subdev *sd,
0420 struct v4l2_dbg_register *reg)
0421 {
0422 struct i2c_client *client = v4l2_get_subdevdata(sd);
0423
0424 if (reg->reg > 0xff)
0425 return -EINVAL;
0426
0427 reg->size = 2;
0428 reg->val = reg_read(client, reg->reg);
0429
0430 if (reg->val > 0xffff)
0431 return -EIO;
0432
0433 return 0;
0434 }
0435
0436 static int mt9m001_s_register(struct v4l2_subdev *sd,
0437 const struct v4l2_dbg_register *reg)
0438 {
0439 struct i2c_client *client = v4l2_get_subdevdata(sd);
0440
0441 if (reg->reg > 0xff)
0442 return -EINVAL;
0443
0444 if (reg_write(client, reg->reg, reg->val) < 0)
0445 return -EIO;
0446
0447 return 0;
0448 }
0449 #endif
0450
0451 static int mt9m001_power_on(struct device *dev)
0452 {
0453 struct i2c_client *client = to_i2c_client(dev);
0454 struct mt9m001 *mt9m001 = to_mt9m001(client);
0455 int ret;
0456
0457 ret = clk_prepare_enable(mt9m001->clk);
0458 if (ret)
0459 return ret;
0460
0461 if (mt9m001->standby_gpio) {
0462 gpiod_set_value_cansleep(mt9m001->standby_gpio, 0);
0463 usleep_range(1000, 2000);
0464 }
0465
0466 if (mt9m001->reset_gpio) {
0467 gpiod_set_value_cansleep(mt9m001->reset_gpio, 1);
0468 usleep_range(1000, 2000);
0469 gpiod_set_value_cansleep(mt9m001->reset_gpio, 0);
0470 usleep_range(1000, 2000);
0471 }
0472
0473 return 0;
0474 }
0475
0476 static int mt9m001_power_off(struct device *dev)
0477 {
0478 struct i2c_client *client = to_i2c_client(dev);
0479 struct mt9m001 *mt9m001 = to_mt9m001(client);
0480
0481 gpiod_set_value_cansleep(mt9m001->standby_gpio, 1);
0482 clk_disable_unprepare(mt9m001->clk);
0483
0484 return 0;
0485 }
0486
0487 static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
0488 {
0489 struct mt9m001 *mt9m001 = container_of(ctrl->handler,
0490 struct mt9m001, hdl);
0491 s32 min, max;
0492
0493 switch (ctrl->id) {
0494 case V4L2_CID_EXPOSURE_AUTO:
0495 min = mt9m001->exposure->minimum;
0496 max = mt9m001->exposure->maximum;
0497 mt9m001->exposure->val =
0498 (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
0499 break;
0500 }
0501 return 0;
0502 }
0503
0504 static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
0505 {
0506 struct mt9m001 *mt9m001 = container_of(ctrl->handler,
0507 struct mt9m001, hdl);
0508 struct v4l2_subdev *sd = &mt9m001->subdev;
0509 struct i2c_client *client = v4l2_get_subdevdata(sd);
0510 struct v4l2_ctrl *exp = mt9m001->exposure;
0511 int data;
0512 int ret;
0513
0514 if (!pm_runtime_get_if_in_use(&client->dev))
0515 return 0;
0516
0517 switch (ctrl->id) {
0518 case V4L2_CID_VFLIP:
0519 if (ctrl->val)
0520 ret = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
0521 else
0522 ret = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
0523 break;
0524
0525 case V4L2_CID_GAIN:
0526
0527 if (ctrl->val <= ctrl->default_value) {
0528
0529 unsigned long range = ctrl->default_value - ctrl->minimum;
0530 data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range;
0531
0532 dev_dbg(&client->dev, "Setting gain %d\n", data);
0533 ret = reg_write(client, MT9M001_GLOBAL_GAIN, data);
0534 } else {
0535
0536
0537 unsigned long range = ctrl->maximum - ctrl->default_value - 1;
0538 unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) *
0539 111 + range / 2) / range + 9;
0540
0541 if (gain <= 32)
0542 data = gain;
0543 else if (gain <= 64)
0544 data = ((gain - 32) * 16 + 16) / 32 + 80;
0545 else
0546 data = ((gain - 64) * 7 + 28) / 56 + 96;
0547
0548 dev_dbg(&client->dev, "Setting gain from %d to %d\n",
0549 reg_read(client, MT9M001_GLOBAL_GAIN), data);
0550 ret = reg_write(client, MT9M001_GLOBAL_GAIN, data);
0551 }
0552 break;
0553
0554 case V4L2_CID_EXPOSURE_AUTO:
0555 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
0556 unsigned long range = exp->maximum - exp->minimum;
0557 unsigned long shutter = ((exp->val - (s32)exp->minimum) * 1048 +
0558 range / 2) / range + 1;
0559
0560 dev_dbg(&client->dev,
0561 "Setting shutter width from %d to %lu\n",
0562 reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
0563 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, shutter);
0564 } else {
0565 mt9m001->total_h = mt9m001->rect.height +
0566 mt9m001->y_skip_top + MT9M001_DEFAULT_VBLANK;
0567 ret = reg_write(client, MT9M001_SHUTTER_WIDTH,
0568 mt9m001->total_h);
0569 }
0570 break;
0571 default:
0572 ret = -EINVAL;
0573 break;
0574 }
0575
0576 pm_runtime_put(&client->dev);
0577
0578 return ret;
0579 }
0580
0581
0582
0583
0584
0585 static int mt9m001_video_probe(struct i2c_client *client)
0586 {
0587 struct mt9m001 *mt9m001 = to_mt9m001(client);
0588 s32 data;
0589 int ret;
0590
0591
0592 data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
0593 dev_dbg(&client->dev, "write: %d\n", data);
0594
0595
0596 data = reg_read(client, MT9M001_CHIP_VERSION);
0597
0598
0599 switch (data) {
0600 case 0x8411:
0601 case 0x8421:
0602 mt9m001->fmts = mt9m001_colour_fmts;
0603 mt9m001->num_fmts = ARRAY_SIZE(mt9m001_colour_fmts);
0604 break;
0605 case 0x8431:
0606 mt9m001->fmts = mt9m001_monochrome_fmts;
0607 mt9m001->num_fmts = ARRAY_SIZE(mt9m001_monochrome_fmts);
0608 break;
0609 default:
0610 dev_err(&client->dev,
0611 "No MT9M001 chip detected, register read %x\n", data);
0612 ret = -ENODEV;
0613 goto done;
0614 }
0615
0616 mt9m001->fmt = &mt9m001->fmts[0];
0617
0618 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
0619 data == 0x8431 ? "C12STM" : "C12ST");
0620
0621 ret = mt9m001_init(client);
0622 if (ret < 0) {
0623 dev_err(&client->dev, "Failed to initialise the camera\n");
0624 goto done;
0625 }
0626
0627
0628 ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
0629
0630 done:
0631 return ret;
0632 }
0633
0634 static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
0635 {
0636 struct i2c_client *client = v4l2_get_subdevdata(sd);
0637 struct mt9m001 *mt9m001 = to_mt9m001(client);
0638
0639 *lines = mt9m001->y_skip_top;
0640
0641 return 0;
0642 }
0643
0644 static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
0645 .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
0646 .s_ctrl = mt9m001_s_ctrl,
0647 };
0648
0649 static const struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
0650 .log_status = v4l2_ctrl_subdev_log_status,
0651 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
0652 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
0653 #ifdef CONFIG_VIDEO_ADV_DEBUG
0654 .g_register = mt9m001_g_register,
0655 .s_register = mt9m001_s_register,
0656 #endif
0657 };
0658
0659 static int mt9m001_init_cfg(struct v4l2_subdev *sd,
0660 struct v4l2_subdev_state *sd_state)
0661 {
0662 struct i2c_client *client = v4l2_get_subdevdata(sd);
0663 struct mt9m001 *mt9m001 = to_mt9m001(client);
0664 struct v4l2_mbus_framefmt *try_fmt =
0665 v4l2_subdev_get_try_format(sd, sd_state, 0);
0666
0667 try_fmt->width = MT9M001_MAX_WIDTH;
0668 try_fmt->height = MT9M001_MAX_HEIGHT;
0669 try_fmt->code = mt9m001->fmts[0].code;
0670 try_fmt->colorspace = mt9m001->fmts[0].colorspace;
0671 try_fmt->field = V4L2_FIELD_NONE;
0672 try_fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
0673 try_fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
0674 try_fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
0675
0676 return 0;
0677 }
0678
0679 static int mt9m001_enum_mbus_code(struct v4l2_subdev *sd,
0680 struct v4l2_subdev_state *sd_state,
0681 struct v4l2_subdev_mbus_code_enum *code)
0682 {
0683 struct i2c_client *client = v4l2_get_subdevdata(sd);
0684 struct mt9m001 *mt9m001 = to_mt9m001(client);
0685
0686 if (code->pad || code->index >= mt9m001->num_fmts)
0687 return -EINVAL;
0688
0689 code->code = mt9m001->fmts[code->index].code;
0690 return 0;
0691 }
0692
0693 static int mt9m001_get_mbus_config(struct v4l2_subdev *sd,
0694 unsigned int pad,
0695 struct v4l2_mbus_config *cfg)
0696 {
0697
0698 cfg->type = V4L2_MBUS_PARALLEL;
0699 cfg->bus.parallel.flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
0700 V4L2_MBUS_HSYNC_ACTIVE_HIGH |
0701 V4L2_MBUS_VSYNC_ACTIVE_HIGH |
0702 V4L2_MBUS_DATA_ACTIVE_HIGH |
0703 V4L2_MBUS_MASTER;
0704
0705 return 0;
0706 }
0707
0708 static const struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
0709 .s_stream = mt9m001_s_stream,
0710 };
0711
0712 static const struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
0713 .g_skip_top_lines = mt9m001_g_skip_top_lines,
0714 };
0715
0716 static const struct v4l2_subdev_pad_ops mt9m001_subdev_pad_ops = {
0717 .init_cfg = mt9m001_init_cfg,
0718 .enum_mbus_code = mt9m001_enum_mbus_code,
0719 .get_selection = mt9m001_get_selection,
0720 .set_selection = mt9m001_set_selection,
0721 .get_fmt = mt9m001_get_fmt,
0722 .set_fmt = mt9m001_set_fmt,
0723 .get_mbus_config = mt9m001_get_mbus_config,
0724 };
0725
0726 static const struct v4l2_subdev_ops mt9m001_subdev_ops = {
0727 .core = &mt9m001_subdev_core_ops,
0728 .video = &mt9m001_subdev_video_ops,
0729 .sensor = &mt9m001_subdev_sensor_ops,
0730 .pad = &mt9m001_subdev_pad_ops,
0731 };
0732
0733 static int mt9m001_probe(struct i2c_client *client)
0734 {
0735 struct mt9m001 *mt9m001;
0736 struct i2c_adapter *adapter = client->adapter;
0737 int ret;
0738
0739 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
0740 dev_warn(&adapter->dev,
0741 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
0742 return -EIO;
0743 }
0744
0745 mt9m001 = devm_kzalloc(&client->dev, sizeof(*mt9m001), GFP_KERNEL);
0746 if (!mt9m001)
0747 return -ENOMEM;
0748
0749 mt9m001->clk = devm_clk_get(&client->dev, NULL);
0750 if (IS_ERR(mt9m001->clk))
0751 return PTR_ERR(mt9m001->clk);
0752
0753 mt9m001->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby",
0754 GPIOD_OUT_LOW);
0755 if (IS_ERR(mt9m001->standby_gpio))
0756 return PTR_ERR(mt9m001->standby_gpio);
0757
0758 mt9m001->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
0759 GPIOD_OUT_LOW);
0760 if (IS_ERR(mt9m001->reset_gpio))
0761 return PTR_ERR(mt9m001->reset_gpio);
0762
0763 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
0764 mt9m001->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
0765 V4L2_SUBDEV_FL_HAS_EVENTS;
0766 v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
0767 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
0768 V4L2_CID_VFLIP, 0, 1, 1, 0);
0769 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
0770 V4L2_CID_GAIN, 0, 127, 1, 64);
0771 mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
0772 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
0773
0774
0775
0776
0777 mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
0778 &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
0779 V4L2_EXPOSURE_AUTO);
0780 mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
0781 if (mt9m001->hdl.error)
0782 return mt9m001->hdl.error;
0783
0784 v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
0785 V4L2_EXPOSURE_MANUAL, true);
0786
0787 mutex_init(&mt9m001->mutex);
0788 mt9m001->hdl.lock = &mt9m001->mutex;
0789
0790
0791 mt9m001->y_skip_top = 0;
0792 mt9m001->rect.left = MT9M001_COLUMN_SKIP;
0793 mt9m001->rect.top = MT9M001_ROW_SKIP;
0794 mt9m001->rect.width = MT9M001_MAX_WIDTH;
0795 mt9m001->rect.height = MT9M001_MAX_HEIGHT;
0796
0797 ret = mt9m001_power_on(&client->dev);
0798 if (ret)
0799 goto error_hdl_free;
0800
0801 pm_runtime_set_active(&client->dev);
0802 pm_runtime_enable(&client->dev);
0803
0804 ret = mt9m001_video_probe(client);
0805 if (ret)
0806 goto error_power_off;
0807
0808 mt9m001->pad.flags = MEDIA_PAD_FL_SOURCE;
0809 mt9m001->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
0810 ret = media_entity_pads_init(&mt9m001->subdev.entity, 1, &mt9m001->pad);
0811 if (ret)
0812 goto error_power_off;
0813
0814 ret = v4l2_async_register_subdev(&mt9m001->subdev);
0815 if (ret)
0816 goto error_entity_cleanup;
0817
0818 pm_runtime_idle(&client->dev);
0819
0820 return 0;
0821
0822 error_entity_cleanup:
0823 media_entity_cleanup(&mt9m001->subdev.entity);
0824 error_power_off:
0825 pm_runtime_disable(&client->dev);
0826 pm_runtime_set_suspended(&client->dev);
0827 mt9m001_power_off(&client->dev);
0828
0829 error_hdl_free:
0830 v4l2_ctrl_handler_free(&mt9m001->hdl);
0831 mutex_destroy(&mt9m001->mutex);
0832
0833 return ret;
0834 }
0835
0836 static int mt9m001_remove(struct i2c_client *client)
0837 {
0838 struct mt9m001 *mt9m001 = to_mt9m001(client);
0839
0840
0841
0842
0843
0844 pm_runtime_get_sync(&client->dev);
0845
0846 v4l2_async_unregister_subdev(&mt9m001->subdev);
0847 media_entity_cleanup(&mt9m001->subdev.entity);
0848
0849 pm_runtime_disable(&client->dev);
0850 pm_runtime_set_suspended(&client->dev);
0851 pm_runtime_put_noidle(&client->dev);
0852 mt9m001_power_off(&client->dev);
0853
0854 v4l2_ctrl_handler_free(&mt9m001->hdl);
0855 mutex_destroy(&mt9m001->mutex);
0856
0857 return 0;
0858 }
0859
0860 static const struct i2c_device_id mt9m001_id[] = {
0861 { "mt9m001", 0 },
0862 { }
0863 };
0864 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
0865
0866 static const struct dev_pm_ops mt9m001_pm_ops = {
0867 SET_RUNTIME_PM_OPS(mt9m001_power_off, mt9m001_power_on, NULL)
0868 };
0869
0870 static const struct of_device_id mt9m001_of_match[] = {
0871 { .compatible = "onnn,mt9m001", },
0872 { },
0873 };
0874 MODULE_DEVICE_TABLE(of, mt9m001_of_match);
0875
0876 static struct i2c_driver mt9m001_i2c_driver = {
0877 .driver = {
0878 .name = "mt9m001",
0879 .pm = &mt9m001_pm_ops,
0880 .of_match_table = mt9m001_of_match,
0881 },
0882 .probe_new = mt9m001_probe,
0883 .remove = mt9m001_remove,
0884 .id_table = mt9m001_id,
0885 };
0886
0887 module_i2c_driver(mt9m001_i2c_driver);
0888
0889 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
0890 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
0891 MODULE_LICENSE("GPL v2");