em28xx-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #define dprintk2(lvl, fmt, args...) \
  35. do { \
  36. if (i2c_debug >= lvl) { \
  37. printk(KERN_DEBUG "%s at %s: " fmt, \
  38. dev->name, __func__ , ##args); \
  39. } \
  40. } while (0)
  41. /*
  42. * em2800_i2c_send_max4()
  43. * send up to 4 bytes to the i2c device
  44. */
  45. static int em2800_i2c_send_max4(struct em28xx *dev, unsigned char addr,
  46. char *buf, int len)
  47. {
  48. int ret;
  49. int write_timeout;
  50. unsigned char b2[6];
  51. BUG_ON(len < 1 || len > 4);
  52. b2[5] = 0x80 + len - 1;
  53. b2[4] = addr;
  54. b2[3] = buf[0];
  55. if (len > 1)
  56. b2[2] = buf[1];
  57. if (len > 2)
  58. b2[1] = buf[2];
  59. if (len > 3)
  60. b2[0] = buf[3];
  61. ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
  62. if (ret != 2 + len) {
  63. em28xx_warn("writing to i2c device failed (error=%i)\n", ret);
  64. return -EIO;
  65. }
  66. for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0;
  67. write_timeout -= 5) {
  68. ret = dev->em28xx_read_reg(dev, 0x05);
  69. if (ret == 0x80 + len - 1)
  70. return len;
  71. msleep(5);
  72. }
  73. em28xx_warn("i2c write timed out\n");
  74. return -EIO;
  75. }
  76. /*
  77. * em2800_i2c_send_bytes()
  78. */
  79. static int em2800_i2c_send_bytes(void *data, unsigned char addr, char *buf,
  80. short len)
  81. {
  82. char *bufPtr = buf;
  83. int ret;
  84. int wrcount = 0;
  85. int count;
  86. int maxLen = 4;
  87. struct em28xx *dev = (struct em28xx *)data;
  88. while (len > 0) {
  89. count = (len > maxLen) ? maxLen : len;
  90. ret = em2800_i2c_send_max4(dev, addr, bufPtr, count);
  91. if (ret > 0) {
  92. len -= count;
  93. bufPtr += count;
  94. wrcount += count;
  95. } else
  96. return (ret < 0) ? ret : -EFAULT;
  97. }
  98. return wrcount;
  99. }
  100. /*
  101. * em2800_i2c_check_for_device()
  102. * check if there is a i2c_device at the supplied address
  103. */
  104. static int em2800_i2c_check_for_device(struct em28xx *dev, unsigned char addr)
  105. {
  106. char msg;
  107. int ret;
  108. int write_timeout;
  109. msg = addr;
  110. ret = dev->em28xx_write_regs(dev, 0x04, &msg, 1);
  111. if (ret < 0) {
  112. em28xx_warn("setting i2c device address failed (error=%i)\n",
  113. ret);
  114. return ret;
  115. }
  116. msg = 0x84;
  117. ret = dev->em28xx_write_regs(dev, 0x05, &msg, 1);
  118. if (ret < 0) {
  119. em28xx_warn("preparing i2c read failed (error=%i)\n", ret);
  120. return ret;
  121. }
  122. for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0;
  123. write_timeout -= 5) {
  124. unsigned reg = dev->em28xx_read_reg(dev, 0x5);
  125. if (reg == 0x94)
  126. return -ENODEV;
  127. else if (reg == 0x84)
  128. return 0;
  129. msleep(5);
  130. }
  131. return -ENODEV;
  132. }
  133. /*
  134. * em2800_i2c_recv_bytes()
  135. * read from the i2c device
  136. */
  137. static int em2800_i2c_recv_bytes(struct em28xx *dev, unsigned char addr,
  138. char *buf, int len)
  139. {
  140. int ret;
  141. /* check for the device and set i2c read address */
  142. ret = em2800_i2c_check_for_device(dev, addr);
  143. if (ret) {
  144. em28xx_warn
  145. ("preparing read at i2c address 0x%x failed (error=%i)\n",
  146. addr, ret);
  147. return ret;
  148. }
  149. ret = dev->em28xx_read_reg_req_len(dev, 0x0, 0x3, buf, len);
  150. if (ret < 0) {
  151. em28xx_warn("reading from i2c device at 0x%x failed (error=%i)",
  152. addr, ret);
  153. return ret;
  154. }
  155. return ret;
  156. }
  157. /*
  158. * em28xx_i2c_send_bytes()
  159. */
  160. static int em28xx_i2c_send_bytes(void *data, unsigned char addr, char *buf,
  161. short len, int stop)
  162. {
  163. int wrcount = 0;
  164. struct em28xx *dev = (struct em28xx *)data;
  165. int write_timeout, ret;
  166. wrcount = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  167. /* Seems to be required after a write */
  168. for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0;
  169. write_timeout -= 5) {
  170. ret = dev->em28xx_read_reg(dev, 0x05);
  171. if (!ret)
  172. break;
  173. msleep(5);
  174. }
  175. return wrcount;
  176. }
  177. /*
  178. * em28xx_i2c_recv_bytes()
  179. * read a byte from the i2c device
  180. */
  181. static int em28xx_i2c_recv_bytes(struct em28xx *dev, unsigned char addr,
  182. char *buf, int len)
  183. {
  184. int ret;
  185. ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
  186. if (ret < 0) {
  187. em28xx_warn("reading i2c device failed (error=%i)\n", ret);
  188. return ret;
  189. }
  190. if (dev->em28xx_read_reg(dev, 0x5) != 0)
  191. return -ENODEV;
  192. return ret;
  193. }
  194. /*
  195. * em28xx_i2c_check_for_device()
  196. * check if there is a i2c_device at the supplied address
  197. */
  198. static int em28xx_i2c_check_for_device(struct em28xx *dev, unsigned char addr)
  199. {
  200. int ret;
  201. ret = dev->em28xx_read_reg_req(dev, 2, addr);
  202. if (ret < 0) {
  203. em28xx_warn("reading from i2c device failed (error=%i)\n", ret);
  204. return ret;
  205. }
  206. if (dev->em28xx_read_reg(dev, 0x5) != 0)
  207. return -ENODEV;
  208. return 0;
  209. }
  210. /*
  211. * em28xx_i2c_xfer()
  212. * the main i2c transfer function
  213. */
  214. static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  215. struct i2c_msg msgs[], int num)
  216. {
  217. struct em28xx *dev = i2c_adap->algo_data;
  218. int addr, rc, i, byte;
  219. if (num <= 0)
  220. return 0;
  221. for (i = 0; i < num; i++) {
  222. addr = msgs[i].addr << 1;
  223. dprintk2(2, "%s %s addr=%x len=%d:",
  224. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  225. i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
  226. if (!msgs[i].len) { /* no len: check only for device presence */
  227. if (dev->board.is_em2800)
  228. rc = em2800_i2c_check_for_device(dev, addr);
  229. else
  230. rc = em28xx_i2c_check_for_device(dev, addr);
  231. if (rc < 0) {
  232. dprintk2(2, " no device\n");
  233. return rc;
  234. }
  235. } else if (msgs[i].flags & I2C_M_RD) {
  236. /* read bytes */
  237. if (dev->board.is_em2800)
  238. rc = em2800_i2c_recv_bytes(dev, addr,
  239. msgs[i].buf,
  240. msgs[i].len);
  241. else
  242. rc = em28xx_i2c_recv_bytes(dev, addr,
  243. msgs[i].buf,
  244. msgs[i].len);
  245. if (i2c_debug >= 2) {
  246. for (byte = 0; byte < msgs[i].len; byte++)
  247. printk(" %02x", msgs[i].buf[byte]);
  248. }
  249. } else {
  250. /* write bytes */
  251. if (i2c_debug >= 2) {
  252. for (byte = 0; byte < msgs[i].len; byte++)
  253. printk(" %02x", msgs[i].buf[byte]);
  254. }
  255. if (dev->board.is_em2800)
  256. rc = em2800_i2c_send_bytes(dev, addr,
  257. msgs[i].buf,
  258. msgs[i].len);
  259. else
  260. rc = em28xx_i2c_send_bytes(dev, addr,
  261. msgs[i].buf,
  262. msgs[i].len,
  263. i == num - 1);
  264. }
  265. if (rc < 0)
  266. goto err;
  267. if (i2c_debug >= 2)
  268. printk("\n");
  269. }
  270. return num;
  271. err:
  272. dprintk2(2, " ERROR: %i\n", rc);
  273. return rc;
  274. }
  275. /* based on linux/sunrpc/svcauth.h and linux/hash.h
  276. * The original hash function returns a different value, if arch is x86_64
  277. * or i386.
  278. */
  279. static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
  280. {
  281. unsigned long hash = 0;
  282. unsigned long l = 0;
  283. int len = 0;
  284. unsigned char c;
  285. do {
  286. if (len == length) {
  287. c = (char)len;
  288. len = -1;
  289. } else
  290. c = *buf++;
  291. l = (l << 8) | c;
  292. len++;
  293. if ((len & (32 / 8 - 1)) == 0)
  294. hash = ((hash^l) * 0x9e370001UL);
  295. } while (len);
  296. return (hash >> (32 - bits)) & 0xffffffffUL;
  297. }
  298. static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
  299. {
  300. unsigned char buf, *p = eedata;
  301. struct em28xx_eeprom *em_eeprom = (void *)eedata;
  302. int i, err, size = len, block;
  303. if (dev->chip_id == CHIP_ID_EM2874 ||
  304. dev->chip_id == CHIP_ID_EM28174 ||
  305. dev->chip_id == CHIP_ID_EM2884) {
  306. /* Empia switched to a 16-bit addressable eeprom in newer
  307. devices. While we could certainly write a routine to read
  308. the eeprom, there is nothing of use in there that cannot be
  309. accessed through registers, and there is the risk that we
  310. could corrupt the eeprom (since a 16-bit read call is
  311. interpreted as a write call by 8-bit eeproms).
  312. */
  313. return 0;
  314. }
  315. dev->i2c_client.addr = 0xa0 >> 1;
  316. /* Check if board has eeprom */
  317. err = i2c_master_recv(&dev->i2c_client, &buf, 0);
  318. if (err < 0) {
  319. em28xx_errdev("board has no eeprom\n");
  320. memset(eedata, 0, len);
  321. return -ENODEV;
  322. }
  323. buf = 0;
  324. err = i2c_master_send(&dev->i2c_client, &buf, 1);
  325. if (err != 1) {
  326. printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
  327. dev->name, err);
  328. return err;
  329. }
  330. while (size > 0) {
  331. if (size > 16)
  332. block = 16;
  333. else
  334. block = size;
  335. if (block !=
  336. (err = i2c_master_recv(&dev->i2c_client, p, block))) {
  337. printk(KERN_WARNING
  338. "%s: i2c eeprom read error (err=%d)\n",
  339. dev->name, err);
  340. return err;
  341. }
  342. size -= block;
  343. p += block;
  344. }
  345. for (i = 0; i < len; i++) {
  346. if (0 == (i % 16))
  347. printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
  348. printk(" %02x", eedata[i]);
  349. if (15 == (i % 16))
  350. printk("\n");
  351. }
  352. if (em_eeprom->id == 0x9567eb1a)
  353. dev->hash = em28xx_hash_mem(eedata, len, 32);
  354. printk(KERN_INFO "%s: EEPROM ID= 0x%08x, EEPROM hash = 0x%08lx\n",
  355. dev->name, em_eeprom->id, dev->hash);
  356. printk(KERN_INFO "%s: EEPROM info:\n", dev->name);
  357. switch (em_eeprom->chip_conf >> 4 & 0x3) {
  358. case 0:
  359. printk(KERN_INFO "%s:\tNo audio on board.\n", dev->name);
  360. break;
  361. case 1:
  362. printk(KERN_INFO "%s:\tAC97 audio (5 sample rates)\n",
  363. dev->name);
  364. break;
  365. case 2:
  366. printk(KERN_INFO "%s:\tI2S audio, sample rate=32k\n",
  367. dev->name);
  368. break;
  369. case 3:
  370. printk(KERN_INFO "%s:\tI2S audio, 3 sample rates\n",
  371. dev->name);
  372. break;
  373. }
  374. if (em_eeprom->chip_conf & 1 << 3)
  375. printk(KERN_INFO "%s:\tUSB Remote wakeup capable\n", dev->name);
  376. if (em_eeprom->chip_conf & 1 << 2)
  377. printk(KERN_INFO "%s:\tUSB Self power capable\n", dev->name);
  378. switch (em_eeprom->chip_conf & 0x3) {
  379. case 0:
  380. printk(KERN_INFO "%s:\t500mA max power\n", dev->name);
  381. break;
  382. case 1:
  383. printk(KERN_INFO "%s:\t400mA max power\n", dev->name);
  384. break;
  385. case 2:
  386. printk(KERN_INFO "%s:\t300mA max power\n", dev->name);
  387. break;
  388. case 3:
  389. printk(KERN_INFO "%s:\t200mA max power\n", dev->name);
  390. break;
  391. }
  392. printk(KERN_INFO "%s:\tTable at 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  393. dev->name,
  394. em_eeprom->string_idx_table,
  395. em_eeprom->string1,
  396. em_eeprom->string2,
  397. em_eeprom->string3);
  398. return 0;
  399. }
  400. /* ----------------------------------------------------------- */
  401. /*
  402. * functionality()
  403. */
  404. static u32 functionality(struct i2c_adapter *adap)
  405. {
  406. return I2C_FUNC_SMBUS_EMUL;
  407. }
  408. static struct i2c_algorithm em28xx_algo = {
  409. .master_xfer = em28xx_i2c_xfer,
  410. .functionality = functionality,
  411. };
  412. static struct i2c_adapter em28xx_adap_template = {
  413. .owner = THIS_MODULE,
  414. .name = "em28xx",
  415. .algo = &em28xx_algo,
  416. };
  417. static struct i2c_client em28xx_client_template = {
  418. .name = "em28xx internal",
  419. };
  420. /* ----------------------------------------------------------- */
  421. /*
  422. * i2c_devs
  423. * incomplete list of known devices
  424. */
  425. static char *i2c_devs[128] = {
  426. [0x4a >> 1] = "saa7113h",
  427. [0x60 >> 1] = "remote IR sensor",
  428. [0x8e >> 1] = "remote IR sensor",
  429. [0x86 >> 1] = "tda9887",
  430. [0x80 >> 1] = "msp34xx",
  431. [0x88 >> 1] = "msp34xx",
  432. [0xa0 >> 1] = "eeprom",
  433. [0xb0 >> 1] = "tda9874",
  434. [0xb8 >> 1] = "tvp5150a",
  435. [0xba >> 1] = "webcam sensor or tvp5150a",
  436. [0xc0 >> 1] = "tuner (analog)",
  437. [0xc2 >> 1] = "tuner (analog)",
  438. [0xc4 >> 1] = "tuner (analog)",
  439. [0xc6 >> 1] = "tuner (analog)",
  440. };
  441. /*
  442. * do_i2c_scan()
  443. * check i2c address range for devices
  444. */
  445. void em28xx_do_i2c_scan(struct em28xx *dev)
  446. {
  447. u8 i2c_devicelist[128];
  448. unsigned char buf;
  449. int i, rc;
  450. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  451. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  452. dev->i2c_client.addr = i;
  453. rc = i2c_master_recv(&dev->i2c_client, &buf, 0);
  454. if (rc < 0)
  455. continue;
  456. i2c_devicelist[i] = i;
  457. printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n",
  458. dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  459. }
  460. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  461. ARRAY_SIZE(i2c_devicelist), 32);
  462. }
  463. /*
  464. * em28xx_i2c_register()
  465. * register i2c bus
  466. */
  467. int em28xx_i2c_register(struct em28xx *dev)
  468. {
  469. int retval;
  470. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  471. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  472. dev->i2c_adap = em28xx_adap_template;
  473. dev->i2c_adap.dev.parent = &dev->udev->dev;
  474. strcpy(dev->i2c_adap.name, dev->name);
  475. dev->i2c_adap.algo_data = dev;
  476. i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
  477. retval = i2c_add_adapter(&dev->i2c_adap);
  478. if (retval < 0) {
  479. em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
  480. __func__, retval);
  481. return retval;
  482. }
  483. dev->i2c_client = em28xx_client_template;
  484. dev->i2c_client.adapter = &dev->i2c_adap;
  485. retval = em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
  486. if ((retval < 0) && (retval != -ENODEV)) {
  487. em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
  488. __func__, retval);
  489. return retval;
  490. }
  491. if (i2c_scan)
  492. em28xx_do_i2c_scan(dev);
  493. /* Instantiate the IR receiver device, if present */
  494. em28xx_register_i2c_ir(dev);
  495. return 0;
  496. }
  497. /*
  498. * em28xx_i2c_unregister()
  499. * unregister i2c_bus
  500. */
  501. int em28xx_i2c_unregister(struct em28xx *dev)
  502. {
  503. i2c_del_adapter(&dev->i2c_adap);
  504. return 0;
  505. }