em28xx-camera.c 9.5 KB

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