2 em28xx-camera.c - driver for Empia EM25xx/27xx/28xx USB video capture devices
4 Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@infradead.org>
5 Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/i2c.h>
25 #include <linux/usb.h>
26 #include <media/i2c/mt9v011.h>
27 #include <media/v4l2-common.h>
29 /* Possible i2c addresses of Micron sensors */
30 static unsigned short micron_sensor_addrs
[] = {
31 0xb8 >> 1, /* MT9V111, MT9V403 */
32 0xba >> 1, /* MT9M001/011/111/112, MT9V011/012/112, MT9D011 */
33 0x90 >> 1, /* MT9V012/112, MT9D011 (alternative address) */
37 /* Possible i2c addresses of Omnivision sensors */
38 static unsigned short omnivision_sensor_addrs
[] = {
39 0x42 >> 1, /* OV7725, OV7670/60/48 */
40 0x60 >> 1, /* OV2640, OV9650/53/55 */
44 /* FIXME: Should be replaced by a proper mt9m111 driver */
45 static int em28xx_initialize_mt9m111(struct em28xx
*dev
)
48 unsigned char regs
[][3] = {
49 { 0x0d, 0x00, 0x01, }, /* reset and use defaults */
50 { 0x0d, 0x00, 0x00, },
51 { 0x0a, 0x00, 0x21, },
52 { 0x21, 0x04, 0x00, }, /* full readout speed, no row/col skipping */
55 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
56 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
59 /* FIXME: This won't be creating a sensor at the media graph */
64 /* FIXME: Should be replaced by a proper mt9m001 driver */
65 static int em28xx_initialize_mt9m001(struct em28xx
*dev
)
68 unsigned char regs
[][3] = {
69 { 0x0d, 0x00, 0x01, },
70 { 0x0d, 0x00, 0x00, },
71 { 0x04, 0x05, 0x00, }, /* hres = 1280 */
72 { 0x03, 0x04, 0x00, }, /* vres = 1024 */
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, },
84 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
85 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
88 /* FIXME: This won't be creating a sensor at the media graph */
94 * Probes Micron sensors with 8 bit address and 16 bit register width
96 static int em28xx_probe_sensor_micron(struct em28xx
*dev
)
102 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
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 /* Read chip ID from register 0x00 */
108 ret
= i2c_smbus_read_word_data(client
, 0x00); /* assumes LE */
111 dev_err(&dev
->intf
->dev
,
112 "couldn't read from i2c device 0x%02x: error %i\n",
113 client
->addr
<< 1, ret
);
116 id
= swab16(ret
); /* LE -> BE */
117 /* Read chip ID from register 0xff */
118 ret
= i2c_smbus_read_word_data(client
, 0xff);
120 dev_err(&dev
->intf
->dev
,
121 "couldn't read from i2c device 0x%02x: error %i\n",
122 client
->addr
<< 1, ret
);
125 /* Validate chip ID to be sure we have a Micron device */
126 if (id
!= swab16(ret
))
131 name
= "MT9V012"; /* MI370 */ /* 640x480 */
134 name
= "MT9V112"; /* 640x480 */
137 name
= "MT9M011"; /* 1280x1024 */
139 case 0x143a: /* found in the ECS G200 */
140 name
= "MT9M111"; /* MI1310 */ /* 1280x1024 */
141 dev
->em28xx_sensor
= EM28XX_MT9M111
;
144 name
= "MT9M112"; /* MI1320 */ /* 1280x1024 */
147 name
= "MT9D011"; /* MI2010 */ /* 1600x1200 */
150 case 0x8243: /* rev B */
151 name
= "MT9V011"; /* MI360 */ /* 640x480 */
152 dev
->em28xx_sensor
= EM28XX_MT9V011
;
155 name
= "MT9M001"; /* 1280x1024 */
156 dev
->em28xx_sensor
= EM28XX_MT9M001
;
159 dev_info(&dev
->intf
->dev
,
160 "unknown Micron sensor detected: 0x%04x\n", id
);
164 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
165 dev_info(&dev
->intf
->dev
,
166 "unsupported sensor detected: %s\n", name
);
168 dev_info(&dev
->intf
->dev
,
169 "sensor %s detected\n", name
);
178 * Probes Omnivision sensors with 8 bit address and register width
180 static int em28xx_probe_sensor_omnivision(struct em28xx
*dev
)
186 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
188 dev
->em28xx_sensor
= EM28XX_NOSENSOR
;
189 /* NOTE: these devices have the register auto incrementation disabled
190 * by default, so we have to use single byte reads ! */
191 for (i
= 0; omnivision_sensor_addrs
[i
] != I2C_CLIENT_END
; i
++) {
192 client
->addr
= omnivision_sensor_addrs
[i
];
193 /* Read manufacturer ID from registers 0x1c-0x1d (BE) */
195 ret
= i2c_smbus_read_byte_data(client
, reg
);
198 dev_err(&dev
->intf
->dev
,
199 "couldn't read from i2c device 0x%02x: error %i\n",
200 client
->addr
<< 1, ret
);
205 ret
= i2c_smbus_read_byte_data(client
, reg
);
207 dev_err(&dev
->intf
->dev
,
208 "couldn't read from i2c device 0x%02x: error %i\n",
209 client
->addr
<< 1, ret
);
213 /* Check manufacturer ID */
216 /* Read product ID from registers 0x0a-0x0b (BE) */
218 ret
= i2c_smbus_read_byte_data(client
, reg
);
220 dev_err(&dev
->intf
->dev
,
221 "couldn't read from i2c device 0x%02x: error %i\n",
222 client
->addr
<< 1, ret
);
227 ret
= i2c_smbus_read_byte_data(client
, reg
);
229 dev_err(&dev
->intf
->dev
,
230 "couldn't read from i2c device 0x%02x: error %i\n",
231 client
->addr
<< 1, ret
);
235 /* Check product ID */
239 dev
->em28xx_sensor
= EM28XX_OV2640
;
256 case 0x9648: /* Rev 2 */
257 case 0x9649: /* Rev 3 */
261 case 0x9652: /* OV9653 */
264 case 0x9656: /* Rev 4 */
265 case 0x9657: /* Rev 5 */
269 dev_info(&dev
->intf
->dev
,
270 "unknown OmniVision sensor detected: 0x%04x\n",
275 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
276 dev_info(&dev
->intf
->dev
,
277 "unsupported sensor detected: %s\n", name
);
279 dev_info(&dev
->intf
->dev
,
280 "sensor %s detected\n", name
);
288 int em28xx_detect_sensor(struct em28xx
*dev
)
292 ret
= em28xx_probe_sensor_micron(dev
);
294 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0)
295 ret
= em28xx_probe_sensor_omnivision(dev
);
298 * NOTE: the Windows driver also probes i2c addresses
299 * 0x22 (Samsung ?) and 0x66 (Kodak ?)
302 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0) {
303 dev_info(&dev
->intf
->dev
,
304 "No sensor detected\n");
311 int em28xx_init_camera(struct em28xx
*dev
)
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
;
317 switch (dev
->em28xx_sensor
) {
320 struct mt9v011_platform_data pdata
;
321 struct i2c_board_info mt9v011_info
= {
323 .addr
= client
->addr
,
324 .platform_data
= &pdata
,
327 v4l2
->sensor_xres
= 640;
328 v4l2
->sensor_yres
= 480;
331 * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
332 * the Silvercrest cam I have here for testing - for higher
333 * resolutions, a high clock cause horizontal artifacts, so we
334 * need to use a lower xclk frequency.
335 * Yet, it would be possible to adjust xclk depending on the
336 * desired resolution, since this affects directly the
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
;
344 v4l2_i2c_new_subdev_board(&v4l2
->v4l2_dev
, adap
,
345 &mt9v011_info
, NULL
))
347 v4l2
->vinmode
= EM28XX_VINMODE_RGB8_GRBG
;
353 v4l2
->sensor_xres
= 1280;
354 v4l2
->sensor_yres
= 1024;
356 em28xx_initialize_mt9m001(dev
);
358 v4l2
->vinmode
= EM28XX_VINMODE_RGB8_BGGR
;
363 v4l2
->sensor_xres
= 640;
364 v4l2
->sensor_yres
= 512;
366 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_48MHZ
;
367 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
368 em28xx_initialize_mt9m111(dev
);
370 v4l2
->vinmode
= EM28XX_VINMODE_YUV422_UYVY
;
376 struct v4l2_subdev
*subdev
;
377 struct i2c_board_info ov2640_info
= {
379 .flags
= I2C_CLIENT_SCCB
,
380 .addr
= client
->addr
,
382 struct v4l2_subdev_format format
= {
383 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
387 * FIXME: sensor supports resolutions up to 1600x1200, but
388 * resolution setting/switching needs to be modified to
389 * - switch sensor output resolution (including further
390 * configuration changes)
391 * - adjust bridge xclk
392 * - disable 16 bit (12 bit) output formats on high resolutions
394 v4l2
->sensor_xres
= 640;
395 v4l2
->sensor_yres
= 480;
398 v4l2_i2c_new_subdev_board(&v4l2
->v4l2_dev
, adap
,
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
);
408 /* NOTE: for UXGA=1600x1200 switch to 12MHz */
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
;
416 case EM28XX_NOSENSOR
:
423 EXPORT_SYMBOL_GPL(em28xx_init_camera
);