24 #include <linux/i2c.h>
26 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/slab.h>
56 #define MT9M032_PIXEL_ARRAY_WIDTH 1600
57 #define MT9M032_PIXEL_ARRAY_HEIGHT 1152
59 #define MT9M032_CHIP_VERSION 0x00
60 #define MT9M032_CHIP_VERSION_VALUE 0x1402
61 #define MT9M032_ROW_START 0x01
62 #define MT9M032_ROW_START_MIN 0
63 #define MT9M032_ROW_START_MAX 1152
64 #define MT9M032_ROW_START_DEF 60
65 #define MT9M032_COLUMN_START 0x02
66 #define MT9M032_COLUMN_START_MIN 0
67 #define MT9M032_COLUMN_START_MAX 1600
68 #define MT9M032_COLUMN_START_DEF 16
69 #define MT9M032_ROW_SIZE 0x03
70 #define MT9M032_ROW_SIZE_MIN 32
71 #define MT9M032_ROW_SIZE_MAX 1152
72 #define MT9M032_ROW_SIZE_DEF 1080
73 #define MT9M032_COLUMN_SIZE 0x04
74 #define MT9M032_COLUMN_SIZE_MIN 32
75 #define MT9M032_COLUMN_SIZE_MAX 1600
76 #define MT9M032_COLUMN_SIZE_DEF 1440
77 #define MT9M032_HBLANK 0x05
78 #define MT9M032_VBLANK 0x06
79 #define MT9M032_VBLANK_MAX 0x7ff
80 #define MT9M032_SHUTTER_WIDTH_HIGH 0x08
81 #define MT9M032_SHUTTER_WIDTH_LOW 0x09
82 #define MT9M032_SHUTTER_WIDTH_MIN 1
83 #define MT9M032_SHUTTER_WIDTH_MAX 1048575
84 #define MT9M032_SHUTTER_WIDTH_DEF 1943
85 #define MT9M032_PIX_CLK_CTRL 0x0a
86 #define MT9M032_PIX_CLK_CTRL_INV_PIXCLK 0x8000
87 #define MT9M032_RESTART 0x0b
88 #define MT9M032_RESET 0x0d
89 #define MT9M032_PLL_CONFIG1 0x11
90 #define MT9M032_PLL_CONFIG1_OUTDIV_MASK 0x3f
91 #define MT9M032_PLL_CONFIG1_MUL_SHIFT 8
92 #define MT9M032_READ_MODE1 0x1e
93 #define MT9M032_READ_MODE2 0x20
94 #define MT9M032_READ_MODE2_VFLIP_SHIFT 15
95 #define MT9M032_READ_MODE2_HFLIP_SHIFT 14
96 #define MT9M032_READ_MODE2_ROW_BLC 0x40
97 #define MT9M032_GAIN_GREEN1 0x2b
98 #define MT9M032_GAIN_BLUE 0x2c
99 #define MT9M032_GAIN_RED 0x2d
100 #define MT9M032_GAIN_GREEN2 0x2e
103 #define MT9M032_GAIN_ALL 0x35
104 #define MT9M032_GAIN_DIGITAL_MASK 0x7f
105 #define MT9M032_GAIN_DIGITAL_SHIFT 8
106 #define MT9M032_GAIN_AMUL_SHIFT 6
107 #define MT9M032_GAIN_ANALOG_MASK 0x3f
108 #define MT9M032_FORMATTER1 0x9e
109 #define MT9M032_FORMATTER2 0x9f
110 #define MT9M032_FORMATTER2_DOUT_EN 0x1000
111 #define MT9M032_FORMATTER2_PIXCLK_EN 0x2000
120 #define MT9P031_PLL_CONTROL 0x10
121 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
122 #define MT9P031_PLL_CONTROL_PWRON 0x0051
123 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
124 #define MT9P031_PLL_CONFIG2 0x11
125 #define MT9P031_PLL_CONFIG2_P1_DIV_MASK 0x1f
149 #define to_mt9m032(sd) container_of(sd, struct mt9m032, subdev)
150 #define to_dev(sensor) \
151 (&((struct i2c_client *)v4l2_get_subdevdata(&(sensor)->subdev))->dev)
155 return i2c_smbus_read_word_swapped(client, reg);
160 return i2c_smbus_write_word_swapped(client, reg, data);
163 static u32 mt9m032_row_time(
struct mt9m032 *sensor,
unsigned int width)
165 unsigned int effective_width;
168 effective_width = width + 716;
169 ns = div_u64(1000000000ULL * effective_width, sensor->
pix_clock);
174 static int mt9m032_update_timing(
struct mt9m032 *sensor,
179 unsigned int min_vblank;
186 row_time = mt9m032_row_time(sensor, crop->
width);
188 vblank = div_u64(1000000000ULL * interval->
numerator,
199 vblank = div_u64(1000000000ULL * interval->
numerator,
204 min_vblank = 1600000 / row_time;
210 static int mt9m032_update_geom_timing(
struct mt9m032 *sensor)
216 sensor->
crop.width - 1);
219 sensor->
crop.height - 1);
227 ret = mt9m032_update_timing(sensor,
NULL);
231 static int update_formatter2(
struct mt9m032 *sensor,
bool streaming)
244 static int mt9m032_setup_pll(
struct mt9m032 *sensor)
248 .ext_clock_max = 16500000,
249 .int_clock_min = 2000000,
250 .int_clock_max = 24000000,
251 .out_clock_min = 322000000,
252 .out_clock_max = 693000000,
253 .pix_clock_max = 99000000,
299 struct v4l2_subdev_mbus_code_enum *
code)
301 if (code->index != 0)
308 static int mt9m032_enum_frame_size(
struct v4l2_subdev *subdev,
310 struct v4l2_subdev_frame_size_enum *fse)
337 return v4l2_subdev_get_try_crop(fh, 0);
339 return &sensor->
crop;
353 static struct v4l2_mbus_framefmt *
359 return v4l2_subdev_get_try_format(fh, 0);
367 static int mt9m032_get_pad_format(
struct v4l2_subdev *subdev,
374 fmt->
format = *__mt9m032_get_pad_format(sensor, fh, fmt->
which);
380 static int mt9m032_set_pad_format(
struct v4l2_subdev *subdev,
395 fmt->
format = *__mt9m032_get_pad_format(sensor, fh, fmt->
which);
403 static int mt9m032_get_pad_crop(
struct v4l2_subdev *subdev,
410 crop->
rect = *__mt9m032_get_pad_crop(sensor, fh, crop->
which);
416 static int mt9m032_set_pad_crop(
struct v4l2_subdev *subdev,
421 struct v4l2_mbus_framefmt *
format;
448 __crop = __mt9m032_get_pad_crop(sensor, fh, crop->
which);
450 if (rect.width != __crop->
width || rect.height != __crop->
height) {
454 format = __mt9m032_get_pad_format(sensor, fh, crop->
which);
455 format->width = rect.width;
456 format->height = rect.height;
463 ret = mt9m032_update_geom_timing(sensor);
470 static int mt9m032_get_frame_interval(
struct v4l2_subdev *subdev,
471 struct v4l2_subdev_frame_interval *fi)
476 memset(fi, 0,
sizeof(*fi));
483 static int mt9m032_set_frame_interval(
struct v4l2_subdev *subdev,
484 struct v4l2_subdev_frame_interval *fi)
497 if (fi->interval.denominator == 0)
498 fi->interval.denominator = 1;
500 ret = mt9m032_update_timing(sensor, &fi->interval);
509 static int mt9m032_s_stream(
struct v4l2_subdev *subdev,
int streaming)
515 ret = update_formatter2(sensor, streaming);
527 #ifdef CONFIG_VIDEO_ADV_DEBUG
540 val = mt9m032_read(client, reg->
reg);
550 static int mt9m032_s_register(
struct v4l2_subdev *sd,
562 return mt9m032_write(client, reg->
reg, reg->
val);
570 static int update_read_mode2(
struct mt9m032 *sensor,
bool vflip,
bool hflip)
581 static int mt9m032_set_gain(
struct mt9m032 *sensor,
s32 val)
584 int digital_gain_val;
589 digital_gain_val = 51;
593 analog_gain_val =
val;
596 analog_gain_val = val / 2;
620 static int mt9m032_set_ctrl(
struct v4l2_ctrl *ctrl)
629 return mt9m032_set_gain(sensor, ctrl->
val);
633 return update_read_mode2(sensor, sensor->
vflip->val,
638 (ctrl->
val >> 16) & 0xffff);
650 .s_ctrl = mt9m032_set_ctrl,
651 .try_ctrl = mt9m032_try_ctrl,
657 #ifdef CONFIG_VIDEO_ADV_DEBUG
658 .g_register = mt9m032_g_register,
659 .s_register = mt9m032_s_register,
664 .s_stream = mt9m032_s_stream,
665 .g_frame_interval = mt9m032_get_frame_interval,
666 .s_frame_interval = mt9m032_set_frame_interval,
670 .enum_mbus_code = mt9m032_enum_mbus_code,
671 .enum_frame_size = mt9m032_enum_frame_size,
672 .get_fmt = mt9m032_get_pad_format,
673 .set_fmt = mt9m032_set_pad_format,
674 .set_crop = mt9m032_set_pad_crop,
675 .get_crop = mt9m032_get_pad_crop,
679 .core = &mt9m032_core_ops,
680 .video = &mt9m032_video_ops,
681 .pad = &mt9m032_pad_ops,
688 static int mt9m032_probe(
struct i2c_client *client,
704 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
708 if (!client->
dev.platform_data)
711 sensor = kzalloc(
sizeof(*sensor),
GFP_KERNEL);
724 dev_err(&client->
dev,
"MT9M032 not detected, wrong version "
725 "0x%04x\n", chip_version);
730 dev_info(&client->
dev,
"MT9M032 detected at address 0x%02x\n",
767 if (sensor->
ctrls.error) {
768 ret = sensor->
ctrls.error;
769 dev_err(&client->
dev,
"control initialization error %d\n", ret);
788 ret = mt9m032_setup_pll(sensor);
798 ret = mt9m032_update_geom_timing(sensor);
802 ret = mt9m032_write(client, 0x41, 0x0000);
805 ret = mt9m032_write(client, 0x42, 0x0003);
808 ret = mt9m032_write(client, 0x43, 0x0003);
811 ret = mt9m032_write(client, 0x7f, 0x0000);
814 if (sensor->
pdata->invert_pixclock) {
829 ret = update_formatter2(sensor,
false);
845 static int mt9m032_remove(
struct i2c_client *client)
847 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
865 static struct i2c_driver mt9m032_i2c_driver = {
869 .probe = mt9m032_probe,
870 .remove = mt9m032_remove,
871 .id_table = mt9m032_id_table,