blob: d744af6f7d086f76f02113256aaca88a63fcda10 [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>
23#include <media/mt9v011.h>
24#include <media/v4l2-common.h>
25
26#include "em28xx.h"
27
28
Frank Schaefer176013b2013-03-27 17:06:32 -030029/* Possible i2c addresses of Micron sensors */
30static unsigned short micron_sensor_addrs[] = {
31 0xb8 >> 1, /* MT9V111, MT9V403 */
32 0xba >> 1, /* MT9M001/011/111/112, MT9V011/012/112, MT9D011 */
33 0x90 >> 1, /* MT9V012/112, MT9D011 (alternative address) */
34 I2C_CLIENT_END
35};
36
37
Frank Schaefer855ff382013-03-27 17:06:31 -030038/* FIXME: Should be replaced by a proper mt9m111 driver */
39static int em28xx_initialize_mt9m111(struct em28xx *dev)
40{
41 int i;
42 unsigned char regs[][3] = {
43 { 0x0d, 0x00, 0x01, }, /* reset and use defaults */
44 { 0x0d, 0x00, 0x00, },
45 { 0x0a, 0x00, 0x21, },
46 { 0x21, 0x04, 0x00, }, /* full readout speed, no row/col skipping */
47 };
48
49 for (i = 0; i < ARRAY_SIZE(regs); i++)
50 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
51 &regs[i][0], 3);
52
53 return 0;
54}
55
56
57/* FIXME: Should be replaced by a proper mt9m001 driver */
58static int em28xx_initialize_mt9m001(struct em28xx *dev)
59{
60 int i;
61 unsigned char regs[][3] = {
62 { 0x0d, 0x00, 0x01, },
63 { 0x0d, 0x00, 0x00, },
64 { 0x04, 0x05, 0x00, }, /* hres = 1280 */
65 { 0x03, 0x04, 0x00, }, /* vres = 1024 */
66 { 0x20, 0x11, 0x00, },
67 { 0x06, 0x00, 0x10, },
68 { 0x2b, 0x00, 0x24, },
69 { 0x2e, 0x00, 0x24, },
70 { 0x35, 0x00, 0x24, },
71 { 0x2d, 0x00, 0x20, },
72 { 0x2c, 0x00, 0x20, },
73 { 0x09, 0x0a, 0xd4, },
74 { 0x35, 0x00, 0x57, },
75 };
76
77 for (i = 0; i < ARRAY_SIZE(regs); i++)
78 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
79 &regs[i][0], 3);
80
81 return 0;
82}
83
84
85/*
Frank Schaefer0af0b252013-03-27 17:06:33 -030086 * Probes Micron sensors with 8 bit address and 16 bit register width
Frank Schaefer855ff382013-03-27 17:06:31 -030087 */
Frank Schaefer0af0b252013-03-27 17:06:33 -030088static int em28xx_probe_sensor_micron(struct em28xx *dev)
Frank Schaefer855ff382013-03-27 17:06:31 -030089{
Frank Schaefer176013b2013-03-27 17:06:32 -030090 int ret, i;
Frank Schaefer855ff382013-03-27 17:06:31 -030091 char *name;
92 u8 reg;
93 __be16 id_be;
94 u16 id;
95
Frank Schaefer176013b2013-03-27 17:06:32 -030096 struct i2c_client client = dev->i2c_client[dev->def_i2c_bus];
Frank Schaefer855ff382013-03-27 17:06:31 -030097
Frank Schaefer176013b2013-03-27 17:06:32 -030098 dev->em28xx_sensor = EM28XX_NOSENSOR;
Frank Schaefer176013b2013-03-27 17:06:32 -030099 for (i = 0; micron_sensor_addrs[i] != I2C_CLIENT_END; i++) {
100 client.addr = micron_sensor_addrs[i];
101 /* NOTE: i2c_smbus_read_word_data() doesn't work with BE data */
102 /* Read chip ID from register 0x00 */
103 reg = 0x00;
104 ret = i2c_master_send(&client, &reg, 1);
105 if (ret < 0) {
106 if (ret != -ENODEV)
107 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
108 client.addr << 1, ret);
109 continue;
110 }
111 ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
112 if (ret < 0) {
113 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
114 client.addr << 1, ret);
115 continue;
116 }
117 id = be16_to_cpu(id_be);
118 /* Read chip ID from register 0xff */
119 reg = 0xff;
120 ret = i2c_master_send(&client, &reg, 1);
121 if (ret < 0) {
122 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
123 client.addr << 1, ret);
124 continue;
125 }
126 ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
127 if (ret < 0) {
128 em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
129 client.addr << 1, ret);
130 continue;
131 }
132 /* Validate chip ID to be sure we have a Micron device */
133 if (id != be16_to_cpu(id_be))
134 continue;
135 /* Check chip ID */
136 id = be16_to_cpu(id_be);
137 switch (id) {
138 case 0x1222:
139 name = "MT9V012"; /* MI370 */ /* 640x480 */
140 break;
141 case 0x1229:
142 name = "MT9V112"; /* 640x480 */
143 break;
144 case 0x1433:
145 name = "MT9M011"; /* 1280x1024 */
146 break;
147 case 0x143a: /* found in the ECS G200 */
148 name = "MT9M111"; /* MI1310 */ /* 1280x1024 */
149 dev->em28xx_sensor = EM28XX_MT9M111;
150 break;
151 case 0x148c:
152 name = "MT9M112"; /* MI1320 */ /* 1280x1024 */
153 break;
154 case 0x1511:
155 name = "MT9D011"; /* MI2010 */ /* 1600x1200 */
156 break;
157 case 0x8232:
158 case 0x8243: /* rev B */
159 name = "MT9V011"; /* MI360 */ /* 640x480 */
160 dev->em28xx_sensor = EM28XX_MT9V011;
161 break;
162 case 0x8431:
163 name = "MT9M001"; /* 1280x1024 */
164 dev->em28xx_sensor = EM28XX_MT9M001;
165 break;
166 default:
167 em28xx_info("unknown Micron sensor detected: 0x%04x\n",
168 id);
Frank Schaefer0af0b252013-03-27 17:06:33 -0300169 return 0;
Frank Schaefer176013b2013-03-27 17:06:32 -0300170 }
171
172 if (dev->em28xx_sensor == EM28XX_NOSENSOR)
173 em28xx_info("unsupported sensor detected: %s\n", name);
174 else
175 em28xx_info("sensor %s detected\n", name);
176
177 dev->i2c_client[dev->def_i2c_bus].addr = client.addr;
178 return 0;
Frank Schaefer855ff382013-03-27 17:06:31 -0300179 }
180
Frank Schaefer176013b2013-03-27 17:06:32 -0300181 return -ENODEV;
Frank Schaefer855ff382013-03-27 17:06:31 -0300182}
183
Frank Schaefer0af0b252013-03-27 17:06:33 -0300184/*
185 * This method works for webcams with Micron sensors
186 */
187int em28xx_detect_sensor(struct em28xx *dev)
188{
189 int ret;
190
191 ret = em28xx_probe_sensor_micron(dev);
192 if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) {
193 em28xx_info("No sensor detected\n");
194 return -ENODEV;
195 }
196
197 return 0;
198}
199
Frank Schaefer855ff382013-03-27 17:06:31 -0300200int em28xx_init_camera(struct em28xx *dev)
201{
202 switch (dev->em28xx_sensor) {
203 case EM28XX_MT9V011:
204 {
205 struct mt9v011_platform_data pdata;
206 struct i2c_board_info mt9v011_info = {
207 .type = "mt9v011",
208 .addr = dev->i2c_client[dev->def_i2c_bus].addr,
209 .platform_data = &pdata,
210 };
211
212 dev->sensor_xres = 640;
213 dev->sensor_yres = 480;
214
215 /*
216 * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
217 * the Silvercrest cam I have here for testing - for higher
218 * resolutions, a high clock cause horizontal artifacts, so we
219 * need to use a lower xclk frequency.
220 * Yet, it would be possible to adjust xclk depending on the
221 * desired resolution, since this affects directly the
222 * frame rate.
223 */
224 dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ;
225 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
226 dev->sensor_xtal = 4300000;
227 pdata.xtal = dev->sensor_xtal;
228 if (NULL ==
229 v4l2_i2c_new_subdev_board(&dev->v4l2_dev,
230 &dev->i2c_adap[dev->def_i2c_bus],
231 &mt9v011_info, NULL))
232 return -ENODEV;
233 /* probably means GRGB 16 bit bayer */
234 dev->vinmode = 0x0d;
235 dev->vinctl = 0x00;
236
237 break;
238 }
239 case EM28XX_MT9M001:
240 dev->sensor_xres = 1280;
241 dev->sensor_yres = 1024;
242
243 em28xx_initialize_mt9m001(dev);
244
245 /* probably means BGGR 16 bit bayer */
246 dev->vinmode = 0x0c;
247 dev->vinctl = 0x00;
248
249 break;
250 case EM28XX_MT9M111:
251 dev->sensor_xres = 640;
252 dev->sensor_yres = 512;
253
254 dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ;
255 em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
256 em28xx_initialize_mt9m111(dev);
257
258 dev->vinmode = 0x0a;
259 dev->vinctl = 0x00;
260
261 break;
262 case EM28XX_NOSENSOR:
263 default:
264 return -EINVAL;
265 }
266
267 return 0;
268}