em28xx-i2c.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  4. Markus Rechberger <mrechberger@gmail.com>
  5. Mauro Carvalho Chehab <mchehab@infradead.org>
  6. Sascha Sommer <saschasommer@freenet.de>
  7. Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
  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. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/usb.h>
  23. #include <linux/i2c.h>
  24. #include "em28xx.h"
  25. #include "tuner-xc2028.h"
  26. #include <media/v4l2-common.h>
  27. #include <media/tuner.h>
  28. /* ----------------------------------------------------------- */
  29. static unsigned int i2c_scan;
  30. module_param(i2c_scan, int, 0444);
  31. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  32. static unsigned int i2c_debug;
  33. module_param(i2c_debug, int, 0644);
  34. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  35. /*
  36. * em2800_i2c_send_bytes()
  37. * send up to 4 bytes to the em2800 i2c device
  38. */
  39. static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  40. {
  41. int ret;
  42. int write_timeout;
  43. u8 b2[6];
  44. if (len < 1 || len > 4)
  45. return -EOPNOTSUPP;
  46. BUG_ON(len < 1 || len > 4);
  47. b2[5] = 0x80 + len - 1;
  48. b2[4] = addr;
  49. b2[3] = buf[0];
  50. if (len > 1)
  51. b2[2] = buf[1];
  52. if (len > 2)
  53. b2[1] = buf[2];
  54. if (len > 3)
  55. b2[0] = buf[3];
  56. /* trigger write */
  57. ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
  58. if (ret != 2 + len) {
  59. em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
  60. addr, ret);
  61. return (ret < 0) ? ret : -EIO;
  62. }
  63. /* wait for completion */
  64. for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
  65. write_timeout -= 5) {
  66. ret = dev->em28xx_read_reg(dev, 0x05);
  67. if (ret == 0x80 + len - 1) {
  68. return len;
  69. } else if (ret == 0x94 + len - 1) {
  70. return -ENODEV;
  71. } else if (ret < 0) {
  72. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  73. ret);
  74. return ret;
  75. }
  76. msleep(5);
  77. }
  78. em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
  79. return -EIO;
  80. }
  81. /*
  82. * em2800_i2c_recv_bytes()
  83. * read up to 4 bytes from the em2800 i2c device
  84. */
  85. static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  86. {
  87. u8 buf2[4];
  88. int ret;
  89. int read_timeout;
  90. int i;
  91. if (len < 1 || len > 4)
  92. return -EOPNOTSUPP;
  93. /* trigger read */
  94. buf2[1] = 0x84 + len - 1;
  95. buf2[0] = addr;
  96. ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
  97. if (ret != 2) {
  98. em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
  99. addr, ret);
  100. return (ret < 0) ? ret : -EIO;
  101. }
  102. /* wait for completion */
  103. for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0;
  104. read_timeout -= 5) {
  105. ret = dev->em28xx_read_reg(dev, 0x05);
  106. if (ret == 0x84 + len - 1) {
  107. break;
  108. } else if (ret == 0x94 + len - 1) {
  109. return -ENODEV;
  110. } else if (ret < 0) {
  111. em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
  112. ret);
  113. return ret;
  114. }
  115. msleep(5);
  116. }
  117. if (ret != 0x84 + len - 1)
  118. em28xx_warn("read from i2c device at 0x%x timed out\n", addr);
  119. /* get the received message */
  120. ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
  121. if (ret != len) {
  122. em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
  123. addr, ret);
  124. return (ret < 0) ? ret : -EIO;
  125. }
  126. for (i = 0; i < len; i++)
  127. buf[i] = buf2[len - 1 - i];
  128. return ret;
  129. }
  130. /*
  131. * em2800_i2c_check_for_device()
  132. * check if there is an i2c device at the supplied address
  133. */
  134. static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
  135. {
  136. u8 buf;
  137. int ret;
  138. ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
  139. if (ret == 1)
  140. return 0;
  141. return (ret < 0) ? ret : -EIO;
  142. }
  143. /*
  144. * em28xx_i2c_send_bytes()
  145. */
  146. static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
  147. u16 len, int stop)
  148. {
  149. int write_timeout, ret;
  150. if (len < 1 || len > 64)
  151. return -EOPNOTSUPP;
  152. /*
  153. * NOTE: limited by the USB ctrl message constraints
  154. * Zero length reads always succeed, even if no device is connected
  155. */
  156. /* Write to i2c device */
  157. ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  158. if (ret != len) {
  159. if (ret < 0) {
  160. em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
  161. addr, ret);
  162. return ret;
  163. } else {
  164. em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
  165. len, addr, ret);
  166. return -EIO;
  167. }
  168. }
  169. /* Check success of the i2c operation */
  170. for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
  171. write_timeout -= 5) {
  172. ret = dev->em28xx_read_reg(dev, 0x05);
  173. if (ret == 0) { /* success */
  174. return len;
  175. } else if (ret == 0x10) {
  176. return -ENODEV;
  177. } else if (ret < 0) {
  178. em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
  179. ret);
  180. return ret;
  181. }
  182. msleep(5);
  183. /*
  184. * NOTE: do we really have to wait for success ?
  185. * Never seen anything else than 0x00 or 0x10
  186. * (even with high payload) ...
  187. */
  188. }
  189. em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
  190. return -EIO;
  191. }
  192. /*
  193. * em28xx_i2c_recv_bytes()
  194. * read a byte from the i2c device
  195. */
  196. static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
  197. {
  198. int ret;
  199. if (len < 1 || len > 64)
  200. return -EOPNOTSUPP;
  201. /*
  202. * NOTE: limited by the USB ctrl message constraints
  203. * Zero length reads always succeed, even if no device is connected
  204. */
  205. /* Read data from i2c device */
  206. ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
  207. if (ret < 0) {
  208. em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
  209. addr, ret);
  210. return ret;
  211. }
  212. /*
  213. * NOTE: some devices with two i2c busses have the bad habit to return 0
  214. * bytes if we are on bus B AND there was no write attempt to the
  215. * specified slave address before AND no device is present at the
  216. * requested slave address.
  217. * Anyway, the next check will fail with -ENODEV in this case, so avoid
  218. * spamming the system log on device probing and do nothing here.
  219. */
  220. /* Check success of the i2c operation */
  221. ret = dev->em28xx_read_reg(dev, 0x05);
  222. if (ret < 0) {
  223. em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n",
  224. ret);
  225. return ret;
  226. }
  227. if (ret > 0) {
  228. if (ret == 0x10) {
  229. return -ENODEV;
  230. } else {
  231. em28xx_warn("unknown i2c error (status=%i)\n", ret);
  232. return -EIO;
  233. }
  234. }
  235. return len;
  236. }
  237. /*
  238. * em28xx_i2c_check_for_device()
  239. * check if there is a i2c_device at the supplied address
  240. */
  241. static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
  242. {
  243. int ret;
  244. u8 buf;
  245. ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
  246. if (ret == 1)
  247. return 0;
  248. return (ret < 0) ? ret : -EIO;
  249. }
  250. /*
  251. * em25xx_bus_B_send_bytes
  252. * write bytes to the i2c device
  253. */
  254. static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
  255. u16 len)
  256. {
  257. int ret;
  258. if (len < 1 || len > 64)
  259. return -EOPNOTSUPP;
  260. /*
  261. * NOTE: limited by the USB ctrl message constraints
  262. * Zero length reads always succeed, even if no device is connected
  263. */
  264. /* Set register and write value */
  265. ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
  266. if (ret != len) {
  267. if (ret < 0) {
  268. em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
  269. addr, ret);
  270. return ret;
  271. } else {
  272. em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
  273. len, addr, ret);
  274. return -EIO;
  275. }
  276. }
  277. /* Check success */
  278. ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
  279. /*
  280. * NOTE: the only error we've seen so far is
  281. * 0x01 when the slave device is not present
  282. */
  283. if (!ret)
  284. return len;
  285. else if (ret > 0)
  286. return -ENODEV;
  287. return ret;
  288. /*
  289. * NOTE: With chip types (other chip IDs) which actually don't support
  290. * this operation, it seems to succeed ALWAYS ! (even if there is no
  291. * slave device or even no second i2c bus provided)
  292. */
  293. }
  294. /*
  295. * em25xx_bus_B_recv_bytes
  296. * read bytes from the i2c device
  297. */
  298. static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
  299. u16 len)
  300. {
  301. int ret;
  302. if (len < 1 || len > 64)
  303. return -EOPNOTSUPP;
  304. /*
  305. * NOTE: limited by the USB ctrl message constraints
  306. * Zero length reads always succeed, even if no device is connected
  307. */
  308. /* Read value */
  309. ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
  310. if (ret < 0) {
  311. em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
  312. addr, ret);
  313. return ret;
  314. }
  315. /*
  316. * NOTE: some devices with two i2c busses have the bad habit to return 0
  317. * bytes if we are on bus B AND there was no write attempt to the
  318. * specified slave address before AND no device is present at the
  319. * requested slave address.
  320. * Anyway, the next check will fail with -ENODEV in this case, so avoid
  321. * spamming the system log on device probing and do nothing here.
  322. */
  323. /* Check success */
  324. ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
  325. /*
  326. * NOTE: the only error we've seen so far is
  327. * 0x01 when the slave device is not present
  328. */
  329. if (!ret)
  330. return len;
  331. else if (ret > 0)
  332. return -ENODEV;
  333. return ret;
  334. /*
  335. * NOTE: With chip types (other chip IDs) which actually don't support
  336. * this operation, it seems to succeed ALWAYS ! (even if there is no
  337. * slave device or even no second i2c bus provided)
  338. */
  339. }
  340. /*
  341. * em25xx_bus_B_check_for_device()
  342. * check if there is a i2c device at the supplied address
  343. */
  344. static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
  345. {
  346. u8 buf;
  347. int ret;
  348. ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
  349. if (ret < 0)
  350. return ret;
  351. return 0;
  352. /*
  353. * NOTE: With chips which do not support this operation,
  354. * it seems to succeed ALWAYS ! (even if no device connected)
  355. */
  356. }
  357. static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
  358. {
  359. struct em28xx *dev = i2c_bus->dev;
  360. int rc = -EOPNOTSUPP;
  361. if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
  362. rc = em28xx_i2c_check_for_device(dev, addr);
  363. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
  364. rc = em2800_i2c_check_for_device(dev, addr);
  365. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
  366. rc = em25xx_bus_B_check_for_device(dev, addr);
  367. if (rc == -ENODEV) {
  368. if (i2c_debug)
  369. printk(" no device\n");
  370. }
  371. return rc;
  372. }
  373. static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
  374. struct i2c_msg msg)
  375. {
  376. struct em28xx *dev = i2c_bus->dev;
  377. u16 addr = msg.addr << 1;
  378. int byte, rc = -EOPNOTSUPP;
  379. if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
  380. rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
  381. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
  382. rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
  383. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
  384. rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
  385. if (i2c_debug) {
  386. for (byte = 0; byte < msg.len; byte++)
  387. printk(" %02x", msg.buf[byte]);
  388. }
  389. return rc;
  390. }
  391. static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
  392. struct i2c_msg msg, int stop)
  393. {
  394. struct em28xx *dev = i2c_bus->dev;
  395. u16 addr = msg.addr << 1;
  396. int byte, rc = -EOPNOTSUPP;
  397. if (i2c_debug) {
  398. for (byte = 0; byte < msg.len; byte++)
  399. printk(" %02x", msg.buf[byte]);
  400. }
  401. if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
  402. rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
  403. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
  404. rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
  405. else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
  406. rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
  407. return rc;
  408. }
  409. /*
  410. * em28xx_i2c_xfer()
  411. * the main i2c transfer function
  412. */
  413. static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  414. struct i2c_msg msgs[], int num)
  415. {
  416. struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
  417. struct em28xx *dev = i2c_bus->dev;
  418. unsigned bus = i2c_bus->bus;
  419. int addr, rc, i;
  420. u8 reg;
  421. rc = rt_mutex_trylock(&dev->i2c_bus_lock);
  422. if (rc < 0)
  423. return rc;
  424. /* Switch I2C bus if needed */
  425. if (bus != dev->cur_i2c_bus &&
  426. i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
  427. if (bus == 1)
  428. reg = EM2874_I2C_SECONDARY_BUS_SELECT;
  429. else
  430. reg = 0;
  431. em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
  432. EM2874_I2C_SECONDARY_BUS_SELECT);
  433. dev->cur_i2c_bus = bus;
  434. }
  435. if (num <= 0) {
  436. rt_mutex_unlock(&dev->i2c_bus_lock);
  437. return 0;
  438. }
  439. for (i = 0; i < num; i++) {
  440. addr = msgs[i].addr << 1;
  441. if (i2c_debug)
  442. printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
  443. dev->name, __func__ ,
  444. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  445. i == num - 1 ? "stop" : "nonstop",
  446. addr, msgs[i].len);
  447. if (!msgs[i].len) { /* no len: check only for device presence */
  448. rc = i2c_check_for_device(i2c_bus, addr);
  449. if (rc == -ENODEV) {
  450. rt_mutex_unlock(&dev->i2c_bus_lock);
  451. return rc;
  452. }
  453. } else if (msgs[i].flags & I2C_M_RD) {
  454. /* read bytes */
  455. rc = i2c_recv_bytes(i2c_bus, msgs[i]);
  456. } else {
  457. /* write bytes */
  458. rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
  459. }
  460. if (rc < 0) {
  461. if (i2c_debug)
  462. printk(" ERROR: %i\n", rc);
  463. rt_mutex_unlock(&dev->i2c_bus_lock);
  464. return rc;
  465. }
  466. if (i2c_debug)
  467. printk("\n");
  468. }
  469. rt_mutex_unlock(&dev->i2c_bus_lock);
  470. return num;
  471. }
  472. /*
  473. * based on linux/sunrpc/svcauth.h and linux/hash.h
  474. * The original hash function returns a different value, if arch is x86_64
  475. * or i386.
  476. */
  477. static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
  478. {
  479. unsigned long hash = 0;
  480. unsigned long l = 0;
  481. int len = 0;
  482. unsigned char c;
  483. do {
  484. if (len == length) {
  485. c = (char)len;
  486. len = -1;
  487. } else
  488. c = *buf++;
  489. l = (l << 8) | c;
  490. len++;
  491. if ((len & (32 / 8 - 1)) == 0)
  492. hash = ((hash^l) * 0x9e370001UL);
  493. } while (len);
  494. return (hash >> (32 - bits)) & 0xffffffffUL;
  495. }
  496. /*
  497. * Helper function to read data blocks from i2c clients with 8 or 16 bit
  498. * address width, 8 bit register width and auto incrementation been activated
  499. */
  500. static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
  501. bool addr_w16, u16 len, u8 *data)
  502. {
  503. int remain = len, rsize, rsize_max, ret;
  504. u8 buf[2];
  505. /* Sanity check */
  506. if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
  507. return -EINVAL;
  508. /* Select address */
  509. buf[0] = addr >> 8;
  510. buf[1] = addr & 0xff;
  511. ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
  512. if (ret < 0)
  513. return ret;
  514. /* Read data */
  515. if (dev->board.is_em2800)
  516. rsize_max = 4;
  517. else
  518. rsize_max = 64;
  519. while (remain > 0) {
  520. if (remain > rsize_max)
  521. rsize = rsize_max;
  522. else
  523. rsize = remain;
  524. ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
  525. if (ret < 0)
  526. return ret;
  527. remain -= rsize;
  528. data += rsize;
  529. }
  530. return len;
  531. }
  532. static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
  533. u8 **eedata, u16 *eedata_len)
  534. {
  535. const u16 len = 256;
  536. /*
  537. * FIXME common length/size for bytes to read, to display, hash
  538. * calculation and returned device dataset. Simplifies the code a lot,
  539. * but we might have to deal with multiple sizes in the future !
  540. */
  541. int i, err;
  542. struct em28xx_eeprom *dev_config;
  543. u8 buf, *data;
  544. *eedata = NULL;
  545. *eedata_len = 0;
  546. /* EEPROM is always on i2c bus 0 on all known devices. */
  547. dev->i2c_client[bus].addr = 0xa0 >> 1;
  548. /* Check if board has eeprom */
  549. err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  550. if (err < 0) {
  551. em28xx_info("board has no eeprom\n");
  552. return -ENODEV;
  553. }
  554. data = kzalloc(len, GFP_KERNEL);
  555. if (data == NULL)
  556. return -ENOMEM;
  557. /* Read EEPROM content */
  558. err = em28xx_i2c_read_block(dev, bus, 0x0000,
  559. dev->eeprom_addrwidth_16bit,
  560. len, data);
  561. if (err != len) {
  562. em28xx_errdev("failed to read eeprom (err=%d)\n", err);
  563. goto error;
  564. }
  565. /* Display eeprom content */
  566. for (i = 0; i < len; i++) {
  567. if (0 == (i % 16)) {
  568. if (dev->eeprom_addrwidth_16bit)
  569. em28xx_info("i2c eeprom %04x:", i);
  570. else
  571. em28xx_info("i2c eeprom %02x:", i);
  572. }
  573. printk(" %02x", data[i]);
  574. if (15 == (i % 16))
  575. printk("\n");
  576. }
  577. if (dev->eeprom_addrwidth_16bit)
  578. em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
  579. if (dev->eeprom_addrwidth_16bit &&
  580. data[0] == 0x26 && data[3] == 0x00) {
  581. /* new eeprom format; size 4-64kb */
  582. u16 mc_start;
  583. u16 hwconf_offset;
  584. dev->hash = em28xx_hash_mem(data, len, 32);
  585. mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
  586. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  587. data[0], data[1], data[2], data[3], dev->hash);
  588. em28xx_info("EEPROM info:\n");
  589. em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
  590. mc_start, data[2]);
  591. /*
  592. * boot configuration (address 0x0002):
  593. * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
  594. * [1] always selects 12 kb RAM
  595. * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
  596. * [4] 1 = force fast mode and no suspend for device testing
  597. * [5:7] USB PHY tuning registers; determined by device
  598. * characterization
  599. */
  600. /*
  601. * Read hardware config dataset offset from address
  602. * (microcode start + 46)
  603. */
  604. err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
  605. data);
  606. if (err != 2) {
  607. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  608. err);
  609. goto error;
  610. }
  611. /* Calculate hardware config dataset start address */
  612. hwconf_offset = mc_start + data[0] + (data[1] << 8);
  613. /* Read hardware config dataset */
  614. /*
  615. * NOTE: the microcode copy can be multiple pages long, but
  616. * we assume the hardware config dataset is the same as in
  617. * the old eeprom and not longer than 256 bytes.
  618. * tveeprom is currently also limited to 256 bytes.
  619. */
  620. err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
  621. data);
  622. if (err != len) {
  623. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  624. err);
  625. goto error;
  626. }
  627. /* Verify hardware config dataset */
  628. /* NOTE: not all devices provide this type of dataset */
  629. if (data[0] != 0x1a || data[1] != 0xeb ||
  630. data[2] != 0x67 || data[3] != 0x95) {
  631. em28xx_info("\tno hardware configuration dataset found in eeprom\n");
  632. kfree(data);
  633. return 0;
  634. }
  635. /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
  636. } else if (!dev->eeprom_addrwidth_16bit &&
  637. data[0] == 0x1a && data[1] == 0xeb &&
  638. data[2] == 0x67 && data[3] == 0x95) {
  639. dev->hash = em28xx_hash_mem(data, len, 32);
  640. em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
  641. data[0], data[1], data[2], data[3], dev->hash);
  642. em28xx_info("EEPROM info:\n");
  643. } else {
  644. em28xx_info("unknown eeprom format or eeprom corrupted !\n");
  645. err = -ENODEV;
  646. goto error;
  647. }
  648. *eedata = data;
  649. *eedata_len = len;
  650. dev_config = (void *)*eedata;
  651. switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
  652. case 0:
  653. em28xx_info("\tNo audio on board.\n");
  654. break;
  655. case 1:
  656. em28xx_info("\tAC97 audio (5 sample rates)\n");
  657. break;
  658. case 2:
  659. em28xx_info("\tI2S audio, sample rate=32k\n");
  660. break;
  661. case 3:
  662. em28xx_info("\tI2S audio, 3 sample rates\n");
  663. break;
  664. }
  665. if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
  666. em28xx_info("\tUSB Remote wakeup capable\n");
  667. if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
  668. em28xx_info("\tUSB Self power capable\n");
  669. switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
  670. case 0:
  671. em28xx_info("\t500mA max power\n");
  672. break;
  673. case 1:
  674. em28xx_info("\t400mA max power\n");
  675. break;
  676. case 2:
  677. em28xx_info("\t300mA max power\n");
  678. break;
  679. case 3:
  680. em28xx_info("\t200mA max power\n");
  681. break;
  682. }
  683. em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  684. dev_config->string_idx_table,
  685. le16_to_cpu(dev_config->string1),
  686. le16_to_cpu(dev_config->string2),
  687. le16_to_cpu(dev_config->string3));
  688. return 0;
  689. error:
  690. kfree(data);
  691. return err;
  692. }
  693. /* ----------------------------------------------------------- */
  694. /*
  695. * functionality()
  696. */
  697. static u32 functionality(struct i2c_adapter *i2c_adap)
  698. {
  699. struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
  700. if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
  701. (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
  702. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  703. } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
  704. return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
  705. ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
  706. }
  707. WARN(1, "Unknown i2c bus algorithm.\n");
  708. return 0;
  709. }
  710. static struct i2c_algorithm em28xx_algo = {
  711. .master_xfer = em28xx_i2c_xfer,
  712. .functionality = functionality,
  713. };
  714. static struct i2c_adapter em28xx_adap_template = {
  715. .owner = THIS_MODULE,
  716. .name = "em28xx",
  717. .algo = &em28xx_algo,
  718. };
  719. static struct i2c_client em28xx_client_template = {
  720. .name = "em28xx internal",
  721. };
  722. /* ----------------------------------------------------------- */
  723. /*
  724. * i2c_devs
  725. * incomplete list of known devices
  726. */
  727. static char *i2c_devs[128] = {
  728. [0x3e >> 1] = "remote IR sensor",
  729. [0x4a >> 1] = "saa7113h",
  730. [0x52 >> 1] = "drxk",
  731. [0x60 >> 1] = "remote IR sensor",
  732. [0x8e >> 1] = "remote IR sensor",
  733. [0x86 >> 1] = "tda9887",
  734. [0x80 >> 1] = "msp34xx",
  735. [0x88 >> 1] = "msp34xx",
  736. [0xa0 >> 1] = "eeprom",
  737. [0xb0 >> 1] = "tda9874",
  738. [0xb8 >> 1] = "tvp5150a",
  739. [0xba >> 1] = "webcam sensor or tvp5150a",
  740. [0xc0 >> 1] = "tuner (analog)",
  741. [0xc2 >> 1] = "tuner (analog)",
  742. [0xc4 >> 1] = "tuner (analog)",
  743. [0xc6 >> 1] = "tuner (analog)",
  744. };
  745. /*
  746. * do_i2c_scan()
  747. * check i2c address range for devices
  748. */
  749. void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
  750. {
  751. u8 i2c_devicelist[128];
  752. unsigned char buf;
  753. int i, rc;
  754. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  755. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  756. dev->i2c_client[bus].addr = i;
  757. rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
  758. if (rc < 0)
  759. continue;
  760. i2c_devicelist[i] = i;
  761. em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
  762. i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
  763. }
  764. if (bus == dev->def_i2c_bus)
  765. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  766. ARRAY_SIZE(i2c_devicelist), 32);
  767. }
  768. /*
  769. * em28xx_i2c_register()
  770. * register i2c bus
  771. */
  772. int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
  773. enum em28xx_i2c_algo_type algo_type)
  774. {
  775. int retval;
  776. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  777. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  778. if (bus >= NUM_I2C_BUSES)
  779. return -ENODEV;
  780. dev->i2c_adap[bus] = em28xx_adap_template;
  781. dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
  782. strcpy(dev->i2c_adap[bus].name, dev->name);
  783. dev->i2c_bus[bus].bus = bus;
  784. dev->i2c_bus[bus].algo_type = algo_type;
  785. dev->i2c_bus[bus].dev = dev;
  786. dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
  787. i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev);
  788. retval = i2c_add_adapter(&dev->i2c_adap[bus]);
  789. if (retval < 0) {
  790. em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
  791. __func__, retval);
  792. return retval;
  793. }
  794. dev->i2c_client[bus] = em28xx_client_template;
  795. dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
  796. /* Up to now, all eeproms are at bus 0 */
  797. if (!bus) {
  798. retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
  799. if ((retval < 0) && (retval != -ENODEV)) {
  800. em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
  801. __func__, retval);
  802. return retval;
  803. }
  804. }
  805. if (i2c_scan)
  806. em28xx_do_i2c_scan(dev, bus);
  807. return 0;
  808. }
  809. /*
  810. * em28xx_i2c_unregister()
  811. * unregister i2c_bus
  812. */
  813. int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
  814. {
  815. if (bus >= NUM_I2C_BUSES)
  816. return -ENODEV;
  817. i2c_del_adapter(&dev->i2c_adap[bus]);
  818. return 0;
  819. }