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