21 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
26 #include <linux/videodev2.h>
42 #define MAX_WIDTH 2048
43 #define MAX_HEIGHT 1536
46 #define VGA_HEIGHT 480
51 #define ECHECKER(ret, x) \
58 #define mt9t112_reg_write(ret, client, a, b) \
59 ECHECKER(ret, __mt9t112_reg_write(client, a, b))
60 #define mt9t112_mcu_write(ret, client, a, b) \
61 ECHECKER(ret, __mt9t112_mcu_write(client, a, b))
63 #define mt9t112_reg_mask_set(ret, client, a, b, c) \
64 ECHECKER(ret, __mt9t112_reg_mask_set(client, a, b, c))
65 #define mt9t112_mcu_mask_set(ret, client, a, b, c) \
66 ECHECKER(ret, __mt9t112_mcu_mask_set(client, a, b, c))
68 #define mt9t112_reg_read(ret, client, a) \
69 ECHECKER(ret, __mt9t112_reg_read(client, a))
74 #define _VAR(id, offset, base) (base | (id & 0x1f) << 10 | (offset & 0x3ff))
75 #define VAR(id, offset) _VAR(id, offset, 0x0000)
76 #define VAR8(id, offset) _VAR(id, offset, 0x8000)
97 #define INIT_DONE (1 << 0)
98 #define PCLK_RISING (1 << 1)
155 command =
swab16(command);
160 msg[0].buf = (
u8 *)&command;
180 static int __mt9t112_reg_write(
const struct i2c_client *client,
187 command =
swab16(command);
190 memcpy(buf + 0, &command, 2);
191 memcpy(buf + 2, &data, 2);
209 static int __mt9t112_reg_mask_set(
const struct i2c_client *client,
214 int val = __mt9t112_reg_read(client, command);
221 return __mt9t112_reg_write(client, command, val);
225 static int __mt9t112_mcu_read(
const struct i2c_client *client,
u16 command)
229 ret = __mt9t112_reg_write(client, 0x098E, command);
233 return __mt9t112_reg_read(client, 0x0990);
236 static int __mt9t112_mcu_write(
const struct i2c_client *client,
241 ret = __mt9t112_reg_write(client, 0x098E, command);
245 return __mt9t112_reg_write(client, 0x0990, data);
248 static int __mt9t112_mcu_mask_set(
const struct i2c_client *client,
253 int val = __mt9t112_mcu_read(client, command);
260 return __mt9t112_mcu_write(client, command, val);
263 static int mt9t112_reset(
const struct i2c_client *client)
275 #define CLOCK_INFO(a, b)
277 #define CLOCK_INFO(a, b) mt9t112_clock_info(a, b)
280 int m,
n,
p1, p2, p3, p4, p5, p6, p7;
305 n = (n >> 8) & 0x003f;
307 enable = ((6000 >
ext) || (54000 < ext)) ?
"X" :
"";
308 dev_dbg(&client->
dev,
"EXTCLK : %10u K %s\n", ext, enable);
310 vco = 2 * m * ext / (n+1);
311 enable = ((384000 > vco) || (768000 < vco)) ?
"X" :
"";
312 dev_dbg(&client->
dev,
"VCO : %10u K %s\n", vco, enable);
314 clk = vco / (p1+1) / (p2+1);
315 enable = (96000 < clk) ?
"X" :
"";
316 dev_dbg(&client->
dev,
"PIXCLK : %10u K %s\n", clk, enable);
319 enable = (768000 < clk) ?
"X" :
"";
320 dev_dbg(&client->
dev,
"MIPICLK : %10u K %s\n", clk, enable);
323 enable = (96000 < clk) ?
"X" :
"";
324 dev_dbg(&client->
dev,
"MCU CLK : %10u K %s\n", clk, enable);
327 enable = (54000 < clk) ?
"X" :
"";
328 dev_dbg(&client->
dev,
"SOC CLK : %10u K %s\n", clk, enable);
331 enable = (70000 < clk) ?
"X" :
"";
332 dev_dbg(&client->
dev,
"Sensor CLK : %10u K %s\n", clk, enable);
335 dev_dbg(&client->
dev,
"External sensor : %10u K\n", clk);
338 enable = ((2000 > clk) || (24000 < clk)) ?
"X" :
"";
339 dev_dbg(&client->
dev,
"PFD : %10u K %s\n", clk, enable);
347 soc_camera_limit_side(left, width, 0, 0,
MAX_WIDTH);
348 soc_camera_limit_side(top, height, 0, 0,
MAX_HEIGHT);
351 static int mt9t112_set_a_frame_size(
const struct i2c_client *client,
380 static int mt9t112_set_pll_dividers(
const struct i2c_client *client,
395 val = ((p3 & 0x0F) << 8) |
415 static int mt9t112_init_pll(
const struct i2c_client *client)
426 mt9t112_set_pll_dividers(client,
427 priv->
info->divider.m,
428 priv->
info->divider.n,
429 priv->
info->divider.p1,
430 priv->
info->divider.p2,
431 priv->
info->divider.p3,
432 priv->
info->divider.p4,
433 priv->
info->divider.p5,
434 priv->
info->divider.p6,
435 priv->
info->divider.p7);
492 for (i = 0; i < 100; i++) {
494 if (!(0x4000 & data))
503 static int mt9t112_init_setting(
const struct i2c_client *client)
661 static int mt9t112_auto_focus_setting(
const struct i2c_client *client)
683 static int mt9t112_auto_focus_trigger(
const struct i2c_client *client)
692 static int mt9t112_init_camera(
const struct i2c_client *client)
696 ECHECKER(ret, mt9t112_reset(client));
698 ECHECKER(ret, mt9t112_init_pll(client));
700 ECHECKER(ret, mt9t112_init_setting(client));
702 ECHECKER(ret, mt9t112_auto_focus_setting(client));
743 struct i2c_client *client = v4l2_get_subdevdata(sd);
746 id->ident = priv->
model;
752 #ifdef CONFIG_VIDEO_ADV_DEBUG
756 struct i2c_client *client = v4l2_get_subdevdata(sd);
770 struct i2c_client *client = v4l2_get_subdevdata(sd);
781 struct i2c_client *client = v4l2_get_subdevdata(sd);
784 return soc_camera_set_power(&client->
dev, icl, on);
788 .g_chip_ident = mt9t112_g_chip_ident,
789 #ifdef CONFIG_VIDEO_ADV_DEBUG
790 .g_register = mt9t112_g_register,
791 .s_register = mt9t112_s_register,
793 .s_power = mt9t112_s_power,
802 struct i2c_client *client = v4l2_get_subdevdata(sd);
823 ECHECKER(ret, mt9t112_init_camera(client));
837 mt9t112_set_a_frame_size(client,
841 ECHECKER(ret, mt9t112_auto_focus_trigger(client));
853 static int mt9t112_set_params(
struct mt9t112_priv *priv,
862 for (i = 0; i <
ARRAY_SIZE(mt9t112_cfmts); i++)
863 if (mt9t112_cfmts[i].code == code)
874 mt9t112_frame_check(&priv->
frame.width, &priv->
frame.height,
877 priv->
format = mt9t112_cfmts +
i;
901 struct i2c_client *client = v4l2_get_subdevdata(sd);
912 struct i2c_client *client = v4l2_get_subdevdata(sd);
916 return mt9t112_set_params(priv, rect, priv->
format->code);
920 struct v4l2_mbus_framefmt *mf)
922 struct i2c_client *client = v4l2_get_subdevdata(sd);
925 mf->width = priv->
frame.width;
926 mf->height = priv->
frame.height;
927 mf->colorspace = priv->
format->colorspace;
928 mf->code = priv->
format->code;
935 struct v4l2_mbus_framefmt *mf)
937 struct i2c_client *client = v4l2_get_subdevdata(sd);
941 .height = mf->height,
942 .left = priv->
frame.left,
943 .top = priv->
frame.top,
947 ret = mt9t112_set_params(priv, &rect, mf->code);
950 mf->colorspace = priv->
format->colorspace;
956 struct v4l2_mbus_framefmt *mf)
961 for (i = 0; i <
ARRAY_SIZE(mt9t112_cfmts); i++)
962 if (mt9t112_cfmts[i].code == mf->
code)
972 mt9t112_frame_check(&mf->width, &mf->height, &left, &top);
990 static int mt9t112_g_mbus_config(
struct v4l2_subdev *sd,
993 struct i2c_client *client = v4l2_get_subdevdata(sd);
1005 static int mt9t112_s_mbus_config(
struct v4l2_subdev *sd,
1008 struct i2c_client *client = v4l2_get_subdevdata(sd);
1019 .s_stream = mt9t112_s_stream,
1020 .g_mbus_fmt = mt9t112_g_fmt,
1021 .s_mbus_fmt = mt9t112_s_fmt,
1022 .try_mbus_fmt = mt9t112_try_fmt,
1023 .cropcap = mt9t112_cropcap,
1024 .g_crop = mt9t112_g_crop,
1025 .s_crop = mt9t112_s_crop,
1026 .enum_mbus_fmt = mt9t112_enum_fmt,
1027 .g_mbus_config = mt9t112_g_mbus_config,
1028 .s_mbus_config = mt9t112_s_mbus_config,
1035 .core = &mt9t112_subdev_core_ops,
1036 .video = &mt9t112_subdev_video_ops,
1039 static int mt9t112_camera_probe(
struct i2c_client *client)
1042 const char *devname;
1046 ret = mt9t112_s_power(&priv->
subdev, 1);
1057 devname =
"mt9t111";
1061 devname =
"mt9t112";
1065 dev_err(&client->
dev,
"Product ID error %04x\n", chipid);
1070 dev_info(&client->
dev,
"%s chip ID %04x\n", devname, chipid);
1073 mt9t112_s_power(&priv->
subdev, 0);
1077 static int mt9t112_probe(
struct i2c_client *client,
1090 if (!icl || !icl->
priv) {
1091 dev_err(&client->
dev,
"mt9t112: missing platform data!\n");
1103 ret = mt9t112_camera_probe(client);
1115 static int mt9t112_remove(
struct i2c_client *client)
1129 static struct i2c_driver mt9t112_i2c_driver = {
1133 .probe = mt9t112_probe,
1134 .remove = mt9t112_remove,
1135 .id_table = mt9t112_id,