em28xx-i2c.c 26 KB

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