em28xx-camera.c 11 KB

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