14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/slab.h>
23 #define to_ov9740(sd) container_of(sd, struct ov9740_priv, subdev)
26 #define OV9740_MODEL_ID_HI 0x0000
27 #define OV9740_MODEL_ID_LO 0x0001
28 #define OV9740_REVISION_NUMBER 0x0002
29 #define OV9740_MANUFACTURER_ID 0x0003
30 #define OV9740_SMIA_VERSION 0x0004
33 #define OV9740_MODE_SELECT 0x0100
34 #define OV9740_IMAGE_ORT 0x0101
35 #define OV9740_SOFTWARE_RESET 0x0103
36 #define OV9740_GRP_PARAM_HOLD 0x0104
37 #define OV9740_MSK_CORRUP_FM 0x0105
40 #define OV9740_FRM_LENGTH_LN_HI 0x0340
41 #define OV9740_FRM_LENGTH_LN_LO 0x0341
42 #define OV9740_LN_LENGTH_PCK_HI 0x0342
43 #define OV9740_LN_LENGTH_PCK_LO 0x0343
44 #define OV9740_X_ADDR_START_HI 0x0344
45 #define OV9740_X_ADDR_START_LO 0x0345
46 #define OV9740_Y_ADDR_START_HI 0x0346
47 #define OV9740_Y_ADDR_START_LO 0x0347
48 #define OV9740_X_ADDR_END_HI 0x0348
49 #define OV9740_X_ADDR_END_LO 0x0349
50 #define OV9740_Y_ADDR_END_HI 0x034a
51 #define OV9740_Y_ADDR_END_LO 0x034b
52 #define OV9740_X_OUTPUT_SIZE_HI 0x034c
53 #define OV9740_X_OUTPUT_SIZE_LO 0x034d
54 #define OV9740_Y_OUTPUT_SIZE_HI 0x034e
55 #define OV9740_Y_OUTPUT_SIZE_LO 0x034f
58 #define OV9740_IO_CREL00 0x3002
59 #define OV9740_IO_CREL01 0x3004
60 #define OV9740_IO_CREL02 0x3005
61 #define OV9740_IO_OUTPUT_SEL01 0x3026
62 #define OV9740_IO_OUTPUT_SEL02 0x3027
65 #define OV9740_AWB_MANUAL_CTRL 0x3406
68 #define OV9740_ANALOG_CTRL01 0x3601
69 #define OV9740_ANALOG_CTRL02 0x3602
70 #define OV9740_ANALOG_CTRL03 0x3603
71 #define OV9740_ANALOG_CTRL04 0x3604
72 #define OV9740_ANALOG_CTRL10 0x3610
73 #define OV9740_ANALOG_CTRL12 0x3612
74 #define OV9740_ANALOG_CTRL15 0x3615
75 #define OV9740_ANALOG_CTRL20 0x3620
76 #define OV9740_ANALOG_CTRL21 0x3621
77 #define OV9740_ANALOG_CTRL22 0x3622
78 #define OV9740_ANALOG_CTRL30 0x3630
79 #define OV9740_ANALOG_CTRL31 0x3631
80 #define OV9740_ANALOG_CTRL32 0x3632
81 #define OV9740_ANALOG_CTRL33 0x3633
84 #define OV9740_SENSOR_CTRL03 0x3703
85 #define OV9740_SENSOR_CTRL04 0x3704
86 #define OV9740_SENSOR_CTRL05 0x3705
87 #define OV9740_SENSOR_CTRL07 0x3707
90 #define OV9740_TIMING_CTRL17 0x3817
91 #define OV9740_TIMING_CTRL19 0x3819
92 #define OV9740_TIMING_CTRL33 0x3833
93 #define OV9740_TIMING_CTRL35 0x3835
96 #define OV9740_AEC_MAXEXPO_60_H 0x3a02
97 #define OV9740_AEC_MAXEXPO_60_L 0x3a03
98 #define OV9740_AEC_B50_STEP_HI 0x3a08
99 #define OV9740_AEC_B50_STEP_LO 0x3a09
100 #define OV9740_AEC_B60_STEP_HI 0x3a0a
101 #define OV9740_AEC_B60_STEP_LO 0x3a0b
102 #define OV9740_AEC_CTRL0D 0x3a0d
103 #define OV9740_AEC_CTRL0E 0x3a0e
104 #define OV9740_AEC_MAXEXPO_50_H 0x3a14
105 #define OV9740_AEC_MAXEXPO_50_L 0x3a15
108 #define OV9740_AEC_ENABLE 0x3503
109 #define OV9740_GAIN_CEILING_01 0x3a18
110 #define OV9740_GAIN_CEILING_02 0x3a19
111 #define OV9740_AEC_HI_THRESHOLD 0x3a11
112 #define OV9740_AEC_3A1A 0x3a1a
113 #define OV9740_AEC_CTRL1B_WPT2 0x3a1b
114 #define OV9740_AEC_CTRL0F_WPT 0x3a0f
115 #define OV9740_AEC_CTRL10_BPT 0x3a10
116 #define OV9740_AEC_CTRL1E_BPT2 0x3a1e
117 #define OV9740_AEC_LO_THRESHOLD 0x3a1f
120 #define OV9740_BLC_AUTO_ENABLE 0x4002
121 #define OV9740_BLC_MODE 0x4005
124 #define OV9740_VFIFO_READ_START_HI 0x4608
125 #define OV9740_VFIFO_READ_START_LO 0x4609
128 #define OV9740_DVP_VSYNC_CTRL02 0x4702
129 #define OV9740_DVP_VSYNC_MODE 0x4704
130 #define OV9740_DVP_VSYNC_CTRL06 0x4706
133 #define OV9740_PLL_MODE_CTRL01 0x3104
134 #define OV9740_PRE_PLL_CLK_DIV 0x0305
135 #define OV9740_PLL_MULTIPLIER 0x0307
136 #define OV9740_VT_SYS_CLK_DIV 0x0303
137 #define OV9740_VT_PIX_CLK_DIV 0x0301
138 #define OV9740_PLL_CTRL3010 0x3010
139 #define OV9740_VFIFO_CTRL00 0x460e
142 #define OV9740_ISP_CTRL00 0x5000
143 #define OV9740_ISP_CTRL01 0x5001
144 #define OV9740_ISP_CTRL03 0x5003
145 #define OV9740_ISP_CTRL05 0x5005
146 #define OV9740_ISP_CTRL12 0x5012
147 #define OV9740_ISP_CTRL19 0x5019
148 #define OV9740_ISP_CTRL1A 0x501a
149 #define OV9740_ISP_CTRL1E 0x501e
150 #define OV9740_ISP_CTRL1F 0x501f
151 #define OV9740_ISP_CTRL20 0x5020
152 #define OV9740_ISP_CTRL21 0x5021
155 #define OV9740_AWB_CTRL00 0x5180
156 #define OV9740_AWB_CTRL01 0x5181
157 #define OV9740_AWB_CTRL02 0x5182
158 #define OV9740_AWB_CTRL03 0x5183
159 #define OV9740_AWB_ADV_CTRL01 0x5184
160 #define OV9740_AWB_ADV_CTRL02 0x5185
161 #define OV9740_AWB_ADV_CTRL03 0x5186
162 #define OV9740_AWB_ADV_CTRL04 0x5187
163 #define OV9740_AWB_ADV_CTRL05 0x5188
164 #define OV9740_AWB_ADV_CTRL06 0x5189
165 #define OV9740_AWB_ADV_CTRL07 0x518a
166 #define OV9740_AWB_ADV_CTRL08 0x518b
167 #define OV9740_AWB_ADV_CTRL09 0x518c
168 #define OV9740_AWB_ADV_CTRL10 0x518d
169 #define OV9740_AWB_ADV_CTRL11 0x518e
170 #define OV9740_AWB_CTRL0F 0x518f
171 #define OV9740_AWB_CTRL10 0x5190
172 #define OV9740_AWB_CTRL11 0x5191
173 #define OV9740_AWB_CTRL12 0x5192
174 #define OV9740_AWB_CTRL13 0x5193
175 #define OV9740_AWB_CTRL14 0x5194
178 #define OV9740_MIPI_CTRL00 0x4800
179 #define OV9740_MIPI_3837 0x3837
180 #define OV9740_MIPI_CTRL01 0x4801
181 #define OV9740_MIPI_CTRL03 0x4803
182 #define OV9740_MIPI_CTRL05 0x4805
183 #define OV9740_VFIFO_RD_CTRL 0x4601
184 #define OV9740_MIPI_CTRL_3012 0x3012
185 #define OV9740_SC_CMMM_MIPI_CTR 0x3014
187 #define OV9740_MAX_WIDTH 1280
188 #define OV9740_MAX_HEIGHT 720
214 static const struct ov9740_reg ov9740_defaults[] = {
231 { 0x5842, 0x02 }, { 0x5843, 0x5e }, { 0x5844, 0x04 }, { 0x5845, 0x32 },
232 { 0x5846, 0x03 }, { 0x5847, 0x29 }, { 0x5848, 0x02 }, { 0x5849, 0xcc },
235 { 0x5800, 0x29 }, { 0x5801, 0x25 }, { 0x5802, 0x20 }, { 0x5803, 0x21 },
236 { 0x5804, 0x26 }, { 0x5805, 0x2e }, { 0x5806, 0x11 }, { 0x5807, 0x0c },
237 { 0x5808, 0x09 }, { 0x5809, 0x0a }, { 0x580a, 0x0e }, { 0x580b, 0x16 },
238 { 0x580c, 0x06 }, { 0x580d, 0x02 }, { 0x580e, 0x00 }, { 0x580f, 0x00 },
239 { 0x5810, 0x04 }, { 0x5811, 0x0a }, { 0x5812, 0x05 }, { 0x5813, 0x02 },
240 { 0x5814, 0x00 }, { 0x5815, 0x00 }, { 0x5816, 0x03 }, { 0x5817, 0x09 },
241 { 0x5818, 0x0f }, { 0x5819, 0x0a }, { 0x581a, 0x07 }, { 0x581b, 0x08 },
242 { 0x581c, 0x0b }, { 0x581d, 0x14 }, { 0x581e, 0x28 }, { 0x581f, 0x23 },
243 { 0x5820, 0x1d }, { 0x5821, 0x1e }, { 0x5822, 0x24 }, { 0x5823, 0x2a },
244 { 0x5824, 0x4f }, { 0x5825, 0x6f }, { 0x5826, 0x5f }, { 0x5827, 0x7f },
245 { 0x5828, 0x9f }, { 0x5829, 0x5f }, { 0x582a, 0x8f }, { 0x582b, 0x9e },
246 { 0x582c, 0x8f }, { 0x582d, 0x9f }, { 0x582e, 0x4f }, { 0x582f, 0x87 },
247 { 0x5830, 0x86 }, { 0x5831, 0x97 }, { 0x5832, 0xae }, { 0x5833, 0x3f },
248 { 0x5834, 0x8e }, { 0x5835, 0x7c }, { 0x5836, 0x7e }, { 0x5837, 0xaf },
249 { 0x5838, 0x8f }, { 0x5839, 0x8f }, { 0x583a, 0x9f }, { 0x583b, 0x7f },
253 { 0x5480, 0x07 }, { 0x5481, 0x18 }, { 0x5482, 0x2c }, { 0x5483, 0x4e },
254 { 0x5484, 0x5e }, { 0x5485, 0x6b }, { 0x5486, 0x77 }, { 0x5487, 0x82 },
255 { 0x5488, 0x8c }, { 0x5489, 0x95 }, { 0x548a, 0xa4 }, { 0x548b, 0xb1 },
256 { 0x548c, 0xc6 }, { 0x548d, 0xd8 }, { 0x548e, 0xe9 },
259 { 0x5490, 0x0f }, { 0x5491, 0xff }, { 0x5492, 0x0d }, { 0x5493, 0x05 },
260 { 0x5494, 0x07 }, { 0x5495, 0x1a }, { 0x5496, 0x04 }, { 0x5497, 0x01 },
261 { 0x5498, 0x03 }, { 0x5499, 0x53 }, { 0x549a, 0x02 }, { 0x549b, 0xeb },
262 { 0x549c, 0x02 }, { 0x549d, 0xa0 }, { 0x549e, 0x02 }, { 0x549f, 0x67 },
263 { 0x54a0, 0x02 }, { 0x54a1, 0x3b }, { 0x54a2, 0x02 }, { 0x54a3, 0x18 },
264 { 0x54a4, 0x01 }, { 0x54a5, 0xe7 }, { 0x54a6, 0x01 }, { 0x54a7, 0xc3 },
265 { 0x54a8, 0x01 }, { 0x54a9, 0x94 }, { 0x54aa, 0x01 }, { 0x54ab, 0x72 },
266 { 0x54ac, 0x01 }, { 0x54ad, 0x57 },
295 { 0x5380, 0x01 }, { 0x5381, 0x00 }, { 0x5382, 0x00 }, { 0x5383, 0x17 },
296 { 0x5384, 0x00 }, { 0x5385, 0x01 }, { 0x5386, 0x00 }, { 0x5387, 0x00 },
297 { 0x5388, 0x00 }, { 0x5389, 0xe0 }, { 0x538a, 0x00 }, { 0x538b, 0x20 },
298 { 0x538c, 0x00 }, { 0x538d, 0x00 }, { 0x538e, 0x00 }, { 0x538f, 0x16 },
299 { 0x5390, 0x00 }, { 0x5391, 0x9c }, { 0x5392, 0x00 }, { 0x5393, 0xa0 },
303 { 0x3c0a, 0x9c }, { 0x3c0b, 0x3f },
405 .addr = client->
addr,
411 .addr = client->
addr,
422 dev_err(&client->
dev,
"Failed reading register 0x%04x!\n", reg);
430 static int ov9740_reg_write(
struct i2c_client *client,
u16 reg,
u8 val)
447 msg.
buf = (
u8 *)&buf;
451 dev_err(&client->
dev,
"Failed writing register 0x%04x!\n", reg);
465 ret = ov9740_reg_read(client, reg, &val);
468 "[Read]-Modify-Write of register 0x%04x failed!\n",
476 ret = ov9740_reg_write(client, reg, val);
479 "Read-Modify-[Write] of register 0x%04x failed!\n",
487 static int ov9740_reg_write_array(
struct i2c_client *client,
494 for (i = 0; i < regarraylen; i++) {
495 ret = ov9740_reg_write(client,
496 regarray[i].reg, regarray[i].val);
507 struct i2c_client *client = v4l2_get_subdevdata(sd);
527 dev_dbg(&client->
dev,
"Enabling Streaming\n");
532 dev_dbg(&client->
dev,
"Disabling Streaming\n");
550 *width =
ALIGN(*width, 4);
593 x_end = x_start + scale_input_x - 1;
594 y_end = y_start + scale_input_y - 1;
649 (scale_input_x - width) >> 8);
653 (scale_input_x - width) & 0xff);
672 struct v4l2_mbus_framefmt *mf)
674 struct i2c_client *client = v4l2_get_subdevdata(sd);
680 ov9740_res_roundup(&mf->width, &mf->height);
690 ret = ov9740_reg_write_array(client, ov9740_defaults,
695 ret = ov9740_set_res(client, mf->width, mf->height);
700 mf->colorspace = cspace;
708 struct v4l2_mbus_framefmt *mf)
710 ov9740_res_roundup(&mf->width, &mf->height);
725 *code = ov9740_codes[
index];
776 static int ov9740_g_chip_ident(
struct v4l2_subdev *sd,
781 id->ident = priv->
ident;
787 static int ov9740_s_power(
struct v4l2_subdev *sd,
int on)
789 struct i2c_client *client = v4l2_get_subdevdata(sd);
801 ov9740_s_stream(sd, 1);
805 ov9740_s_stream(sd, 0);
815 #ifdef CONFIG_VIDEO_ADV_DEBUG
816 static int ov9740_get_register(
struct v4l2_subdev *sd,
819 struct i2c_client *client = v4l2_get_subdevdata(sd);
823 if (reg->
reg & ~0xffff)
828 ret = ov9740_reg_read(client, reg->
reg, &val);
837 static int ov9740_set_register(
struct v4l2_subdev *sd,
840 struct i2c_client *client = v4l2_get_subdevdata(sd);
842 if (reg->
reg & ~0xffff || reg->
val & ~0xff)
845 return ov9740_reg_write(client, reg->
reg, reg->
val);
849 static int ov9740_video_probe(
struct i2c_client *client)
851 struct v4l2_subdev *sd = i2c_get_clientdata(client);
856 ret = ov9740_s_power(&priv->
subdev, 1);
871 priv->
model = (modelhi << 8) | modello;
885 if (priv->
model != 0x9740) {
892 dev_info(&client->
dev,
"ov9740 Model ID 0x%04x, Revision 0x%02x, "
893 "Manufacturer 0x%02x, SMIA Version 0x%02x\n",
899 ov9740_s_power(&priv->
subdev, 0);
904 static int ov9740_g_mbus_config(
struct v4l2_subdev *sd,
907 struct i2c_client *client = v4l2_get_subdevdata(sd);
920 .s_stream = ov9740_s_stream,
921 .s_mbus_fmt = ov9740_s_fmt,
922 .try_mbus_fmt = ov9740_try_fmt,
923 .enum_mbus_fmt = ov9740_enum_fmt,
924 .cropcap = ov9740_cropcap,
925 .g_crop = ov9740_g_crop,
926 .g_mbus_config = ov9740_g_mbus_config,
930 .g_chip_ident = ov9740_g_chip_ident,
931 .s_power = ov9740_s_power,
932 #ifdef CONFIG_VIDEO_ADV_DEBUG
933 .g_register = ov9740_get_register,
934 .s_register = ov9740_set_register,
939 .core = &ov9740_core_ops,
940 .video = &ov9740_video_ops,
944 .s_ctrl = ov9740_s_ctrl,
950 static int ov9740_probe(
struct i2c_client *client,
958 dev_err(&client->
dev,
"Missing platform_data for driver\n");
964 dev_err(&client->
dev,
"Failed to allocate private data!\n");
975 if (priv->
hdl.error) {
976 int err = priv->
hdl.error;
982 ret = ov9740_video_probe(client);
991 static int ov9740_remove(
struct i2c_client *client)
993 struct ov9740_priv *priv = i2c_get_clientdata(client);
1007 static struct i2c_driver ov9740_i2c_driver = {
1011 .probe = ov9740_probe,
1012 .remove = ov9740_remove,
1013 .id_table = ov9740_id,