em28xx-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 <linux/video_decoder.h>
  24. #include "em28xx.h"
  25. #include <media/v4l2-common.h>
  26. #include <media/tuner.h>
  27. /* ----------------------------------------------------------- */
  28. static unsigned int i2c_scan = 0;
  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 = 0;
  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, __FUNCTION__ , ##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("writting 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. static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
  269. {
  270. unsigned char buf, *p = eedata;
  271. struct em28xx_eeprom *em_eeprom = (void *)eedata;
  272. int i, err, size = len, block;
  273. dev->i2c_client.addr = 0xa0 >> 1;
  274. /* Check if board has eeprom */
  275. err = i2c_master_recv(&dev->i2c_client, &buf, 0);
  276. if (err < 0)
  277. return -1;
  278. buf = 0;
  279. if (1 != (err = i2c_master_send(&dev->i2c_client, &buf, 1))) {
  280. printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
  281. dev->name, err);
  282. return -1;
  283. }
  284. while (size > 0) {
  285. if (size > 16)
  286. block = 16;
  287. else
  288. block = size;
  289. if (block !=
  290. (err = i2c_master_recv(&dev->i2c_client, p, block))) {
  291. printk(KERN_WARNING
  292. "%s: i2c eeprom read error (err=%d)\n",
  293. dev->name, err);
  294. return -1;
  295. }
  296. size -= block;
  297. p += block;
  298. }
  299. for (i = 0; i < len; i++) {
  300. if (0 == (i % 16))
  301. printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
  302. printk(" %02x", eedata[i]);
  303. if (15 == (i % 16))
  304. printk("\n");
  305. }
  306. printk(KERN_INFO "EEPROM ID= 0x%08x\n", em_eeprom->id);
  307. printk(KERN_INFO "Vendor/Product ID= %04x:%04x\n", em_eeprom->vendor_ID,
  308. em_eeprom->product_ID);
  309. switch (em_eeprom->chip_conf >> 4 & 0x3) {
  310. case 0:
  311. printk(KERN_INFO "No audio on board.\n");
  312. break;
  313. case 1:
  314. printk(KERN_INFO "AC97 audio (5 sample rates)\n");
  315. break;
  316. case 2:
  317. printk(KERN_INFO "I2S audio, sample rate=32k\n");
  318. break;
  319. case 3:
  320. printk(KERN_INFO "I2S audio, 3 sample rates\n");
  321. break;
  322. }
  323. if (em_eeprom->chip_conf & 1 << 3)
  324. printk(KERN_INFO "USB Remote wakeup capable\n");
  325. if (em_eeprom->chip_conf & 1 << 2)
  326. printk(KERN_INFO "USB Self power capable\n");
  327. switch (em_eeprom->chip_conf & 0x3) {
  328. case 0:
  329. printk(KERN_INFO "500mA max power\n");
  330. break;
  331. case 1:
  332. printk(KERN_INFO "400mA max power\n");
  333. break;
  334. case 2:
  335. printk(KERN_INFO "300mA max power\n");
  336. break;
  337. case 3:
  338. printk(KERN_INFO "200mA max power\n");
  339. break;
  340. }
  341. printk(KERN_INFO "Table at 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
  342. em_eeprom->string_idx_table,em_eeprom->string1,
  343. em_eeprom->string2,em_eeprom->string3);
  344. return 0;
  345. }
  346. /* ----------------------------------------------------------- */
  347. /*
  348. * algo_control()
  349. */
  350. static int algo_control(struct i2c_adapter *adapter,
  351. unsigned int cmd, unsigned long arg)
  352. {
  353. return 0;
  354. }
  355. /*
  356. * functionality()
  357. */
  358. static u32 functionality(struct i2c_adapter *adap)
  359. {
  360. return I2C_FUNC_SMBUS_EMUL;
  361. }
  362. static int em28xx_set_tuner(int check_eeprom, struct i2c_client *client)
  363. {
  364. struct em28xx *dev = client->adapter->algo_data;
  365. struct tuner_setup tun_setup;
  366. if (dev->has_tuner) {
  367. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  368. tun_setup.type = dev->tuner_type;
  369. tun_setup.addr = dev->tuner_addr;
  370. em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
  371. }
  372. return (0);
  373. }
  374. /*
  375. * attach_inform()
  376. * gets called when a device attaches to the i2c bus
  377. * does some basic configuration
  378. */
  379. static int attach_inform(struct i2c_client *client)
  380. {
  381. struct em28xx *dev = client->adapter->algo_data;
  382. switch (client->addr << 1) {
  383. case 0x43:
  384. case 0x4b:
  385. {
  386. struct tuner_setup tun_setup;
  387. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  388. tun_setup.type = TUNER_TDA9887;
  389. tun_setup.addr = client->addr;
  390. em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
  391. em28xx_i2c_call_clients(dev, TDA9887_SET_CONFIG, &dev->tda9887_conf);
  392. break;
  393. }
  394. case 0x42:
  395. dprintk1(1,"attach_inform: saa7114 detected.\n");
  396. break;
  397. case 0x4a:
  398. dprintk1(1,"attach_inform: saa7113 detected.\n");
  399. break;
  400. case 0xa0:
  401. dprintk1(1,"attach_inform: eeprom detected.\n");
  402. break;
  403. case 0x60:
  404. case 0x8e:
  405. {
  406. struct IR_i2c *ir = i2c_get_clientdata(client);
  407. dprintk1(1,"attach_inform: IR detected (%s).\n",ir->phys);
  408. em28xx_set_ir(dev,ir);
  409. break;
  410. }
  411. case 0x80:
  412. case 0x88:
  413. dprintk1(1,"attach_inform: msp34xx detected.\n");
  414. break;
  415. case 0xb8:
  416. case 0xba:
  417. dprintk1(1,"attach_inform: tvp5150 detected.\n");
  418. break;
  419. default:
  420. dprintk1(1,"attach inform: detected I2C address %x\n", client->addr << 1);
  421. dev->tuner_addr = client->addr;
  422. em28xx_set_tuner(-1, client);
  423. }
  424. return 0;
  425. }
  426. static struct i2c_algorithm em28xx_algo = {
  427. .master_xfer = em28xx_i2c_xfer,
  428. .algo_control = algo_control,
  429. .functionality = functionality,
  430. };
  431. static struct i2c_adapter em28xx_adap_template = {
  432. .owner = THIS_MODULE,
  433. .class = I2C_CLASS_TV_ANALOG,
  434. .name = "em28xx",
  435. .id = I2C_HW_B_EM28XX,
  436. .algo = &em28xx_algo,
  437. .client_register = attach_inform,
  438. };
  439. static struct i2c_client em28xx_client_template = {
  440. .name = "em28xx internal",
  441. };
  442. /* ----------------------------------------------------------- */
  443. /*
  444. * i2c_devs
  445. * incomplete list of known devices
  446. */
  447. static char *i2c_devs[128] = {
  448. [0x4a >> 1] = "saa7113h",
  449. [0x60 >> 1] = "remote IR sensor",
  450. [0x8e >> 1] = "remote IR sensor",
  451. [0x86 >> 1] = "tda9887",
  452. [0x80 >> 1] = "msp34xx",
  453. [0x88 >> 1] = "msp34xx",
  454. [0xa0 >> 1] = "eeprom",
  455. [0xb8 >> 1] = "tvp5150a",
  456. [0xba >> 1] = "tvp5150a",
  457. [0xc0 >> 1] = "tuner (analog)",
  458. [0xc2 >> 1] = "tuner (analog)",
  459. [0xc4 >> 1] = "tuner (analog)",
  460. [0xc6 >> 1] = "tuner (analog)",
  461. };
  462. /*
  463. * do_i2c_scan()
  464. * check i2c address range for devices
  465. */
  466. static void do_i2c_scan(char *name, struct i2c_client *c)
  467. {
  468. unsigned char buf;
  469. int i, rc;
  470. for (i = 0; i < 128; i++) {
  471. c->addr = i;
  472. rc = i2c_master_recv(c, &buf, 0);
  473. if (rc < 0)
  474. continue;
  475. printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", name,
  476. i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  477. }
  478. }
  479. /*
  480. * em28xx_i2c_call_clients()
  481. * send commands to all attached i2c devices
  482. */
  483. void em28xx_i2c_call_clients(struct em28xx *dev, unsigned int cmd, void *arg)
  484. {
  485. BUG_ON(NULL == dev->i2c_adap.algo_data);
  486. i2c_clients_command(&dev->i2c_adap, cmd, arg);
  487. }
  488. /*
  489. * em28xx_i2c_register()
  490. * register i2c bus
  491. */
  492. int em28xx_i2c_register(struct em28xx *dev)
  493. {
  494. BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
  495. BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
  496. dev->i2c_adap = em28xx_adap_template;
  497. dev->i2c_adap.dev.parent = &dev->udev->dev;
  498. strcpy(dev->i2c_adap.name, dev->name);
  499. dev->i2c_adap.algo_data = dev;
  500. i2c_add_adapter(&dev->i2c_adap);
  501. dev->i2c_client = em28xx_client_template;
  502. dev->i2c_client.adapter = &dev->i2c_adap;
  503. em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
  504. if (i2c_scan)
  505. do_i2c_scan(dev->name, &dev->i2c_client);
  506. return 0;
  507. }
  508. /*
  509. * em28xx_i2c_unregister()
  510. * unregister i2c_bus
  511. */
  512. int em28xx_i2c_unregister(struct em28xx *dev)
  513. {
  514. i2c_del_adapter(&dev->i2c_adap);
  515. return 0;
  516. }