em28xx-camera.c 10 KB

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