| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 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/mt9v011.h> |
| 24 | #include <media/v4l2-common.h> |
| 25 | |
| 26 | #include "em28xx.h" |
| 27 | |
| 28 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 29 | /* Possible i2c addresses of Micron sensors */ |
| 30 | static 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 | |
| Frank Schaefer | bde0368 | 2013-03-27 17:06:34 -0300 | [diff] [blame^] | 37 | /* Possible i2c addresses of Omnivision sensors */ |
| 38 | static unsigned short omnivision_sensor_addrs[] = { |
| 39 | 0x42 >> 1, /* OV7725, OV7670/60/48 */ |
| 40 | 0x60 >> 1, /* OV2640, OV9650/53/55 */ |
| 41 | I2C_CLIENT_END |
| 42 | }; |
| 43 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 44 | |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 45 | /* FIXME: Should be replaced by a proper mt9m111 driver */ |
| 46 | static int em28xx_initialize_mt9m111(struct em28xx *dev) |
| 47 | { |
| 48 | int i; |
| 49 | unsigned char regs[][3] = { |
| 50 | { 0x0d, 0x00, 0x01, }, /* reset and use defaults */ |
| 51 | { 0x0d, 0x00, 0x00, }, |
| 52 | { 0x0a, 0x00, 0x21, }, |
| 53 | { 0x21, 0x04, 0x00, }, /* full readout speed, no row/col skipping */ |
| 54 | }; |
| 55 | |
| 56 | for (i = 0; i < ARRAY_SIZE(regs); i++) |
| 57 | i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], |
| 58 | ®s[i][0], 3); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* FIXME: Should be replaced by a proper mt9m001 driver */ |
| 65 | static int em28xx_initialize_mt9m001(struct em28xx *dev) |
| 66 | { |
| 67 | int i; |
| 68 | unsigned char regs[][3] = { |
| 69 | { 0x0d, 0x00, 0x01, }, |
| 70 | { 0x0d, 0x00, 0x00, }, |
| 71 | { 0x04, 0x05, 0x00, }, /* hres = 1280 */ |
| 72 | { 0x03, 0x04, 0x00, }, /* vres = 1024 */ |
| 73 | { 0x20, 0x11, 0x00, }, |
| 74 | { 0x06, 0x00, 0x10, }, |
| 75 | { 0x2b, 0x00, 0x24, }, |
| 76 | { 0x2e, 0x00, 0x24, }, |
| 77 | { 0x35, 0x00, 0x24, }, |
| 78 | { 0x2d, 0x00, 0x20, }, |
| 79 | { 0x2c, 0x00, 0x20, }, |
| 80 | { 0x09, 0x0a, 0xd4, }, |
| 81 | { 0x35, 0x00, 0x57, }, |
| 82 | }; |
| 83 | |
| 84 | for (i = 0; i < ARRAY_SIZE(regs); i++) |
| 85 | i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], |
| 86 | ®s[i][0], 3); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /* |
| Frank Schaefer | 0af0b25 | 2013-03-27 17:06:33 -0300 | [diff] [blame] | 93 | * Probes Micron sensors with 8 bit address and 16 bit register width |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 94 | */ |
| Frank Schaefer | 0af0b25 | 2013-03-27 17:06:33 -0300 | [diff] [blame] | 95 | static int em28xx_probe_sensor_micron(struct em28xx *dev) |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 96 | { |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 97 | int ret, i; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 98 | char *name; |
| 99 | u8 reg; |
| 100 | __be16 id_be; |
| 101 | u16 id; |
| 102 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 103 | struct i2c_client client = dev->i2c_client[dev->def_i2c_bus]; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 104 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 105 | dev->em28xx_sensor = EM28XX_NOSENSOR; |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 106 | for (i = 0; micron_sensor_addrs[i] != I2C_CLIENT_END; i++) { |
| 107 | client.addr = micron_sensor_addrs[i]; |
| 108 | /* NOTE: i2c_smbus_read_word_data() doesn't work with BE data */ |
| 109 | /* Read chip ID from register 0x00 */ |
| 110 | reg = 0x00; |
| 111 | ret = i2c_master_send(&client, ®, 1); |
| 112 | if (ret < 0) { |
| 113 | if (ret != -ENODEV) |
| 114 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 115 | client.addr << 1, ret); |
| 116 | continue; |
| 117 | } |
| 118 | ret = i2c_master_recv(&client, (u8 *)&id_be, 2); |
| 119 | if (ret < 0) { |
| 120 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 121 | client.addr << 1, ret); |
| 122 | continue; |
| 123 | } |
| 124 | id = be16_to_cpu(id_be); |
| 125 | /* Read chip ID from register 0xff */ |
| 126 | reg = 0xff; |
| 127 | ret = i2c_master_send(&client, ®, 1); |
| 128 | if (ret < 0) { |
| 129 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 130 | client.addr << 1, ret); |
| 131 | continue; |
| 132 | } |
| 133 | ret = i2c_master_recv(&client, (u8 *)&id_be, 2); |
| 134 | if (ret < 0) { |
| 135 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 136 | client.addr << 1, ret); |
| 137 | continue; |
| 138 | } |
| 139 | /* Validate chip ID to be sure we have a Micron device */ |
| 140 | if (id != be16_to_cpu(id_be)) |
| 141 | continue; |
| 142 | /* Check chip ID */ |
| 143 | id = be16_to_cpu(id_be); |
| 144 | switch (id) { |
| 145 | case 0x1222: |
| 146 | name = "MT9V012"; /* MI370 */ /* 640x480 */ |
| 147 | break; |
| 148 | case 0x1229: |
| 149 | name = "MT9V112"; /* 640x480 */ |
| 150 | break; |
| 151 | case 0x1433: |
| 152 | name = "MT9M011"; /* 1280x1024 */ |
| 153 | break; |
| 154 | case 0x143a: /* found in the ECS G200 */ |
| 155 | name = "MT9M111"; /* MI1310 */ /* 1280x1024 */ |
| 156 | dev->em28xx_sensor = EM28XX_MT9M111; |
| 157 | break; |
| 158 | case 0x148c: |
| 159 | name = "MT9M112"; /* MI1320 */ /* 1280x1024 */ |
| 160 | break; |
| 161 | case 0x1511: |
| 162 | name = "MT9D011"; /* MI2010 */ /* 1600x1200 */ |
| 163 | break; |
| 164 | case 0x8232: |
| 165 | case 0x8243: /* rev B */ |
| 166 | name = "MT9V011"; /* MI360 */ /* 640x480 */ |
| 167 | dev->em28xx_sensor = EM28XX_MT9V011; |
| 168 | break; |
| 169 | case 0x8431: |
| 170 | name = "MT9M001"; /* 1280x1024 */ |
| 171 | dev->em28xx_sensor = EM28XX_MT9M001; |
| 172 | break; |
| 173 | default: |
| 174 | em28xx_info("unknown Micron sensor detected: 0x%04x\n", |
| 175 | id); |
| Frank Schaefer | 0af0b25 | 2013-03-27 17:06:33 -0300 | [diff] [blame] | 176 | return 0; |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (dev->em28xx_sensor == EM28XX_NOSENSOR) |
| 180 | em28xx_info("unsupported sensor detected: %s\n", name); |
| 181 | else |
| 182 | em28xx_info("sensor %s detected\n", name); |
| 183 | |
| 184 | dev->i2c_client[dev->def_i2c_bus].addr = client.addr; |
| 185 | return 0; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 186 | } |
| 187 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame] | 188 | return -ENODEV; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 189 | } |
| 190 | |
| Frank Schaefer | 0af0b25 | 2013-03-27 17:06:33 -0300 | [diff] [blame] | 191 | /* |
| Frank Schaefer | bde0368 | 2013-03-27 17:06:34 -0300 | [diff] [blame^] | 192 | * Probes Omnivision sensors with 8 bit address and register width |
| Frank Schaefer | 0af0b25 | 2013-03-27 17:06:33 -0300 | [diff] [blame] | 193 | */ |
| Frank Schaefer | bde0368 | 2013-03-27 17:06:34 -0300 | [diff] [blame^] | 194 | static int em28xx_probe_sensor_omnivision(struct em28xx *dev) |
| 195 | { |
| 196 | int ret, i; |
| 197 | char *name; |
| 198 | u8 reg; |
| 199 | u16 id; |
| 200 | struct i2c_client client = dev->i2c_client[dev->def_i2c_bus]; |
| 201 | |
| 202 | dev->em28xx_sensor = EM28XX_NOSENSOR; |
| 203 | /* NOTE: these devices have the register auto incrementation disabled |
| 204 | * by default, so we have to use single byte reads ! */ |
| 205 | for (i = 0; omnivision_sensor_addrs[i] != I2C_CLIENT_END; i++) { |
| 206 | client.addr = omnivision_sensor_addrs[i]; |
| 207 | /* Read manufacturer ID from registers 0x1c-0x1d (BE) */ |
| 208 | reg = 0x1c; |
| 209 | ret = i2c_smbus_read_byte_data(&client, reg); |
| 210 | if (ret < 0) { |
| 211 | if (ret != -ENODEV) |
| 212 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 213 | client.addr << 1, ret); |
| 214 | continue; |
| 215 | } |
| 216 | id = ret << 8; |
| 217 | reg = 0x1d; |
| 218 | ret = i2c_smbus_read_byte_data(&client, reg); |
| 219 | if (ret < 0) { |
| 220 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 221 | client.addr << 1, ret); |
| 222 | continue; |
| 223 | } |
| 224 | id += ret; |
| 225 | /* Check manufacturer ID */ |
| 226 | if (id != 0x7fa2) |
| 227 | continue; |
| 228 | /* Read product ID from registers 0x0a-0x0b (BE) */ |
| 229 | reg = 0x0a; |
| 230 | ret = i2c_smbus_read_byte_data(&client, reg); |
| 231 | if (ret < 0) { |
| 232 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 233 | client.addr << 1, ret); |
| 234 | continue; |
| 235 | } |
| 236 | id = ret << 8; |
| 237 | reg = 0x0b; |
| 238 | ret = i2c_smbus_read_byte_data(&client, reg); |
| 239 | if (ret < 0) { |
| 240 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 241 | client.addr << 1, ret); |
| 242 | continue; |
| 243 | } |
| 244 | id += ret; |
| 245 | /* Check product ID */ |
| 246 | switch (id) { |
| 247 | case 0x2642: |
| 248 | name = "OV2640"; |
| 249 | break; |
| 250 | case 0x7648: |
| 251 | name = "OV7648"; |
| 252 | break; |
| 253 | case 0x7660: |
| 254 | name = "OV7660"; |
| 255 | break; |
| 256 | case 0x7673: |
| 257 | name = "OV7670"; |
| 258 | break; |
| 259 | case 0x7720: |
| 260 | name = "OV7720"; |
| 261 | break; |
| 262 | case 0x7721: |
| 263 | name = "OV7725"; |
| 264 | break; |
| 265 | case 0x9648: /* Rev 2 */ |
| 266 | case 0x9649: /* Rev 3 */ |
| 267 | name = "OV9640"; |
| 268 | break; |
| 269 | case 0x9650: |
| 270 | case 0x9652: /* OV9653 */ |
| 271 | name = "OV9650"; |
| 272 | break; |
| 273 | case 0x9656: /* Rev 4 */ |
| 274 | case 0x9657: /* Rev 5 */ |
| 275 | name = "OV9655"; |
| 276 | break; |
| 277 | default: |
| 278 | em28xx_info("unknown OmniVision sensor detected: 0x%04x\n", |
| 279 | id); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | if (dev->em28xx_sensor == EM28XX_NOSENSOR) |
| 284 | em28xx_info("unsupported sensor detected: %s\n", name); |
| 285 | else |
| 286 | em28xx_info("sensor %s detected\n", name); |
| 287 | |
| 288 | dev->i2c_client[dev->def_i2c_bus].addr = client.addr; |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | return -ENODEV; |
| 293 | } |
| 294 | |
| Frank Schaefer | 0af0b25 | 2013-03-27 17:06:33 -0300 | [diff] [blame] | 295 | int em28xx_detect_sensor(struct em28xx *dev) |
| 296 | { |
| 297 | int ret; |
| 298 | |
| 299 | ret = em28xx_probe_sensor_micron(dev); |
| Frank Schaefer | bde0368 | 2013-03-27 17:06:34 -0300 | [diff] [blame^] | 300 | |
| 301 | if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) |
| 302 | ret = em28xx_probe_sensor_omnivision(dev); |
| 303 | |
| Frank Schaefer | 0af0b25 | 2013-03-27 17:06:33 -0300 | [diff] [blame] | 304 | if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) { |
| 305 | em28xx_info("No sensor detected\n"); |
| 306 | return -ENODEV; |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 312 | int em28xx_init_camera(struct em28xx *dev) |
| 313 | { |
| 314 | switch (dev->em28xx_sensor) { |
| 315 | case EM28XX_MT9V011: |
| 316 | { |
| 317 | struct mt9v011_platform_data pdata; |
| 318 | struct i2c_board_info mt9v011_info = { |
| 319 | .type = "mt9v011", |
| 320 | .addr = dev->i2c_client[dev->def_i2c_bus].addr, |
| 321 | .platform_data = &pdata, |
| 322 | }; |
| 323 | |
| 324 | dev->sensor_xres = 640; |
| 325 | dev->sensor_yres = 480; |
| 326 | |
| 327 | /* |
| 328 | * FIXME: mt9v011 uses I2S speed as xtal clk - at least with |
| 329 | * the Silvercrest cam I have here for testing - for higher |
| 330 | * resolutions, a high clock cause horizontal artifacts, so we |
| 331 | * need to use a lower xclk frequency. |
| 332 | * Yet, it would be possible to adjust xclk depending on the |
| 333 | * desired resolution, since this affects directly the |
| 334 | * frame rate. |
| 335 | */ |
| 336 | dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ; |
| 337 | em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk); |
| 338 | dev->sensor_xtal = 4300000; |
| 339 | pdata.xtal = dev->sensor_xtal; |
| 340 | if (NULL == |
| 341 | v4l2_i2c_new_subdev_board(&dev->v4l2_dev, |
| 342 | &dev->i2c_adap[dev->def_i2c_bus], |
| 343 | &mt9v011_info, NULL)) |
| 344 | return -ENODEV; |
| 345 | /* probably means GRGB 16 bit bayer */ |
| 346 | dev->vinmode = 0x0d; |
| 347 | dev->vinctl = 0x00; |
| 348 | |
| 349 | break; |
| 350 | } |
| 351 | case EM28XX_MT9M001: |
| 352 | dev->sensor_xres = 1280; |
| 353 | dev->sensor_yres = 1024; |
| 354 | |
| 355 | em28xx_initialize_mt9m001(dev); |
| 356 | |
| 357 | /* probably means BGGR 16 bit bayer */ |
| 358 | dev->vinmode = 0x0c; |
| 359 | dev->vinctl = 0x00; |
| 360 | |
| 361 | break; |
| 362 | case EM28XX_MT9M111: |
| 363 | dev->sensor_xres = 640; |
| 364 | dev->sensor_yres = 512; |
| 365 | |
| 366 | dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ; |
| 367 | em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk); |
| 368 | em28xx_initialize_mt9m111(dev); |
| 369 | |
| 370 | dev->vinmode = 0x0a; |
| 371 | dev->vinctl = 0x00; |
| 372 | |
| 373 | break; |
| 374 | case EM28XX_NOSENSOR: |
| 375 | default: |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |