16 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <linux/videodev2.h>
33 #define MT9P031_PIXEL_ARRAY_WIDTH 2752
34 #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
36 #define MT9P031_CHIP_VERSION 0x00
37 #define MT9P031_CHIP_VERSION_VALUE 0x1801
38 #define MT9P031_ROW_START 0x01
39 #define MT9P031_ROW_START_MIN 0
40 #define MT9P031_ROW_START_MAX 2004
41 #define MT9P031_ROW_START_DEF 54
42 #define MT9P031_COLUMN_START 0x02
43 #define MT9P031_COLUMN_START_MIN 0
44 #define MT9P031_COLUMN_START_MAX 2750
45 #define MT9P031_COLUMN_START_DEF 16
46 #define MT9P031_WINDOW_HEIGHT 0x03
47 #define MT9P031_WINDOW_HEIGHT_MIN 2
48 #define MT9P031_WINDOW_HEIGHT_MAX 2006
49 #define MT9P031_WINDOW_HEIGHT_DEF 1944
50 #define MT9P031_WINDOW_WIDTH 0x04
51 #define MT9P031_WINDOW_WIDTH_MIN 2
52 #define MT9P031_WINDOW_WIDTH_MAX 2752
53 #define MT9P031_WINDOW_WIDTH_DEF 2592
54 #define MT9P031_HORIZONTAL_BLANK 0x05
55 #define MT9P031_HORIZONTAL_BLANK_MIN 0
56 #define MT9P031_HORIZONTAL_BLANK_MAX 4095
57 #define MT9P031_VERTICAL_BLANK 0x06
58 #define MT9P031_VERTICAL_BLANK_MIN 1
59 #define MT9P031_VERTICAL_BLANK_MAX 4096
60 #define MT9P031_VERTICAL_BLANK_DEF 26
61 #define MT9P031_OUTPUT_CONTROL 0x07
62 #define MT9P031_OUTPUT_CONTROL_CEN 2
63 #define MT9P031_OUTPUT_CONTROL_SYN 1
64 #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
65 #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
66 #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
67 #define MT9P031_SHUTTER_WIDTH_MIN 1
68 #define MT9P031_SHUTTER_WIDTH_MAX 1048575
69 #define MT9P031_SHUTTER_WIDTH_DEF 1943
70 #define MT9P031_PLL_CONTROL 0x10
71 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
72 #define MT9P031_PLL_CONTROL_PWRON 0x0051
73 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
74 #define MT9P031_PLL_CONFIG_1 0x11
75 #define MT9P031_PLL_CONFIG_2 0x12
76 #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
77 #define MT9P031_FRAME_RESTART 0x0b
78 #define MT9P031_SHUTTER_DELAY 0x0c
79 #define MT9P031_RST 0x0d
80 #define MT9P031_RST_ENABLE 1
81 #define MT9P031_RST_DISABLE 0
82 #define MT9P031_READ_MODE_1 0x1e
83 #define MT9P031_READ_MODE_2 0x20
84 #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
85 #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
86 #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
87 #define MT9P031_ROW_ADDRESS_MODE 0x22
88 #define MT9P031_COLUMN_ADDRESS_MODE 0x23
89 #define MT9P031_GLOBAL_GAIN 0x35
90 #define MT9P031_GLOBAL_GAIN_MIN 8
91 #define MT9P031_GLOBAL_GAIN_MAX 1024
92 #define MT9P031_GLOBAL_GAIN_DEF 8
93 #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
94 #define MT9P031_ROW_BLACK_TARGET 0x49
95 #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
96 #define MT9P031_GREEN1_OFFSET 0x60
97 #define MT9P031_GREEN2_OFFSET 0x61
98 #define MT9P031_BLACK_LEVEL_CALIBRATION 0x62
99 #define MT9P031_BLC_MANUAL_BLC (1 << 0)
100 #define MT9P031_RED_OFFSET 0x63
101 #define MT9P031_BLUE_OFFSET 0x64
102 #define MT9P031_TEST_PATTERN 0xa0
103 #define MT9P031_TEST_PATTERN_SHIFT 3
104 #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
105 #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
106 #define MT9P031_TEST_PATTERN_GREEN 0xa1
107 #define MT9P031_TEST_PATTERN_RED 0xa2
108 #define MT9P031_TEST_PATTERN_BLUE 0xa3
144 return i2c_smbus_read_word_swapped(client, reg);
149 return i2c_smbus_write_word_swapped(client, reg, data);
167 static int mt9p031_set_mode2(
struct mt9p031 *mt9p031,
u16 clear,
u16 set)
170 u16 value = (mt9p031->
mode2 & ~clear) |
set;
181 static int mt9p031_reset(
struct mt9p031 *mt9p031)
198 static int mt9p031_pll_setup(
struct mt9p031 *mt9p031)
202 .ext_clock_max = 27000000,
203 .int_clock_min = 2000000,
204 .int_clock_max = 13500000,
205 .out_clock_min = 180000000,
206 .out_clock_max = 360000000,
207 .pix_clock_max = 96000000,
225 static int mt9p031_pll_enable(
struct mt9p031 *mt9p031)
236 (mt9p031->
pll.m << 8) | (mt9p031->
pll.n - 1));
251 static inline int mt9p031_pll_disable(
struct mt9p031 *mt9p031)
259 static int mt9p031_power_on(
struct mt9p031 *mt9p031)
262 if (mt9p031->
reset != -1) {
268 if (mt9p031->
pdata->set_xclk)
270 mt9p031->
pdata->ext_freq);
273 if (mt9p031->
reset != -1) {
281 static void mt9p031_power_off(
struct mt9p031 *mt9p031)
283 if (mt9p031->
reset != -1) {
288 if (mt9p031->
pdata->set_xclk)
292 static int __mt9p031_set_power(
struct mt9p031 *mt9p031,
bool on)
298 mt9p031_power_off(mt9p031);
302 ret = mt9p031_power_on(mt9p031);
306 ret = mt9p031_reset(mt9p031);
308 dev_err(&client->
dev,
"Failed to reset the camera\n");
319 static int mt9p031_set_params(
struct mt9p031 *mt9p031)
356 xbin = 1 << (
ffs(xskip) - 1);
357 ybin = 1 << (
ffs(yskip) - 1);
360 ((xbin - 1) << 4) | (xskip - 1));
364 ((ybin - 1) << 4) | (yskip - 1));
371 hblank = 346 * ybin + 64 + (80 >>
min_t(
unsigned int, xbin, 3));
386 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
391 ret = mt9p031_set_output_control(mt9p031,
396 return mt9p031_pll_disable(mt9p031);
399 ret = mt9p031_set_params(mt9p031);
404 ret = mt9p031_set_output_control(mt9p031, 0,
409 return mt9p031_pll_enable(mt9p031);
412 static int mt9p031_enum_mbus_code(
struct v4l2_subdev *subdev,
414 struct v4l2_subdev_mbus_code_enum *
code)
416 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
418 if (code->pad || code->index)
421 code->code = mt9p031->
format.code;
425 static int mt9p031_enum_frame_size(
struct v4l2_subdev *subdev,
427 struct v4l2_subdev_frame_size_enum *fse)
429 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
431 if (fse->index >= 8 || fse->code != mt9p031->
format.code)
435 /
min_t(
unsigned int, 7, fse->index + 1);
436 fse->max_width = fse->min_width;
438 fse->max_height = fse->min_height;
443 static struct v4l2_mbus_framefmt *
444 __mt9p031_get_pad_format(
struct mt9p031 *mt9p031,
struct v4l2_subdev_fh *fh,
445 unsigned int pad,
u32 which)
449 return v4l2_subdev_get_try_format(fh, pad);
458 __mt9p031_get_pad_crop(
struct mt9p031 *mt9p031,
struct v4l2_subdev_fh *fh,
459 unsigned int pad,
u32 which)
463 return v4l2_subdev_get_try_crop(fh, pad);
465 return &mt9p031->
crop;
471 static int mt9p031_get_format(
struct v4l2_subdev *subdev,
475 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
477 fmt->
format = *__mt9p031_get_pad_format(mt9p031, fh, fmt->
pad,
482 static int mt9p031_set_format(
struct v4l2_subdev *subdev,
486 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
487 struct v4l2_mbus_framefmt *__format;
494 __crop = __mt9p031_get_pad_crop(mt9p031, fh, format->
pad,
508 __format = __mt9p031_get_pad_format(mt9p031, fh, format->
pad,
510 __format->width = __crop->
width / hratio;
511 __format->height = __crop->
height / vratio;
513 format->
format = *__format;
518 static int mt9p031_get_crop(
struct v4l2_subdev *subdev,
522 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
524 crop->
rect = *__mt9p031_get_pad_crop(mt9p031, fh, crop->
pad,
529 static int mt9p031_set_crop(
struct v4l2_subdev *subdev,
533 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
534 struct v4l2_mbus_framefmt *__format;
555 __crop = __mt9p031_get_pad_crop(mt9p031, fh, crop->
pad, crop->
which);
557 if (rect.width != __crop->
width || rect.height != __crop->
height) {
561 __format = __mt9p031_get_pad_format(mt9p031, fh, crop->
pad,
563 __format->width = rect.width;
564 __format->height = rect.height;
577 #define V4L2_CID_BLC_AUTO (V4L2_CID_USER_BASE | 0x1002)
578 #define V4L2_CID_BLC_TARGET_LEVEL (V4L2_CID_USER_BASE | 0x1003)
579 #define V4L2_CID_BLC_ANALOG_OFFSET (V4L2_CID_USER_BASE | 0x1004)
580 #define V4L2_CID_BLC_DIGITAL_OFFSET (V4L2_CID_USER_BASE | 0x1005)
584 struct mt9p031 *mt9p031 =
593 (ctrl->
val >> 16) & 0xffff);
615 if (ctrl->
val <= 32) {
617 }
else if (ctrl->
val <= 64) {
619 data = (1 << 6) | (ctrl->
val >> 1);
622 data = ((ctrl->
val - 64) << 5) | (1 << 6) | 32;
629 return mt9p031_set_mode2(mt9p031,
632 return mt9p031_set_mode2(mt9p031,
637 return mt9p031_set_mode2(mt9p031,
640 return mt9p031_set_mode2(mt9p031,
646 if (mt9p031->
blc_auto->cur.val != 0) {
647 ret = mt9p031_s_ctrl(mt9p031->
blc_auto);
687 ret = mt9p031_set_mode2(mt9p031,
701 data = ctrl->
val & ((1 << 9) - 1);
716 ctrl->
val & ((1 << 12) - 1));
723 .s_ctrl = mt9p031_s_ctrl,
726 static const char *
const mt9p031_test_pattern_menu[] = {
729 "Horizontal Gradient",
732 "Classic Test Pattern",
734 "Monochrome Horizontal Bars",
735 "Monochrome Vertical Bars",
736 "Vertical Color Bars",
741 .ops = &mt9p031_ctrl_ops,
751 .ops = &mt9p031_ctrl_ops,
754 .name =
"BLC Target Level",
761 .ops = &mt9p031_ctrl_ops,
764 .name =
"BLC Analog Offset",
771 .ops = &mt9p031_ctrl_ops,
774 .name =
"BLC Digital Offset",
787 static int mt9p031_set_power(
struct v4l2_subdev *subdev,
int on)
789 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
798 ret = __mt9p031_set_power(mt9p031, !!on);
816 static int mt9p031_registered(
struct v4l2_subdev *subdev)
818 struct i2c_client *client = v4l2_get_subdevdata(subdev);
819 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
823 ret = mt9p031_power_on(mt9p031);
825 dev_err(&client->
dev,
"MT9P031 power up failed\n");
832 dev_err(&client->
dev,
"MT9P031 not detected, wrong version "
837 mt9p031_power_off(mt9p031);
839 dev_info(&client->
dev,
"MT9P031 detected at address 0x%02x\n",
847 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
848 struct v4l2_mbus_framefmt *
format;
851 crop = v4l2_subdev_get_try_crop(fh, 0);
857 format = v4l2_subdev_get_try_format(fh, 0);
869 return mt9p031_set_power(subdev, 1);
874 return mt9p031_set_power(subdev, 0);
878 .s_power = mt9p031_set_power,
882 .s_stream = mt9p031_s_stream,
886 .enum_mbus_code = mt9p031_enum_mbus_code,
887 .enum_frame_size = mt9p031_enum_frame_size,
888 .get_fmt = mt9p031_get_format,
889 .set_fmt = mt9p031_set_format,
890 .get_crop = mt9p031_get_crop,
891 .set_crop = mt9p031_set_crop,
895 .core = &mt9p031_subdev_core_ops,
896 .video = &mt9p031_subdev_video_ops,
897 .pad = &mt9p031_subdev_pad_ops,
901 .registered = mt9p031_registered,
902 .open = mt9p031_open,
903 .close = mt9p031_close,
910 static int mt9p031_probe(
struct i2c_client *client,
915 struct mt9p031 *mt9p031;
926 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
930 mt9p031 = kzalloc(
sizeof(*mt9p031),
GFP_KERNEL);
937 mt9p031->
model = did->driver_data;
959 0, mt9p031_test_pattern_menu);
966 if (mt9p031->
ctrls.error) {
968 __func__, mt9p031->
ctrls.error);
969 ret = mt9p031->
ctrls.error;
979 mt9p031->
subdev.internal_ops = &mt9p031_subdev_internal_ops;
1003 if (pdata->
reset != -1) {
1012 ret = mt9p031_pll_setup(mt9p031);
1016 if (mt9p031->
reset != -1)
1027 static int mt9p031_remove(
struct i2c_client *client)
1029 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1030 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
1035 if (mt9p031->
reset != -1)
1049 static struct i2c_driver mt9p031_i2c_driver = {
1053 .probe = mt9p031_probe,
1054 .remove = mt9p031_remove,
1055 .id_table = mt9p031_id,