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>
10 #include <linux/i2c.h>
11 #include <linux/usb.h>
12 #include <media/i2c/mt9v011.h>
13 #include <media/v4l2-common.h>
15 /* Possible i2c addresses of Micron sensors */
16 static unsigned short micron_sensor_addrs
[] = {
17 0xb8 >> 1, /* MT9V111, MT9V403 */
18 0xba >> 1, /* MT9M001/011/111/112, MT9V011/012/112, MT9D011 */
19 0x90 >> 1, /* MT9V012/112, MT9D011 (alternative address) */
23 /* Possible i2c addresses of Omnivision sensors */
24 static unsigned short omnivision_sensor_addrs
[] = {
25 0x42 >> 1, /* OV7725, OV7670/60/48 */
26 0x60 >> 1, /* OV2640, OV9650/53/55 */
30 /* FIXME: Should be replaced by a proper mt9m111 driver */
31 static int em28xx_initialize_mt9m111(struct em28xx
*dev
)
34 unsigned char regs
[][3] = {
35 { 0x0d, 0x00, 0x01, }, /* reset and use defaults */
36 { 0x0d, 0x00, 0x00, },
37 { 0x0a, 0x00, 0x21, },
38 { 0x21, 0x04, 0x00, }, /* full readout spd, no row/col skip */
41 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
42 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
45 /* FIXME: This won't be creating a sensor at the media graph */
50 /* FIXME: Should be replaced by a proper mt9m001 driver */
51 static int em28xx_initialize_mt9m001(struct em28xx
*dev
)
54 unsigned char regs
[][3] = {
55 { 0x0d, 0x00, 0x01, },
56 { 0x0d, 0x00, 0x00, },
57 { 0x04, 0x05, 0x00, }, /* hres = 1280 */
58 { 0x03, 0x04, 0x00, }, /* vres = 1024 */
59 { 0x20, 0x11, 0x00, },
60 { 0x06, 0x00, 0x10, },
61 { 0x2b, 0x00, 0x24, },
62 { 0x2e, 0x00, 0x24, },
63 { 0x35, 0x00, 0x24, },
64 { 0x2d, 0x00, 0x20, },
65 { 0x2c, 0x00, 0x20, },
66 { 0x09, 0x0a, 0xd4, },
67 { 0x35, 0x00, 0x57, },
70 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++)
71 i2c_master_send(&dev
->i2c_client
[dev
->def_i2c_bus
],
74 /* FIXME: This won't be creating a sensor at the media graph */
80 * Probes Micron sensors with 8 bit address and 16 bit register width
82 static int em28xx_probe_sensor_micron(struct em28xx
*dev
)
88 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
90 dev
->em28xx_sensor
= EM28XX_NOSENSOR
;
91 for (i
= 0; micron_sensor_addrs
[i
] != I2C_CLIENT_END
; i
++) {
92 client
->addr
= micron_sensor_addrs
[i
];
93 /* Read chip ID from register 0x00 */
94 ret
= i2c_smbus_read_word_data(client
, 0x00); /* assumes LE */
97 dev_err(&dev
->intf
->dev
,
98 "couldn't read from i2c device 0x%02x: error %i\n",
99 client
->addr
<< 1, ret
);
102 id
= swab16(ret
); /* LE -> BE */
103 /* Read chip ID from register 0xff */
104 ret
= i2c_smbus_read_word_data(client
, 0xff);
106 dev_err(&dev
->intf
->dev
,
107 "couldn't read from i2c device 0x%02x: error %i\n",
108 client
->addr
<< 1, ret
);
111 /* Validate chip ID to be sure we have a Micron device */
112 if (id
!= swab16(ret
))
117 name
= "MT9V012"; /* MI370 */ /* 640x480 */
120 name
= "MT9V112"; /* 640x480 */
123 name
= "MT9M011"; /* 1280x1024 */
125 case 0x143a: /* found in the ECS G200 */
126 name
= "MT9M111"; /* MI1310 */ /* 1280x1024 */
127 dev
->em28xx_sensor
= EM28XX_MT9M111
;
130 name
= "MT9M112"; /* MI1320 */ /* 1280x1024 */
133 name
= "MT9D011"; /* MI2010 */ /* 1600x1200 */
136 case 0x8243: /* rev B */
137 name
= "MT9V011"; /* MI360 */ /* 640x480 */
138 dev
->em28xx_sensor
= EM28XX_MT9V011
;
141 name
= "MT9M001"; /* 1280x1024 */
142 dev
->em28xx_sensor
= EM28XX_MT9M001
;
145 dev_info(&dev
->intf
->dev
,
146 "unknown Micron sensor detected: 0x%04x\n",
151 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
152 dev_info(&dev
->intf
->dev
,
153 "unsupported sensor detected: %s\n", name
);
155 dev_info(&dev
->intf
->dev
,
156 "sensor %s detected\n", name
);
165 * Probes Omnivision sensors with 8 bit address and register width
167 static int em28xx_probe_sensor_omnivision(struct em28xx
*dev
)
173 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
175 dev
->em28xx_sensor
= EM28XX_NOSENSOR
;
177 * NOTE: these devices have the register auto incrementation disabled
178 * by default, so we have to use single byte reads !
180 for (i
= 0; omnivision_sensor_addrs
[i
] != I2C_CLIENT_END
; i
++) {
181 client
->addr
= omnivision_sensor_addrs
[i
];
182 /* Read manufacturer ID from registers 0x1c-0x1d (BE) */
184 ret
= i2c_smbus_read_byte_data(client
, reg
);
187 dev_err(&dev
->intf
->dev
,
188 "couldn't read from i2c device 0x%02x: error %i\n",
189 client
->addr
<< 1, ret
);
194 ret
= i2c_smbus_read_byte_data(client
, reg
);
196 dev_err(&dev
->intf
->dev
,
197 "couldn't read from i2c device 0x%02x: error %i\n",
198 client
->addr
<< 1, ret
);
202 /* Check manufacturer ID */
205 /* Read product ID from registers 0x0a-0x0b (BE) */
207 ret
= i2c_smbus_read_byte_data(client
, reg
);
209 dev_err(&dev
->intf
->dev
,
210 "couldn't read from i2c device 0x%02x: error %i\n",
211 client
->addr
<< 1, ret
);
216 ret
= i2c_smbus_read_byte_data(client
, reg
);
218 dev_err(&dev
->intf
->dev
,
219 "couldn't read from i2c device 0x%02x: error %i\n",
220 client
->addr
<< 1, ret
);
224 /* Check product ID */
228 dev
->em28xx_sensor
= EM28XX_OV2640
;
245 case 0x9648: /* Rev 2 */
246 case 0x9649: /* Rev 3 */
250 case 0x9652: /* OV9653 */
253 case 0x9656: /* Rev 4 */
254 case 0x9657: /* Rev 5 */
258 dev_info(&dev
->intf
->dev
,
259 "unknown OmniVision sensor detected: 0x%04x\n",
264 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
)
265 dev_info(&dev
->intf
->dev
,
266 "unsupported sensor detected: %s\n", name
);
268 dev_info(&dev
->intf
->dev
,
269 "sensor %s detected\n", name
);
277 int em28xx_detect_sensor(struct em28xx
*dev
)
281 ret
= em28xx_probe_sensor_micron(dev
);
283 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0)
284 ret
= em28xx_probe_sensor_omnivision(dev
);
287 * NOTE: the Windows driver also probes i2c addresses
288 * 0x22 (Samsung ?) and 0x66 (Kodak ?)
291 if (dev
->em28xx_sensor
== EM28XX_NOSENSOR
&& ret
< 0) {
292 dev_info(&dev
->intf
->dev
,
293 "No sensor detected\n");
300 int em28xx_init_camera(struct em28xx
*dev
)
302 struct i2c_client
*client
= &dev
->i2c_client
[dev
->def_i2c_bus
];
303 struct i2c_adapter
*adap
= &dev
->i2c_adap
[dev
->def_i2c_bus
];
304 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
306 switch (dev
->em28xx_sensor
) {
309 struct mt9v011_platform_data pdata
;
310 struct i2c_board_info mt9v011_info
= {
312 .addr
= client
->addr
,
313 .platform_data
= &pdata
,
316 v4l2
->sensor_xres
= 640;
317 v4l2
->sensor_yres
= 480;
320 * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
321 * the Silvercrest cam I have here for testing - for higher
322 * resolutions, a high clock cause horizontal artifacts, so we
323 * need to use a lower xclk frequency.
324 * Yet, it would be possible to adjust xclk depending on the
325 * desired resolution, since this affects directly the
328 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_4_3MHZ
;
329 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
330 v4l2
->sensor_xtal
= 4300000;
331 pdata
.xtal
= v4l2
->sensor_xtal
;
333 v4l2_i2c_new_subdev_board(&v4l2
->v4l2_dev
, adap
,
334 &mt9v011_info
, NULL
))
336 v4l2
->vinmode
= EM28XX_VINMODE_RGB8_GRBG
;
342 v4l2
->sensor_xres
= 1280;
343 v4l2
->sensor_yres
= 1024;
345 em28xx_initialize_mt9m001(dev
);
347 v4l2
->vinmode
= EM28XX_VINMODE_RGB8_BGGR
;
352 v4l2
->sensor_xres
= 640;
353 v4l2
->sensor_yres
= 512;
355 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_48MHZ
;
356 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
357 em28xx_initialize_mt9m111(dev
);
359 v4l2
->vinmode
= EM28XX_VINMODE_YUV422_UYVY
;
365 struct v4l2_subdev
*subdev
;
366 struct i2c_board_info ov2640_info
= {
368 .flags
= I2C_CLIENT_SCCB
,
369 .addr
= client
->addr
,
371 struct v4l2_subdev_format format
= {
372 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
376 * FIXME: sensor supports resolutions up to 1600x1200, but
377 * resolution setting/switching needs to be modified to
378 * - switch sensor output resolution (including further
379 * configuration changes)
380 * - adjust bridge xclk
381 * - disable 16 bit (12 bit) output formats on high resolutions
383 v4l2
->sensor_xres
= 640;
384 v4l2
->sensor_yres
= 480;
387 v4l2_i2c_new_subdev_board(&v4l2
->v4l2_dev
, adap
,
392 format
.format
.code
= MEDIA_BUS_FMT_YUYV8_2X8
;
393 format
.format
.width
= 640;
394 format
.format
.height
= 480;
395 v4l2_subdev_call(subdev
, pad
, set_fmt
, NULL
, &format
);
397 /* NOTE: for UXGA=1600x1200 switch to 12MHz */
398 dev
->board
.xclk
= EM28XX_XCLK_FREQUENCY_24MHZ
;
399 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, dev
->board
.xclk
);
400 v4l2
->vinmode
= EM28XX_VINMODE_YUV422_YUYV
;
405 case EM28XX_NOSENSOR
:
412 EXPORT_SYMBOL_GPL(em28xx_init_camera
);