em28xx-i2c.c 14 KB

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