usbvision-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * I2C_ALGO_USB.C
  3. * i2c algorithm for USB-I2C Bridges
  4. *
  5. * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  6. * Dwaine Garden <dwainegarden@rogers.com>
  7. *
  8. * This module is part of usbvision driver project.
  9. * Updates to driver completed by Dwaine P. Garden
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/version.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/ioport.h>
  34. #include <linux/errno.h>
  35. #include <linux/sched.h>
  36. #include <linux/usb.h>
  37. #include <linux/i2c.h>
  38. #include "usbvision.h"
  39. #define DBG_I2C 1<<0
  40. #define DBG_ALGO 1<<1
  41. static int i2c_debug = 0;
  42. module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
  43. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  44. #define PDEBUG(level, fmt, args...) \
  45. if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
  46. static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
  47. short len);
  48. static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
  49. short len);
  50. static inline int try_write_address(struct i2c_adapter *i2c_adap,
  51. unsigned char addr, int retries)
  52. {
  53. void *data;
  54. int i, ret = -1;
  55. char buf[4];
  56. data = i2c_get_adapdata(i2c_adap);
  57. buf[0] = 0x00;
  58. for (i = 0; i <= retries; i++) {
  59. ret = (usbvision_i2c_write(data, addr, buf, 1));
  60. if (ret == 1)
  61. break; /* success! */
  62. udelay(5);
  63. if (i == retries) /* no success */
  64. break;
  65. udelay(10);
  66. }
  67. if (i) {
  68. PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
  69. PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
  70. }
  71. return ret;
  72. }
  73. static inline int try_read_address(struct i2c_adapter *i2c_adap,
  74. unsigned char addr, int retries)
  75. {
  76. void *data;
  77. int i, ret = -1;
  78. char buf[4];
  79. data = i2c_get_adapdata(i2c_adap);
  80. for (i = 0; i <= retries; i++) {
  81. ret = (usbvision_i2c_read(data, addr, buf, 1));
  82. if (ret == 1)
  83. break; /* success! */
  84. udelay(5);
  85. if (i == retries) /* no success */
  86. break;
  87. udelay(10);
  88. }
  89. if (i) {
  90. PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
  91. PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
  92. }
  93. return ret;
  94. }
  95. static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  96. struct i2c_msg *msg, int retries,
  97. unsigned char *add)
  98. {
  99. unsigned short flags = msg->flags;
  100. unsigned char addr;
  101. int ret;
  102. if ((flags & I2C_M_TEN)) {
  103. /* a ten bit address */
  104. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  105. /* try extended address code... */
  106. ret = try_write_address(i2c_adap, addr, retries);
  107. if (ret != 1) {
  108. err("died at extended address code, while writing");
  109. return -EREMOTEIO;
  110. }
  111. add[0] = addr;
  112. if (flags & I2C_M_RD) {
  113. /* okay, now switch into reading mode */
  114. addr |= 0x01;
  115. ret = try_read_address(i2c_adap, addr, retries);
  116. if (ret != 1) {
  117. err("died at extended address code, while reading");
  118. return -EREMOTEIO;
  119. }
  120. }
  121. } else { /* normal 7bit address */
  122. addr = (msg->addr << 1);
  123. if (flags & I2C_M_RD)
  124. addr |= 1;
  125. if (flags & I2C_M_REV_DIR_ADDR)
  126. addr ^= 1;
  127. add[0] = addr;
  128. if (flags & I2C_M_RD)
  129. ret = try_read_address(i2c_adap, addr, retries);
  130. else
  131. ret = try_write_address(i2c_adap, addr, retries);
  132. if (ret != 1) {
  133. return -EREMOTEIO;
  134. }
  135. }
  136. return 0;
  137. }
  138. static int
  139. usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  140. {
  141. struct i2c_msg *pmsg;
  142. void *data;
  143. int i, ret;
  144. unsigned char addr;
  145. data = i2c_get_adapdata(i2c_adap);
  146. for (i = 0; i < num; i++) {
  147. pmsg = &msgs[i];
  148. ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
  149. if (ret != 0) {
  150. PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i);
  151. return (ret < 0) ? ret : -EREMOTEIO;
  152. }
  153. if (pmsg->flags & I2C_M_RD) {
  154. /* read bytes into buffer */
  155. ret = (usbvision_i2c_read(data, addr, pmsg->buf, pmsg->len));
  156. if (ret < pmsg->len) {
  157. return (ret < 0) ? ret : -EREMOTEIO;
  158. }
  159. } else {
  160. /* write bytes from buffer */
  161. ret = (usbvision_i2c_write(data, addr, pmsg->buf, pmsg->len));
  162. if (ret < pmsg->len) {
  163. return (ret < 0) ? ret : -EREMOTEIO;
  164. }
  165. }
  166. }
  167. return num;
  168. }
  169. static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
  170. {
  171. return 0;
  172. }
  173. static u32 usb_func(struct i2c_adapter *adap)
  174. {
  175. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  176. }
  177. /* -----exported algorithm data: ------------------------------------- */
  178. static struct i2c_algorithm i2c_usb_algo = {
  179. .master_xfer = usb_xfer,
  180. .smbus_xfer = NULL,
  181. .algo_control = algo_control,
  182. .functionality = usb_func,
  183. };
  184. /*
  185. * registering functions to load algorithms at runtime
  186. */
  187. static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
  188. {
  189. PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
  190. PDEBUG(DBG_ALGO, "ALGO debugging is enabled [i2c]");
  191. /* register new adapter to i2c module... */
  192. adap->algo = &i2c_usb_algo;
  193. adap->timeout = 100; /* default values, should */
  194. adap->retries = 3; /* be replaced by defines */
  195. i2c_add_adapter(adap);
  196. PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
  197. return 0;
  198. }
  199. int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
  200. {
  201. i2c_del_adapter(adap);
  202. PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
  203. return 0;
  204. }
  205. /* ----------------------------------------------------------------------- */
  206. /* usbvision specific I2C functions */
  207. /* ----------------------------------------------------------------------- */
  208. static struct i2c_adapter i2c_adap_template;
  209. static struct i2c_client i2c_client_template;
  210. int usbvision_init_i2c(struct usb_usbvision *usbvision)
  211. {
  212. memcpy(&usbvision->i2c_adap, &i2c_adap_template,
  213. sizeof(struct i2c_adapter));
  214. memcpy(&usbvision->i2c_client, &i2c_client_template,
  215. sizeof(struct i2c_client));
  216. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  217. " #%d", usbvision->vdev->minor & 0x1f);
  218. PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
  219. i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
  220. i2c_set_clientdata(&usbvision->i2c_client, usbvision);
  221. usbvision->i2c_client.adapter = &usbvision->i2c_adap;
  222. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  223. printk(KERN_ERR "usbvision_init_i2c: can't write reg\n");
  224. return -EBUSY;
  225. }
  226. #ifdef CONFIG_MODULES
  227. /* Request the load of the i2c modules we need */
  228. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  229. case CODEC_SAA7113:
  230. request_module("saa7115");
  231. break;
  232. case CODEC_SAA7111:
  233. request_module("saa7115");
  234. break;
  235. }
  236. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  237. request_module("tuner");
  238. }
  239. #endif
  240. return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
  241. }
  242. void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
  243. void *arg)
  244. {
  245. i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
  246. }
  247. static int attach_inform(struct i2c_client *client)
  248. {
  249. struct usb_usbvision *usbvision;
  250. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  251. switch (client->addr << 1) {
  252. case 0x43:
  253. case 0x4b:
  254. {
  255. struct tuner_setup tun_setup;
  256. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  257. tun_setup.type = TUNER_TDA9887;
  258. tun_setup.addr = client->addr;
  259. call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
  260. break;
  261. }
  262. case 0x42:
  263. PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
  264. break;
  265. case 0x4a:
  266. PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
  267. break;
  268. case 0xa0:
  269. PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
  270. break;
  271. default:
  272. {
  273. struct tuner_setup tun_setup;
  274. PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
  275. usbvision->tuner_addr = client->addr;
  276. if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
  277. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  278. tun_setup.type = usbvision->tuner_type;
  279. tun_setup.addr = usbvision->tuner_addr;
  280. call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
  281. }
  282. }
  283. break;
  284. }
  285. return 0;
  286. }
  287. static int detach_inform(struct i2c_client *client)
  288. {
  289. struct usb_usbvision *usbvision;
  290. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  291. PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
  292. return 0;
  293. }
  294. static int
  295. usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
  296. char *buf, short len)
  297. {
  298. int rc, retries;
  299. for (retries = 5;;) {
  300. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  301. if (rc < 0)
  302. return rc;
  303. /* Initiate byte read cycle */
  304. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  305. /* d3 0=Wr 1=Rd */
  306. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  307. (len & 0x07) | 0x18);
  308. if (rc < 0)
  309. return rc;
  310. /* Test for Busy and ACK */
  311. do {
  312. /* USBVISION_SER_CONT -> d4 == 0 busy */
  313. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  314. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  315. if (rc < 0)
  316. return rc;
  317. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  318. if ((rc & 0x20) == 0) /* Ack? */
  319. break;
  320. /* I2C abort */
  321. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  322. if (rc < 0)
  323. return rc;
  324. if (--retries < 0)
  325. return -1;
  326. }
  327. switch (len) {
  328. case 4:
  329. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  330. case 3:
  331. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  332. case 2:
  333. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  334. case 1:
  335. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  336. break;
  337. default:
  338. printk(KERN_ERR
  339. "usbvision_i2c_read_max4: buffer length > 4\n");
  340. }
  341. if (i2c_debug & DBG_I2C) {
  342. int idx;
  343. for (idx = 0; idx < len; idx++) {
  344. PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
  345. }
  346. }
  347. return len;
  348. }
  349. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  350. unsigned char addr, const char *buf,
  351. short len)
  352. {
  353. int rc, retries;
  354. int i;
  355. unsigned char value[6];
  356. unsigned char ser_cont;
  357. ser_cont = (len & 0x07) | 0x10;
  358. value[0] = addr;
  359. value[1] = ser_cont;
  360. for (i = 0; i < len; i++)
  361. value[i + 2] = buf[i];
  362. for (retries = 5;;) {
  363. rc = usb_control_msg(usbvision->dev,
  364. usb_sndctrlpipe(usbvision->dev, 1),
  365. USBVISION_OP_CODE,
  366. USB_DIR_OUT | USB_TYPE_VENDOR |
  367. USB_RECIP_ENDPOINT, 0,
  368. (__u16) USBVISION_SER_ADRS, value,
  369. len + 2, HZ);
  370. if (rc < 0)
  371. return rc;
  372. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  373. (len & 0x07) | 0x10);
  374. if (rc < 0)
  375. return rc;
  376. /* Test for Busy and ACK */
  377. do {
  378. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  379. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  380. if (rc < 0)
  381. return rc;
  382. if ((rc & 0x20) == 0) /* Ack? */
  383. break;
  384. /* I2C abort */
  385. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  386. if (--retries < 0)
  387. return -1;
  388. }
  389. if (i2c_debug & DBG_I2C) {
  390. int idx;
  391. for (idx = 0; idx < len; idx++) {
  392. PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
  393. }
  394. }
  395. return len;
  396. }
  397. static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
  398. short len)
  399. {
  400. char *bufPtr = buf;
  401. int retval;
  402. int wrcount = 0;
  403. int count;
  404. int maxLen = 4;
  405. struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
  406. while (len > 0) {
  407. count = (len > maxLen) ? maxLen : len;
  408. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  409. if (retval > 0) {
  410. len -= count;
  411. bufPtr += count;
  412. wrcount += count;
  413. } else
  414. return (retval < 0) ? retval : -EFAULT;
  415. }
  416. return wrcount;
  417. }
  418. static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
  419. short len)
  420. {
  421. char temp[4];
  422. int retval, i;
  423. int rdcount = 0;
  424. int count;
  425. struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
  426. while (len > 0) {
  427. count = (len > 3) ? 4 : len;
  428. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  429. if (retval > 0) {
  430. for (i = 0; i < len; i++)
  431. buf[rdcount + i] = temp[i];
  432. len -= count;
  433. rdcount += count;
  434. } else
  435. return (retval < 0) ? retval : -EFAULT;
  436. }
  437. return rdcount;
  438. }
  439. static struct i2c_adapter i2c_adap_template = {
  440. .owner = THIS_MODULE,
  441. .name = "usbvision",
  442. .id = I2C_HW_B_BT848, /* FIXME */
  443. .client_register = attach_inform,
  444. .client_unregister = detach_inform,
  445. #ifdef I2C_ADAP_CLASS_TV_ANALOG
  446. .class = I2C_ADAP_CLASS_TV_ANALOG,
  447. #else
  448. .class = I2C_CLASS_TV_ANALOG,
  449. #endif
  450. };
  451. static struct i2c_client i2c_client_template = {
  452. .name = "usbvision internal",
  453. };
  454. /*
  455. * Overrides for Emacs so that we follow Linus's tabbing style.
  456. * ---------------------------------------------------------------------------
  457. * Local variables:
  458. * c-basic-offset: 8
  459. * End:
  460. */