1// SPDX-License-Identifier: GPL-2.0+
2//
3// em28xx-camera.c - driver for Empia EM25xx/27xx/28xx USB video capture devices
4//
5// Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
6// Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
7
8#include "em28xx.h"
9
10#include <linux/i2c.h>
11#include <linux/usb.h>
12#include <media/i2c/mt9v011.h>
13#include <media/v4l2-common.h>
14
15/* Possible i2c addresses of Micron sensors */
16static 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) */
20 I2C_CLIENT_END
21};
22
23/* Possible i2c addresses of Omnivision sensors */
24static unsigned short omnivision_sensor_addrs[] = {
25 0x42 >> 1, /* OV7725, OV7670/60/48 */
26 0x60 >> 1, /* OV2640, OV9650/53/55 */
27 I2C_CLIENT_END
28};
29
30/* FIXME: Should be replaced by a proper mt9m111 driver */
31static int em28xx_initialize_mt9m111(struct em28xx *dev)
32{
33 int i;
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 */
39 };
40
41 for (i = 0; i < ARRAY_SIZE(regs); i++)
42 i2c_master_send(client: &dev->i2c_client[dev->def_i2c_bus],
43 buf: &regs[i][0], count: 3);
44
45 /* FIXME: This won't be creating a sensor at the media graph */
46
47 return 0;
48}
49
50/* FIXME: Should be replaced by a proper mt9m001 driver */
51static int em28xx_initialize_mt9m001(struct em28xx *dev)
52{
53 int i;
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, },
68 };
69
70 for (i = 0; i < ARRAY_SIZE(regs); i++)
71 i2c_master_send(client: &dev->i2c_client[dev->def_i2c_bus],
72 buf: &regs[i][0], count: 3);
73
74 /* FIXME: This won't be creating a sensor at the media graph */
75
76 return 0;
77}
78
79/*
80 * Probes Micron sensors with 8 bit address and 16 bit register width
81 */
82static int em28xx_probe_sensor_micron(struct em28xx *dev)
83{
84 int ret, i;
85 char *name;
86 u16 id;
87
88 struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
89
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, command: 0x00); /* assumes LE */
95 if (ret < 0) {
96 if (ret != -ENXIO)
97 dev_err(&dev->intf->dev,
98 "couldn't read from i2c device 0x%02x: error %i\n",
99 client->addr << 1, ret);
100 continue;
101 }
102 id = swab16(ret); /* LE -> BE */
103 /* Read chip ID from register 0xff */
104 ret = i2c_smbus_read_word_data(client, command: 0xff);
105 if (ret < 0) {
106 dev_err(&dev->intf->dev,
107 "couldn't read from i2c device 0x%02x: error %i\n",
108 client->addr << 1, ret);
109 continue;
110 }
111 /* Validate chip ID to be sure we have a Micron device */
112 if (id != swab16(ret))
113 continue;
114 /* Check chip ID */
115 switch (id) {
116 case 0x1222:
117 name = "MT9V012"; /* MI370 */ /* 640x480 */
118 break;
119 case 0x1229:
120 name = "MT9V112"; /* 640x480 */
121 break;
122 case 0x1433:
123 name = "MT9M011"; /* 1280x1024 */
124 break;
125 case 0x143a: /* found in the ECS G200 */
126 name = "MT9M111"; /* MI1310 */ /* 1280x1024 */
127 dev->em28xx_sensor = EM28XX_MT9M111;
128 break;
129 case 0x148c:
130 name = "MT9M112"; /* MI1320 */ /* 1280x1024 */
131 break;
132 case 0x1511:
133 name = "MT9D011"; /* MI2010 */ /* 1600x1200 */
134 break;
135 case 0x8232:
136 case 0x8243: /* rev B */
137 name = "MT9V011"; /* MI360 */ /* 640x480 */
138 dev->em28xx_sensor = EM28XX_MT9V011;
139 break;
140 case 0x8431:
141 name = "MT9M001"; /* 1280x1024 */
142 dev->em28xx_sensor = EM28XX_MT9M001;
143 break;
144 default:
145 dev_info(&dev->intf->dev,
146 "unknown Micron sensor detected: 0x%04x\n",
147 id);
148 return 0;
149 }
150
151 if (dev->em28xx_sensor == EM28XX_NOSENSOR)
152 dev_info(&dev->intf->dev,
153 "unsupported sensor detected: %s\n", name);
154 else
155 dev_info(&dev->intf->dev,
156 "sensor %s detected\n", name);
157
158 return 0;
159 }
160
161 return -ENODEV;
162}
163
164/*
165 * Probes Omnivision sensors with 8 bit address and register width
166 */
167static int em28xx_probe_sensor_omnivision(struct em28xx *dev)
168{
169 int ret, i;
170 char *name;
171 u8 reg;
172 u16 id;
173 struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
174
175 dev->em28xx_sensor = EM28XX_NOSENSOR;
176 /*
177 * NOTE: these devices have the register auto incrementation disabled
178 * by default, so we have to use single byte reads !
179 */
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) */
183 reg = 0x1c;
184 ret = i2c_smbus_read_byte_data(client, command: reg);
185 if (ret < 0) {
186 if (ret != -ENXIO)
187 dev_err(&dev->intf->dev,
188 "couldn't read from i2c device 0x%02x: error %i\n",
189 client->addr << 1, ret);
190 continue;
191 }
192 id = ret << 8;
193 reg = 0x1d;
194 ret = i2c_smbus_read_byte_data(client, command: reg);
195 if (ret < 0) {
196 dev_err(&dev->intf->dev,
197 "couldn't read from i2c device 0x%02x: error %i\n",
198 client->addr << 1, ret);
199 continue;
200 }
201 id += ret;
202 /* Check manufacturer ID */
203 if (id != 0x7fa2)
204 continue;
205 /* Read product ID from registers 0x0a-0x0b (BE) */
206 reg = 0x0a;
207 ret = i2c_smbus_read_byte_data(client, command: reg);
208 if (ret < 0) {
209 dev_err(&dev->intf->dev,
210 "couldn't read from i2c device 0x%02x: error %i\n",
211 client->addr << 1, ret);
212 continue;
213 }
214 id = ret << 8;
215 reg = 0x0b;
216 ret = i2c_smbus_read_byte_data(client, command: reg);
217 if (ret < 0) {
218 dev_err(&dev->intf->dev,
219 "couldn't read from i2c device 0x%02x: error %i\n",
220 client->addr << 1, ret);
221 continue;
222 }
223 id += ret;
224 /* Check product ID */
225 switch (id) {
226 case 0x2642:
227 name = "OV2640";
228 dev->em28xx_sensor = EM28XX_OV2640;
229 break;
230 case 0x7648:
231 name = "OV7648";
232 break;
233 case 0x7660:
234 name = "OV7660";
235 break;
236 case 0x7673:
237 name = "OV7670";
238 break;
239 case 0x7720:
240 name = "OV7720";
241 break;
242 case 0x7721:
243 name = "OV7725";
244 break;
245 case 0x9648: /* Rev 2 */
246 case 0x9649: /* Rev 3 */
247 name = "OV9640";
248 break;
249 case 0x9650:
250 case 0x9652: /* OV9653 */
251 name = "OV9650";
252 break;
253 case 0x9656: /* Rev 4 */
254 case 0x9657: /* Rev 5 */
255 name = "OV9655";
256 break;
257 default:
258 dev_info(&dev->intf->dev,
259 "unknown OmniVision sensor detected: 0x%04x\n",
260 id);
261 return 0;
262 }
263
264 if (dev->em28xx_sensor == EM28XX_NOSENSOR)
265 dev_info(&dev->intf->dev,
266 "unsupported sensor detected: %s\n", name);
267 else
268 dev_info(&dev->intf->dev,
269 "sensor %s detected\n", name);
270
271 return 0;
272 }
273
274 return -ENODEV;
275}
276
277int em28xx_detect_sensor(struct em28xx *dev)
278{
279 int ret;
280
281 ret = em28xx_probe_sensor_micron(dev);
282
283 if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0)
284 ret = em28xx_probe_sensor_omnivision(dev);
285
286 /*
287 * NOTE: the Windows driver also probes i2c addresses
288 * 0x22 (Samsung ?) and 0x66 (Kodak ?)
289 */
290
291 if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) {
292 dev_info(&dev->intf->dev,
293 "No sensor detected\n");
294 return -ENODEV;
295 }
296
297 return 0;
298}
299
300int em28xx_init_camera(struct em28xx *dev)
301{
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;
305
306 switch (dev->em28xx_sensor) {
307 case EM28XX_MT9V011:
308 {
309 struct mt9v011_platform_data pdata;
310 struct i2c_board_info mt9v011_info = {
311 .type = "mt9v011",
312 .addr = client->addr,
313 .platform_data = &pdata,
314 };
315
316 v4l2->sensor_xres = 640;
317 v4l2->sensor_yres = 480;
318
319 /*
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
326 * frame rate.
327 */
328 dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ;
329 em28xx_write_reg(dev, EM28XX_R0F_XCLK, val: dev->board.xclk);
330 v4l2->sensor_xtal = 4300000;
331 pdata.xtal = v4l2->sensor_xtal;
332 if (NULL ==
333 v4l2_i2c_new_subdev_board(v4l2_dev: &v4l2->v4l2_dev, adapter: adap,
334 info: &mt9v011_info, NULL))
335 return -ENODEV;
336 v4l2->vinmode = EM28XX_VINMODE_RGB8_GRBG;
337 v4l2->vinctl = 0x00;
338
339 break;
340 }
341 case EM28XX_MT9M001:
342 v4l2->sensor_xres = 1280;
343 v4l2->sensor_yres = 1024;
344
345 em28xx_initialize_mt9m001(dev);
346
347 v4l2->vinmode = EM28XX_VINMODE_RGB8_BGGR;
348 v4l2->vinctl = 0x00;
349
350 break;
351 case EM28XX_MT9M111:
352 v4l2->sensor_xres = 640;
353 v4l2->sensor_yres = 512;
354
355 dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ;
356 em28xx_write_reg(dev, EM28XX_R0F_XCLK, val: dev->board.xclk);
357 em28xx_initialize_mt9m111(dev);
358
359 v4l2->vinmode = EM28XX_VINMODE_YUV422_UYVY;
360 v4l2->vinctl = 0x00;
361
362 break;
363 case EM28XX_OV2640:
364 {
365 struct v4l2_subdev *subdev;
366 struct i2c_board_info ov2640_info = {
367 .type = "ov2640",
368 .flags = I2C_CLIENT_SCCB,
369 .addr = client->addr,
370 };
371 struct v4l2_subdev_format format = {
372 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
373 };
374
375 /*
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
382 */
383 v4l2->sensor_xres = 640;
384 v4l2->sensor_yres = 480;
385
386 subdev =
387 v4l2_i2c_new_subdev_board(v4l2_dev: &v4l2->v4l2_dev, adapter: adap,
388 info: &ov2640_info, NULL);
389 if (!subdev)
390 return -ENODEV;
391
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);
396
397 /* NOTE: for UXGA=1600x1200 switch to 12MHz */
398 dev->board.xclk = EM28XX_XCLK_FREQUENCY_24MHZ;
399 em28xx_write_reg(dev, EM28XX_R0F_XCLK, val: dev->board.xclk);
400 v4l2->vinmode = EM28XX_VINMODE_YUV422_YUYV;
401 v4l2->vinctl = 0x00;
402
403 break;
404 }
405 case EM28XX_NOSENSOR:
406 default:
407 return -EINVAL;
408 }
409
410 return 0;
411}
412EXPORT_SYMBOL_GPL(em28xx_init_camera);
413

source code of linux/drivers/media/usb/em28xx/em28xx-camera.c