em28xx-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 msg = dev->em28xx_read_reg(dev, 0x5);
  131. if (msg == 0x94)
  132. return -ENODEV;
  133. else if (msg == 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. return -1;
  308. buf = 0;
  309. err = i2c_master_send(&dev->i2c_client, &buf, 1);
  310. if (err != 1) {
  311. printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
  312. dev->name, err);
  313. return -1;
  314. }
  315. while (size > 0) {
  316. if (size > 16)
  317. block = 16;
  318. else
  319. block = size;
  320. if (block !=
  321. (err = i2c_master_recv(&dev->i2c_client, p, block))) {
  322. printk(KERN_WARNING
  323. "%s: i2c eeprom read error (err=%d)\n",
  324. dev->name, err);
  325. return -1;
  326. }
  327. size -= block;
  328. p += block;
  329. }
  330. for (i = 0; i < len; i++) {
  331. if (0 == (i % 16))
  332. printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
  333. printk(" %02x", eedata[i]);
  334. if (15 == (i % 16))
  335. printk("\n");
  336. }
  337. if (em_eeprom->id == 0x9567eb1a)
  338. dev->hash = em28xx_hash_mem(eedata, len, 32);
  339. printk(KERN_INFO "EEPROM ID= 0x%08x, hash = 0x%08lx\n",
  340. em_eeprom->id, dev->hash);
  341. printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID,
  342. em_eeprom->product_ID);
  343. switch (em_eeprom->chip_conf >> 4 & 0x3) {
  344. case 0:
  345. printk(KERN_INFO "No audio on board.\n");
  346. break;
  347. case 1:
  348. printk(KERN_INFO "AC97 audio (5 sample rates)\n");
  349. break;
  350. case 2:
  351. printk(KERN_INFO "I2S audio, sample rate=32k\n");
  352. break;
  353. case 3:
  354. printk(KERN_INFO "I2S audio, 3 sample rates\n");
  355. break;
  356. }
  357. if (em_eeprom->chip_conf & 1 << 3)
  358. printk(KERN_INFO "USB Remote wakeup capable\n");
  359. if (em_eeprom->chip_conf & 1 << 2)
  360. printk(KERN_INFO "USB Self power capable\n");
  361. switch (em_eeprom->chip_conf & 0x3) {
  362. case 0:
  363. printk(KERN_INFO "500mA max power\n");
  364. break;
  365. case 1:
  366. printk(KERN_INFO "400mA max power\n");
  367. break;
  368. case 2:
  369. printk(KERN_INFO "300mA max power\n");
  370. break;
  371. case 3:
  372. printk(KERN_INFO "200mA max power\n");
  373. break;
  374. }
  375. printk(KERN_INFO "Table at 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  376. em_eeprom->string_idx_table,
  377. em_eeprom->string1,
  378. em_eeprom->string2,
  379. em_eeprom->string3);
  380. return 0;
  381. }
  382. /* ----------------------------------------------------------- */
  383. /*
  384. * functionality()
  385. */
  386. static u32 functionality(struct i2c_adapter *adap)
  387. {
  388. return I2C_FUNC_SMBUS_EMUL;
  389. }
  390. /*
  391. * attach_inform()
  392. * gets called when a device attaches to the i2c bus
  393. * does some basic configuration
  394. */
  395. static int attach_inform(struct i2c_client *client)
  396. {
  397. struct em28xx *dev = client->adapter->algo_data;
  398. switch (client->addr << 1) {
  399. case 0x86:
  400. case 0x84:
  401. case 0x96:
  402. case 0x94:
  403. {
  404. struct v4l2_priv_tun_config tda9887_cfg;
  405. struct tuner_setup tun_setup;
  406. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  407. tun_setup.type = TUNER_TDA9887;
  408. tun_setup.addr = client->addr;
  409. em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR,
  410. &tun_setup);
  411. tda9887_cfg.tuner = TUNER_TDA9887;
  412. tda9887_cfg.priv = &dev->tda9887_conf;
  413. em28xx_i2c_call_clients(dev, TUNER_SET_CONFIG,
  414. &tda9887_cfg);
  415. break;
  416. }
  417. case 0x42:
  418. dprintk1(1, "attach_inform: saa7114 detected.\n");
  419. break;
  420. case 0x4a:
  421. dprintk1(1, "attach_inform: saa7113 detected.\n");
  422. break;
  423. case 0xa0:
  424. dprintk1(1, "attach_inform: eeprom detected.\n");
  425. break;
  426. case 0x60:
  427. case 0x8e:
  428. {
  429. struct IR_i2c *ir = i2c_get_clientdata(client);
  430. dprintk1(1, "attach_inform: IR detected (%s).\n",
  431. ir->phys);
  432. em28xx_set_ir(dev, ir);
  433. break;
  434. }
  435. case 0x80:
  436. case 0x88:
  437. dprintk1(1, "attach_inform: msp34xx detected.\n");
  438. break;
  439. case 0xb8:
  440. case 0xba:
  441. dprintk1(1, "attach_inform: tvp5150 detected.\n");
  442. break;
  443. default:
  444. if (!dev->tuner_addr)
  445. dev->tuner_addr = client->addr;
  446. dprintk1(1, "attach inform: detected I2C address %x\n",
  447. client->addr << 1);
  448. }
  449. return 0;
  450. }
  451. static struct i2c_algorithm em28xx_algo = {
  452. .master_xfer = em28xx_i2c_xfer,
  453. .functionality = functionality,
  454. };
  455. static struct i2c_adapter em28xx_adap_template = {
  456. .owner = THIS_MODULE,
  457. .class = I2C_CLASS_TV_ANALOG,
  458. .name = "em28xx",
  459. .id = I2C_HW_B_EM28XX,
  460. .algo = &em28xx_algo,
  461. .client_register = attach_inform,
  462. };
  463. static struct i2c_client em28xx_client_template = {
  464. .name = "em28xx internal",
  465. };
  466. /* ----------------------------------------------------------- */
  467. /*
  468. * i2c_devs
  469. * incomplete list of known devices
  470. */
  471. static char *i2c_devs[128] = {
  472. [0x4a >> 1] = "saa7113h",
  473. [0x60 >> 1] = "remote IR sensor",
  474. [0x8e >> 1] = "remote IR sensor",
  475. [0x86 >> 1] = "tda9887",
  476. [0x80 >> 1] = "msp34xx",
  477. [0x88 >> 1] = "msp34xx",
  478. [0xa0 >> 1] = "eeprom",
  479. [0xb8 >> 1] = "tvp5150a",
  480. [0xba >> 1] = "tvp5150a",
  481. [0xc0 >> 1] = "tuner (analog)",
  482. [0xc2 >> 1] = "tuner (analog)",
  483. [0xc4 >> 1] = "tuner (analog)",
  484. [0xc6 >> 1] = "tuner (analog)",
  485. };
  486. /*
  487. * do_i2c_scan()
  488. * check i2c address range for devices
  489. */
  490. void em28xx_do_i2c_scan(struct em28xx *dev)
  491. {
  492. u8 i2c_devicelist[128];
  493. unsigned char buf;
  494. int i, rc;
  495. memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
  496. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  497. dev->i2c_client.addr = i;
  498. rc = i2c_master_recv(&dev->i2c_client, &buf, 0);
  499. if (rc < 0)
  500. continue;
  501. i2c_devicelist[i] = i;
  502. printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n",
  503. dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  504. }
  505. dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
  506. ARRAY_SIZE(i2c_devicelist), 32);
  507. }
  508. /*
  509. * em28xx_i2c_call_clients()
  510. * send commands to all attached i2c devices
  511. */
  512. void em28xx_i2c_call_clients(struct em28xx *dev, unsigned int cmd, void *arg)
  513. {
  514. BUG_ON(NULL == dev->i2c_adap.algo_data);
  515. i2c_clients_command(&dev->i2c_adap, cmd, arg);
  516. }
  517. /*
  518. * em28xx_i2c_register()
  519. * register i2c bus
  520. */
  521. int em28xx_i2c_register(struct em28xx *dev)
  522. {
  523. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  524. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  525. dev->i2c_adap = em28xx_adap_template;
  526. dev->i2c_adap.dev.parent = &dev->udev->dev;
  527. strcpy(dev->i2c_adap.name, dev->name);
  528. dev->i2c_adap.algo_data = dev;
  529. i2c_add_adapter(&dev->i2c_adap);
  530. dev->i2c_client = em28xx_client_template;
  531. dev->i2c_client.adapter = &dev->i2c_adap;
  532. em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
  533. if (i2c_scan)
  534. em28xx_do_i2c_scan(dev);
  535. return 0;
  536. }
  537. /*
  538. * em28xx_i2c_unregister()
  539. * unregister i2c_bus
  540. */
  541. int em28xx_i2c_unregister(struct em28xx *dev)
  542. {
  543. i2c_del_adapter(&dev->i2c_adap);
  544. return 0;
  545. }