em28xx-camera.c 12 KB

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