em28xx-i2c.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/usb.h>
  22. #include <linux/i2c.h>
  23. #include "em28xx.h"
  24. #include "tuner-xc2028.h"
  25. #include <media/v4l2-common.h>
  26. #include <media/tuner.h>
  27. /* ----------------------------------------------------------- */
  28. static unsigned int i2c_scan;
  29. module_param(i2c_scan, int, 0444);
  30. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  31. static unsigned int i2c_debug;
  32. module_param(i2c_debug, int, 0644);
  33. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  34. /*
  35. * em2800_i2c_send_bytes()
  36. * send up to 4 bytes to the em2800 i2c device
  37. */
  38. static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  39. {
  40. int ret;
  41. int write_timeout;
  42. u8 b2[6];
  43. if (len < 1 || len > 4)
  44. return -EOPNOTSUPP;
  45. BUG_ON(len < 1 || len > 4);
  46. b2[5] = 0x80 + len - 1;
  47. b2[4] = addr;
  48. b2[3] = buf[0];
  49. if (len > 1)
  50. b2[2] = buf[1];
  51. if (len > 2)
  52. b2[1] = buf[2];
  53. if (len > 3)
  54. b2[0] = buf[3];
  55. /* trigger write */
  56. ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
  57. if (ret != 2 + len) {
  58. em28xx_warn("failed to trigger write to i2c address 0x%x "
  59. "(error=%i)\n", addr, ret);
  60. return (ret < 0) ? ret : -EIO;
  61. }
  62. /* wait for completion */
  63. for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
  64. write_timeout -= 5) {
  65. ret = dev->em28xx_read_reg(dev, 0x05);
  66. if (ret == 0x80 + len - 1) {
  67. return len;
  68. } else if (ret == 0x94 + len - 1) {
  69. return -ENODEV;
  70. } else if (ret < 0) {
  71. em28xx_warn("failed to get i2c transfer status from "
  72. "bridge register (error=%i)\n", ret);
  73. return ret;
  74. }
  75. msleep(5);
  76. }
  77. em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
  78. return -EIO;
  79. }
  80. /*
  81. * em2800_i2c_recv_bytes()
  82. * read up to 4 bytes from the em2800 i2c device
  83. */
  84. static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
  85. {
  86. u8 buf2[4];
  87. int ret;
  88. int read_timeout;
  89. int i;
  90. if (len < 1 || len > 4)
  91. return -EOPNOTSUPP;
  92. /* trigger read */
  93. buf2[1] = 0x84 + len - 1;
  94. buf2[0] = addr;
  95. ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
  96. if (ret != 2) {
  97. em28xx_warn("failed to trigger read from i2c address 0x%x "
  98. "(error=%i)\n", addr, ret);
  99. return (ret < 0) ? ret : -EIO;
  100. }
  101. /* wait for completion */
  102. for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0;
  103. read_timeout -= 5) {
  104. ret = dev->em28xx_read_reg(dev, 0x05);
  105. if (ret == 0x84 + len - 1) {
  106. break;
  107. } else if (ret == 0x94 + len - 1) {
  108. return -ENODEV;
  109. } else if (ret < 0) {
  110. em28xx_warn("failed to get i2c transfer status from "
  111. "bridge register (error=%i)\n", ret);
  112. return ret;
  113. }
  114. msleep(5);
  115. }
  116. if (ret != 0x84 + len - 1)
  117. em28xx_warn("read from i2c device at 0x%x timed out\n", addr);
  118. /* get the received message */
  119. ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
  120. if (ret != len) {
  121. em28xx_warn("reading from i2c device at 0x%x failed: "
  122. "couldn't get the received message from the bridge "
  123. "(error=%i)\n", 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. /* NOTE: limited by the USB ctrl message constraints
  153. * Zero length reads always succeed, even if no device is connected */
  154. /* Write to i2c device */
  155. ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  156. if (ret != len) {
  157. if (ret < 0) {
  158. em28xx_warn("writing to i2c device at 0x%x failed "
  159. "(error=%i)\n", addr, ret);
  160. return ret;
  161. } else {
  162. em28xx_warn("%i bytes write to i2c device at 0x%x "
  163. "requested, but %i bytes written\n",
  164. len, addr, ret);
  165. return -EIO;
  166. }
  167. }
  168. /* Check success of the i2c operation */
  169. for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
  170. write_timeout -= 5) {
  171. ret = dev->em28xx_read_reg(dev, 0x05);
  172. if (ret == 0) { /* success */
  173. return len;
  174. } else if (ret == 0x10) {
  175. return -ENODEV;
  176. } else if (ret < 0) {
  177. em28xx_warn("failed to read i2c transfer status from "
  178. "bridge (error=%i)\n", ret);
  179. return ret;
  180. }
  181. msleep(5);
  182. /* NOTE: do we really have to wait for success ?
  183. Never seen anything else than 0x00 or 0x10
  184. (even with high payload) ... */
  185. }
  186. em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
  187. return -EIO;
  188. }
  189. /*
  190. * em28xx_i2c_recv_bytes()
  191. * read a byte from the i2c device
  192. */
  193. static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
  194. {
  195. int ret;
  196. if (len < 1 || len > 64)
  197. return -EOPNOTSUPP;
  198. /* NOTE: limited by the USB ctrl message constraints
  199. * Zero length reads always succeed, even if no device is connected */
  200. /* Read data from i2c device */
  201. ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
  202. if (ret != len) {
  203. if (ret < 0) {
  204. em28xx_warn("reading from i2c device at 0x%x failed "
  205. "(error=%i)\n", addr, ret);
  206. return ret;
  207. } else {
  208. em28xx_warn("%i bytes requested from i2c device at "
  209. "0x%x, but %i bytes received\n",
  210. len, addr, ret);
  211. return -EIO;
  212. }
  213. }
  214. /* Check success of the i2c operation */
  215. ret = dev->em28xx_read_reg(dev, 0x05);
  216. if (ret < 0) {
  217. em28xx_warn("failed to read i2c transfer status from "
  218. "bridge (error=%i)\n", ret);
  219. return ret;
  220. }
  221. if (ret > 0) {
  222. if (ret == 0x10) {
  223. return -ENODEV;
  224. } else {
  225. em28xx_warn("unknown i2c error (status=%i)\n", ret);
  226. return -EIO;
  227. }
  228. }
  229. return len;
  230. }
  231. /*
  232. * em28xx_i2c_check_for_device()
  233. * check if there is a i2c_device at the supplied address
  234. */
  235. static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
  236. {
  237. int ret;
  238. u8 buf;
  239. ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
  240. if (ret == 1)
  241. return 0;
  242. return (ret < 0) ? ret : -EIO;
  243. }
  244. /*
  245. * em28xx_i2c_xfer()
  246. * the main i2c transfer function
  247. */
  248. static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  249. struct i2c_msg msgs[], int num)
  250. {
  251. struct em28xx *dev = i2c_adap->algo_data;
  252. int addr, rc, i, byte;
  253. if (num <= 0)
  254. return 0;
  255. for (i = 0; i < num; i++) {
  256. addr = msgs[i].addr << 1;
  257. if (i2c_debug)
  258. printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
  259. dev->name, __func__ ,
  260. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  261. i == num - 1 ? "stop" : "nonstop",
  262. addr, msgs[i].len);
  263. if (!msgs[i].len) { /* no len: check only for device presence */
  264. if (dev->board.is_em2800)
  265. rc = em2800_i2c_check_for_device(dev, addr);
  266. else
  267. rc = em28xx_i2c_check_for_device(dev, addr);
  268. if (rc == -ENODEV) {
  269. if (i2c_debug)
  270. printk(" no device\n");
  271. return rc;
  272. }
  273. } else if (msgs[i].flags & I2C_M_RD) {
  274. /* read bytes */
  275. if (dev->board.is_em2800)
  276. rc = em2800_i2c_recv_bytes(dev, addr,
  277. msgs[i].buf,
  278. msgs[i].len);
  279. else
  280. rc = em28xx_i2c_recv_bytes(dev, addr,
  281. msgs[i].buf,
  282. msgs[i].len);
  283. if (i2c_debug) {
  284. for (byte = 0; byte < msgs[i].len; byte++)
  285. printk(" %02x", msgs[i].buf[byte]);
  286. }
  287. } else {
  288. /* write bytes */
  289. if (i2c_debug) {
  290. for (byte = 0; byte < msgs[i].len; byte++)
  291. printk(" %02x", msgs[i].buf[byte]);
  292. }
  293. if (dev->board.is_em2800)
  294. rc = em2800_i2c_send_bytes(dev, addr,
  295. msgs[i].buf,
  296. msgs[i].len);
  297. else
  298. rc = em28xx_i2c_send_bytes(dev, addr,
  299. msgs[i].buf,
  300. msgs[i].len,
  301. i == num - 1);
  302. }
  303. if (rc < 0) {
  304. if (i2c_debug)
  305. printk(" ERROR: %i\n", rc);
  306. return rc;
  307. }
  308. if (i2c_debug)
  309. printk("\n");
  310. }
  311. return num;
  312. }
  313. /* based on linux/sunrpc/svcauth.h and linux/hash.h
  314. * The original hash function returns a different value, if arch is x86_64
  315. * or i386.
  316. */
  317. static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
  318. {
  319. unsigned long hash = 0;
  320. unsigned long l = 0;
  321. int len = 0;
  322. unsigned char c;
  323. do {
  324. if (len == length) {
  325. c = (char)len;
  326. len = -1;
  327. } else
  328. c = *buf++;
  329. l = (l << 8) | c;
  330. len++;
  331. if ((len & (32 / 8 - 1)) == 0)
  332. hash = ((hash^l) * 0x9e370001UL);
  333. } while (len);
  334. return (hash >> (32 - bits)) & 0xffffffffUL;
  335. }
  336. /* Helper function to read data blocks from i2c clients with 8 or 16 bit
  337. * address width, 8 bit register width and auto incrementation been activated */
  338. static int em28xx_i2c_read_block(struct em28xx *dev, u16 addr, bool addr_w16,
  339. u16 len, u8 *data)
  340. {
  341. int remain = len, rsize, rsize_max, ret;
  342. u8 buf[2];
  343. /* Sanity check */
  344. if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
  345. return -EINVAL;
  346. /* Select address */
  347. buf[0] = addr >> 8;
  348. buf[1] = addr & 0xff;
  349. ret = i2c_master_send(&dev->i2c_client, buf + !addr_w16, 1 + addr_w16);
  350. if (ret < 0)
  351. return ret;
  352. /* Read data */
  353. if (dev->board.is_em2800)
  354. rsize_max = 4;
  355. else
  356. rsize_max = 64;
  357. while (remain > 0) {
  358. if (remain > rsize_max)
  359. rsize = rsize_max;
  360. else
  361. rsize = remain;
  362. ret = i2c_master_recv(&dev->i2c_client, data, rsize);
  363. if (ret < 0)
  364. return ret;
  365. remain -= rsize;
  366. data += rsize;
  367. }
  368. return len;
  369. }
  370. static int em28xx_i2c_eeprom(struct em28xx *dev, u8 **eedata, u16 *eedata_len)
  371. {
  372. const u16 len = 256;
  373. /* FIXME common length/size for bytes to read, to display, hash
  374. * calculation and returned device dataset. Simplifies the code a lot,
  375. * but we might have to deal with multiple sizes in the future ! */
  376. int i, err;
  377. struct em28xx_eeprom *dev_config;
  378. u8 buf, *data;
  379. *eedata = NULL;
  380. *eedata_len = 0;
  381. dev->i2c_client.addr = 0xa0 >> 1;
  382. /* Check if board has eeprom */
  383. err = i2c_master_recv(&dev->i2c_client, &buf, 0);
  384. if (err < 0) {
  385. em28xx_info("board has no eeprom\n");
  386. return -ENODEV;
  387. }
  388. data = kzalloc(len, GFP_KERNEL);
  389. if (data == NULL)
  390. return -ENOMEM;
  391. /* Read EEPROM content */
  392. err = em28xx_i2c_read_block(dev, 0x0000, dev->eeprom_addrwidth_16bit,
  393. len, data);
  394. if (err != len) {
  395. em28xx_errdev("failed to read eeprom (err=%d)\n", err);
  396. goto error;
  397. }
  398. /* Display eeprom content */
  399. for (i = 0; i < len; i++) {
  400. if (0 == (i % 16)) {
  401. if (dev->eeprom_addrwidth_16bit)
  402. em28xx_info("i2c eeprom %04x:", i);
  403. else
  404. em28xx_info("i2c eeprom %02x:", i);
  405. }
  406. printk(" %02x", data[i]);
  407. if (15 == (i % 16))
  408. printk("\n");
  409. }
  410. if (dev->eeprom_addrwidth_16bit)
  411. em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
  412. if (dev->eeprom_addrwidth_16bit &&
  413. data[0] == 0x26 && data[3] == 0x00) {
  414. /* new eeprom format; size 4-64kb */
  415. u16 mc_start;
  416. u16 hwconf_offset;
  417. dev->hash = em28xx_hash_mem(data, len, 32);
  418. mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
  419. em28xx_info("EEPROM ID = %02x %02x %02x %02x, "
  420. "EEPROM hash = 0x%08lx\n",
  421. data[0], data[1], data[2], data[3], dev->hash);
  422. em28xx_info("EEPROM info:\n");
  423. em28xx_info("\tmicrocode start address = 0x%04x, "
  424. "boot configuration = 0x%02x\n",
  425. mc_start, data[2]);
  426. /* boot configuration (address 0x0002):
  427. * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
  428. * [1] always selects 12 kb RAM
  429. * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
  430. * [4] 1 = force fast mode and no suspend for device testing
  431. * [5:7] USB PHY tuning registers; determined by device
  432. * characterization
  433. */
  434. /* Read hardware config dataset offset from address
  435. * (microcode start + 46) */
  436. err = em28xx_i2c_read_block(dev, mc_start + 46, 1, 2, data);
  437. if (err != 2) {
  438. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  439. err);
  440. goto error;
  441. }
  442. /* Calculate hardware config dataset start address */
  443. hwconf_offset = mc_start + data[0] + (data[1] << 8);
  444. /* Read hardware config dataset */
  445. /* NOTE: the microcode copy can be multiple pages long, but
  446. * we assume the hardware config dataset is the same as in
  447. * the old eeprom and not longer than 256 bytes.
  448. * tveeprom is currently also limited to 256 bytes.
  449. */
  450. err = em28xx_i2c_read_block(dev, hwconf_offset, 1, len, data);
  451. if (err != len) {
  452. em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
  453. err);
  454. goto error;
  455. }
  456. /* Verify hardware config dataset */
  457. /* NOTE: not all devices provide this type of dataset */
  458. if (data[0] != 0x1a || data[1] != 0xeb ||
  459. data[2] != 0x67 || data[3] != 0x95) {
  460. em28xx_info("\tno hardware configuration dataset found in eeprom\n");
  461. kfree(data);
  462. return 0;
  463. }
  464. /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
  465. } else if (!dev->eeprom_addrwidth_16bit &&
  466. data[0] == 0x1a && data[1] == 0xeb &&
  467. data[2] == 0x67 && data[3] == 0x95) {
  468. dev->hash = em28xx_hash_mem(data, len, 32);
  469. em28xx_info("EEPROM ID = %02x %02x %02x %02x, "
  470. "EEPROM hash = 0x%08lx\n",
  471. data[0], data[1], data[2], data[3], dev->hash);
  472. em28xx_info("EEPROM info:\n");
  473. } else {
  474. em28xx_info("unknown eeprom format or eeprom corrupted !\n");
  475. err = -ENODEV;
  476. goto error;
  477. }
  478. *eedata = data;
  479. *eedata_len = len;
  480. dev_config = (void *)eedata;
  481. switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
  482. case 0:
  483. em28xx_info("\tNo audio on board.\n");
  484. break;
  485. case 1:
  486. em28xx_info("\tAC97 audio (5 sample rates)\n");
  487. break;
  488. case 2:
  489. em28xx_info("\tI2S audio, sample rate=32k\n");
  490. break;
  491. case 3:
  492. em28xx_info("\tI2S audio, 3 sample rates\n");
  493. break;
  494. }
  495. if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
  496. em28xx_info("\tUSB Remote wakeup capable\n");
  497. if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
  498. em28xx_info("\tUSB Self power capable\n");
  499. switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
  500. case 0:
  501. em28xx_info("\t500mA max power\n");
  502. break;
  503. case 1:
  504. em28xx_info("\t400mA max power\n");
  505. break;
  506. case 2:
  507. em28xx_info("\t300mA max power\n");
  508. break;
  509. case 3:
  510. em28xx_info("\t200mA max power\n");
  511. break;
  512. }
  513. em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  514. dev_config->string_idx_table,
  515. le16_to_cpu(dev_config->string1),
  516. le16_to_cpu(dev_config->string2),
  517. le16_to_cpu(dev_config->string3));
  518. return 0;
  519. error:
  520. kfree(data);
  521. return err;
  522. }
  523. /* ----------------------------------------------------------- */
  524. /*
  525. * functionality()
  526. */
  527. static u32 functionality(struct i2c_adapter *adap)
  528. {
  529. struct em28xx *dev = adap->algo_data;
  530. u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  531. if (dev->board.is_em2800)
  532. func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
  533. return func_flags;
  534. }
  535. static struct i2c_algorithm em28xx_algo = {
  536. .master_xfer = em28xx_i2c_xfer,
  537. .functionality = functionality,
  538. };
  539. static struct i2c_adapter em28xx_adap_template = {
  540. .owner = THIS_MODULE,
  541. .name = "em28xx",
  542. .algo = &em28xx_algo,
  543. };
  544. static struct i2c_client em28xx_client_template = {
  545. .name = "em28xx internal",
  546. };
  547. /* ----------------------------------------------------------- */
  548. /*
  549. * i2c_devs
  550. * incomplete list of known devices
  551. */
  552. static char *i2c_devs[128] = {
  553. [0x3e >> 1] = "remote IR sensor",
  554. [0x4a >> 1] = "saa7113h",
  555. [0x52 >> 1] = "drxk",
  556. [0x60 >> 1] = "remote IR sensor",
  557. [0x8e >> 1] = "remote IR sensor",
  558. [0x86 >> 1] = "tda9887",
  559. [0x80 >> 1] = "msp34xx",
  560. [0x88 >> 1] = "msp34xx",
  561. [0xa0 >> 1] = "eeprom",
  562. [0xb0 >> 1] = "tda9874",
  563. [0xb8 >> 1] = "tvp5150a",
  564. [0xba >> 1] = "webcam sensor or tvp5150a",
  565. [0xc0 >> 1] = "tuner (analog)",
  566. [0xc2 >> 1] = "tuner (analog)",
  567. [0xc4 >> 1] = "tuner (analog)",
  568. [0xc6 >> 1] = "tuner (analog)",
  569. };
  570. /*
  571. * do_i2c_scan()
  572. * check i2c address range for devices
  573. */
  574. void em28xx_do_i2c_scan(struct em28xx *dev)
  575. {
  576. u8 i2c_devicelist[128];
  577. unsigned char buf;
  578. int i, rc;
  579. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  580. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  581. dev->i2c_client.addr = i;
  582. rc = i2c_master_recv(&dev->i2c_client, &buf, 0);
  583. if (rc < 0)
  584. continue;
  585. i2c_devicelist[i] = i;
  586. em28xx_info("found i2c device @ 0x%x [%s]\n",
  587. i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  588. }
  589. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  590. ARRAY_SIZE(i2c_devicelist), 32);
  591. }
  592. /*
  593. * em28xx_i2c_register()
  594. * register i2c bus
  595. */
  596. int em28xx_i2c_register(struct em28xx *dev)
  597. {
  598. int retval;
  599. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  600. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  601. dev->i2c_adap = em28xx_adap_template;
  602. dev->i2c_adap.dev.parent = &dev->udev->dev;
  603. strcpy(dev->i2c_adap.name, dev->name);
  604. dev->i2c_adap.algo_data = dev;
  605. i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
  606. retval = i2c_add_adapter(&dev->i2c_adap);
  607. if (retval < 0) {
  608. em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
  609. __func__, retval);
  610. return retval;
  611. }
  612. dev->i2c_client = em28xx_client_template;
  613. dev->i2c_client.adapter = &dev->i2c_adap;
  614. retval = em28xx_i2c_eeprom(dev, &dev->eedata, &dev->eedata_len);
  615. if ((retval < 0) && (retval != -ENODEV)) {
  616. em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
  617. __func__, retval);
  618. return retval;
  619. }
  620. if (i2c_scan)
  621. em28xx_do_i2c_scan(dev);
  622. return 0;
  623. }
  624. /*
  625. * em28xx_i2c_unregister()
  626. * unregister i2c_bus
  627. */
  628. int em28xx_i2c_unregister(struct em28xx *dev)
  629. {
  630. i2c_del_adapter(&dev->i2c_adap);
  631. return 0;
  632. }