em28xx-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 dprintk1(lvl, fmt, args...) \
  35. do { \
  36. if (i2c_debug >= lvl) { \
  37. printk(fmt, ##args); \
  38. } \
  39. } while (0)
  40. #define dprintk2(lvl, fmt, args...) \
  41. do { \
  42. if (i2c_debug >= lvl) { \
  43. printk(KERN_DEBUG "%s at %s: " fmt, \
  44. dev->name, __func__ , ##args); \
  45. } \
  46. } while (0)
  47. /*
  48. * em2800_i2c_send_max4()
  49. * send up to 4 bytes to the i2c device
  50. */
  51. static int em2800_i2c_send_max4(struct em28xx *dev, unsigned char addr,
  52. char *buf, int len)
  53. {
  54. int ret;
  55. int write_timeout;
  56. unsigned char b2[6];
  57. BUG_ON(len < 1 || len > 4);
  58. b2[5] = 0x80 + len - 1;
  59. b2[4] = addr;
  60. b2[3] = buf[0];
  61. if (len > 1)
  62. b2[2] = buf[1];
  63. if (len > 2)
  64. b2[1] = buf[2];
  65. if (len > 3)
  66. b2[0] = buf[3];
  67. ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
  68. if (ret != 2 + len) {
  69. em28xx_warn("writing to i2c device failed (error=%i)\n", ret);
  70. return -EIO;
  71. }
  72. for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0;
  73. write_timeout -= 5) {
  74. ret = dev->em28xx_read_reg(dev, 0x05);
  75. if (ret == 0x80 + len - 1)
  76. return len;
  77. msleep(5);
  78. }
  79. em28xx_warn("i2c write timed out\n");
  80. return -EIO;
  81. }
  82. /*
  83. * em2800_i2c_send_bytes()
  84. */
  85. static int em2800_i2c_send_bytes(void *data, unsigned char addr, char *buf,
  86. short len)
  87. {
  88. char *bufPtr = buf;
  89. int ret;
  90. int wrcount = 0;
  91. int count;
  92. int maxLen = 4;
  93. struct em28xx *dev = (struct em28xx *)data;
  94. while (len > 0) {
  95. count = (len > maxLen) ? maxLen : len;
  96. ret = em2800_i2c_send_max4(dev, addr, bufPtr, count);
  97. if (ret > 0) {
  98. len -= count;
  99. bufPtr += count;
  100. wrcount += count;
  101. } else
  102. return (ret < 0) ? ret : -EFAULT;
  103. }
  104. return wrcount;
  105. }
  106. /*
  107. * em2800_i2c_check_for_device()
  108. * check if there is a i2c_device at the supplied address
  109. */
  110. static int em2800_i2c_check_for_device(struct em28xx *dev, unsigned char addr)
  111. {
  112. char msg;
  113. int ret;
  114. int write_timeout;
  115. msg = addr;
  116. ret = dev->em28xx_write_regs(dev, 0x04, &msg, 1);
  117. if (ret < 0) {
  118. em28xx_warn("setting i2c device address failed (error=%i)\n",
  119. ret);
  120. return ret;
  121. }
  122. msg = 0x84;
  123. ret = dev->em28xx_write_regs(dev, 0x05, &msg, 1);
  124. if (ret < 0) {
  125. em28xx_warn("preparing i2c read failed (error=%i)\n", ret);
  126. return ret;
  127. }
  128. for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0;
  129. write_timeout -= 5) {
  130. unsigned reg = dev->em28xx_read_reg(dev, 0x5);
  131. if (reg == 0x94)
  132. return -ENODEV;
  133. else if (reg == 0x84)
  134. return 0;
  135. msleep(5);
  136. }
  137. return -ENODEV;
  138. }
  139. /*
  140. * em2800_i2c_recv_bytes()
  141. * read from the i2c device
  142. */
  143. static int em2800_i2c_recv_bytes(struct em28xx *dev, unsigned char addr,
  144. char *buf, int len)
  145. {
  146. int ret;
  147. /* check for the device and set i2c read address */
  148. ret = em2800_i2c_check_for_device(dev, addr);
  149. if (ret) {
  150. em28xx_warn
  151. ("preparing read at i2c address 0x%x failed (error=%i)\n",
  152. addr, ret);
  153. return ret;
  154. }
  155. ret = dev->em28xx_read_reg_req_len(dev, 0x0, 0x3, buf, len);
  156. if (ret < 0) {
  157. em28xx_warn("reading from i2c device at 0x%x failed (error=%i)",
  158. addr, ret);
  159. return ret;
  160. }
  161. return ret;
  162. }
  163. /*
  164. * em28xx_i2c_send_bytes()
  165. * untested for more than 4 bytes
  166. */
  167. static int em28xx_i2c_send_bytes(void *data, unsigned char addr, char *buf,
  168. short len, int stop)
  169. {
  170. int wrcount = 0;
  171. struct em28xx *dev = (struct em28xx *)data;
  172. wrcount = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  173. return wrcount;
  174. }
  175. /*
  176. * em28xx_i2c_recv_bytes()
  177. * read a byte from the i2c device
  178. */
  179. static int em28xx_i2c_recv_bytes(struct em28xx *dev, unsigned char addr,
  180. char *buf, int len)
  181. {
  182. int ret;
  183. ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
  184. if (ret < 0) {
  185. em28xx_warn("reading i2c device failed (error=%i)\n", ret);
  186. return ret;
  187. }
  188. if (dev->em28xx_read_reg(dev, 0x5) != 0)
  189. return -ENODEV;
  190. return ret;
  191. }
  192. /*
  193. * em28xx_i2c_check_for_device()
  194. * check if there is a i2c_device at the supplied address
  195. */
  196. static int em28xx_i2c_check_for_device(struct em28xx *dev, unsigned char addr)
  197. {
  198. char msg;
  199. int ret;
  200. msg = addr;
  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->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->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->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. dev->i2c_client.addr = 0xa0 >> 1;
  304. /* Check if board has eeprom */
  305. err = i2c_master_recv(&dev->i2c_client, &buf, 0);
  306. if (err < 0) {
  307. em28xx_errdev("board has no eeprom\n");
  308. memset(eedata, 0, len);
  309. return -ENODEV;
  310. }
  311. buf = 0;
  312. err = i2c_master_send(&dev->i2c_client, &buf, 1);
  313. if (err != 1) {
  314. printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
  315. dev->name, err);
  316. return err;
  317. }
  318. while (size > 0) {
  319. if (size > 16)
  320. block = 16;
  321. else
  322. block = size;
  323. if (block !=
  324. (err = i2c_master_recv(&dev->i2c_client, p, block))) {
  325. printk(KERN_WARNING
  326. "%s: i2c eeprom read error (err=%d)\n",
  327. dev->name, err);
  328. return err;
  329. }
  330. size -= block;
  331. p += block;
  332. }
  333. for (i = 0; i < len; i++) {
  334. if (0 == (i % 16))
  335. printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
  336. printk(" %02x", eedata[i]);
  337. if (15 == (i % 16))
  338. printk("\n");
  339. }
  340. if (em_eeprom->id == 0x9567eb1a)
  341. dev->hash = em28xx_hash_mem(eedata, len, 32);
  342. printk(KERN_INFO "EEPROM ID= 0x%08x, hash = 0x%08lx\n",
  343. em_eeprom->id, dev->hash);
  344. printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID,
  345. em_eeprom->product_ID);
  346. switch (em_eeprom->chip_conf >> 4 & 0x3) {
  347. case 0:
  348. printk(KERN_INFO "No audio on board.\n");
  349. break;
  350. case 1:
  351. printk(KERN_INFO "AC97 audio (5 sample rates)\n");
  352. break;
  353. case 2:
  354. printk(KERN_INFO "I2S audio, sample rate=32k\n");
  355. break;
  356. case 3:
  357. printk(KERN_INFO "I2S audio, 3 sample rates\n");
  358. break;
  359. }
  360. if (em_eeprom->chip_conf & 1 << 3)
  361. printk(KERN_INFO "USB Remote wakeup capable\n");
  362. if (em_eeprom->chip_conf & 1 << 2)
  363. printk(KERN_INFO "USB Self power capable\n");
  364. switch (em_eeprom->chip_conf & 0x3) {
  365. case 0:
  366. printk(KERN_INFO "500mA max power\n");
  367. break;
  368. case 1:
  369. printk(KERN_INFO "400mA max power\n");
  370. break;
  371. case 2:
  372. printk(KERN_INFO "300mA max power\n");
  373. break;
  374. case 3:
  375. printk(KERN_INFO "200mA max power\n");
  376. break;
  377. }
  378. printk(KERN_INFO "Table at 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  379. em_eeprom->string_idx_table,
  380. em_eeprom->string1,
  381. em_eeprom->string2,
  382. em_eeprom->string3);
  383. return 0;
  384. }
  385. /* ----------------------------------------------------------- */
  386. /*
  387. * functionality()
  388. */
  389. static u32 functionality(struct i2c_adapter *adap)
  390. {
  391. return I2C_FUNC_SMBUS_EMUL;
  392. }
  393. /*
  394. * attach_inform()
  395. * gets called when a device attaches to the i2c bus
  396. * does some basic configuration
  397. */
  398. static int attach_inform(struct i2c_client *client)
  399. {
  400. struct em28xx *dev = client->adapter->algo_data;
  401. switch (client->addr << 1) {
  402. case 0x86:
  403. case 0x84:
  404. case 0x96:
  405. case 0x94:
  406. {
  407. struct v4l2_priv_tun_config tda9887_cfg;
  408. struct tuner_setup tun_setup;
  409. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  410. tun_setup.type = TUNER_TDA9887;
  411. tun_setup.addr = client->addr;
  412. em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR,
  413. &tun_setup);
  414. tda9887_cfg.tuner = TUNER_TDA9887;
  415. tda9887_cfg.priv = &dev->tda9887_conf;
  416. em28xx_i2c_call_clients(dev, TUNER_SET_CONFIG,
  417. &tda9887_cfg);
  418. break;
  419. }
  420. case 0x42:
  421. dprintk1(1, "attach_inform: saa7114 detected.\n");
  422. break;
  423. case 0x4a:
  424. dprintk1(1, "attach_inform: saa7113 detected.\n");
  425. break;
  426. case 0xa0:
  427. dprintk1(1, "attach_inform: eeprom detected.\n");
  428. break;
  429. case 0x60:
  430. case 0x8e:
  431. {
  432. struct IR_i2c *ir = i2c_get_clientdata(client);
  433. dprintk1(1, "attach_inform: IR detected (%s).\n",
  434. ir->phys);
  435. em28xx_set_ir(dev, ir);
  436. break;
  437. }
  438. case 0x80:
  439. case 0x88:
  440. dprintk1(1, "attach_inform: msp34xx detected.\n");
  441. break;
  442. case 0xb8:
  443. case 0xba:
  444. dprintk1(1, "attach_inform: tvp5150 detected.\n");
  445. break;
  446. default:
  447. if (!dev->tuner_addr)
  448. dev->tuner_addr = client->addr;
  449. dprintk1(1, "attach inform: detected I2C address %x\n",
  450. client->addr << 1);
  451. }
  452. return 0;
  453. }
  454. static struct i2c_algorithm em28xx_algo = {
  455. .master_xfer = em28xx_i2c_xfer,
  456. .functionality = functionality,
  457. };
  458. static struct i2c_adapter em28xx_adap_template = {
  459. .owner = THIS_MODULE,
  460. .class = I2C_CLASS_TV_ANALOG,
  461. .name = "em28xx",
  462. .id = I2C_HW_B_EM28XX,
  463. .algo = &em28xx_algo,
  464. .client_register = attach_inform,
  465. };
  466. static struct i2c_client em28xx_client_template = {
  467. .name = "em28xx internal",
  468. };
  469. /* ----------------------------------------------------------- */
  470. /*
  471. * i2c_devs
  472. * incomplete list of known devices
  473. */
  474. static char *i2c_devs[128] = {
  475. [0x4a >> 1] = "saa7113h",
  476. [0x60 >> 1] = "remote IR sensor",
  477. [0x8e >> 1] = "remote IR sensor",
  478. [0x86 >> 1] = "tda9887",
  479. [0x80 >> 1] = "msp34xx",
  480. [0x88 >> 1] = "msp34xx",
  481. [0xa0 >> 1] = "eeprom",
  482. [0xb8 >> 1] = "tvp5150a",
  483. [0xba >> 1] = "tvp5150a",
  484. [0xc0 >> 1] = "tuner (analog)",
  485. [0xc2 >> 1] = "tuner (analog)",
  486. [0xc4 >> 1] = "tuner (analog)",
  487. [0xc6 >> 1] = "tuner (analog)",
  488. };
  489. /*
  490. * do_i2c_scan()
  491. * check i2c address range for devices
  492. */
  493. void em28xx_do_i2c_scan(struct em28xx *dev)
  494. {
  495. u8 i2c_devicelist[128];
  496. unsigned char buf;
  497. int i, rc;
  498. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  499. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  500. dev->i2c_client.addr = i;
  501. rc = i2c_master_recv(&dev->i2c_client, &buf, 0);
  502. if (rc < 0)
  503. continue;
  504. i2c_devicelist[i] = i;
  505. printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n",
  506. dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  507. }
  508. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  509. ARRAY_SIZE(i2c_devicelist), 32);
  510. }
  511. /*
  512. * em28xx_i2c_call_clients()
  513. * send commands to all attached i2c devices
  514. */
  515. void em28xx_i2c_call_clients(struct em28xx *dev, unsigned int cmd, void *arg)
  516. {
  517. BUG_ON(NULL == dev->i2c_adap.algo_data);
  518. i2c_clients_command(&dev->i2c_adap, cmd, arg);
  519. }
  520. /*
  521. * em28xx_i2c_register()
  522. * register i2c bus
  523. */
  524. int em28xx_i2c_register(struct em28xx *dev)
  525. {
  526. int retval;
  527. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  528. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  529. dev->i2c_adap = em28xx_adap_template;
  530. dev->i2c_adap.dev.parent = &dev->udev->dev;
  531. strcpy(dev->i2c_adap.name, dev->name);
  532. dev->i2c_adap.algo_data = dev;
  533. retval = i2c_add_adapter(&dev->i2c_adap);
  534. if (retval < 0) {
  535. em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
  536. __func__, retval);
  537. return retval;
  538. }
  539. dev->i2c_client = em28xx_client_template;
  540. dev->i2c_client.adapter = &dev->i2c_adap;
  541. retval = em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
  542. if ((retval < 0) && (retval != -ENODEV)) {
  543. em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
  544. __func__, retval);
  545. return retval;
  546. }
  547. if (i2c_scan)
  548. em28xx_do_i2c_scan(dev);
  549. return 0;
  550. }
  551. /*
  552. * em28xx_i2c_unregister()
  553. * unregister i2c_bus
  554. */
  555. int em28xx_i2c_unregister(struct em28xx *dev)
  556. {
  557. i2c_del_adapter(&dev->i2c_adap);
  558. return 0;
  559. }