blob: 12d4c0326e3168bd025fba7fa82db693fa74053b [file] [log] [blame]
Frank Schaefer855ff382013-03-27 17:06:31 -03001/*
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>
Frank Schaefere4b71312013-03-27 17:06:36 -030023#include <media/soc_camera.h>
Frank Schaefer855ff382013-03-27 17:06:31 -030024#include <media/mt9v011.h>
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -030025#include <media/v4l2-clk.h>
Frank Schaefer855ff382013-03-27 17:06:31 -030026#include <media/v4l2-common.h>
27
28#include "em28xx.h"
29
30
Frank Schaefer176013b2013-03-27 17:06:32 -030031/* Possible i2c addresses of Micron sensors */
32static 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
Frank Schaeferbde03682013-03-27 17:06:34 -030039/* Possible i2c addresses of Omnivision sensors */
40static 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
Frank Schaefer176013b2013-03-27 17:06:32 -030046
Frank Schaefere4b71312013-03-27 17:06:36 -030047static struct soc_camera_link camlink = {
48 .bus_id = 0,
49 .flags = 0,
50 .module_name = "em28xx",
Guennadi Liakhovetskibe353fa2013-10-21 06:28:06 -030051 .unbalanced_power = true,
Frank Schaefere4b71312013-03-27 17:06:36 -030052};
53
54
Frank Schaefer855ff382013-03-27 17:06:31 -030055/* FIXME: Should be replaced by a proper mt9m111 driver */
56static int em28xx_initialize_mt9m111(struct em28xx *dev)
57{
58 int i;
59 unsigned char regs[][3] = {
60 { 0x0d, 0x00, 0x01, }, /* reset and use defaults */
61 { 0x0d, 0x00, 0x00, },
62 { 0x0a, 0x00, 0x21, },
63 { 0x21, 0x04, 0x00, }, /* full readout speed, no row/col skipping */
64 };
65
66 for (i = 0; i < ARRAY_SIZE(regs); i++)
67 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
68 &regs[i][0], 3);
69
70 return 0;
71}
72
73
74/* FIXME: Should be replaced by a proper mt9m001 driver */
75static int em28xx_initialize_mt9m001(struct em28xx *dev)
76{
77 int i;
78 unsigned char regs[][3] = {
79 { 0x0d, 0x00, 0x01, },
80 { 0x0d, 0x00, 0x00, },
81 { 0x04, 0x05, 0x00, }, /* hres = 1280 */
82 { 0x03, 0x04, 0x00, }, /* vres = 1024 */
83 { 0x20, 0x11, 0x00, },
84 { 0x06, 0x00, 0x10, },
85 { 0x2b, 0x00, 0x24, },
86 { 0x2e, 0x00, 0x24, },
87 { 0x35, 0x00, 0x24, },
88 { 0x2d, 0x00, 0x20, },
89 { 0x2c, 0x00, 0x20, },
90 { 0x09, 0x0a, 0xd4, },
91 { 0x35, 0x00, 0x57, },
92 };
93
94 for (i = 0; i < ARRAY_SIZE(regs); i++)
95 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
96 &regs[i][0], 3);
97
98 return 0;
99}
100
101
102/*
Frank Schaefer0af0b252013-03-27 17:06:33 -0300103 * Probes Micron sensors with 8 bit address and 16 bit register width
Frank Schaefer855ff382013-03-27 17:06:31 -0300104 */
Frank Schaefer0af0b252013-03-27 17:06:33 -0300105static int em28xx_probe_sensor_micron(struct em28xx *dev)
Frank Schaefer855ff382013-03-27 17:06:31 -0300106{
Frank Schaefer176013b2013-03-27 17:06:32 -0300107 int ret, i;
Frank Schaefer855ff382013-03-27 17:06:31 -0300108 char *name;
109 u8 reg;
110 __be16 id_be;
111 u16 id;
112
Frank Schaefer176013b2013-03-27 17:06:32 -0300113 struct i2c_client client = dev->i2c_client[dev->def_i2c_bus];
Frank Schaefer855ff382013-03-27 17:06:31 -0300114
Frank Schaefer176013b2013-03-27 17:06:32 -0300115 dev->em28xx_sensor = EM28XX_NOSENSOR;
Frank Schaefer176013b2013-03-27 17:06:32 -0300116 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) {
Frank Schaefercb497c72014-01-17 14:45:31 -0300123 if (ret != -ENXIO)
Frank Schaefer176013b2013-03-27 17:06:32 -0300124 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
125 client.addr << 1, ret);
126 continue;
127 }
128 ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
129 if (ret < 0) {
130 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
131 client.addr << 1, ret);
132 continue;
133 }
134 id = be16_to_cpu(id_be);
135 /* Read chip ID from register 0xff */
136 reg = 0xff;
137 ret = i2c_master_send(&client, &reg, 1);
138 if (ret < 0) {
139 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
140 client.addr << 1, ret);
141 continue;
142 }
143 ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
144 if (ret < 0) {
145 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
146 client.addr << 1, ret);
147 continue;
148 }
149 /* Validate chip ID to be sure we have a Micron device */
150 if (id != be16_to_cpu(id_be))
151 continue;
152 /* Check chip ID */
153 id = be16_to_cpu(id_be);
154 switch (id) {
155 case 0x1222:
156 name = "MT9V012"; /* MI370 */ /* 640x480 */
157 break;
158 case 0x1229:
159 name = "MT9V112"; /* 640x480 */
160 break;
161 case 0x1433:
162 name = "MT9M011"; /* 1280x1024 */
163 break;
164 case 0x143a: /* found in the ECS G200 */
165 name = "MT9M111"; /* MI1310 */ /* 1280x1024 */
166 dev->em28xx_sensor = EM28XX_MT9M111;
167 break;
168 case 0x148c:
169 name = "MT9M112"; /* MI1320 */ /* 1280x1024 */
170 break;
171 case 0x1511:
172 name = "MT9D011"; /* MI2010 */ /* 1600x1200 */
173 break;
174 case 0x8232:
175 case 0x8243: /* rev B */
176 name = "MT9V011"; /* MI360 */ /* 640x480 */
177 dev->em28xx_sensor = EM28XX_MT9V011;
178 break;
179 case 0x8431:
180 name = "MT9M001"; /* 1280x1024 */
181 dev->em28xx_sensor = EM28XX_MT9M001;
182 break;
183 default:
184 em28xx_info("unknown Micron sensor detected: 0x%04x\n",
185 id);
Frank Schaefer0af0b252013-03-27 17:06:33 -0300186 return 0;
Frank Schaefer176013b2013-03-27 17:06:32 -0300187 }
188
189 if (dev->em28xx_sensor == EM28XX_NOSENSOR)
190 em28xx_info("unsupported sensor detected: %s\n", name);
191 else
192 em28xx_info("sensor %s detected\n", name);
193
194 dev->i2c_client[dev->def_i2c_bus].addr = client.addr;
195 return 0;
Frank Schaefer855ff382013-03-27 17:06:31 -0300196 }
197
Frank Schaefer176013b2013-03-27 17:06:32 -0300198 return -ENODEV;
Frank Schaefer855ff382013-03-27 17:06:31 -0300199}
200
Frank Schaefer0af0b252013-03-27 17:06:33 -0300201/*
Frank Schaeferbde03682013-03-27 17:06:34 -0300202 * Probes Omnivision sensors with 8 bit address and register width
Frank Schaefer0af0b252013-03-27 17:06:33 -0300203 */
Frank Schaeferbde03682013-03-27 17:06:34 -0300204static int em28xx_probe_sensor_omnivision(struct em28xx *dev)
205{
206 int ret, i;
207 char *name;
208 u8 reg;
209 u16 id;
210 struct i2c_client client = dev->i2c_client[dev->def_i2c_bus];
211
212 dev->em28xx_sensor = EM28XX_NOSENSOR;
213 /* NOTE: these devices have the register auto incrementation disabled
214 * by default, so we have to use single byte reads ! */
215 for (i = 0; omnivision_sensor_addrs[i] != I2C_CLIENT_END; i++) {
216 client.addr = omnivision_sensor_addrs[i];
217 /* Read manufacturer ID from registers 0x1c-0x1d (BE) */
218 reg = 0x1c;
219 ret = i2c_smbus_read_byte_data(&client, reg);
220 if (ret < 0) {
Frank Schaefercb497c72014-01-17 14:45:31 -0300221 if (ret != -ENXIO)
Frank Schaeferbde03682013-03-27 17:06:34 -0300222 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
223 client.addr << 1, ret);
224 continue;
225 }
226 id = ret << 8;
227 reg = 0x1d;
228 ret = i2c_smbus_read_byte_data(&client, reg);
229 if (ret < 0) {
230 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
231 client.addr << 1, ret);
232 continue;
233 }
234 id += ret;
235 /* Check manufacturer ID */
236 if (id != 0x7fa2)
237 continue;
238 /* Read product ID from registers 0x0a-0x0b (BE) */
239 reg = 0x0a;
240 ret = i2c_smbus_read_byte_data(&client, reg);
241 if (ret < 0) {
242 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
243 client.addr << 1, ret);
244 continue;
245 }
246 id = ret << 8;
247 reg = 0x0b;
248 ret = i2c_smbus_read_byte_data(&client, reg);
249 if (ret < 0) {
250 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
251 client.addr << 1, ret);
252 continue;
253 }
254 id += ret;
255 /* Check product ID */
256 switch (id) {
257 case 0x2642:
258 name = "OV2640";
Frank Schaefere4b71312013-03-27 17:06:36 -0300259 dev->em28xx_sensor = EM28XX_OV2640;
Frank Schaeferbde03682013-03-27 17:06:34 -0300260 break;
261 case 0x7648:
262 name = "OV7648";
263 break;
264 case 0x7660:
265 name = "OV7660";
266 break;
267 case 0x7673:
268 name = "OV7670";
269 break;
270 case 0x7720:
271 name = "OV7720";
272 break;
273 case 0x7721:
274 name = "OV7725";
275 break;
276 case 0x9648: /* Rev 2 */
277 case 0x9649: /* Rev 3 */
278 name = "OV9640";
279 break;
280 case 0x9650:
281 case 0x9652: /* OV9653 */
282 name = "OV9650";
283 break;
284 case 0x9656: /* Rev 4 */
285 case 0x9657: /* Rev 5 */
286 name = "OV9655";
287 break;
288 default:
289 em28xx_info("unknown OmniVision sensor detected: 0x%04x\n",
290 id);
291 return 0;
292 }
293
294 if (dev->em28xx_sensor == EM28XX_NOSENSOR)
295 em28xx_info("unsupported sensor detected: %s\n", name);
296 else
297 em28xx_info("sensor %s detected\n", name);
298
299 dev->i2c_client[dev->def_i2c_bus].addr = client.addr;
300 return 0;
301 }
302
303 return -ENODEV;
304}
305
Frank Schaefer0af0b252013-03-27 17:06:33 -0300306int em28xx_detect_sensor(struct em28xx *dev)
307{
308 int ret;
309
310 ret = em28xx_probe_sensor_micron(dev);
Frank Schaeferbde03682013-03-27 17:06:34 -0300311
312 if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0)
313 ret = em28xx_probe_sensor_omnivision(dev);
314
Frank Schaefer52f1f232013-03-27 17:06:35 -0300315 /*
316 * NOTE: the Windows driver also probes i2c addresses
317 * 0x22 (Samsung ?) and 0x66 (Kodak ?)
318 */
319
Frank Schaefer0af0b252013-03-27 17:06:33 -0300320 if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) {
321 em28xx_info("No sensor detected\n");
322 return -ENODEV;
323 }
324
325 return 0;
326}
327
Frank Schaefer855ff382013-03-27 17:06:31 -0300328int em28xx_init_camera(struct em28xx *dev)
329{
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300330 char clk_name[V4L2_SUBDEV_NAME_SIZE];
331 struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
332 struct i2c_adapter *adap = &dev->i2c_adap[dev->def_i2c_bus];
Frank Schaefer2c52a2f2014-03-24 16:33:11 -0300333 struct em28xx_v4l2 *v4l2 = dev->v4l2;
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300334 int ret = 0;
335
336 v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
337 i2c_adapter_id(adap), client->addr);
Frank Schaefer2c52a2f2014-03-24 16:33:11 -0300338 v4l2->clk = v4l2_clk_register_fixed(clk_name, "mclk", -EINVAL);
339 if (IS_ERR(v4l2->clk))
340 return PTR_ERR(v4l2->clk);
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300341
Frank Schaefer855ff382013-03-27 17:06:31 -0300342 switch (dev->em28xx_sensor) {
343 case EM28XX_MT9V011:
344 {
345 struct mt9v011_platform_data pdata;
346 struct i2c_board_info mt9v011_info = {
347 .type = "mt9v011",
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300348 .addr = client->addr,
Frank Schaefer855ff382013-03-27 17:06:31 -0300349 .platform_data = &pdata,
350 };
351
Frank Schaeferd7dc18d2014-03-24 16:33:19 -0300352 v4l2->sensor_xres = 640;
353 v4l2->sensor_yres = 480;
Frank Schaefer855ff382013-03-27 17:06:31 -0300354
355 /*
356 * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
357 * the Silvercrest cam I have here for testing - for higher
358 * resolutions, a high clock cause horizontal artifacts, so we
359 * need to use a lower xclk frequency.
360 * Yet, it would be possible to adjust xclk depending on the
361 * desired resolution, since this affects directly the
362 * frame rate.
363 */
364 dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ;
365 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
Frank Schaeferd7dc18d2014-03-24 16:33:19 -0300366 v4l2->sensor_xtal = 4300000;
367 pdata.xtal = v4l2->sensor_xtal;
Frank Schaefer855ff382013-03-27 17:06:31 -0300368 if (NULL ==
Frank Schaefer95d26082014-03-24 16:33:09 -0300369 v4l2_i2c_new_subdev_board(&dev->v4l2->v4l2_dev, adap,
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300370 &mt9v011_info, NULL)) {
371 ret = -ENODEV;
372 break;
373 }
Frank Schaefer855ff382013-03-27 17:06:31 -0300374 /* probably means GRGB 16 bit bayer */
Frank Schaefer92972852014-03-24 16:33:15 -0300375 v4l2->vinmode = 0x0d;
376 v4l2->vinctl = 0x00;
Frank Schaefer855ff382013-03-27 17:06:31 -0300377
378 break;
379 }
380 case EM28XX_MT9M001:
Frank Schaeferd7dc18d2014-03-24 16:33:19 -0300381 v4l2->sensor_xres = 1280;
382 v4l2->sensor_yres = 1024;
Frank Schaefer855ff382013-03-27 17:06:31 -0300383
384 em28xx_initialize_mt9m001(dev);
385
386 /* probably means BGGR 16 bit bayer */
Frank Schaefer92972852014-03-24 16:33:15 -0300387 v4l2->vinmode = 0x0c;
388 v4l2->vinctl = 0x00;
Frank Schaefer855ff382013-03-27 17:06:31 -0300389
390 break;
391 case EM28XX_MT9M111:
Frank Schaeferd7dc18d2014-03-24 16:33:19 -0300392 v4l2->sensor_xres = 640;
393 v4l2->sensor_yres = 512;
Frank Schaefer855ff382013-03-27 17:06:31 -0300394
395 dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ;
396 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
397 em28xx_initialize_mt9m111(dev);
398
Frank Schaefer92972852014-03-24 16:33:15 -0300399 v4l2->vinmode = 0x0a;
400 v4l2->vinctl = 0x00;
Frank Schaefer855ff382013-03-27 17:06:31 -0300401
402 break;
Frank Schaefere4b71312013-03-27 17:06:36 -0300403 case EM28XX_OV2640:
404 {
405 struct v4l2_subdev *subdev;
406 struct i2c_board_info ov2640_info = {
407 .type = "ov2640",
408 .flags = I2C_CLIENT_SCCB,
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300409 .addr = client->addr,
Frank Schaefere4b71312013-03-27 17:06:36 -0300410 .platform_data = &camlink,
411 };
412 struct v4l2_mbus_framefmt fmt;
413
414 /*
415 * FIXME: sensor supports resolutions up to 1600x1200, but
416 * resolution setting/switching needs to be modified to
417 * - switch sensor output resolution (including further
418 * configuration changes)
419 * - adjust bridge xclk
420 * - disable 16 bit (12 bit) output formats on high resolutions
421 */
Frank Schaeferd7dc18d2014-03-24 16:33:19 -0300422 v4l2->sensor_xres = 640;
423 v4l2->sensor_yres = 480;
Frank Schaefere4b71312013-03-27 17:06:36 -0300424
425 subdev =
Frank Schaefer95d26082014-03-24 16:33:09 -0300426 v4l2_i2c_new_subdev_board(&dev->v4l2->v4l2_dev, adap,
Frank Schaefere4b71312013-03-27 17:06:36 -0300427 &ov2640_info, NULL);
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300428 if (NULL == subdev) {
429 ret = -ENODEV;
430 break;
431 }
Frank Schaefere4b71312013-03-27 17:06:36 -0300432
433 fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
434 fmt.width = 640;
435 fmt.height = 480;
436 v4l2_subdev_call(subdev, video, s_mbus_fmt, &fmt);
437
438 /* NOTE: for UXGA=1600x1200 switch to 12MHz */
439 dev->board.xclk = EM28XX_XCLK_FREQUENCY_24MHZ;
440 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
Frank Schaefer92972852014-03-24 16:33:15 -0300441 v4l2->vinmode = 0x08;
442 v4l2->vinctl = 0x00;
Frank Schaefere4b71312013-03-27 17:06:36 -0300443
444 break;
445 }
Frank Schaefer855ff382013-03-27 17:06:31 -0300446 case EM28XX_NOSENSOR:
447 default:
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300448 ret = -EINVAL;
Frank Schaefer855ff382013-03-27 17:06:31 -0300449 }
450
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300451 if (ret < 0) {
Frank Schaefer2c52a2f2014-03-24 16:33:11 -0300452 v4l2_clk_unregister_fixed(v4l2->clk);
453 v4l2->clk = NULL;
Guennadi Liakhovetskifc5d0f82013-08-28 10:28:28 -0300454 }
455
456 return ret;
Frank Schaefer855ff382013-03-27 17:06:31 -0300457}
Mauro Carvalho Chehab01c28192013-12-22 13:27:02 -0300458EXPORT_SYMBOL_GPL(em28xx_init_camera);