11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
15 #include <linux/module.h>
30 #define MT9M001_CHIP_VERSION 0x00
31 #define MT9M001_ROW_START 0x01
32 #define MT9M001_COLUMN_START 0x02
33 #define MT9M001_WINDOW_HEIGHT 0x03
34 #define MT9M001_WINDOW_WIDTH 0x04
35 #define MT9M001_HORIZONTAL_BLANKING 0x05
36 #define MT9M001_VERTICAL_BLANKING 0x06
37 #define MT9M001_OUTPUT_CONTROL 0x07
38 #define MT9M001_SHUTTER_WIDTH 0x09
39 #define MT9M001_FRAME_RESTART 0x0b
40 #define MT9M001_SHUTTER_DELAY 0x0c
41 #define MT9M001_RESET 0x0d
42 #define MT9M001_READ_OPTIONS1 0x1e
43 #define MT9M001_READ_OPTIONS2 0x20
44 #define MT9M001_GLOBAL_GAIN 0x35
45 #define MT9M001_CHIP_ENABLE 0xF1
47 #define MT9M001_MAX_WIDTH 1280
48 #define MT9M001_MAX_HEIGHT 1024
49 #define MT9M001_MIN_WIDTH 48
50 #define MT9M001_MIN_HEIGHT 32
51 #define MT9M001_COLUMN_SKIP 20
52 #define MT9M001_ROW_SKIP 12
66 for (i = 0; i <
n; i++)
67 if (fmt[i].code == code)
112 return i2c_smbus_read_word_swapped(client, reg);
118 return i2c_smbus_write_word_swapped(client, reg, data);
129 return reg_write(client, reg, ret | data);
140 return reg_write(client, reg, ret & ~data);
143 static int mt9m001_init(
struct i2c_client *client)
166 struct i2c_client *client = v4l2_get_subdevdata(sd);
176 struct i2c_client *client = v4l2_get_subdevdata(sd);
180 const u16 hblank = 9, vblank = 25;
182 if (mt9m001->
fmts == mt9m001_colour_fmts)
193 soc_camera_limit_side(&rect.
left, &rect.
width,
196 soc_camera_limit_side(&rect.
top, &rect.
height,
223 mt9m001->
rect = rect;
230 struct i2c_client *client = v4l2_get_subdevdata(sd);
231 struct mt9m001 *mt9m001 = to_mt9m001(client);
233 a->
c = mt9m001->
rect;
254 struct v4l2_mbus_framefmt *mf)
256 struct i2c_client *client = v4l2_get_subdevdata(sd);
257 struct mt9m001 *mt9m001 = to_mt9m001(client);
259 mf->width = mt9m001->
rect.width;
260 mf->height = mt9m001->
rect.height;
261 mf->code = mt9m001->
fmt->code;
262 mf->colorspace = mt9m001->
fmt->colorspace;
269 struct v4l2_mbus_framefmt *mf)
271 struct i2c_client *client = v4l2_get_subdevdata(sd);
272 struct mt9m001 *mt9m001 = to_mt9m001(client);
275 .left = mt9m001->
rect.left,
276 .top = mt9m001->
rect.top,
278 .height = mf->height,
284 ret = mt9m001_s_crop(sd, &a);
286 mf->width = mt9m001->
rect.width;
287 mf->height = mt9m001->
rect.height;
288 mt9m001->
fmt = mt9m001_find_datafmt(mf->code,
290 mf->colorspace = mt9m001->
fmt->colorspace;
297 struct v4l2_mbus_framefmt *mf)
299 struct i2c_client *client = v4l2_get_subdevdata(sd);
300 struct mt9m001 *mt9m001 = to_mt9m001(client);
308 if (mt9m001->
fmts == mt9m001_colour_fmts)
309 mf->height =
ALIGN(mf->height - 1, 2);
311 fmt = mt9m001_find_datafmt(mf->code, mt9m001->
fmts,
315 mf->code = fmt->
code;
323 static int mt9m001_g_chip_ident(
struct v4l2_subdev *sd,
326 struct i2c_client *client = v4l2_get_subdevdata(sd);
327 struct mt9m001 *mt9m001 = to_mt9m001(client);
335 id->ident = mt9m001->
model;
341 #ifdef CONFIG_VIDEO_ADV_DEBUG
342 static int mt9m001_g_register(
struct v4l2_subdev *sd,
345 struct i2c_client *client = v4l2_get_subdevdata(sd);
356 if (reg->
val > 0xffff)
362 static int mt9m001_s_register(
struct v4l2_subdev *sd,
365 struct i2c_client *client = v4l2_get_subdevdata(sd);
380 static int mt9m001_s_power(
struct v4l2_subdev *sd,
int on)
382 struct i2c_client *client = v4l2_get_subdevdata(sd);
385 return soc_camera_set_power(&client->
dev, icl, on);
391 struct mt9m001,
hdl);
399 (524 + (mt9m001->
total_h - 1) * (max - min)) / 1048 + min;
405 static int mt9m001_s_ctrl(
struct v4l2_ctrl *ctrl)
408 struct mt9m001,
hdl);
410 struct i2c_client *client = v4l2_get_subdevdata(sd);
431 dev_dbg(&client->
dev,
"Setting gain %d\n", data);
440 111 + range / 2) / range + 9;
445 data = ((gain - 32) * 16 + 16) / 32 + 80;
447 data = ((gain - 64) * 7 + 28) / 56 + 96;
449 dev_dbg(&client->
dev,
"Setting gain from %d to %d\n",
460 unsigned long shutter = ((exp->
val - exp->
minimum) * 1048 +
461 range / 2) / range + 1;
464 "Setting shutter width from %d to %lu\n",
469 const u16 vblank = 25;
488 struct mt9m001 *mt9m001 = to_mt9m001(client);
493 ret = mt9m001_s_power(&mt9m001->
subdev, 1);
509 mt9m001->
fmts = mt9m001_colour_fmts;
513 mt9m001->
fmts = mt9m001_monochrome_fmts;
517 "No MT9M001 chip detected, register read %x\n", data);
534 if (flags & SOCAM_DATAWIDTH_10)
539 if (flags & SOCAM_DATAWIDTH_8)
542 mt9m001->
fmt = &mt9m001->
fmts[0];
544 dev_info(&client->
dev,
"Detected a MT9M001 chip ID %x (%s)\n", data,
545 data == 0x8431 ?
"C12STM" :
"C12ST");
547 ret = mt9m001_init(client);
549 dev_err(&client->
dev,
"Failed to initialise the camera\n");
557 mt9m001_s_power(&mt9m001->
subdev, 0);
567 static int mt9m001_g_skip_top_lines(
struct v4l2_subdev *sd,
u32 *lines)
569 struct i2c_client *client = v4l2_get_subdevdata(sd);
570 struct mt9m001 *mt9m001 = to_mt9m001(client);
578 .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
579 .s_ctrl = mt9m001_s_ctrl,
583 .g_chip_ident = mt9m001_g_chip_ident,
584 #ifdef CONFIG_VIDEO_ADV_DEBUG
585 .g_register = mt9m001_g_register,
586 .s_register = mt9m001_s_register,
588 .s_power = mt9m001_s_power,
594 struct i2c_client *client = v4l2_get_subdevdata(sd);
595 struct mt9m001 *mt9m001 = to_mt9m001(client);
604 static int mt9m001_g_mbus_config(
struct v4l2_subdev *sd,
607 struct i2c_client *client = v4l2_get_subdevdata(sd);
620 static int mt9m001_s_mbus_config(
struct v4l2_subdev *sd,
623 const struct i2c_client *client = v4l2_get_subdevdata(sd);
625 struct mt9m001 *mt9m001 = to_mt9m001(client);
635 return bps == 10 ? 0 : -
EINVAL;
639 .s_stream = mt9m001_s_stream,
640 .s_mbus_fmt = mt9m001_s_fmt,
641 .g_mbus_fmt = mt9m001_g_fmt,
642 .try_mbus_fmt = mt9m001_try_fmt,
643 .s_crop = mt9m001_s_crop,
644 .g_crop = mt9m001_g_crop,
645 .cropcap = mt9m001_cropcap,
646 .enum_mbus_fmt = mt9m001_enum_fmt,
647 .g_mbus_config = mt9m001_g_mbus_config,
648 .s_mbus_config = mt9m001_s_mbus_config,
652 .g_skip_top_lines = mt9m001_g_skip_top_lines,
656 .core = &mt9m001_subdev_core_ops,
657 .video = &mt9m001_subdev_video_ops,
658 .sensor = &mt9m001_subdev_sensor_ops,
661 static int mt9m001_probe(
struct i2c_client *client,
664 struct mt9m001 *mt9m001;
670 dev_err(&client->
dev,
"MT9M001 driver needs platform data\n");
676 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
680 mt9m001 = kzalloc(
sizeof(
struct mt9m001),
GFP_KERNEL);
699 mt9m001->
subdev.ctrl_handler = &mt9m001->
hdl;
700 if (mt9m001->
hdl.error) {
701 int err = mt9m001->
hdl.error;
716 ret = mt9m001_video_probe(icl, client);
725 static int mt9m001_remove(
struct i2c_client *client)
727 struct mt9m001 *mt9m001 = to_mt9m001(client);
732 mt9m001_video_remove(icl);
744 static struct i2c_driver mt9m001_i2c_driver = {
748 .probe = mt9m001_probe,
749 .remove = mt9m001_remove,
750 .id_table = mt9m001_id,