xref: /linux/drivers/media/usb/em28xx/em28xx-camera.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2    em28xx-camera.c - driver for Empia EM25xx/27xx/28xx USB video capture devices
3 
4    Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@infradead.org>
5    Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
6 
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.
11 
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.
16 
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.
20 */
21 
22 #include "em28xx.h"
23 
24 #include <linux/i2c.h>
25 #include <linux/usb.h>
26 #include <media/soc_camera.h>
27 #include <media/i2c/mt9v011.h>
28 #include <media/v4l2-clk.h>
29 #include <media/v4l2-common.h>
30 
31 /* Possible i2c addresses of Micron sensors */
32 static unsigned short micron_sensor_addrs[] = {
33 	0xb8 >> 1,   /* MT9V111, MT9V403 */
34 	0xba >> 1,   /* MT9M001/011/111/112, MT9V011/012/112, MT9D011 */
35 	0x90 >> 1,   /* MT9V012/112, MT9D011 (alternative address) */
36 	I2C_CLIENT_END
37 };
38 
39 /* Possible i2c addresses of Omnivision sensors */
40 static unsigned short omnivision_sensor_addrs[] = {
41 	0x42 >> 1,   /* OV7725, OV7670/60/48 */
42 	0x60 >> 1,   /* OV2640, OV9650/53/55 */
43 	I2C_CLIENT_END
44 };
45 
46 static struct soc_camera_link camlink = {
47 	.bus_id = 0,
48 	.flags = 0,
49 	.module_name = "em28xx",
50 	.unbalanced_power = true,
51 };
52 
53 /* FIXME: Should be replaced by a proper mt9m111 driver */
54 static int em28xx_initialize_mt9m111(struct em28xx *dev)
55 {
56 	int i;
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 */
62 	};
63 
64 	for (i = 0; i < ARRAY_SIZE(regs); i++)
65 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
66 				&regs[i][0], 3);
67 
68 	/* FIXME: This won't be creating a sensor at the media graph */
69 
70 	return 0;
71 }
72 
73 /* FIXME: Should be replaced by a proper mt9m001 driver */
74 static int em28xx_initialize_mt9m001(struct em28xx *dev)
75 {
76 	int i;
77 	unsigned char regs[][3] = {
78 		{ 0x0d, 0x00, 0x01, },
79 		{ 0x0d, 0x00, 0x00, },
80 		{ 0x04, 0x05, 0x00, },	/* hres = 1280 */
81 		{ 0x03, 0x04, 0x00, },  /* vres = 1024 */
82 		{ 0x20, 0x11, 0x00, },
83 		{ 0x06, 0x00, 0x10, },
84 		{ 0x2b, 0x00, 0x24, },
85 		{ 0x2e, 0x00, 0x24, },
86 		{ 0x35, 0x00, 0x24, },
87 		{ 0x2d, 0x00, 0x20, },
88 		{ 0x2c, 0x00, 0x20, },
89 		{ 0x09, 0x0a, 0xd4, },
90 		{ 0x35, 0x00, 0x57, },
91 	};
92 
93 	for (i = 0; i < ARRAY_SIZE(regs); i++)
94 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
95 				&regs[i][0], 3);
96 
97 	/* FIXME: This won't be creating a sensor at the media graph */
98 
99 	return 0;
100 }
101 
102 /*
103  * Probes Micron sensors with 8 bit address and 16 bit register width
104  */
105 static int em28xx_probe_sensor_micron(struct em28xx *dev)
106 {
107 	int ret, i;
108 	char *name;
109 	u8 reg;
110 	__be16 id_be;
111 	u16 id;
112 
113 	struct i2c_client client = dev->i2c_client[dev->def_i2c_bus];
114 
115 	dev->em28xx_sensor = EM28XX_NOSENSOR;
116 	for (i = 0; micron_sensor_addrs[i] != I2C_CLIENT_END; i++) {
117 		client.addr = micron_sensor_addrs[i];
118 		/* NOTE: i2c_smbus_read_word_data() doesn't work with BE data */
119 		/* Read chip ID from register 0x00 */
120 		reg = 0x00;
121 		ret = i2c_master_send(&client, &reg, 1);
122 		if (ret < 0) {
123 			if (ret != -ENXIO)
124 				dev_err(&dev->intf->dev,
125 					"couldn't read from i2c device 0x%02x: error %i\n",
126 				       client.addr << 1, ret);
127 			continue;
128 		}
129 		ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
130 		if (ret < 0) {
131 			dev_err(&dev->intf->dev,
132 				"couldn't read from i2c device 0x%02x: error %i\n",
133 				client.addr << 1, ret);
134 			continue;
135 		}
136 		id = be16_to_cpu(id_be);
137 		/* Read chip ID from register 0xff */
138 		reg = 0xff;
139 		ret = i2c_master_send(&client, &reg, 1);
140 		if (ret < 0) {
141 			dev_err(&dev->intf->dev,
142 				"couldn't read from i2c device 0x%02x: error %i\n",
143 				client.addr << 1, ret);
144 			continue;
145 		}
146 		ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
147 		if (ret < 0) {
148 			dev_err(&dev->intf->dev,
149 				"couldn't read from i2c device 0x%02x: error %i\n",
150 				client.addr << 1, ret);
151 			continue;
152 		}
153 		/* Validate chip ID to be sure we have a Micron device */
154 		if (id != be16_to_cpu(id_be))
155 			continue;
156 		/* Check chip ID */
157 		id = be16_to_cpu(id_be);
158 		switch (id) {
159 		case 0x1222:
160 			name = "MT9V012"; /* MI370 */ /* 640x480 */
161 			break;
162 		case 0x1229:
163 			name = "MT9V112"; /* 640x480 */
164 			break;
165 		case 0x1433:
166 			name = "MT9M011"; /* 1280x1024 */
167 			break;
168 		case 0x143a:    /* found in the ECS G200 */
169 			name = "MT9M111"; /* MI1310 */ /* 1280x1024 */
170 			dev->em28xx_sensor = EM28XX_MT9M111;
171 			break;
172 		case 0x148c:
173 			name = "MT9M112"; /* MI1320 */ /* 1280x1024 */
174 			break;
175 		case 0x1511:
176 			name = "MT9D011"; /* MI2010 */ /* 1600x1200 */
177 			break;
178 		case 0x8232:
179 		case 0x8243:	/* rev B */
180 			name = "MT9V011"; /* MI360 */ /* 640x480 */
181 			dev->em28xx_sensor = EM28XX_MT9V011;
182 			break;
183 		case 0x8431:
184 			name = "MT9M001"; /* 1280x1024 */
185 			dev->em28xx_sensor = EM28XX_MT9M001;
186 			break;
187 		default:
188 			dev_info(&dev->intf->dev,
189 				 "unknown Micron sensor detected: 0x%04x\n", id);
190 			return 0;
191 		}
192 
193 		if (dev->em28xx_sensor == EM28XX_NOSENSOR)
194 			dev_info(&dev->intf->dev,
195 				 "unsupported sensor detected: %s\n", name);
196 		else
197 			dev_info(&dev->intf->dev,
198 				 "sensor %s detected\n", name);
199 
200 		dev->i2c_client[dev->def_i2c_bus].addr = client.addr;
201 		return 0;
202 	}
203 
204 	return -ENODEV;
205 }
206 
207 /*
208  * Probes Omnivision sensors with 8 bit address and register width
209  */
210 static int em28xx_probe_sensor_omnivision(struct em28xx *dev)
211 {
212 	int ret, i;
213 	char *name;
214 	u8 reg;
215 	u16 id;
216 	struct i2c_client client = dev->i2c_client[dev->def_i2c_bus];
217 
218 	dev->em28xx_sensor = EM28XX_NOSENSOR;
219 	/* NOTE: these devices have the register auto incrementation disabled
220 	 * by default, so we have to use single byte reads !              */
221 	for (i = 0; omnivision_sensor_addrs[i] != I2C_CLIENT_END; i++) {
222 		client.addr = omnivision_sensor_addrs[i];
223 		/* Read manufacturer ID from registers 0x1c-0x1d (BE) */
224 		reg = 0x1c;
225 		ret = i2c_smbus_read_byte_data(&client, reg);
226 		if (ret < 0) {
227 			if (ret != -ENXIO)
228 				dev_err(&dev->intf->dev,
229 					"couldn't read from i2c device 0x%02x: error %i\n",
230 					client.addr << 1, ret);
231 			continue;
232 		}
233 		id = ret << 8;
234 		reg = 0x1d;
235 		ret = i2c_smbus_read_byte_data(&client, reg);
236 		if (ret < 0) {
237 			dev_err(&dev->intf->dev,
238 				"couldn't read from i2c device 0x%02x: error %i\n",
239 				client.addr << 1, ret);
240 			continue;
241 		}
242 		id += ret;
243 		/* Check manufacturer ID */
244 		if (id != 0x7fa2)
245 			continue;
246 		/* Read product ID from registers 0x0a-0x0b (BE) */
247 		reg = 0x0a;
248 		ret = i2c_smbus_read_byte_data(&client, reg);
249 		if (ret < 0) {
250 			dev_err(&dev->intf->dev,
251 				"couldn't read from i2c device 0x%02x: error %i\n",
252 				client.addr << 1, ret);
253 			continue;
254 		}
255 		id = ret << 8;
256 		reg = 0x0b;
257 		ret = i2c_smbus_read_byte_data(&client, reg);
258 		if (ret < 0) {
259 			dev_err(&dev->intf->dev,
260 				"couldn't read from i2c device 0x%02x: error %i\n",
261 				client.addr << 1, ret);
262 			continue;
263 		}
264 		id += ret;
265 		/* Check product ID */
266 		switch (id) {
267 		case 0x2642:
268 			name = "OV2640";
269 			dev->em28xx_sensor = EM28XX_OV2640;
270 			break;
271 		case 0x7648:
272 			name = "OV7648";
273 			break;
274 		case 0x7660:
275 			name = "OV7660";
276 			break;
277 		case 0x7673:
278 			name = "OV7670";
279 			break;
280 		case 0x7720:
281 			name = "OV7720";
282 			break;
283 		case 0x7721:
284 			name = "OV7725";
285 			break;
286 		case 0x9648: /* Rev 2 */
287 		case 0x9649: /* Rev 3 */
288 			name = "OV9640";
289 			break;
290 		case 0x9650:
291 		case 0x9652: /* OV9653 */
292 			name = "OV9650";
293 			break;
294 		case 0x9656: /* Rev 4 */
295 		case 0x9657: /* Rev 5 */
296 			name = "OV9655";
297 			break;
298 		default:
299 			dev_info(&dev->intf->dev,
300 				 "unknown OmniVision sensor detected: 0x%04x\n",
301 				id);
302 			return 0;
303 		}
304 
305 		if (dev->em28xx_sensor == EM28XX_NOSENSOR)
306 			dev_info(&dev->intf->dev,
307 				 "unsupported sensor detected: %s\n", name);
308 		else
309 			dev_info(&dev->intf->dev,
310 				 "sensor %s detected\n", name);
311 
312 		dev->i2c_client[dev->def_i2c_bus].addr = client.addr;
313 		return 0;
314 	}
315 
316 	return -ENODEV;
317 }
318 
319 int em28xx_detect_sensor(struct em28xx *dev)
320 {
321 	int ret;
322 
323 	ret = em28xx_probe_sensor_micron(dev);
324 
325 	if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0)
326 		ret = em28xx_probe_sensor_omnivision(dev);
327 
328 	/*
329 	 * NOTE: the Windows driver also probes i2c addresses
330 	 *       0x22 (Samsung ?) and 0x66 (Kodak ?)
331 	 */
332 
333 	if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) {
334 		dev_info(&dev->intf->dev,
335 			 "No sensor detected\n");
336 		return -ENODEV;
337 	}
338 
339 	return 0;
340 }
341 
342 int em28xx_init_camera(struct em28xx *dev)
343 {
344 	char clk_name[V4L2_CLK_NAME_SIZE];
345 	struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
346 	struct i2c_adapter *adap = &dev->i2c_adap[dev->def_i2c_bus];
347 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
348 	int ret = 0;
349 
350 	v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
351 			  i2c_adapter_id(adap), client->addr);
352 	v4l2->clk = v4l2_clk_register_fixed(clk_name, -EINVAL);
353 	if (IS_ERR(v4l2->clk))
354 		return PTR_ERR(v4l2->clk);
355 
356 	switch (dev->em28xx_sensor) {
357 	case EM28XX_MT9V011:
358 	{
359 		struct mt9v011_platform_data pdata;
360 		struct i2c_board_info mt9v011_info = {
361 			.type = "mt9v011",
362 			.addr = client->addr,
363 			.platform_data = &pdata,
364 		};
365 
366 		v4l2->sensor_xres = 640;
367 		v4l2->sensor_yres = 480;
368 
369 		/*
370 		 * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
371 		 * the Silvercrest cam I have here for testing - for higher
372 		 * resolutions, a high clock cause horizontal artifacts, so we
373 		 * need to use a lower xclk frequency.
374 		 * Yet, it would be possible to adjust xclk depending on the
375 		 * desired resolution, since this affects directly the
376 		 * frame rate.
377 		 */
378 		dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ;
379 		em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
380 		v4l2->sensor_xtal = 4300000;
381 		pdata.xtal = v4l2->sensor_xtal;
382 		if (NULL ==
383 		    v4l2_i2c_new_subdev_board(&v4l2->v4l2_dev, adap,
384 					      &mt9v011_info, NULL)) {
385 			ret = -ENODEV;
386 			break;
387 		}
388 		/* probably means GRGB 16 bit bayer */
389 		v4l2->vinmode = 0x0d;
390 		v4l2->vinctl = 0x00;
391 
392 		break;
393 	}
394 	case EM28XX_MT9M001:
395 		v4l2->sensor_xres = 1280;
396 		v4l2->sensor_yres = 1024;
397 
398 		em28xx_initialize_mt9m001(dev);
399 
400 		/* probably means BGGR 16 bit bayer */
401 		v4l2->vinmode = 0x0c;
402 		v4l2->vinctl = 0x00;
403 
404 		break;
405 	case EM28XX_MT9M111:
406 		v4l2->sensor_xres = 640;
407 		v4l2->sensor_yres = 512;
408 
409 		dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ;
410 		em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
411 		em28xx_initialize_mt9m111(dev);
412 
413 		v4l2->vinmode = 0x0a;
414 		v4l2->vinctl = 0x00;
415 
416 		break;
417 	case EM28XX_OV2640:
418 	{
419 		struct v4l2_subdev *subdev;
420 		struct i2c_board_info ov2640_info = {
421 			.type = "ov2640",
422 			.flags = I2C_CLIENT_SCCB,
423 			.addr = client->addr,
424 			.platform_data = &camlink,
425 		};
426 		struct v4l2_subdev_format format = {
427 			.which = V4L2_SUBDEV_FORMAT_ACTIVE,
428 		};
429 
430 		/*
431 		 * FIXME: sensor supports resolutions up to 1600x1200, but
432 		 * resolution setting/switching needs to be modified to
433 		 * - switch sensor output resolution (including further
434 		 *   configuration changes)
435 		 * - adjust bridge xclk
436 		 * - disable 16 bit (12 bit) output formats on high resolutions
437 		 */
438 		v4l2->sensor_xres = 640;
439 		v4l2->sensor_yres = 480;
440 
441 		subdev =
442 		     v4l2_i2c_new_subdev_board(&v4l2->v4l2_dev, adap,
443 					       &ov2640_info, NULL);
444 		if (NULL == subdev) {
445 			ret = -ENODEV;
446 			break;
447 		}
448 
449 		format.format.code = MEDIA_BUS_FMT_YUYV8_2X8;
450 		format.format.width = 640;
451 		format.format.height = 480;
452 		v4l2_subdev_call(subdev, pad, set_fmt, NULL, &format);
453 
454 		/* NOTE: for UXGA=1600x1200 switch to 12MHz */
455 		dev->board.xclk = EM28XX_XCLK_FREQUENCY_24MHZ;
456 		em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
457 		v4l2->vinmode = 0x08;
458 		v4l2->vinctl = 0x00;
459 
460 		break;
461 	}
462 	case EM28XX_NOSENSOR:
463 	default:
464 		ret = -EINVAL;
465 	}
466 
467 	if (ret < 0) {
468 		v4l2_clk_unregister_fixed(v4l2->clk);
469 		v4l2->clk = NULL;
470 	}
471 
472 	return ret;
473 }
474 EXPORT_SYMBOL_GPL(em28xx_init_camera);
475