em28xx-camera.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <mchehab@kernel.org>
  6. // Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 2 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  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 spd, no row/col skip */
  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",
  142. id);
  143. return 0;
  144. }
  145. if (dev->em28xx_sensor == EM28XX_NOSENSOR)
  146. dev_info(&dev->intf->dev,
  147. "unsupported sensor detected: %s\n", name);
  148. else
  149. dev_info(&dev->intf->dev,
  150. "sensor %s detected\n", name);
  151. return 0;
  152. }
  153. return -ENODEV;
  154. }
  155. /*
  156. * Probes Omnivision sensors with 8 bit address and register width
  157. */
  158. static int em28xx_probe_sensor_omnivision(struct em28xx *dev)
  159. {
  160. int ret, i;
  161. char *name;
  162. u8 reg;
  163. u16 id;
  164. struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
  165. dev->em28xx_sensor = EM28XX_NOSENSOR;
  166. /*
  167. * NOTE: these devices have the register auto incrementation disabled
  168. * by default, so we have to use single byte reads !
  169. */
  170. for (i = 0; omnivision_sensor_addrs[i] != I2C_CLIENT_END; i++) {
  171. client->addr = omnivision_sensor_addrs[i];
  172. /* Read manufacturer ID from registers 0x1c-0x1d (BE) */
  173. reg = 0x1c;
  174. ret = i2c_smbus_read_byte_data(client, reg);
  175. if (ret < 0) {
  176. if (ret != -ENXIO)
  177. dev_err(&dev->intf->dev,
  178. "couldn't read from i2c device 0x%02x: error %i\n",
  179. client->addr << 1, ret);
  180. continue;
  181. }
  182. id = ret << 8;
  183. reg = 0x1d;
  184. ret = i2c_smbus_read_byte_data(client, reg);
  185. if (ret < 0) {
  186. dev_err(&dev->intf->dev,
  187. "couldn't read from i2c device 0x%02x: error %i\n",
  188. client->addr << 1, ret);
  189. continue;
  190. }
  191. id += ret;
  192. /* Check manufacturer ID */
  193. if (id != 0x7fa2)
  194. continue;
  195. /* Read product ID from registers 0x0a-0x0b (BE) */
  196. reg = 0x0a;
  197. ret = i2c_smbus_read_byte_data(client, reg);
  198. if (ret < 0) {
  199. dev_err(&dev->intf->dev,
  200. "couldn't read from i2c device 0x%02x: error %i\n",
  201. client->addr << 1, ret);
  202. continue;
  203. }
  204. id = ret << 8;
  205. reg = 0x0b;
  206. ret = i2c_smbus_read_byte_data(client, reg);
  207. if (ret < 0) {
  208. dev_err(&dev->intf->dev,
  209. "couldn't read from i2c device 0x%02x: error %i\n",
  210. client->addr << 1, ret);
  211. continue;
  212. }
  213. id += ret;
  214. /* Check product ID */
  215. switch (id) {
  216. case 0x2642:
  217. name = "OV2640";
  218. dev->em28xx_sensor = EM28XX_OV2640;
  219. break;
  220. case 0x7648:
  221. name = "OV7648";
  222. break;
  223. case 0x7660:
  224. name = "OV7660";
  225. break;
  226. case 0x7673:
  227. name = "OV7670";
  228. break;
  229. case 0x7720:
  230. name = "OV7720";
  231. break;
  232. case 0x7721:
  233. name = "OV7725";
  234. break;
  235. case 0x9648: /* Rev 2 */
  236. case 0x9649: /* Rev 3 */
  237. name = "OV9640";
  238. break;
  239. case 0x9650:
  240. case 0x9652: /* OV9653 */
  241. name = "OV9650";
  242. break;
  243. case 0x9656: /* Rev 4 */
  244. case 0x9657: /* Rev 5 */
  245. name = "OV9655";
  246. break;
  247. default:
  248. dev_info(&dev->intf->dev,
  249. "unknown OmniVision sensor detected: 0x%04x\n",
  250. id);
  251. return 0;
  252. }
  253. if (dev->em28xx_sensor == EM28XX_NOSENSOR)
  254. dev_info(&dev->intf->dev,
  255. "unsupported sensor detected: %s\n", name);
  256. else
  257. dev_info(&dev->intf->dev,
  258. "sensor %s detected\n", name);
  259. return 0;
  260. }
  261. return -ENODEV;
  262. }
  263. int em28xx_detect_sensor(struct em28xx *dev)
  264. {
  265. int ret;
  266. ret = em28xx_probe_sensor_micron(dev);
  267. if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0)
  268. ret = em28xx_probe_sensor_omnivision(dev);
  269. /*
  270. * NOTE: the Windows driver also probes i2c addresses
  271. * 0x22 (Samsung ?) and 0x66 (Kodak ?)
  272. */
  273. if (dev->em28xx_sensor == EM28XX_NOSENSOR && ret < 0) {
  274. dev_info(&dev->intf->dev,
  275. "No sensor detected\n");
  276. return -ENODEV;
  277. }
  278. return 0;
  279. }
  280. int em28xx_init_camera(struct em28xx *dev)
  281. {
  282. struct i2c_client *client = &dev->i2c_client[dev->def_i2c_bus];
  283. struct i2c_adapter *adap = &dev->i2c_adap[dev->def_i2c_bus];
  284. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  285. switch (dev->em28xx_sensor) {
  286. case EM28XX_MT9V011:
  287. {
  288. struct mt9v011_platform_data pdata;
  289. struct i2c_board_info mt9v011_info = {
  290. .type = "mt9v011",
  291. .addr = client->addr,
  292. .platform_data = &pdata,
  293. };
  294. v4l2->sensor_xres = 640;
  295. v4l2->sensor_yres = 480;
  296. /*
  297. * FIXME: mt9v011 uses I2S speed as xtal clk - at least with
  298. * the Silvercrest cam I have here for testing - for higher
  299. * resolutions, a high clock cause horizontal artifacts, so we
  300. * need to use a lower xclk frequency.
  301. * Yet, it would be possible to adjust xclk depending on the
  302. * desired resolution, since this affects directly the
  303. * frame rate.
  304. */
  305. dev->board.xclk = EM28XX_XCLK_FREQUENCY_4_3MHZ;
  306. em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
  307. v4l2->sensor_xtal = 4300000;
  308. pdata.xtal = v4l2->sensor_xtal;
  309. if (NULL ==
  310. v4l2_i2c_new_subdev_board(&v4l2->v4l2_dev, adap,
  311. &mt9v011_info, NULL))
  312. return -ENODEV;
  313. v4l2->vinmode = EM28XX_VINMODE_RGB8_GRBG;
  314. v4l2->vinctl = 0x00;
  315. break;
  316. }
  317. case EM28XX_MT9M001:
  318. v4l2->sensor_xres = 1280;
  319. v4l2->sensor_yres = 1024;
  320. em28xx_initialize_mt9m001(dev);
  321. v4l2->vinmode = EM28XX_VINMODE_RGB8_BGGR;
  322. v4l2->vinctl = 0x00;
  323. break;
  324. case EM28XX_MT9M111:
  325. v4l2->sensor_xres = 640;
  326. v4l2->sensor_yres = 512;
  327. dev->board.xclk = EM28XX_XCLK_FREQUENCY_48MHZ;
  328. em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
  329. em28xx_initialize_mt9m111(dev);
  330. v4l2->vinmode = EM28XX_VINMODE_YUV422_UYVY;
  331. v4l2->vinctl = 0x00;
  332. break;
  333. case EM28XX_OV2640:
  334. {
  335. struct v4l2_subdev *subdev;
  336. struct i2c_board_info ov2640_info = {
  337. .type = "ov2640",
  338. .flags = I2C_CLIENT_SCCB,
  339. .addr = client->addr,
  340. };
  341. struct v4l2_subdev_format format = {
  342. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  343. };
  344. /*
  345. * FIXME: sensor supports resolutions up to 1600x1200, but
  346. * resolution setting/switching needs to be modified to
  347. * - switch sensor output resolution (including further
  348. * configuration changes)
  349. * - adjust bridge xclk
  350. * - disable 16 bit (12 bit) output formats on high resolutions
  351. */
  352. v4l2->sensor_xres = 640;
  353. v4l2->sensor_yres = 480;
  354. subdev =
  355. v4l2_i2c_new_subdev_board(&v4l2->v4l2_dev, adap,
  356. &ov2640_info, NULL);
  357. if (!subdev)
  358. return -ENODEV;
  359. format.format.code = MEDIA_BUS_FMT_YUYV8_2X8;
  360. format.format.width = 640;
  361. format.format.height = 480;
  362. v4l2_subdev_call(subdev, pad, set_fmt, NULL, &format);
  363. /* NOTE: for UXGA=1600x1200 switch to 12MHz */
  364. dev->board.xclk = EM28XX_XCLK_FREQUENCY_24MHZ;
  365. em28xx_write_reg(dev, EM28XX_R0F_XCLK, dev->board.xclk);
  366. v4l2->vinmode = EM28XX_VINMODE_YUV422_YUYV;
  367. v4l2->vinctl = 0x00;
  368. break;
  369. }
  370. case EM28XX_NOSENSOR:
  371. default:
  372. return -EINVAL;
  373. }
  374. return 0;
  375. }
  376. EXPORT_SYMBOL_GPL(em28xx_init_camera);