em28xx-camera.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. em28xx-camera.c - driver for Empia EM25xx/27xx/28xx USB video capture devices
  3. Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@infradead.org>
  4. Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/i2c.h>
  18. #include <media/mt9v011.h>
  19. #include <media/v4l2-common.h>
  20. #include "em28xx.h"
  21. /* Possible i2c addresses of Micron sensors */
  22. static unsigned short micron_sensor_addrs[] = {
  23. 0xb8 >> 1, /* MT9V111, MT9V403 */
  24. 0xba >> 1, /* MT9M001/011/111/112, MT9V011/012/112, MT9D011 */
  25. 0x90 >> 1, /* MT9V012/112, MT9D011 (alternative address) */
  26. I2C_CLIENT_END
  27. };
  28. /* FIXME: Should be replaced by a proper mt9m111 driver */
  29. static int em28xx_initialize_mt9m111(struct em28xx *dev)
  30. {
  31. int i;
  32. unsigned char regs[][3] = {
  33. { 0x0d, 0x00, 0x01, }, /* reset and use defaults */
  34. { 0x0d, 0x00, 0x00, },
  35. { 0x0a, 0x00, 0x21, },
  36. { 0x21, 0x04, 0x00, }, /* full readout speed, no row/col skipping */
  37. };
  38. for (i = 0; i < ARRAY_SIZE(regs); i++)
  39. i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
  40. &regs[i][0], 3);
  41. return 0;
  42. }
  43. /* FIXME: Should be replaced by a proper mt9m001 driver */
  44. static int em28xx_initialize_mt9m001(struct em28xx *dev)
  45. {
  46. int i;
  47. unsigned char regs[][3] = {
  48. { 0x0d, 0x00, 0x01, },
  49. { 0x0d, 0x00, 0x00, },
  50. { 0x04, 0x05, 0x00, }, /* hres = 1280 */
  51. { 0x03, 0x04, 0x00, }, /* vres = 1024 */
  52. { 0x20, 0x11, 0x00, },
  53. { 0x06, 0x00, 0x10, },
  54. { 0x2b, 0x00, 0x24, },
  55. { 0x2e, 0x00, 0x24, },
  56. { 0x35, 0x00, 0x24, },
  57. { 0x2d, 0x00, 0x20, },
  58. { 0x2c, 0x00, 0x20, },
  59. { 0x09, 0x0a, 0xd4, },
  60. { 0x35, 0x00, 0x57, },
  61. };
  62. for (i = 0; i < ARRAY_SIZE(regs); i++)
  63. i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
  64. &regs[i][0], 3);
  65. return 0;
  66. }
  67. /*
  68. * Probes Micron sensors with 8 bit address and 16 bit register width
  69. */
  70. static int em28xx_probe_sensor_micron(struct em28xx *dev)
  71. {
  72. int ret, i;
  73. char *name;
  74. u8 reg;
  75. __be16 id_be;
  76. u16 id;
  77. struct i2c_client client = dev->i2c_client[dev->def_i2c_bus];
  78. dev->em28xx_sensor = EM28XX_NOSENSOR;
  79. for (i = 0; micron_sensor_addrs[i] != I2C_CLIENT_END; i++) {
  80. client.addr = micron_sensor_addrs[i];
  81. /* NOTE: i2c_smbus_read_word_data() doesn't work with BE data */
  82. /* Read chip ID from register 0x00 */
  83. reg = 0x00;
  84. ret = i2c_master_send(&client, &reg, 1);
  85. if (ret < 0) {
  86. if (ret != -ENODEV)
  87. em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
  88. client.addr << 1, ret);
  89. continue;
  90. }
  91. ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
  92. if (ret < 0) {
  93. em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
  94. client.addr << 1, ret);
  95. continue;
  96. }
  97. id = be16_to_cpu(id_be);
  98. /* Read chip ID from register 0xff */
  99. reg = 0xff;
  100. ret = i2c_master_send(&client, &reg, 1);
  101. if (ret < 0) {
  102. em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
  103. client.addr << 1, ret);
  104. continue;
  105. }
  106. ret = i2c_master_recv(&client, (u8 *)&id_be, 2);
  107. if (ret < 0) {
  108. em28xx_errdev("couldn't read from i2c device 0x%02x: error %i\n",
  109. client.addr << 1, ret);
  110. continue;
  111. }
  112. /* Validate chip ID to be sure we have a Micron device */
  113. if (id != be16_to_cpu(id_be))
  114. continue;
  115. /* Check chip ID */
  116. id = be16_to_cpu(id_be);
  117. switch (id) {
  118. case 0x1222:
  119. name = "MT9V012"; /* MI370 */ /* 640x480 */
  120. break;
  121. case 0x1229:
  122. name = "MT9V112"; /* 640x480 */
  123. break;
  124. case 0x1433:
  125. name = "MT9M011"; /* 1280x1024 */
  126. break;
  127. case 0x143a: /* found in the ECS G200 */
  128. name = "MT9M111"; /* MI1310 */ /* 1280x1024 */
  129. dev->em28xx_sensor = EM28XX_MT9M111;
  130. break;
  131. case 0x148c:
  132. name = "MT9M112"; /* MI1320 */ /* 1280x1024 */
  133. break;
  134. case 0x1511:
  135. name = "MT9D011"; /* MI2010 */ /* 1600x1200 */
  136. break;
  137. case 0x8232:
  138. case 0x8243: /* rev B */
  139. name = "MT9V011"; /* MI360 */ /* 640x480 */
  140. dev->em28xx_sensor = EM28XX_MT9V011;
  141. break;
  142. case 0x8431:
  143. name = "MT9M001"; /* 1280x1024 */
  144. dev->em28xx_sensor = EM28XX_MT9M001;
  145. break;
  146. default:
  147. em28xx_info("unknown Micron sensor detected: 0x%04x\n",
  148. id);
  149. return 0;
  150. }
  151. if (dev->em28xx_sensor == EM28XX_NOSENSOR)
  152. em28xx_info("unsupported sensor detected: %s\n", name);
  153. else
  154. em28xx_info("sensor %s detected\n", name);
  155. dev->i2c_client[dev->def_i2c_bus].addr = client.addr;
  156. return 0;
  157. }
  158. return -ENODEV;
  159. }
  160. /*
  161. * This method works for webcams with Micron sensors
  162. */
  163. int em28xx_detect_sensor(struct em28xx *dev)
  164. {
  165. int ret;
  166. ret = em28xx_probe_sensor_micron(dev);
  167. if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) {
  168. em28xx_info("No sensor detected\n");
  169. return -ENODEV;
  170. }
  171. return 0;
  172. }
  173. int em28xx_init_camera(struct em28xx *dev)
  174. {
  175. switch (dev->em28xx_sensor) {
  176. case EM28XX_MT9V011:
  177. {
  178. struct mt9v011_platform_data pdata;
  179. struct i2c_board_info mt9v011_info = {
  180. .type = "mt9v011",
  181. .addr = dev->i2c_client[dev->def_i2c_bus].addr,
  182. .platform_data = &pdata,
  183. };
  184. dev->sensor_xres = 640;
  185. dev->sensor_yres = 480;
  186. /*
  187. * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
  188. * the Silvercrest cam I have here for testing - for higher
  189. * resolutions, a high clock cause horizontal artifacts, so we
  190. * need to use a lower xclk frequency.
  191. * Yet, it would be possible to adjust xclk depending on the
  192. * desired resolution, since this affects directly the
  193. * frame rate.
  194. */
  195. dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ;
  196. em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
  197. dev->sensor_xtal = 4300000;
  198. pdata.xtal = dev->sensor_xtal;
  199. if (NULL ==
  200. v4l2_i2c_new_subdev_board(&dev->v4l2_dev,
  201. &dev->i2c_adap[dev->def_i2c_bus],
  202. &mt9v011_info, NULL))
  203. return -ENODEV;
  204. /* probably means GRGB 16 bit bayer */
  205. dev->vinmode = 0x0d;
  206. dev->vinctl = 0x00;
  207. break;
  208. }
  209. case EM28XX_MT9M001:
  210. dev->sensor_xres = 1280;
  211. dev->sensor_yres = 1024;
  212. em28xx_initialize_mt9m001(dev);
  213. /* probably means BGGR 16 bit bayer */
  214. dev->vinmode = 0x0c;
  215. dev->vinctl = 0x00;
  216. break;
  217. case EM28XX_MT9M111:
  218. dev->sensor_xres = 640;
  219. dev->sensor_yres = 512;
  220. dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ;
  221. em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
  222. em28xx_initialize_mt9m111(dev);
  223. dev->vinmode = 0x0a;
  224. dev->vinctl = 0x00;
  225. break;
  226. case EM28XX_NOSENSOR:
  227. default:
  228. return -EINVAL;
  229. }
  230. return 0;
  231. }