| 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 | |
| 29 | /* FIXME: Should be replaced by a proper mt9m111 driver */ |
| 30 | static int em28xx_initialize_mt9m111(struct em28xx *dev) |
| 31 | { |
| 32 | int i; |
| 33 | unsigned char regs[][3] = { |
| 34 | { 0x0d, 0x00, 0x01, }, /* reset and use defaults */ |
| 35 | { 0x0d, 0x00, 0x00, }, |
| 36 | { 0x0a, 0x00, 0x21, }, |
| 37 | { 0x21, 0x04, 0x00, }, /* full readout speed, no row/col skipping */ |
| 38 | }; |
| 39 | |
| 40 | for (i = 0; i < ARRAY_SIZE(regs); i++) |
| 41 | i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], |
| 42 | ®s[i][0], 3); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* FIXME: Should be replaced by a proper mt9m001 driver */ |
| 49 | static int em28xx_initialize_mt9m001(struct em28xx *dev) |
| 50 | { |
| 51 | int i; |
| 52 | unsigned char regs[][3] = { |
| 53 | { 0x0d, 0x00, 0x01, }, |
| 54 | { 0x0d, 0x00, 0x00, }, |
| 55 | { 0x04, 0x05, 0x00, }, /* hres = 1280 */ |
| 56 | { 0x03, 0x04, 0x00, }, /* vres = 1024 */ |
| 57 | { 0x20, 0x11, 0x00, }, |
| 58 | { 0x06, 0x00, 0x10, }, |
| 59 | { 0x2b, 0x00, 0x24, }, |
| 60 | { 0x2e, 0x00, 0x24, }, |
| 61 | { 0x35, 0x00, 0x24, }, |
| 62 | { 0x2d, 0x00, 0x20, }, |
| 63 | { 0x2c, 0x00, 0x20, }, |
| 64 | { 0x09, 0x0a, 0xd4, }, |
| 65 | { 0x35, 0x00, 0x57, }, |
| 66 | }; |
| 67 | |
| 68 | for (i = 0; i < ARRAY_SIZE(regs); i++) |
| 69 | i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], |
| 70 | ®s[i][0], 3); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * This method works for webcams with Micron sensors |
| 78 | */ |
| 79 | int em28xx_detect_sensor(struct em28xx *dev) |
| 80 | { |
| 81 | int ret; |
| 82 | char *name; |
| 83 | u8 reg; |
| 84 | __be16 id_be; |
| 85 | u16 id; |
| 86 | |
| 87 | /* Micron sensor detection */ |
| 88 | dev->i2c_client[dev->def_i2c_bus].addr = 0xba >> 1; |
| 89 | reg = 0; |
| 90 | i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], ®, 1); |
| 91 | ret = i2c_master_recv(&dev->i2c_client[dev->def_i2c_bus], |
| 92 | (char *)&id_be, 2); |
| 93 | if (ret != 2) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | id = be16_to_cpu(id_be); |
| 97 | switch (id) { |
| 98 | case 0x8232: /* mt9v011 640x480 1.3 Mpix sensor */ |
| 99 | case 0x8243: /* mt9v011 rev B 640x480 1.3 Mpix sensor */ |
| 100 | name = "mt9v011"; |
| 101 | dev->em28xx_sensor = EM28XX_MT9V011; |
| 102 | break; |
| 103 | case 0x143a: /* MT9M111 as found in the ECS G200 */ |
| 104 | name = "mt9m111"; |
| 105 | dev->em28xx_sensor = EM28XX_MT9M111; |
| 106 | break; |
| 107 | case 0x8431: |
| 108 | name = "mt9m001"; |
| 109 | dev->em28xx_sensor = EM28XX_MT9M001; |
| 110 | break; |
| 111 | default: |
| 112 | em28xx_info("unknown Micron sensor detected: 0x%04x\n", id); |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | |
| 116 | em28xx_info("sensor %s detected\n", name); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int em28xx_init_camera(struct em28xx *dev) |
| 122 | { |
| 123 | switch (dev->em28xx_sensor) { |
| 124 | case EM28XX_MT9V011: |
| 125 | { |
| 126 | struct mt9v011_platform_data pdata; |
| 127 | struct i2c_board_info mt9v011_info = { |
| 128 | .type = "mt9v011", |
| 129 | .addr = dev->i2c_client[dev->def_i2c_bus].addr, |
| 130 | .platform_data = &pdata, |
| 131 | }; |
| 132 | |
| 133 | dev->sensor_xres = 640; |
| 134 | dev->sensor_yres = 480; |
| 135 | |
| 136 | /* |
| 137 | * FIXME: mt9v011 uses I2S speed as xtal clk - at least with |
| 138 | * the Silvercrest cam I have here for testing - for higher |
| 139 | * resolutions, a high clock cause horizontal artifacts, so we |
| 140 | * need to use a lower xclk frequency. |
| 141 | * Yet, it would be possible to adjust xclk depending on the |
| 142 | * desired resolution, since this affects directly the |
| 143 | * frame rate. |
| 144 | */ |
| 145 | dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ; |
| 146 | em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk); |
| 147 | dev->sensor_xtal = 4300000; |
| 148 | pdata.xtal = dev->sensor_xtal; |
| 149 | if (NULL == |
| 150 | v4l2_i2c_new_subdev_board(&dev->v4l2_dev, |
| 151 | &dev->i2c_adap[dev->def_i2c_bus], |
| 152 | &mt9v011_info, NULL)) |
| 153 | return -ENODEV; |
| 154 | /* probably means GRGB 16 bit bayer */ |
| 155 | dev->vinmode = 0x0d; |
| 156 | dev->vinctl = 0x00; |
| 157 | |
| 158 | break; |
| 159 | } |
| 160 | case EM28XX_MT9M001: |
| 161 | dev->sensor_xres = 1280; |
| 162 | dev->sensor_yres = 1024; |
| 163 | |
| 164 | em28xx_initialize_mt9m001(dev); |
| 165 | |
| 166 | /* probably means BGGR 16 bit bayer */ |
| 167 | dev->vinmode = 0x0c; |
| 168 | dev->vinctl = 0x00; |
| 169 | |
| 170 | break; |
| 171 | case EM28XX_MT9M111: |
| 172 | dev->sensor_xres = 640; |
| 173 | dev->sensor_yres = 512; |
| 174 | |
| 175 | dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ; |
| 176 | em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk); |
| 177 | em28xx_initialize_mt9m111(dev); |
| 178 | |
| 179 | dev->vinmode = 0x0a; |
| 180 | dev->vinctl = 0x00; |
| 181 | |
| 182 | break; |
| 183 | case EM28XX_NOSENSOR: |
| 184 | default: |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |