em28xx-camera.c 11 KB

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