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