1 // SPDX-License-Identifier: GPL-2.0+
3 // em28xx-camera.c - driver for Empia EM25xx/27xx/28xx USB video capture devices
5 // Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
6 // Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
20 #include <linux/i2c.h>
21 #include <linux/usb.h>
22 #include <media/i2c/mt9v011.h>
23 #include <media/v4l2-common.h>
25 /* Possible i2c addresses of Micron sensors */
26 static unsigned short micron_sensor_addrs
[] = {
27 0xb8 >> 1, /* MT9V111, MT9V403 */
28 0xba >> 1, /* MT9M001/011/111/112, MT9V011/012/112, MT9D011 */
29 0x90 >> 1, /* MT9V012/112, MT9D011 (alternative address) */
33 /* Possible i2c addresses of Omnivision sensors */
34 static unsigned short omnivision_sensor_addrs
[] = {
35 0x42 >> 1, /* OV7725, OV7670/60/48 */
36 0x60 >> 1, /* OV2640, OV9650/53/55 */
40 /* FIXME: Should be replaced by a proper mt9m111 driver */
41 static int em28xx_initialize_mt9m111(struct em28xx
*dev
)
44 unsigned char regs
[][3] = {
45 { 0x0d, 0x00, 0x01, }, /* reset and use defaults */
46 { 0x0d, 0x00, 0x00, },
47 { 0x0a, 0x00, 0x21, },
48 { 0x21, 0x04, 0x00, }, /* full readout spd, no row/col skip */
51 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
52 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
55 /* FIXME: This won't be creating a sensor at the media graph */
60 /* FIXME: Should be replaced by a proper mt9m001 driver */
61 static int em28xx_initialize_mt9m001(struct em28xx
*dev
)
64 unsigned char regs
[][3] = {
65 { 0x0d, 0x00, 0x01, },
66 { 0x0d, 0x00, 0x00, },
67 { 0x04, 0x05, 0x00, }, /* hres = 1280 */
68 { 0x03, 0x04, 0x00, }, /* vres = 1024 */
69 { 0x20, 0x11, 0x00, },
70 { 0x06, 0x00, 0x10, },
71 { 0x2b, 0x00, 0x24, },
72 { 0x2e, 0x00, 0x24, },
73 { 0x35, 0x00, 0x24, },
74 { 0x2d, 0x00, 0x20, },
75 { 0x2c, 0x00, 0x20, },
76 { 0x09, 0x0a, 0xd4, },
77 { 0x35, 0x00, 0x57, },
80 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
81 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
84 /* FIXME: This won't be creating a sensor at the media graph */
90 * Probes Micron sensors with 8 bit address and 16 bit register width
92 static int em28xx_probe_sensor_micron(struct em28xx
*dev
)
98 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
100 dev
->em28xx_sensor
= EM28XX_NOSENSOR
;
101 for (i
= 0; micron_sensor_addrs
[i
] != I2C_CLIENT_END
; i
++) {
102 client
->addr
= micron_sensor_addrs
[i
];
103 /* Read chip ID from register 0x00 */
104 ret
= i2c_smbus_read_word_data(client
, 0x00); /* assumes LE */
107 dev_err(&dev
->intf
->dev
,
108 "couldn't read from i2c device 0x%02x: error %i\n",
109 client
->addr
<< 1, ret
);
112 id
= swab16(ret
); /* LE -> BE */
113 /* Read chip ID from register 0xff */
114 ret
= i2c_smbus_read_word_data(client
, 0xff);
116 dev_err(&dev
->intf
->dev
,
117 "couldn't read from i2c device 0x%02x: error %i\n",
118 client
->addr
<< 1, ret
);
121 /* Validate chip ID to be sure we have a Micron device */
122 if (id
!= swab16(ret
))
127 name
= "MT9V012"; /* MI370 */ /* 640x480 */
130 name
= "MT9V112"; /* 640x480 */
133 name
= "MT9M011"; /* 1280x1024 */
135 case 0x143a: /* found in the ECS G200 */
136 name
= "MT9M111"; /* MI1310 */ /* 1280x1024 */
137 dev
->em28xx_sensor
= EM28XX_MT9M111
;
140 name
= "MT9M112"; /* MI1320 */ /* 1280x1024 */
143 name
= "MT9D011"; /* MI2010 */ /* 1600x1200 */
146 case 0x8243: /* rev B */
147 name
= "MT9V011"; /* MI360 */ /* 640x480 */
148 dev
->em28xx_sensor
= EM28XX_MT9V011
;
151 name
= "MT9M001"; /* 1280x1024 */
152 dev
->em28xx_sensor
= EM28XX_MT9M001
;
155 dev_info(&dev
->intf
->dev
,
156 "unknown Micron sensor detected: 0x%04x\n",
161 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
162 dev_info(&dev
->intf
->dev
,
163 "unsupported sensor detected: %s\n", name
);
165 dev_info(&dev
->intf
->dev
,
166 "sensor %s detected\n", name
);
175 * Probes Omnivision sensors with 8 bit address and register width
177 static int em28xx_probe_sensor_omnivision(struct em28xx
*dev
)
183 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
185 dev
->em28xx_sensor
= EM28XX_NOSENSOR
;
187 * NOTE: these devices have the register auto incrementation disabled
188 * by default, so we have to use single byte reads !
190 for (i
= 0; omnivision_sensor_addrs
[i
] != I2C_CLIENT_END
; i
++) {
191 client
->addr
= omnivision_sensor_addrs
[i
];
192 /* Read manufacturer ID from registers 0x1c-0x1d (BE) */
194 ret
= i2c_smbus_read_byte_data(client
, reg
);
197 dev_err(&dev
->intf
->dev
,
198 "couldn't read from i2c device 0x%02x: error %i\n",
199 client
->addr
<< 1, ret
);
204 ret
= i2c_smbus_read_byte_data(client
, reg
);
206 dev_err(&dev
->intf
->dev
,
207 "couldn't read from i2c device 0x%02x: error %i\n",
208 client
->addr
<< 1, ret
);
212 /* Check manufacturer ID */
215 /* Read product ID from registers 0x0a-0x0b (BE) */
217 ret
= i2c_smbus_read_byte_data(client
, reg
);
219 dev_err(&dev
->intf
->dev
,
220 "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 dev_err(&dev
->intf
->dev
,
229 "couldn't read from i2c device 0x%02x: error %i\n",
230 client
->addr
<< 1, ret
);
234 /* Check product ID */
238 dev
->em28xx_sensor
= EM28XX_OV2640
;
255 case 0x9648: /* Rev 2 */
256 case 0x9649: /* Rev 3 */
260 case 0x9652: /* OV9653 */
263 case 0x9656: /* Rev 4 */
264 case 0x9657: /* Rev 5 */
268 dev_info(&dev
->intf
->dev
,
269 "unknown OmniVision sensor detected: 0x%04x\n",
274 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
275 dev_info(&dev
->intf
->dev
,
276 "unsupported sensor detected: %s\n", name
);
278 dev_info(&dev
->intf
->dev
,
279 "sensor %s detected\n", name
);
287 int em28xx_detect_sensor(struct em28xx
*dev
)
291 ret
= em28xx_probe_sensor_micron(dev
);
293 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0)
294 ret
= em28xx_probe_sensor_omnivision(dev
);
297 * NOTE: the Windows driver also probes i2c addresses
298 * 0x22 (Samsung ?) and 0x66 (Kodak ?)
301 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0) {
302 dev_info(&dev
->intf
->dev
,
303 "No sensor detected\n");
310 int em28xx_init_camera(struct em28xx
*dev
)
312 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
313 struct i2c_adapter
*adap
= &dev
->i2c_adap
[dev
->def_i2c_bus
];
314 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
316 switch (dev
->em28xx_sensor
) {
319 struct mt9v011_platform_data pdata
;
320 struct i2c_board_info mt9v011_info
= {
322 .addr
= client
->addr
,
323 .platform_data
= &pdata
,
326 v4l2
->sensor_xres
= 640;
327 v4l2
->sensor_yres
= 480;
330 * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
331 * the Silvercrest cam I have here for testing - for higher
332 * resolutions, a high clock cause horizontal artifacts, so we
333 * need to use a lower xclk frequency.
334 * Yet, it would be possible to adjust xclk depending on the
335 * desired resolution, since this affects directly the
338 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_4_3MHZ
;
339 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
340 v4l2
->sensor_xtal
= 4300000;
341 pdata
.xtal
= v4l2
->sensor_xtal
;
343 v4l2_i2c_new_subdev_board(&v4l2
->v4l2_dev
, adap
,
344 &mt9v011_info
, NULL
))
346 v4l2
->vinmode
= EM28XX_VINMODE_RGB8_GRBG
;
352 v4l2
->sensor_xres
= 1280;
353 v4l2
->sensor_yres
= 1024;
355 em28xx_initialize_mt9m001(dev
);
357 v4l2
->vinmode
= EM28XX_VINMODE_RGB8_BGGR
;
362 v4l2
->sensor_xres
= 640;
363 v4l2
->sensor_yres
= 512;
365 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_48MHZ
;
366 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
367 em28xx_initialize_mt9m111(dev
);
369 v4l2
->vinmode
= EM28XX_VINMODE_YUV422_UYVY
;
375 struct v4l2_subdev
*subdev
;
376 struct i2c_board_info ov2640_info
= {
378 .flags
= I2C_CLIENT_SCCB
,
379 .addr
= client
->addr
,
381 struct v4l2_subdev_format format
= {
382 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
386 * FIXME: sensor supports resolutions up to 1600x1200, but
387 * resolution setting/switching needs to be modified to
388 * - switch sensor output resolution (including further
389 * configuration changes)
390 * - adjust bridge xclk
391 * - disable 16 bit (12 bit) output formats on high resolutions
393 v4l2
->sensor_xres
= 640;
394 v4l2
->sensor_yres
= 480;
397 v4l2_i2c_new_subdev_board(&v4l2
->v4l2_dev
, adap
,
402 format
.format
.code
= MEDIA_BUS_FMT_YUYV8_2X8
;
403 format
.format
.width
= 640;
404 format
.format
.height
= 480;
405 v4l2_subdev_call(subdev
, pad
, set_fmt
, NULL
, &format
);
407 /* NOTE: for UXGA=1600x1200 switch to 12MHz */
408 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_24MHZ
;
409 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
410 v4l2
->vinmode
= EM28XX_VINMODE_YUV422_YUYV
;
415 case EM28XX_NOSENSOR
:
422 EXPORT_SYMBOL_GPL(em28xx_init_camera
);