| 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 | |
| 37 | |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 38 | /* FIXME: Should be replaced by a proper mt9m111 driver */ |
| 39 | static 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 | ®s[i][0], 3); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /* FIXME: Should be replaced by a proper mt9m001 driver */ |
| 58 | static 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 | ®s[i][0], 3); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | * This method works for webcams with Micron sensors |
| 87 | */ |
| 88 | int em28xx_detect_sensor(struct em28xx *dev) |
| 89 | { |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame^] | 90 | int ret, i; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 91 | char *name; |
| 92 | u8 reg; |
| 93 | __be16 id_be; |
| 94 | u16 id; |
| 95 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame^] | 96 | struct i2c_client client = dev->i2c_client[dev->def_i2c_bus]; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 97 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame^] | 98 | dev->em28xx_sensor = EM28XX_NOSENSOR; |
| 99 | /* Probe Micron sensors with 8 bit address and 16 bit register width */ |
| 100 | for (i = 0; micron_sensor_addrs[i] != I2C_CLIENT_END; i++) { |
| 101 | client.addr = micron_sensor_addrs[i]; |
| 102 | /* NOTE: i2c_smbus_read_word_data() doesn't work with BE data */ |
| 103 | /* Read chip ID from register 0x00 */ |
| 104 | reg = 0x00; |
| 105 | ret = i2c_master_send(&client, ®, 1); |
| 106 | if (ret < 0) { |
| 107 | if (ret != -ENODEV) |
| 108 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 109 | client.addr << 1, ret); |
| 110 | continue; |
| 111 | } |
| 112 | ret = i2c_master_recv(&client, (u8 *)&id_be, 2); |
| 113 | if (ret < 0) { |
| 114 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 115 | client.addr << 1, ret); |
| 116 | continue; |
| 117 | } |
| 118 | id = be16_to_cpu(id_be); |
| 119 | /* Read chip ID from register 0xff */ |
| 120 | reg = 0xff; |
| 121 | ret = i2c_master_send(&client, ®, 1); |
| 122 | if (ret < 0) { |
| 123 | em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n", |
| 124 | client.addr << 1, ret); |
| 125 | continue; |
| 126 | } |
| 127 | ret = i2c_master_recv(&client, (u8 *)&id_be, 2); |
| 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 | /* Validate chip ID to be sure we have a Micron device */ |
| 134 | if (id != be16_to_cpu(id_be)) |
| 135 | continue; |
| 136 | /* Check chip ID */ |
| 137 | id = be16_to_cpu(id_be); |
| 138 | switch (id) { |
| 139 | case 0x1222: |
| 140 | name = "MT9V012"; /* MI370 */ /* 640x480 */ |
| 141 | break; |
| 142 | case 0x1229: |
| 143 | name = "MT9V112"; /* 640x480 */ |
| 144 | break; |
| 145 | case 0x1433: |
| 146 | name = "MT9M011"; /* 1280x1024 */ |
| 147 | break; |
| 148 | case 0x143a: /* found in the ECS G200 */ |
| 149 | name = "MT9M111"; /* MI1310 */ /* 1280x1024 */ |
| 150 | dev->em28xx_sensor = EM28XX_MT9M111; |
| 151 | break; |
| 152 | case 0x148c: |
| 153 | name = "MT9M112"; /* MI1320 */ /* 1280x1024 */ |
| 154 | break; |
| 155 | case 0x1511: |
| 156 | name = "MT9D011"; /* MI2010 */ /* 1600x1200 */ |
| 157 | break; |
| 158 | case 0x8232: |
| 159 | case 0x8243: /* rev B */ |
| 160 | name = "MT9V011"; /* MI360 */ /* 640x480 */ |
| 161 | dev->em28xx_sensor = EM28XX_MT9V011; |
| 162 | break; |
| 163 | case 0x8431: |
| 164 | name = "MT9M001"; /* 1280x1024 */ |
| 165 | dev->em28xx_sensor = EM28XX_MT9M001; |
| 166 | break; |
| 167 | default: |
| 168 | em28xx_info("unknown Micron sensor detected: 0x%04x\n", |
| 169 | id); |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | if (dev->em28xx_sensor == EM28XX_NOSENSOR) |
| 174 | em28xx_info("unsupported sensor detected: %s\n", name); |
| 175 | else |
| 176 | em28xx_info("sensor %s detected\n", name); |
| 177 | |
| 178 | dev->i2c_client[dev->def_i2c_bus].addr = client.addr; |
| 179 | return 0; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 180 | } |
| 181 | |
| Frank Schaefer | 176013b | 2013-03-27 17:06:32 -0300 | [diff] [blame^] | 182 | return -ENODEV; |
| Frank Schaefer | 855ff38 | 2013-03-27 17:06:31 -0300 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int em28xx_init_camera(struct em28xx *dev) |
| 186 | { |
| 187 | switch (dev->em28xx_sensor) { |
| 188 | case EM28XX_MT9V011: |
| 189 | { |
| 190 | struct mt9v011_platform_data pdata; |
| 191 | struct i2c_board_info mt9v011_info = { |
| 192 | .type = "mt9v011", |
| 193 | .addr = dev->i2c_client[dev->def_i2c_bus].addr, |
| 194 | .platform_data = &pdata, |
| 195 | }; |
| 196 | |
| 197 | dev->sensor_xres = 640; |
| 198 | dev->sensor_yres = 480; |
| 199 | |
| 200 | /* |
| 201 | * FIXME: mt9v011 uses I2S speed as xtal clk - at least with |
| 202 | * the Silvercrest cam I have here for testing - for higher |
| 203 | * resolutions, a high clock cause horizontal artifacts, so we |
| 204 | * need to use a lower xclk frequency. |
| 205 | * Yet, it would be possible to adjust xclk depending on the |
| 206 | * desired resolution, since this affects directly the |
| 207 | * frame rate. |
| 208 | */ |
| 209 | dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ; |
| 210 | em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk); |
| 211 | dev->sensor_xtal = 4300000; |
| 212 | pdata.xtal = dev->sensor_xtal; |
| 213 | if (NULL == |
| 214 | v4l2_i2c_new_subdev_board(&dev->v4l2_dev, |
| 215 | &dev->i2c_adap[dev->def_i2c_bus], |
| 216 | &mt9v011_info, NULL)) |
| 217 | return -ENODEV; |
| 218 | /* probably means GRGB 16 bit bayer */ |
| 219 | dev->vinmode = 0x0d; |
| 220 | dev->vinctl = 0x00; |
| 221 | |
| 222 | break; |
| 223 | } |
| 224 | case EM28XX_MT9M001: |
| 225 | dev->sensor_xres = 1280; |
| 226 | dev->sensor_yres = 1024; |
| 227 | |
| 228 | em28xx_initialize_mt9m001(dev); |
| 229 | |
| 230 | /* probably means BGGR 16 bit bayer */ |
| 231 | dev->vinmode = 0x0c; |
| 232 | dev->vinctl = 0x00; |
| 233 | |
| 234 | break; |
| 235 | case EM28XX_MT9M111: |
| 236 | dev->sensor_xres = 640; |
| 237 | dev->sensor_yres = 512; |
| 238 | |
| 239 | dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ; |
| 240 | em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk); |
| 241 | em28xx_initialize_mt9m111(dev); |
| 242 | |
| 243 | dev->vinmode = 0x0a; |
| 244 | dev->vinctl = 0x00; |
| 245 | |
| 246 | break; |
| 247 | case EM28XX_NOSENSOR: |
| 248 | default: |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |