em28xx-i2c.c 14 KB

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