10 #include <linux/videodev2.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
17 #include <linux/module.h>
33 #define MT9M111_CHIP_VERSION 0x000
34 #define MT9M111_ROW_START 0x001
35 #define MT9M111_COLUMN_START 0x002
36 #define MT9M111_WINDOW_HEIGHT 0x003
37 #define MT9M111_WINDOW_WIDTH 0x004
38 #define MT9M111_HORIZONTAL_BLANKING_B 0x005
39 #define MT9M111_VERTICAL_BLANKING_B 0x006
40 #define MT9M111_HORIZONTAL_BLANKING_A 0x007
41 #define MT9M111_VERTICAL_BLANKING_A 0x008
42 #define MT9M111_SHUTTER_WIDTH 0x009
43 #define MT9M111_ROW_SPEED 0x00a
44 #define MT9M111_EXTRA_DELAY 0x00b
45 #define MT9M111_SHUTTER_DELAY 0x00c
46 #define MT9M111_RESET 0x00d
47 #define MT9M111_READ_MODE_B 0x020
48 #define MT9M111_READ_MODE_A 0x021
49 #define MT9M111_FLASH_CONTROL 0x023
50 #define MT9M111_GREEN1_GAIN 0x02b
51 #define MT9M111_BLUE_GAIN 0x02c
52 #define MT9M111_RED_GAIN 0x02d
53 #define MT9M111_GREEN2_GAIN 0x02e
54 #define MT9M111_GLOBAL_GAIN 0x02f
55 #define MT9M111_CONTEXT_CONTROL 0x0c8
56 #define MT9M111_PAGE_MAP 0x0f0
57 #define MT9M111_BYTE_WISE_ADDR 0x0f1
59 #define MT9M111_RESET_SYNC_CHANGES (1 << 15)
60 #define MT9M111_RESET_RESTART_BAD_FRAME (1 << 9)
61 #define MT9M111_RESET_SHOW_BAD_FRAMES (1 << 8)
62 #define MT9M111_RESET_RESET_SOC (1 << 5)
63 #define MT9M111_RESET_OUTPUT_DISABLE (1 << 4)
64 #define MT9M111_RESET_CHIP_ENABLE (1 << 3)
65 #define MT9M111_RESET_ANALOG_STANDBY (1 << 2)
66 #define MT9M111_RESET_RESTART_FRAME (1 << 1)
67 #define MT9M111_RESET_RESET_MODE (1 << 0)
69 #define MT9M111_RM_FULL_POWER_RD (0 << 10)
70 #define MT9M111_RM_LOW_POWER_RD (1 << 10)
71 #define MT9M111_RM_COL_SKIP_4X (1 << 5)
72 #define MT9M111_RM_ROW_SKIP_4X (1 << 4)
73 #define MT9M111_RM_COL_SKIP_2X (1 << 3)
74 #define MT9M111_RM_ROW_SKIP_2X (1 << 2)
75 #define MT9M111_RMB_MIRROR_COLS (1 << 1)
76 #define MT9M111_RMB_MIRROR_ROWS (1 << 0)
77 #define MT9M111_CTXT_CTRL_RESTART (1 << 15)
78 #define MT9M111_CTXT_CTRL_DEFECTCOR_B (1 << 12)
79 #define MT9M111_CTXT_CTRL_RESIZE_B (1 << 10)
80 #define MT9M111_CTXT_CTRL_CTRL2_B (1 << 9)
81 #define MT9M111_CTXT_CTRL_GAMMA_B (1 << 8)
82 #define MT9M111_CTXT_CTRL_XENON_EN (1 << 7)
83 #define MT9M111_CTXT_CTRL_READ_MODE_B (1 << 3)
84 #define MT9M111_CTXT_CTRL_LED_FLASH_EN (1 << 2)
85 #define MT9M111_CTXT_CTRL_VBLANK_SEL_B (1 << 1)
86 #define MT9M111_CTXT_CTRL_HBLANK_SEL_B (1 << 0)
91 #define MT9M111_OPER_MODE_CTRL 0x106
92 #define MT9M111_OUTPUT_FORMAT_CTRL 0x108
93 #define MT9M111_REDUCER_XZOOM_B 0x1a0
94 #define MT9M111_REDUCER_XSIZE_B 0x1a1
95 #define MT9M111_REDUCER_YZOOM_B 0x1a3
96 #define MT9M111_REDUCER_YSIZE_B 0x1a4
97 #define MT9M111_REDUCER_XZOOM_A 0x1a6
98 #define MT9M111_REDUCER_XSIZE_A 0x1a7
99 #define MT9M111_REDUCER_YZOOM_A 0x1a9
100 #define MT9M111_REDUCER_YSIZE_A 0x1aa
102 #define MT9M111_OUTPUT_FORMAT_CTRL2_A 0x13a
103 #define MT9M111_OUTPUT_FORMAT_CTRL2_B 0x19b
105 #define MT9M111_OPMODE_AUTOEXPO_EN (1 << 14)
106 #define MT9M111_OPMODE_AUTOWHITEBAL_EN (1 << 1)
107 #define MT9M111_OUTFMT_FLIP_BAYER_COL (1 << 9)
108 #define MT9M111_OUTFMT_FLIP_BAYER_ROW (1 << 8)
109 #define MT9M111_OUTFMT_PROCESSED_BAYER (1 << 14)
110 #define MT9M111_OUTFMT_BYPASS_IFP (1 << 10)
111 #define MT9M111_OUTFMT_INV_PIX_CLOCK (1 << 9)
112 #define MT9M111_OUTFMT_RGB (1 << 8)
113 #define MT9M111_OUTFMT_RGB565 (0 << 6)
114 #define MT9M111_OUTFMT_RGB555 (1 << 6)
115 #define MT9M111_OUTFMT_RGB444x (2 << 6)
116 #define MT9M111_OUTFMT_RGBx444 (3 << 6)
117 #define MT9M111_OUTFMT_TST_RAMP_OFF (0 << 4)
118 #define MT9M111_OUTFMT_TST_RAMP_COL (1 << 4)
119 #define MT9M111_OUTFMT_TST_RAMP_ROW (2 << 4)
120 #define MT9M111_OUTFMT_TST_RAMP_FRAME (3 << 4)
121 #define MT9M111_OUTFMT_SHIFT_3_UP (1 << 3)
122 #define MT9M111_OUTFMT_AVG_CHROMA (1 << 2)
123 #define MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN (1 << 1)
124 #define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr_RGB_R_B (1 << 0)
130 #define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg)
131 #define reg_write(reg, val) mt9m111_reg_write(client, MT9M111_##reg, (val))
132 #define reg_set(reg, val) mt9m111_reg_set(client, MT9M111_##reg, (val))
133 #define reg_clear(reg, val) mt9m111_reg_clear(client, MT9M111_##reg, (val))
134 #define reg_mask(reg, val, mask) mt9m111_reg_mask(client, MT9M111_##reg, \
137 #define MT9M111_MIN_DARK_ROWS 8
138 #define MT9M111_MIN_DARK_COLS 26
139 #define MT9M111_MAX_HEIGHT 1024
140 #define MT9M111_MAX_WIDTH 1280
224 for (i = 0; i <
ARRAY_SIZE(mt9m111_colour_fmts); i++)
225 if (mt9m111_colour_fmts[i].code == code)
226 return mt9m111_colour_fmts +
i;
254 static int mt9m111_reg_read(
struct i2c_client *client,
const u16 reg)
258 ret = reg_page_map_set(client, reg);
260 ret = i2c_smbus_read_word_swapped(client, reg & 0xff);
262 dev_dbg(&client->
dev,
"read reg.%03x -> %04x\n", reg, ret);
266 static int mt9m111_reg_write(
struct i2c_client *client,
const u16 reg,
271 ret = reg_page_map_set(client, reg);
273 ret = i2c_smbus_write_word_swapped(client, reg & 0xff, data);
274 dev_dbg(&client->
dev,
"write reg.%03x = %04x -> %d\n", reg, data, ret);
278 static int mt9m111_reg_set(
struct i2c_client *client,
const u16 reg,
283 ret = mt9m111_reg_read(client, reg);
285 ret = mt9m111_reg_write(client, reg, ret | data);
289 static int mt9m111_reg_clear(
struct i2c_client *client,
const u16 reg,
294 ret = mt9m111_reg_read(client, reg);
296 ret = mt9m111_reg_write(client, reg, ret & ~data);
300 static int mt9m111_reg_mask(
struct i2c_client *client,
const u16 reg,
305 ret = mt9m111_reg_read(client, reg);
307 ret = mt9m111_reg_write(client, reg, (ret & ~mask) | data);
311 static int mt9m111_set_context(
struct mt9m111 *mt9m111,
318 static int mt9m111_setup_rect_ctx(
struct mt9m111 *mt9m111,
333 static int mt9m111_setup_geometry(
struct mt9m111 *mt9m111,
struct v4l2_rect *rect,
351 ret = mt9m111_setup_rect_ctx(mt9m111, &context_b,
352 rect, width, height);
354 ret = mt9m111_setup_rect_ctx(mt9m111, &context_a,
355 rect, width, height);
358 dev_dbg(&client->
dev,
"%s(%x): %ux%u@%u:%u -> %ux%u = %d\n",
365 static int mt9m111_enable(
struct mt9m111 *mt9m111)
371 static int mt9m111_reset(
struct mt9m111 *mt9m111)
405 soc_camera_limit_side(&rect.
left, &rect.
width,
408 soc_camera_limit_side(&rect.
top, &rect.
height,
414 ret = mt9m111_setup_geometry(mt9m111, &rect, width, height, mt9m111->
fmt->code);
428 a->
c = mt9m111->
rect;
451 struct v4l2_mbus_framefmt *mf)
455 mf->width = mt9m111->
width;
456 mf->height = mt9m111->
height;
457 mf->code = mt9m111->
fmt->code;
458 mf->colorspace = mt9m111->
fmt->colorspace;
464 static int mt9m111_set_pixfmt(
struct mt9m111 *mt9m111,
521 dev_err(&client->
dev,
"Pixel format not handled: %x\n", code);
526 data_outfmt2, mask_outfmt2);
529 data_outfmt2, mask_outfmt2);
535 struct v4l2_mbus_framefmt *mf)
537 struct i2c_client *client = v4l2_get_subdevdata(sd);
543 fmt = mt9m111_find_datafmt(mt9m111, mf->code);
559 mf->width = rect->
width;
560 mf->height = rect->
height;
563 if (mf->width > rect->
width)
564 mf->width = rect->
width;
565 if (mf->height > rect->
height)
566 mf->height = rect->
height;
569 dev_dbg(&client->
dev,
"%s(): %ux%u, code=%x\n", __func__,
570 mf->width, mf->height, fmt->
code);
572 mf->code = fmt->
code;
579 struct v4l2_mbus_framefmt *mf)
586 mt9m111_try_fmt(sd, mf);
587 fmt = mt9m111_find_datafmt(mt9m111, mf->code);
590 ret = mt9m111_setup_geometry(mt9m111, rect, mf->
width, mf->height, mf->code);
592 ret = mt9m111_set_pixfmt(mt9m111, mf->code);
594 mt9m111->
width = mf->width;
595 mt9m111->
height = mf->height;
602 static int mt9m111_g_chip_ident(
struct v4l2_subdev *sd,
605 struct i2c_client *client = v4l2_get_subdevdata(sd);
614 id->ident = mt9m111->
model;
620 #ifdef CONFIG_VIDEO_ADV_DEBUG
621 static int mt9m111_g_register(
struct v4l2_subdev *sd,
624 struct i2c_client *client = v4l2_get_subdevdata(sd);
632 val = mt9m111_reg_read(client, reg->
reg);
636 if (reg->
val > 0xffff)
642 static int mt9m111_s_register(
struct v4l2_subdev *sd,
645 struct i2c_client *client = v4l2_get_subdevdata(sd);
653 if (mt9m111_reg_write(client, reg->
reg, reg->
val) < 0)
660 static int mt9m111_set_flip(
struct mt9m111 *mt9m111,
int flip,
int mask)
666 ret = mt9m111_reg_set(client, mt9m111->
ctx->read_mode, mask);
668 ret = mt9m111_reg_clear(client, mt9m111->
ctx->read_mode, mask);
673 static int mt9m111_get_global_gain(
struct mt9m111 *mt9m111)
680 return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
681 (1 << ((data >> 9) & 1));
685 static int mt9m111_set_global_gain(
struct mt9m111 *mt9m111,
int gain)
690 if (gain > 63 * 2 * 2)
693 if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))
694 val = (1 << 10) | (1 << 9) | (gain / 4);
695 else if ((gain >= 64) && (gain < 64 * 2))
696 val = (1 << 9) | (gain / 2);
703 static int mt9m111_set_autoexposure(
struct mt9m111 *mt9m111,
int on)
712 static int mt9m111_set_autowhitebalance(
struct mt9m111 *mt9m111,
int on)
724 struct mt9m111,
hdl);
728 return mt9m111_set_flip(mt9m111, ctrl->
val,
731 return mt9m111_set_flip(mt9m111, ctrl->
val,
734 return mt9m111_set_global_gain(mt9m111, ctrl->
val);
736 return mt9m111_set_autoexposure(mt9m111, ctrl->
val);
738 return mt9m111_set_autowhitebalance(mt9m111, ctrl->
val);
744 static int mt9m111_suspend(
struct mt9m111 *mt9m111)
762 static void mt9m111_restore_state(
struct mt9m111 *mt9m111)
764 mt9m111_set_context(mt9m111, mt9m111->
ctx);
765 mt9m111_set_pixfmt(mt9m111, mt9m111->
fmt->code);
766 mt9m111_setup_geometry(mt9m111, &mt9m111->
rect,
771 static int mt9m111_resume(
struct mt9m111 *mt9m111)
773 int ret = mt9m111_enable(mt9m111);
775 ret = mt9m111_reset(mt9m111);
777 mt9m111_restore_state(mt9m111);
782 static int mt9m111_init(
struct mt9m111 *mt9m111)
788 mt9m111->
ctx = &context_b;
789 ret = mt9m111_enable(mt9m111);
791 ret = mt9m111_reset(mt9m111);
793 ret = mt9m111_set_context(mt9m111, mt9m111->
ctx);
795 dev_err(&client->
dev,
"mt9m111 init failed: %d\n", ret);
799 static int mt9m111_power_on(
struct mt9m111 *mt9m111)
809 ret = mt9m111_resume(mt9m111);
811 dev_err(&client->
dev,
"Failed to resume the sensor: %d\n", ret);
818 static void mt9m111_power_off(
struct mt9m111 *mt9m111)
823 mt9m111_suspend(mt9m111);
827 static int mt9m111_s_power(
struct v4l2_subdev *sd,
int on)
840 ret = mt9m111_power_on(mt9m111);
842 mt9m111_power_off(mt9m111);
856 .s_ctrl = mt9m111_s_ctrl,
860 .g_chip_ident = mt9m111_g_chip_ident,
861 .s_power = mt9m111_s_power,
862 #ifdef CONFIG_VIDEO_ADV_DEBUG
863 .g_register = mt9m111_g_register,
864 .s_register = mt9m111_s_register,
878 static int mt9m111_g_mbus_config(
struct v4l2_subdev *sd,
881 struct i2c_client *client = v4l2_get_subdevdata(sd);
894 .s_mbus_fmt = mt9m111_s_fmt,
895 .g_mbus_fmt = mt9m111_g_fmt,
896 .try_mbus_fmt = mt9m111_try_fmt,
897 .s_crop = mt9m111_s_crop,
898 .g_crop = mt9m111_g_crop,
899 .cropcap = mt9m111_cropcap,
900 .enum_mbus_fmt = mt9m111_enum_fmt,
901 .g_mbus_config = mt9m111_g_mbus_config,
905 .core = &mt9m111_subdev_core_ops,
906 .video = &mt9m111_subdev_video_ops,
913 static int mt9m111_video_probe(
struct i2c_client *client)
915 struct mt9m111 *mt9m111 = to_mt9m111(client);
919 ret = mt9m111_s_power(&mt9m111->
subdev, 1);
929 "Detected a MT9M111/MT9M131 chip ID %x\n", data);
933 dev_info(&client->
dev,
"Detected a MT9M112 chip ID %x\n", data);
937 "No MT9M111/MT9M112/MT9M131 chip detected register read %x\n",
943 ret = mt9m111_init(mt9m111);
950 mt9m111_s_power(&mt9m111->
subdev, 0);
954 static int mt9m111_probe(
struct i2c_client *client,
957 struct mt9m111 *mt9m111;
963 dev_err(&client->
dev,
"mt9m111: driver needs platform data\n");
969 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
973 mt9m111 = kzalloc(
sizeof(
struct mt9m111),
GFP_KERNEL);
990 mt9m111->
subdev.ctrl_handler = &mt9m111->
hdl;
991 if (mt9m111->
hdl.error) {
992 int err = mt9m111->
hdl.error;
1003 mt9m111->
fmt = &mt9m111_colour_fmts[0];
1007 ret = mt9m111_video_probe(client);
1016 static int mt9m111_remove(
struct i2c_client *client)
1018 struct mt9m111 *mt9m111 = to_mt9m111(client);
1033 static struct i2c_driver mt9m111_i2c_driver = {
1037 .probe = mt9m111_probe,
1038 .remove = mt9m111_remove,
1039 .id_table = mt9m111_id,