15 #include <linux/i2c.h>
16 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/videodev2.h>
27 #define MT9T001_PIXEL_ARRAY_HEIGHT 1568
28 #define MT9T001_PIXEL_ARRAY_WIDTH 2112
30 #define MT9T001_CHIP_VERSION 0x00
31 #define MT9T001_CHIP_ID 0x1621
32 #define MT9T001_ROW_START 0x01
33 #define MT9T001_ROW_START_MIN 0
34 #define MT9T001_ROW_START_DEF 20
35 #define MT9T001_ROW_START_MAX 1534
36 #define MT9T001_COLUMN_START 0x02
37 #define MT9T001_COLUMN_START_MIN 0
38 #define MT9T001_COLUMN_START_DEF 32
39 #define MT9T001_COLUMN_START_MAX 2046
40 #define MT9T001_WINDOW_HEIGHT 0x03
41 #define MT9T001_WINDOW_HEIGHT_MIN 1
42 #define MT9T001_WINDOW_HEIGHT_DEF 1535
43 #define MT9T001_WINDOW_HEIGHT_MAX 1567
44 #define MT9T001_WINDOW_WIDTH 0x04
45 #define MT9T001_WINDOW_WIDTH_MIN 1
46 #define MT9T001_WINDOW_WIDTH_DEF 2047
47 #define MT9T001_WINDOW_WIDTH_MAX 2111
48 #define MT9T001_HORIZONTAL_BLANKING 0x05
49 #define MT9T001_HORIZONTAL_BLANKING_MIN 21
50 #define MT9T001_HORIZONTAL_BLANKING_MAX 1023
51 #define MT9T001_VERTICAL_BLANKING 0x06
52 #define MT9T001_VERTICAL_BLANKING_MIN 3
53 #define MT9T001_VERTICAL_BLANKING_MAX 1023
54 #define MT9T001_OUTPUT_CONTROL 0x07
55 #define MT9T001_OUTPUT_CONTROL_SYNC (1 << 0)
56 #define MT9T001_OUTPUT_CONTROL_CHIP_ENABLE (1 << 1)
57 #define MT9T001_OUTPUT_CONTROL_TEST_DATA (1 << 6)
58 #define MT9T001_SHUTTER_WIDTH_HIGH 0x08
59 #define MT9T001_SHUTTER_WIDTH_LOW 0x09
60 #define MT9T001_SHUTTER_WIDTH_MIN 1
61 #define MT9T001_SHUTTER_WIDTH_DEF 1561
62 #define MT9T001_SHUTTER_WIDTH_MAX (1024 * 1024)
63 #define MT9T001_PIXEL_CLOCK 0x0a
64 #define MT9T001_PIXEL_CLOCK_INVERT (1 << 15)
65 #define MT9T001_PIXEL_CLOCK_SHIFT_MASK (7 << 8)
66 #define MT9T001_PIXEL_CLOCK_SHIFT_SHIFT 8
67 #define MT9T001_PIXEL_CLOCK_DIVIDE_MASK (0x7f << 0)
68 #define MT9T001_FRAME_RESTART 0x0b
69 #define MT9T001_SHUTTER_DELAY 0x0c
70 #define MT9T001_SHUTTER_DELAY_MAX 2047
71 #define MT9T001_RESET 0x0d
72 #define MT9T001_READ_MODE1 0x1e
73 #define MT9T001_READ_MODE_SNAPSHOT (1 << 8)
74 #define MT9T001_READ_MODE_STROBE_ENABLE (1 << 9)
75 #define MT9T001_READ_MODE_STROBE_WIDTH (1 << 10)
76 #define MT9T001_READ_MODE_STROBE_OVERRIDE (1 << 11)
77 #define MT9T001_READ_MODE2 0x20
78 #define MT9T001_READ_MODE_BAD_FRAMES (1 << 0)
79 #define MT9T001_READ_MODE_LINE_VALID_CONTINUOUS (1 << 9)
80 #define MT9T001_READ_MODE_LINE_VALID_FRAME (1 << 10)
81 #define MT9T001_READ_MODE3 0x21
82 #define MT9T001_READ_MODE_GLOBAL_RESET (1 << 0)
83 #define MT9T001_READ_MODE_GHST_CTL (1 << 1)
84 #define MT9T001_ROW_ADDRESS_MODE 0x22
85 #define MT9T001_ROW_SKIP_MASK (7 << 0)
86 #define MT9T001_ROW_BIN_MASK (3 << 3)
87 #define MT9T001_ROW_BIN_SHIFT 3
88 #define MT9T001_COLUMN_ADDRESS_MODE 0x23
89 #define MT9T001_COLUMN_SKIP_MASK (7 << 0)
90 #define MT9T001_COLUMN_BIN_MASK (3 << 3)
91 #define MT9T001_COLUMN_BIN_SHIFT 3
92 #define MT9T001_GREEN1_GAIN 0x2b
93 #define MT9T001_BLUE_GAIN 0x2c
94 #define MT9T001_RED_GAIN 0x2d
95 #define MT9T001_GREEN2_GAIN 0x2e
96 #define MT9T001_TEST_DATA 0x32
97 #define MT9T001_GLOBAL_GAIN 0x35
98 #define MT9T001_GLOBAL_GAIN_MIN 8
99 #define MT9T001_GLOBAL_GAIN_MAX 1024
100 #define MT9T001_BLACK_LEVEL 0x49
101 #define MT9T001_ROW_BLACK_DEFAULT_OFFSET 0x4b
102 #define MT9T001_BLC_DELTA_THRESHOLDS 0x5d
103 #define MT9T001_CAL_THRESHOLDS 0x5f
104 #define MT9T001_GREEN1_OFFSET 0x60
105 #define MT9T001_GREEN2_OFFSET 0x61
106 #define MT9T001_BLACK_LEVEL_CALIBRATION 0x62
107 #define MT9T001_BLACK_LEVEL_OVERRIDE (1 << 0)
108 #define MT9T001_BLACK_LEVEL_DISABLE_OFFSET (1 << 1)
109 #define MT9T001_BLACK_LEVEL_RECALCULATE (1 << 12)
110 #define MT9T001_BLACK_LEVEL_LOCK_RED_BLUE (1 << 13)
111 #define MT9T001_BLACK_LEVEL_LOCK_GREEN (1 << 14)
112 #define MT9T001_RED_OFFSET 0x63
113 #define MT9T001_BLUE_OFFSET 0x64
136 return i2c_smbus_read_word_swapped(client, reg);
141 return i2c_smbus_write_word_swapped(client, reg, data);
166 static struct v4l2_mbus_framefmt *
167 __mt9t001_get_pad_format(
struct mt9t001 *mt9t001,
struct v4l2_subdev_fh *fh,
172 return v4l2_subdev_get_try_format(fh, pad);
181 __mt9t001_get_pad_crop(
struct mt9t001 *mt9t001,
struct v4l2_subdev_fh *fh,
186 return v4l2_subdev_get_try_crop(fh, pad);
188 return &mt9t001->
crop;
197 struct i2c_client *client = v4l2_get_subdevdata(subdev);
198 struct mt9t001 *mt9t001 = to_mt9t001(subdev);
206 return mt9t001_set_output_control(mt9t001, mode, 0);
237 return mt9t001_set_output_control(mt9t001, 0, mode);
240 static int mt9t001_enum_mbus_code(
struct v4l2_subdev *subdev,
242 struct v4l2_subdev_mbus_code_enum *
code)
251 static int mt9t001_enum_frame_size(
struct v4l2_subdev *subdev,
253 struct v4l2_subdev_frame_size_enum *fse)
259 fse->max_width = fse->min_width;
261 fse->max_height = fse->min_height;
266 static int mt9t001_get_format(
struct v4l2_subdev *subdev,
270 struct mt9t001 *mt9t001 = to_mt9t001(subdev);
272 format->
format = *__mt9t001_get_pad_format(mt9t001, fh, format->
pad,
277 static int mt9t001_set_format(
struct v4l2_subdev *subdev,
281 struct mt9t001 *mt9t001 = to_mt9t001(subdev);
282 struct v4l2_mbus_framefmt *__format;
289 __crop = __mt9t001_get_pad_crop(mt9t001, fh, format->
pad,
303 __format = __mt9t001_get_pad_format(mt9t001, fh, format->
pad,
305 __format->width = __crop->
width / hratio;
306 __format->height = __crop->
height / vratio;
308 format->
format = *__format;
313 static int mt9t001_get_crop(
struct v4l2_subdev *subdev,
317 struct mt9t001 *mt9t001 = to_mt9t001(subdev);
319 crop->
rect = *__mt9t001_get_pad_crop(mt9t001, fh, crop->
pad,
324 static int mt9t001_set_crop(
struct v4l2_subdev *subdev,
328 struct mt9t001 *mt9t001 = to_mt9t001(subdev);
329 struct v4l2_mbus_framefmt *__format;
352 __crop = __mt9t001_get_pad_crop(mt9t001, fh, crop->
pad, crop->
which);
354 if (rect.width != __crop->
width || rect.height != __crop->
height) {
358 __format = __mt9t001_get_pad_format(mt9t001, fh, crop->
pad,
360 __format->width = rect.width;
361 __format->height = rect.height;
374 #define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
375 #define V4L2_CID_BLACK_LEVEL_AUTO (V4L2_CID_USER_BASE | 0x1002)
376 #define V4L2_CID_BLACK_LEVEL_OFFSET (V4L2_CID_USER_BASE | 0x1003)
377 #define V4L2_CID_BLACK_LEVEL_CALIBRATE (V4L2_CID_USER_BASE | 0x1004)
379 #define V4L2_CID_GAIN_RED (V4L2_CTRL_CLASS_CAMERA | 0x1001)
380 #define V4L2_CID_GAIN_GREEN_RED (V4L2_CTRL_CLASS_CAMERA | 0x1002)
381 #define V4L2_CID_GAIN_GREEN_BLUE (V4L2_CTRL_CLASS_CAMERA | 0x1003)
382 #define V4L2_CID_GAIN_BLUE (V4L2_CTRL_CLASS_CAMERA | 0x1004)
405 return (1 << 6) | (*gain >> 1);
409 return ((*gain - 64) << 5) | (1 << 6) | 32;
412 static int mt9t001_ctrl_freeze(
struct mt9t001 *mt9t001,
bool freeze)
414 return mt9t001_set_output_control(mt9t001,
416 freeze ? MT9T001_OUTPUT_CONTROL_SYNC : 0);
421 static const u8 gains[4] = {
426 struct mt9t001 *mt9t001 =
443 for (i = 0, count = 0; i < 4; ++
i) {
451 ret = mt9t001_ctrl_freeze(mt9t001,
true);
457 for (i = 0; i < 4; ++
i) {
463 value = mt9t001_gain_value(&gain->
val);
464 ret = mt9t001_write(client, gains[i], value);
466 mt9t001_ctrl_freeze(mt9t001,
false);
473 ret = mt9t001_ctrl_freeze(mt9t001,
false);
490 return mt9t001_set_output_control(mt9t001,
532 .s_ctrl = mt9t001_s_ctrl,
535 static const char *
const mt9t001_test_pattern_menu[] = {
542 .ops = &mt9t001_ctrl_ops,
545 .name =
"Test Pattern Color",
552 .ops = &mt9t001_ctrl_ops,
555 .name =
"Black Level, Auto",
562 .ops = &mt9t001_ctrl_ops,
565 .name =
"Black Level, Offset",
572 .ops = &mt9t001_ctrl_ops,
575 .name =
"Black Level, Calibrate",
586 .ops = &mt9t001_ctrl_ops,
596 .ops = &mt9t001_ctrl_ops,
599 .name =
"Gain, Green (R)",
606 .ops = &mt9t001_ctrl_ops,
609 .name =
"Gain, Green (B)",
616 .ops = &mt9t001_ctrl_ops,
619 .name =
"Gain, Blue",
634 struct v4l2_mbus_framefmt *
format;
637 crop = v4l2_subdev_get_try_crop(fh, 0);
643 format = v4l2_subdev_get_try_format(fh, 0);
654 .s_stream = mt9t001_s_stream,
658 .enum_mbus_code = mt9t001_enum_mbus_code,
659 .enum_frame_size = mt9t001_enum_frame_size,
660 .get_fmt = mt9t001_get_format,
661 .set_fmt = mt9t001_set_format,
662 .get_crop = mt9t001_get_crop,
663 .set_crop = mt9t001_set_crop,
667 .video = &mt9t001_subdev_video_ops,
668 .pad = &mt9t001_subdev_pad_ops,
672 .open = mt9t001_open,
675 static int mt9t001_video_probe(
struct i2c_client *client)
681 dev_info(&client->
dev,
"Probing MT9T001 at address 0x%02x\n",
708 dev_err(&client->
dev,
"MT9T001 not detected, wrong version "
713 dev_info(&client->
dev,
"MT9T001 detected at address 0x%02x\n",
719 static int mt9t001_probe(
struct i2c_client *client,
723 struct mt9t001 *mt9t001;
732 if (!i2c_check_functionality(client->
adapter,
735 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
739 ret = mt9t001_video_probe(client);
743 mt9t001 = kzalloc(
sizeof(*mt9t001),
GFP_KERNEL);
762 0, mt9t001_test_pattern_menu);
769 &mt9t001_gains[i],
NULL);
775 if (mt9t001->
ctrls.error) {
777 __func__, mt9t001->
ctrls.error);
794 mt9t001->
subdev.internal_ops = &mt9t001_subdev_internal_ops;
810 static int mt9t001_remove(
struct i2c_client *client)
812 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
813 struct mt9t001 *mt9t001 = to_mt9t001(subdev);
832 .probe = mt9t001_probe,
833 .remove = mt9t001_remove,
834 .id_table = mt9t001_id,