1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "em28xx.h"
23
24#include <linux/i2c.h>
25#include <linux/usb.h>
26#include <media/i2c/mt9v011.h>
27#include <media/v4l2-common.h>
28
29
30static unsigned short micron_sensor_addrs[] = {
31 0xb8 >> 1,
32 0xba >> 1,
33 0x90 >> 1,
34 I2C_CLIENT_END
35};
36
37
38static unsigned short omnivision_sensor_addrs[] = {
39 0x42 >> 1,
40 0x60 >> 1,
41 I2C_CLIENT_END
42};
43
44
45static int em28xx_initialize_mt9m111(struct em28xx *dev)
46{
47 int i;
48 unsigned char regs[][3] = {
49 { 0x0d, 0x00, 0x01, },
50 { 0x0d, 0x00, 0x00, },
51 { 0x0a, 0x00, 0x21, },
52 { 0x21, 0x04, 0x00, },
53 };
54
55 for (i = 0; i < ARRAY_SIZE(regs); i++)
56 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
57 ®s[i][0], 3);
58
59
60
61 return 0;
62}
63
64
65static int em28xx_initialize_mt9m001(struct em28xx *dev)
66{
67 int i;
68 unsigned char regs[][3] = {
69 { 0x0d, 0x00, 0x01, },
70 { 0x0d, 0x00, 0x00, },
71 { 0x04, 0x05, 0x00, },
72 { 0x03, 0x04, 0x00, },
73 { 0x20, 0x11, 0x00, },
74 { 0x06, 0x00, 0x10, },
75 { 0x2b, 0x00, 0x24, },
76 { 0x2e, 0x00, 0x24, },
77 { 0x35, 0x00, 0x24, },
78 { 0x2d, 0x00, 0x20, },
79 { 0x2c, 0x00, 0x20, },
80 { 0x09, 0x0a, 0xd4, },
81 { 0x35, 0x00, 0x57, },
82 };
83
84 for (i = 0; i < ARRAY_SIZE(regs); i++)
85 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
86 ®s[i][0], 3);
87
88
89
90 return 0;
91}
92
93
94
95
96static int em28xx_probe_sensor_micron(struct em28xx *dev)
97{
98 int ret, i;
99 char *name;
100 u16 id;
101
102 struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
103
104 dev->em28xx_sensor = EM28XX_NOSENSOR;
105 for (i = 0; micron_sensor_addrs[i] != I2C_CLIENT_END; i++) {
106 client->addr = micron_sensor_addrs[i];
107
108 ret = i2c_smbus_read_word_data(client, 0x00);
109 if (ret < 0) {
110 if (ret != -ENXIO)
111 dev_err(&dev->intf->dev,
112 "couldn't read from i2c device 0x%02x: error %i\n",
113 client->addr << 1, ret);
114 continue;
115 }
116 id = swab16(ret);
117
118 ret = i2c_smbus_read_word_data(client, 0xff);
119 if (ret < 0) {
120 dev_err(&dev->intf->dev,
121 "couldn't read from i2c device 0x%02x: error %i\n",
122 client->addr << 1, ret);
123 continue;
124 }
125
126 if (id != swab16(ret))
127 continue;
128
129 switch (id) {
130 case 0x1222:
131 name = "MT9V012";
132 break;
133 case 0x1229:
134 name = "MT9V112";
135 break;
136 case 0x1433:
137 name = "MT9M011";
138 break;
139 case 0x143a:
140 name = "MT9M111";
141 dev->em28xx_sensor = EM28XX_MT9M111;
142 break;
143 case 0x148c:
144 name = "MT9M112";
145 break;
146 case 0x1511:
147 name = "MT9D011";
148 break;
149 case 0x8232:
150 case 0x8243:
151 name = "MT9V011";
152 dev->em28xx_sensor = EM28XX_MT9V011;
153 break;
154 case 0x8431:
155 name = "MT9M001";
156 dev->em28xx_sensor = EM28XX_MT9M001;
157 break;
158 default:
159 dev_info(&dev->intf->dev,
160 "unknown Micron sensor detected: 0x%04x\n", id);
161 return 0;
162 }
163
164 if (dev->em28xx_sensor == EM28XX_NOSENSOR)
165 dev_info(&dev->intf->dev,
166 "unsupported sensor detected: %s\n", name);
167 else
168 dev_info(&dev->intf->dev,
169 "sensor %s detected\n", name);
170
171 return 0;
172 }
173
174 return -ENODEV;
175}
176
177
178
179
180static int em28xx_probe_sensor_omnivision(struct em28xx *dev)
181{
182 int ret, i;
183 char *name;
184 u8 reg;
185 u16 id;
186 struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
187
188 dev->em28xx_sensor = EM28XX_NOSENSOR;
189
190
191 for (i = 0; omnivision_sensor_addrs[i] != I2C_CLIENT_END; i++) {
192 client->addr = omnivision_sensor_addrs[i];
193
194 reg = 0x1c;
195 ret = i2c_smbus_read_byte_data(client, reg);
196 if (ret < 0) {
197 if (ret != -ENXIO)
198 dev_err(&dev->intf->dev,
199 "couldn't read from i2c device 0x%02x: error %i\n",
200 client->addr << 1, ret);
201 continue;
202 }
203 id = ret << 8;
204 reg = 0x1d;
205 ret = i2c_smbus_read_byte_data(client, reg);
206 if (ret < 0) {
207 dev_err(&dev->intf->dev,
208 "couldn't read from i2c device 0x%02x: error %i\n",
209 client->addr << 1, ret);
210 continue;
211 }
212 id += ret;
213
214 if (id != 0x7fa2)
215 continue;
216
217 reg = 0x0a;
218 ret = i2c_smbus_read_byte_data(client, reg);
219 if (ret < 0) {
220 dev_err(&dev->intf->dev,
221 "couldn't read from i2c device 0x%02x: error %i\n",
222 client->addr << 1, ret);
223 continue;
224 }
225 id = ret << 8;
226 reg = 0x0b;
227 ret = i2c_smbus_read_byte_data(client, reg);
228 if (ret < 0) {
229 dev_err(&dev->intf->dev,
230 "couldn't read from i2c device 0x%02x: error %i\n",
231 client->addr << 1, ret);
232 continue;
233 }
234 id += ret;
235
236 switch (id) {
237 case 0x2642:
238 name = "OV2640";
239 dev->em28xx_sensor = EM28XX_OV2640;
240 break;
241 case 0x7648:
242 name = "OV7648";
243 break;
244 case 0x7660:
245 name = "OV7660";
246 break;
247 case 0x7673:
248 name = "OV7670";
249 break;
250 case 0x7720:
251 name = "OV7720";
252 break;
253 case 0x7721:
254 name = "OV7725";
255 break;
256 case 0x9648:
257 case 0x9649:
258 name = "OV9640";
259 break;
260 case 0x9650:
261 case 0x9652:
262 name = "OV9650";
263 break;
264 case 0x9656:
265 case 0x9657:
266 name = "OV9655";
267 break;
268 default:
269 dev_info(&dev->intf->dev,
270 "unknown OmniVision sensor detected: 0x%04x\n",
271 id);
272 return 0;
273 }
274
275 if (dev->em28xx_sensor == EM28XX_NOSENSOR)
276 dev_info(&dev->intf->dev,
277 "unsupported sensor detected: %s\n", name);
278 else
279 dev_info(&dev->intf->dev,
280 "sensor %s detected\n", name);
281
282 return 0;
283 }
284
285 return -ENODEV;
286}
287
288int em28xx_detect_sensor(struct em28xx *dev)
289{
290 int ret;
291
292 ret = em28xx_probe_sensor_micron(dev);
293
294 if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0)
295 ret = em28xx_probe_sensor_omnivision(dev);
296
297
298
299
300
301
302 if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) {
303 dev_info(&dev->intf->dev,
304 "No sensor detected\n");
305 return -ENODEV;
306 }
307
308 return 0;
309}
310
311int em28xx_init_camera(struct em28xx *dev)
312{
313 struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
314 struct i2c_adapter *adap = &dev->i2c_adap[dev->def_i2c_bus];
315 struct em28xx_v4l2 *v4l2 = dev->v4l2;
316
317 switch (dev->em28xx_sensor) {
318 case EM28XX_MT9V011:
319 {
320 struct mt9v011_platform_data pdata;
321 struct i2c_board_info mt9v011_info = {
322 .type = "mt9v011",
323 .addr = client->addr,
324 .platform_data = &pdata,
325 };
326
327 v4l2->sensor_xres = 640;
328 v4l2->sensor_yres = 480;
329
330
331
332
333
334
335
336
337
338
339 dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ;
340 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
341 v4l2->sensor_xtal = 4300000;
342 pdata.xtal = v4l2->sensor_xtal;
343 if (NULL ==
344 v4l2_i2c_new_subdev_board(&v4l2->v4l2_dev, adap,
345 &mt9v011_info, NULL))
346 return -ENODEV;
347 v4l2->vinmode = EM28XX_VINMODE_RGB8_GRBG;
348 v4l2->vinctl = 0x00;
349
350 break;
351 }
352 case EM28XX_MT9M001:
353 v4l2->sensor_xres = 1280;
354 v4l2->sensor_yres = 1024;
355
356 em28xx_initialize_mt9m001(dev);
357
358 v4l2->vinmode = EM28XX_VINMODE_RGB8_BGGR;
359 v4l2->vinctl = 0x00;
360
361 break;
362 case EM28XX_MT9M111:
363 v4l2->sensor_xres = 640;
364 v4l2->sensor_yres = 512;
365
366 dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ;
367 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
368 em28xx_initialize_mt9m111(dev);
369
370 v4l2->vinmode = EM28XX_VINMODE_YUV422_UYVY;
371 v4l2->vinctl = 0x00;
372
373 break;
374 case EM28XX_OV2640:
375 {
376 struct v4l2_subdev *subdev;
377 struct i2c_board_info ov2640_info = {
378 .type = "ov2640",
379 .flags = I2C_CLIENT_SCCB,
380 .addr = client->addr,
381 };
382 struct v4l2_subdev_format format = {
383 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
384 };
385
386
387
388
389
390
391
392
393
394 v4l2->sensor_xres = 640;
395 v4l2->sensor_yres = 480;
396
397 subdev =
398 v4l2_i2c_new_subdev_board(&v4l2->v4l2_dev, adap,
399 &ov2640_info, NULL);
400 if (subdev == NULL)
401 return -ENODEV;
402
403 format.format.code = MEDIA_BUS_FMT_YUYV8_2X8;
404 format.format.width = 640;
405 format.format.height = 480;
406 v4l2_subdev_call(subdev, pad, set_fmt, NULL, &format);
407
408
409 dev->board.xclk = EM28XX_XCLK_FREQUENCY_24MHZ;
410 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
411 v4l2->vinmode = EM28XX_VINMODE_YUV422_YUYV;
412 v4l2->vinctl = 0x00;
413
414 break;
415 }
416 case EM28XX_NOSENSOR:
417 default:
418 return -EINVAL;
419 }
420
421 return 0;
422}
423EXPORT_SYMBOL_GPL(em28xx_init_camera);
424