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.
22 #include <linux/i2c.h>
23 #include <media/soc_camera.h>
24 #include <media/mt9v011.h>
25 #include <media/v4l2-common.h>
30 /* Possible i2c addresses of Micron sensors */
31 static unsigned short micron_sensor_addrs
[] = {
32 0xb8 >> 1, /* MT9V111, MT9V403 */
33 0xba >> 1, /* MT9M001/011/111/112, MT9V011/012/112, MT9D011 */
34 0x90 >> 1, /* MT9V012/112, MT9D011 (alternative address) */
38 /* Possible i2c addresses of Omnivision sensors */
39 static unsigned short omnivision_sensor_addrs
[] = {
40 0x42 >> 1, /* OV7725, OV7670/60/48 */
41 0x60 >> 1, /* OV2640, OV9650/53/55 */
46 static struct soc_camera_link camlink
= {
49 .module_name
= "em28xx",
53 /* FIXME: Should be replaced by a proper mt9m111 driver */
54 static int em28xx_initialize_mt9m111(struct em28xx
*dev
)
57 unsigned char regs
[][3] = {
58 { 0x0d, 0x00, 0x01, }, /* reset and use defaults */
59 { 0x0d, 0x00, 0x00, },
60 { 0x0a, 0x00, 0x21, },
61 { 0x21, 0x04, 0x00, }, /* full readout speed, no row/col skipping */
64 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
65 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
72 /* FIXME: Should be replaced by a proper mt9m001 driver */
73 static int em28xx_initialize_mt9m001(struct em28xx
*dev
)
76 unsigned char regs
[][3] = {
77 { 0x0d, 0x00, 0x01, },
78 { 0x0d, 0x00, 0x00, },
79 { 0x04, 0x05, 0x00, }, /* hres = 1280 */
80 { 0x03, 0x04, 0x00, }, /* vres = 1024 */
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, },
92 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
93 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
101 * Probes Micron sensors with 8 bit address and 16 bit register width
103 static int em28xx_probe_sensor_micron(struct em28xx
*dev
)
111 struct i2c_client client
= dev
->i2c_client
[dev
->def_i2c_bus
];
113 dev
->em28xx_sensor
= EM28XX_NOSENSOR
;
114 for (i
= 0; micron_sensor_addrs
[i
] != I2C_CLIENT_END
; i
++) {
115 client
.addr
= micron_sensor_addrs
[i
];
116 /* NOTE: i2c_smbus_read_word_data() doesn't work with BE data */
117 /* Read chip ID from register 0x00 */
119 ret
= i2c_master_send(&client
, ®
, 1);
122 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
123 client
.addr
<< 1, ret
);
126 ret
= i2c_master_recv(&client
, (u8
*)&id_be
, 2);
128 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
129 client
.addr
<< 1, ret
);
132 id
= be16_to_cpu(id_be
);
133 /* Read chip ID from register 0xff */
135 ret
= i2c_master_send(&client
, ®
, 1);
137 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
138 client
.addr
<< 1, ret
);
141 ret
= i2c_master_recv(&client
, (u8
*)&id_be
, 2);
143 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
144 client
.addr
<< 1, ret
);
147 /* Validate chip ID to be sure we have a Micron device */
148 if (id
!= be16_to_cpu(id_be
))
151 id
= be16_to_cpu(id_be
);
154 name
= "MT9V012"; /* MI370 */ /* 640x480 */
157 name
= "MT9V112"; /* 640x480 */
160 name
= "MT9M011"; /* 1280x1024 */
162 case 0x143a: /* found in the ECS G200 */
163 name
= "MT9M111"; /* MI1310 */ /* 1280x1024 */
164 dev
->em28xx_sensor
= EM28XX_MT9M111
;
167 name
= "MT9M112"; /* MI1320 */ /* 1280x1024 */
170 name
= "MT9D011"; /* MI2010 */ /* 1600x1200 */
173 case 0x8243: /* rev B */
174 name
= "MT9V011"; /* MI360 */ /* 640x480 */
175 dev
->em28xx_sensor
= EM28XX_MT9V011
;
178 name
= "MT9M001"; /* 1280x1024 */
179 dev
->em28xx_sensor
= EM28XX_MT9M001
;
182 em28xx_info("unknown Micron sensor detected: 0x%04x\n",
187 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
188 em28xx_info("unsupported sensor detected: %s\n", name
);
190 em28xx_info("sensor %s detected\n", name
);
192 dev
->i2c_client
[dev
->def_i2c_bus
].addr
= client
.addr
;
200 * Probes Omnivision sensors with 8 bit address and register width
202 static int em28xx_probe_sensor_omnivision(struct em28xx
*dev
)
208 struct i2c_client client
= dev
->i2c_client
[dev
->def_i2c_bus
];
210 dev
->em28xx_sensor
= EM28XX_NOSENSOR
;
211 /* NOTE: these devices have the register auto incrementation disabled
212 * by default, so we have to use single byte reads ! */
213 for (i
= 0; omnivision_sensor_addrs
[i
] != I2C_CLIENT_END
; i
++) {
214 client
.addr
= omnivision_sensor_addrs
[i
];
215 /* Read manufacturer ID from registers 0x1c-0x1d (BE) */
217 ret
= i2c_smbus_read_byte_data(&client
, reg
);
220 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
221 client
.addr
<< 1, ret
);
226 ret
= i2c_smbus_read_byte_data(&client
, reg
);
228 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
229 client
.addr
<< 1, ret
);
233 /* Check manufacturer ID */
236 /* Read product ID from registers 0x0a-0x0b (BE) */
238 ret
= i2c_smbus_read_byte_data(&client
, reg
);
240 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
241 client
.addr
<< 1, ret
);
246 ret
= i2c_smbus_read_byte_data(&client
, reg
);
248 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
249 client
.addr
<< 1, ret
);
253 /* Check product ID */
257 dev
->em28xx_sensor
= EM28XX_OV2640
;
274 case 0x9648: /* Rev 2 */
275 case 0x9649: /* Rev 3 */
279 case 0x9652: /* OV9653 */
282 case 0x9656: /* Rev 4 */
283 case 0x9657: /* Rev 5 */
287 em28xx_info("unknown OmniVision sensor detected: 0x%04x\n",
292 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
293 em28xx_info("unsupported sensor detected: %s\n", name
);
295 em28xx_info("sensor %s detected\n", name
);
297 dev
->i2c_client
[dev
->def_i2c_bus
].addr
= client
.addr
;
304 int em28xx_detect_sensor(struct em28xx
*dev
)
308 ret
= em28xx_probe_sensor_micron(dev
);
310 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0)
311 ret
= em28xx_probe_sensor_omnivision(dev
);
314 * NOTE: the Windows driver also probes i2c addresses
315 * 0x22 (Samsung ?) and 0x66 (Kodak ?)
318 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0) {
319 em28xx_info("No sensor detected\n");
326 int em28xx_init_camera(struct em28xx
*dev
)
328 switch (dev
->em28xx_sensor
) {
331 struct mt9v011_platform_data pdata
;
332 struct i2c_board_info mt9v011_info
= {
334 .addr
= dev
->i2c_client
[dev
->def_i2c_bus
].addr
,
335 .platform_data
= &pdata
,
338 dev
->sensor_xres
= 640;
339 dev
->sensor_yres
= 480;
342 * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
343 * the Silvercrest cam I have here for testing - for higher
344 * resolutions, a high clock cause horizontal artifacts, so we
345 * need to use a lower xclk frequency.
346 * Yet, it would be possible to adjust xclk depending on the
347 * desired resolution, since this affects directly the
350 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_4_3MHZ
;
351 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
352 dev
->sensor_xtal
= 4300000;
353 pdata
.xtal
= dev
->sensor_xtal
;
355 v4l2_i2c_new_subdev_board(&dev
->v4l2_dev
,
356 &dev
->i2c_adap
[dev
->def_i2c_bus
],
357 &mt9v011_info
, NULL
))
359 /* probably means GRGB 16 bit bayer */
366 dev
->sensor_xres
= 1280;
367 dev
->sensor_yres
= 1024;
369 em28xx_initialize_mt9m001(dev
);
371 /* probably means BGGR 16 bit bayer */
377 dev
->sensor_xres
= 640;
378 dev
->sensor_yres
= 512;
380 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_48MHZ
;
381 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
382 em28xx_initialize_mt9m111(dev
);
390 struct v4l2_subdev
*subdev
;
391 struct i2c_board_info ov2640_info
= {
393 .flags
= I2C_CLIENT_SCCB
,
394 .addr
= dev
->i2c_client
[dev
->def_i2c_bus
].addr
,
395 .platform_data
= &camlink
,
397 struct v4l2_mbus_framefmt fmt
;
400 * FIXME: sensor supports resolutions up to 1600x1200, but
401 * resolution setting/switching needs to be modified to
402 * - switch sensor output resolution (including further
403 * configuration changes)
404 * - adjust bridge xclk
405 * - disable 16 bit (12 bit) output formats on high resolutions
407 dev
->sensor_xres
= 640;
408 dev
->sensor_yres
= 480;
411 v4l2_i2c_new_subdev_board(&dev
->v4l2_dev
,
412 &dev
->i2c_adap
[dev
->def_i2c_bus
],
415 fmt
.code
= V4L2_MBUS_FMT_YUYV8_2X8
;
418 v4l2_subdev_call(subdev
, video
, s_mbus_fmt
, &fmt
);
420 /* NOTE: for UXGA=1600x1200 switch to 12MHz */
421 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_24MHZ
;
422 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
428 case EM28XX_NOSENSOR
: