em28xx-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. em2820-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
  4. Ludovico Cavedon <cavedon@sssup.it>
  5. Mauro Carvalho Chehab <mchehab@brturbo.com.br>
  6. Based on the em2800 driver from 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 <media/tuner.h>
  24. #include <linux/video_decoder.h>
  25. #include "em2820.h"
  26. /* ----------------------------------------------------------- */
  27. static unsigned int i2c_scan = 0;
  28. module_param(i2c_scan, int, 0444);
  29. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  30. static unsigned int i2c_debug = 0;
  31. module_param(i2c_debug, int, 0644);
  32. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  33. #define dprintk(fmt, args...) if (i2c_debug) do {\
  34. printk(KERN_DEBUG "%s: %s: " fmt "\n",\
  35. dev->name, __FUNCTION__ , ##args); } while (0)
  36. #define dprintk1(fmt, args...) if (i2c_debug) do{ \
  37. printk(KERN_DEBUG "%s: %s: " fmt, \
  38. dev->name, __FUNCTION__ , ##args); } while (0)
  39. #define dprintk2(fmt, args...) if (i2c_debug) do {\
  40. printk(fmt , ##args); } 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 em2820 *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->em2820_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
  62. if (ret != 2 + len) {
  63. em2820_warn("writting 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->em2820_read_reg(dev, 0x05);
  69. if (ret == 0x80 + len - 1)
  70. return len;
  71. mdelay(5);
  72. }
  73. em2820_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 em2820 *dev = (struct em2820 *)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 em2820 *dev, unsigned char addr)
  105. {
  106. char msg;
  107. int ret;
  108. int write_timeout;
  109. msg = addr;
  110. ret = dev->em2820_write_regs(dev, 0x04, &msg, 1);
  111. if (ret < 0) {
  112. em2820_warn("setting i2c device address failed (error=%i)\n",
  113. ret);
  114. return ret;
  115. }
  116. msg = 0x84;
  117. ret = dev->em2820_write_regs(dev, 0x05, &msg, 1);
  118. if (ret < 0) {
  119. em2820_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 msg = dev->em2820_read_reg(dev, 0x5);
  125. if (msg == 0x94)
  126. return -ENODEV;
  127. else if (msg == 0x84)
  128. return 0;
  129. mdelay(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 em2820 *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. em2820_warn
  145. ("preparing read at i2c address 0x%x failed (error=%i)\n",
  146. addr, ret);
  147. return ret;
  148. }
  149. ret = dev->em2820_read_reg_req_len(dev, 0x0, 0x3, buf, len);
  150. if (ret < 0) {
  151. em2820_warn("reading from i2c device at 0x%x failed (error=%i)",
  152. addr, ret);
  153. return ret;
  154. }
  155. return ret;
  156. }
  157. /*
  158. * em2820_i2c_send_bytes()
  159. * untested for more than 4 bytes
  160. */
  161. static int em2820_i2c_send_bytes(void *data, unsigned char addr, char *buf,
  162. short len, int stop)
  163. {
  164. int wrcount = 0;
  165. struct em2820 *dev = (struct em2820 *)data;
  166. wrcount = dev->em2820_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
  167. return wrcount;
  168. }
  169. /*
  170. * em2820_i2c_recv_bytes()
  171. * read a byte from the i2c device
  172. */
  173. static int em2820_i2c_recv_bytes(struct em2820 *dev, unsigned char addr,
  174. char *buf, int len)
  175. {
  176. int ret;
  177. ret = dev->em2820_read_reg_req_len(dev, 2, addr, buf, len);
  178. if (ret < 0) {
  179. em2820_warn("reading i2c device failed (error=%i)\n", ret);
  180. return ret;
  181. }
  182. if (dev->em2820_read_reg(dev, 0x5) != 0)
  183. return -ENODEV;
  184. return ret;
  185. }
  186. /*
  187. * em2820_i2c_check_for_device()
  188. * check if there is a i2c_device at the supplied address
  189. */
  190. static int em2820_i2c_check_for_device(struct em2820 *dev, unsigned char addr)
  191. {
  192. char msg;
  193. int ret;
  194. msg = addr;
  195. ret = dev->em2820_read_reg_req(dev, 2, addr);
  196. if (ret < 0) {
  197. em2820_warn("reading from i2c device failed (error=%i)\n", ret);
  198. return ret;
  199. }
  200. if (dev->em2820_read_reg(dev, 0x5) != 0)
  201. return -ENODEV;
  202. return 0;
  203. }
  204. /*
  205. * em2820_i2c_xfer()
  206. * the main i2c transfer function
  207. */
  208. static int em2820_i2c_xfer(struct i2c_adapter *i2c_adap,
  209. struct i2c_msg msgs[], int num)
  210. {
  211. struct em2820 *dev = i2c_adap->algo_data;
  212. int addr, rc, i, byte;
  213. if (num <= 0)
  214. return 0;
  215. for (i = 0; i < num; i++) {
  216. addr = msgs[i].addr << 1;
  217. dprintk1("%s %s addr=%x len=%d:",
  218. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  219. i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
  220. if (!msgs[i].len) { /* no len: check only for device presence */
  221. if (dev->is_em2800)
  222. rc = em2800_i2c_check_for_device(dev, addr);
  223. else
  224. rc = em2820_i2c_check_for_device(dev, addr);
  225. if (rc < 0) {
  226. dprintk2(" no device\n");
  227. return rc;
  228. }
  229. } else if (msgs[i].flags & I2C_M_RD) {
  230. /* read bytes */
  231. if (dev->is_em2800)
  232. rc = em2800_i2c_recv_bytes(dev, addr,
  233. msgs[i].buf,
  234. msgs[i].len);
  235. else
  236. rc = em2820_i2c_recv_bytes(dev, addr,
  237. msgs[i].buf,
  238. msgs[i].len);
  239. if (i2c_debug) {
  240. for (byte = 0; byte < msgs[i].len; byte++) {
  241. printk(" %02x", msgs[i].buf[byte]);
  242. }
  243. }
  244. } else {
  245. /* write bytes */
  246. if (i2c_debug) {
  247. for (byte = 0; byte < msgs[i].len; byte++)
  248. printk(" %02x", msgs[i].buf[byte]);
  249. }
  250. if (dev->is_em2800)
  251. rc = em2800_i2c_send_bytes(dev, addr,
  252. msgs[i].buf,
  253. msgs[i].len);
  254. else
  255. rc = em2820_i2c_send_bytes(dev, addr,
  256. msgs[i].buf,
  257. msgs[i].len,
  258. i == num - 1);
  259. if (rc < 0)
  260. goto err;
  261. }
  262. if (i2c_debug)
  263. printk("\n");
  264. }
  265. return num;
  266. err:
  267. dprintk2(" ERROR: %i\n", rc);
  268. return rc;
  269. }
  270. static int em2820_i2c_eeprom(struct em2820 *dev, unsigned char *eedata, int len)
  271. {
  272. unsigned char buf, *p = eedata;
  273. struct em2820_eeprom *em_eeprom = (void *)eedata;
  274. int i, err, size = len, block;
  275. dev->i2c_client.addr = 0xa0 >> 1;
  276. /* Check if board has eeprom */
  277. err = i2c_master_recv(&dev->i2c_client, &buf, 0);
  278. if (err < 0)
  279. return -1;
  280. buf = 0;
  281. if (1 != (err = i2c_master_send(&dev->i2c_client, &buf, 1))) {
  282. printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
  283. dev->name, err);
  284. return -1;
  285. }
  286. while (size > 0) {
  287. if (size > 16)
  288. block = 16;
  289. else
  290. block = size;
  291. if (block !=
  292. (err = i2c_master_recv(&dev->i2c_client, p, block))) {
  293. printk(KERN_WARNING
  294. "%s: i2c eeprom read error (err=%d)\n",
  295. dev->name, err);
  296. return -1;
  297. }
  298. size -= block;
  299. p += block;
  300. }
  301. for (i = 0; i < len; i++) {
  302. if (0 == (i % 16))
  303. printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
  304. printk(" %02x", eedata[i]);
  305. if (15 == (i % 16))
  306. printk("\n");
  307. }
  308. printk(KERN_INFO "EEPROM ID= 0x%08x\n", em_eeprom->id);
  309. printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID,
  310. em_eeprom->product_ID);
  311. switch (em_eeprom->chip_conf >> 4 & 0x3) {
  312. case 0:
  313. printk(KERN_INFO "No audio on board.\n");
  314. break;
  315. case 1:
  316. printk(KERN_INFO "AC97 audio (5 sample rates)\n");
  317. break;
  318. case 2:
  319. printk(KERN_INFO "I2S audio, sample rate=32k\n");
  320. break;
  321. case 3:
  322. printk(KERN_INFO "I2S audio, 3 sample rates\n");
  323. break;
  324. }
  325. if (em_eeprom->chip_conf & 1 << 3)
  326. printk(KERN_INFO "USB Remote wakeup capable\n");
  327. if (em_eeprom->chip_conf & 1 << 2)
  328. printk(KERN_INFO "USB Self power capable\n");
  329. switch (em_eeprom->chip_conf & 0x3) {
  330. case 0:
  331. printk(KERN_INFO "500mA max power\n");
  332. break;
  333. case 1:
  334. printk(KERN_INFO "400mA max power\n");
  335. break;
  336. case 2:
  337. printk(KERN_INFO "300mA max power\n");
  338. break;
  339. case 3:
  340. printk(KERN_INFO "200mA max power\n");
  341. break;
  342. }
  343. return 0;
  344. }
  345. /* ----------------------------------------------------------- */
  346. /*
  347. * algo_control()
  348. */
  349. static int algo_control(struct i2c_adapter *adapter,
  350. unsigned int cmd, unsigned long arg)
  351. {
  352. return 0;
  353. }
  354. /*
  355. * functionality()
  356. */
  357. static u32 functionality(struct i2c_adapter *adap)
  358. {
  359. return I2C_FUNC_SMBUS_EMUL;
  360. }
  361. #ifndef I2C_PEC
  362. static void inc_use(struct i2c_adapter *adap)
  363. {
  364. MOD_INC_USE_COUNT;
  365. }
  366. static void dec_use(struct i2c_adapter *adap)
  367. {
  368. MOD_DEC_USE_COUNT;
  369. }
  370. #endif
  371. static int em2820_set_tuner(int check_eeprom, struct i2c_client *client)
  372. {
  373. struct em2820 *dev = client->adapter->algo_data;
  374. struct tuner_setup tun_setup;
  375. if (dev->has_tuner) {
  376. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  377. tun_setup.type = dev->tuner_type;
  378. tun_setup.addr = dev->tuner_addr;
  379. em2820_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
  380. }
  381. return (0);
  382. }
  383. /*
  384. * attach_inform()
  385. * gets called when a device attaches to the i2c bus
  386. * does some basic configuration
  387. */
  388. static int attach_inform(struct i2c_client *client)
  389. {
  390. struct em2820 *dev = client->adapter->algo_data;
  391. dprintk("address %x", client->addr << 1);
  392. switch (client->addr << 1) {
  393. case 0x86:
  394. em2820_i2c_call_clients(dev, TDA9887_SET_CONFIG, &dev->tda9887_conf);
  395. break;
  396. case 0x4a:
  397. dprintk1("attach_inform: saa7113 detected.\n");
  398. break;
  399. case 0xa0:
  400. dprintk1("attach_inform: eeprom detected.\n");
  401. break;
  402. case 0x80:
  403. case 0x88:
  404. dprintk1("attach_inform: msp34xx detected.\n");
  405. break;
  406. case 0xb8:
  407. case 0xba:
  408. dprintk1("attach_inform: tvp5150 detected.\n");
  409. break;
  410. default:
  411. dev->tuner_addr = client->addr;
  412. em2820_set_tuner(-1, client);
  413. }
  414. return 0;
  415. }
  416. static struct i2c_algorithm em2820_algo = {
  417. .master_xfer = em2820_i2c_xfer,
  418. .algo_control = algo_control,
  419. .functionality = functionality,
  420. };
  421. static struct i2c_adapter em2820_adap_template = {
  422. #ifdef I2C_PEC
  423. .owner = THIS_MODULE,
  424. #else
  425. .inc_use = inc_use,
  426. .dec_use = dec_use,
  427. #endif
  428. #ifdef I2C_CLASS_TV_ANALOG
  429. .class = I2C_CLASS_TV_ANALOG,
  430. #endif
  431. .name = "em2820",
  432. .id = I2C_HW_B_EM2820,
  433. .algo = &em2820_algo,
  434. .client_register = attach_inform,
  435. };
  436. static struct i2c_client em2820_client_template = {
  437. .name = "em2820 internal",
  438. .flags = I2C_CLIENT_ALLOW_USE,
  439. };
  440. /* ----------------------------------------------------------- */
  441. /*
  442. * i2c_devs
  443. * incomplete list of known devices
  444. */
  445. static char *i2c_devs[128] = {
  446. [0x4a >> 1] = "saa7113h",
  447. [0x60 >> 1] = "remote IR sensor",
  448. [0x86 >> 1] = "tda9887",
  449. [0x80 >> 1] = "msp34xx",
  450. [0x88 >> 1] = "msp34xx",
  451. [0xa0 >> 1] = "eeprom",
  452. [0xb8 >> 1] = "tvp5150a",
  453. [0xba >> 1] = "tvp5150a",
  454. [0xc0 >> 1] = "tuner (analog)",
  455. [0xc2 >> 1] = "tuner (analog)",
  456. [0xc4 >> 1] = "tuner (analog)",
  457. [0xc6 >> 1] = "tuner (analog)",
  458. };
  459. /*
  460. * do_i2c_scan()
  461. * check i2c address range for devices
  462. */
  463. static void do_i2c_scan(char *name, struct i2c_client *c)
  464. {
  465. unsigned char buf;
  466. int i, rc;
  467. for (i = 0; i < 128; i++) {
  468. c->addr = i;
  469. rc = i2c_master_recv(c, &buf, 0);
  470. if (rc < 0)
  471. continue;
  472. printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", name,
  473. i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  474. }
  475. }
  476. /*
  477. * em2820_i2c_call_clients()
  478. * send commands to all attached i2c devices
  479. */
  480. void em2820_i2c_call_clients(struct em2820 *dev, unsigned int cmd, void *arg)
  481. {
  482. BUG_ON(NULL == dev->i2c_adap.algo_data);
  483. i2c_clients_command(&dev->i2c_adap, cmd, arg);
  484. }
  485. /*
  486. * em2820_i2c_register()
  487. * register i2c bus
  488. */
  489. int em2820_i2c_register(struct em2820 *dev)
  490. {
  491. BUG_ON(!dev->em2820_write_regs || !dev->em2820_read_reg);
  492. BUG_ON(!dev->em2820_write_regs_req || !dev->em2820_read_reg_req);
  493. dev->i2c_adap = em2820_adap_template;
  494. dev->i2c_adap.dev.parent = &dev->udev->dev;
  495. strcpy(dev->i2c_adap.name, dev->name);
  496. dev->i2c_adap.algo_data = dev;
  497. i2c_add_adapter(&dev->i2c_adap);
  498. dev->i2c_client = em2820_client_template;
  499. dev->i2c_client.adapter = &dev->i2c_adap;
  500. em2820_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
  501. if (i2c_scan)
  502. do_i2c_scan(dev->name, &dev->i2c_client);
  503. return 0;
  504. }
  505. /*
  506. * em2820_i2c_unregister()
  507. * unregister i2c_bus
  508. */
  509. int em2820_i2c_unregister(struct em2820 *dev)
  510. {
  511. i2c_del_adapter(&dev->i2c_adap);
  512. return 0;
  513. }