em28xx-i2c.c 26 KB

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