em28xx-camera.c 12 KB

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