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