usbvision-i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * usbvision_i2c.c
  3. * i2c algorithm for USB-I2C Bridges
  4. *
  5. * Copyright (c) 1999-2007 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/usb.h>
  36. #include <linux/i2c.h>
  37. #include "usbvision.h"
  38. #define DBG_I2C 1<<0
  39. static int i2c_debug;
  40. module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
  41. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  42. #define PDEBUG(level, fmt, args...) { \
  43. if (i2c_debug & (level)) \
  44. printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
  45. __func__, __LINE__ , ## args); \
  46. }
  47. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  48. short len);
  49. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  50. short len);
  51. static inline int try_write_address(struct i2c_adapter *i2c_adap,
  52. unsigned char addr, int retries)
  53. {
  54. struct usb_usbvision *usbvision;
  55. int i, ret = -1;
  56. char buf[4];
  57. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  58. buf[0] = 0x00;
  59. for (i = 0; i <= retries; i++) {
  60. ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
  61. if (ret == 1)
  62. break; /* success! */
  63. udelay(5);
  64. if (i == retries) /* no success */
  65. break;
  66. udelay(10);
  67. }
  68. if (i) {
  69. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  70. PDEBUG(DBG_I2C,"Maybe there's no device at this address");
  71. }
  72. return ret;
  73. }
  74. static inline int try_read_address(struct i2c_adapter *i2c_adap,
  75. unsigned char addr, int retries)
  76. {
  77. struct usb_usbvision *usbvision;
  78. int i, ret = -1;
  79. char buf[4];
  80. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  81. for (i = 0; i <= retries; i++) {
  82. ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
  83. if (ret == 1)
  84. break; /* success! */
  85. udelay(5);
  86. if (i == retries) /* no success */
  87. break;
  88. udelay(10);
  89. }
  90. if (i) {
  91. PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
  92. PDEBUG(DBG_I2C,"Maybe there's no device at this address");
  93. }
  94. return ret;
  95. }
  96. static inline int usb_find_address(struct i2c_adapter *i2c_adap,
  97. struct i2c_msg *msg, int retries,
  98. unsigned char *add)
  99. {
  100. unsigned short flags = msg->flags;
  101. unsigned char addr;
  102. int ret;
  103. if ((flags & I2C_M_TEN)) {
  104. /* a ten bit address */
  105. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  106. /* try extended address code... */
  107. ret = try_write_address(i2c_adap, addr, retries);
  108. if (ret != 1) {
  109. dev_err(&i2c_adap->dev,
  110. "died at extended address code, while writing\n");
  111. return -EREMOTEIO;
  112. }
  113. add[0] = addr;
  114. if (flags & I2C_M_RD) {
  115. /* okay, now switch into reading mode */
  116. addr |= 0x01;
  117. ret = try_read_address(i2c_adap, addr, retries);
  118. if (ret != 1) {
  119. dev_err(&i2c_adap->dev,
  120. "died at extended address code, while reading\n");
  121. return -EREMOTEIO;
  122. }
  123. }
  124. } else { /* normal 7bit address */
  125. addr = (msg->addr << 1);
  126. if (flags & I2C_M_RD)
  127. addr |= 1;
  128. add[0] = addr;
  129. if (flags & I2C_M_RD)
  130. ret = try_read_address(i2c_adap, addr, retries);
  131. else
  132. ret = try_write_address(i2c_adap, addr, retries);
  133. if (ret != 1) {
  134. return -EREMOTEIO;
  135. }
  136. }
  137. return 0;
  138. }
  139. static int
  140. usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
  141. {
  142. struct i2c_msg *pmsg;
  143. struct usb_usbvision *usbvision;
  144. int i, ret;
  145. unsigned char addr = 0;
  146. usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
  147. for (i = 0; i < num; i++) {
  148. pmsg = &msgs[i];
  149. ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
  150. if (ret != 0) {
  151. PDEBUG(DBG_I2C,"got NAK from device, message #%d", i);
  152. return (ret < 0) ? ret : -EREMOTEIO;
  153. }
  154. if (pmsg->flags & I2C_M_RD) {
  155. /* read bytes into buffer */
  156. ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
  157. if (ret < pmsg->len) {
  158. return (ret < 0) ? ret : -EREMOTEIO;
  159. }
  160. } else {
  161. /* write bytes from buffer */
  162. ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
  163. if (ret < pmsg->len) {
  164. return (ret < 0) ? ret : -EREMOTEIO;
  165. }
  166. }
  167. }
  168. return num;
  169. }
  170. static u32 functionality(struct i2c_adapter *adap)
  171. {
  172. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  173. }
  174. /* -----exported algorithm data: ------------------------------------- */
  175. static struct i2c_algorithm usbvision_algo = {
  176. .master_xfer = usbvision_i2c_xfer,
  177. .smbus_xfer = NULL,
  178. .functionality = functionality,
  179. };
  180. /* ----------------------------------------------------------------------- */
  181. /* usbvision specific I2C functions */
  182. /* ----------------------------------------------------------------------- */
  183. static struct i2c_adapter i2c_adap_template;
  184. int usbvision_i2c_register(struct usb_usbvision *usbvision)
  185. {
  186. static unsigned short saa711x_addrs[] = {
  187. 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
  188. 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
  189. I2C_CLIENT_END };
  190. memcpy(&usbvision->i2c_adap, &i2c_adap_template,
  191. sizeof(struct i2c_adapter));
  192. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  193. " #%d", usbvision->vdev->num);
  194. PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
  195. usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
  196. i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
  197. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  198. printk(KERN_ERR "usbvision_register: can't write reg\n");
  199. return -EBUSY;
  200. }
  201. PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
  202. PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
  203. /* register new adapter to i2c module... */
  204. usbvision->i2c_adap.algo = &usbvision_algo;
  205. usbvision->i2c_adap.timeout = 100; /* default values, should */
  206. usbvision->i2c_adap.retries = 3; /* be replaced by defines */
  207. i2c_add_adapter(&usbvision->i2c_adap);
  208. PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
  209. /* Request the load of the i2c modules we need */
  210. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  211. case CODEC_SAA7113:
  212. case CODEC_SAA7111:
  213. v4l2_i2c_new_probed_subdev(&usbvision->i2c_adap, "saa7115",
  214. "saa7115_auto", saa711x_addrs);
  215. break;
  216. }
  217. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  218. struct v4l2_subdev *sd;
  219. enum v4l2_i2c_tuner_type type;
  220. struct tuner_setup tun_setup;
  221. sd = v4l2_i2c_new_probed_subdev(&usbvision->i2c_adap, "tuner",
  222. "tuner", v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
  223. /* depending on whether we found a demod or not, select
  224. the tuner type. */
  225. type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
  226. sd = v4l2_i2c_new_probed_subdev(&usbvision->i2c_adap, "tuner",
  227. "tuner", v4l2_i2c_tuner_addrs(type));
  228. if (usbvision->tuner_type != -1) {
  229. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  230. tun_setup.type = usbvision->tuner_type;
  231. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  232. call_all(usbvision, tuner, s_type_addr, &tun_setup);
  233. }
  234. }
  235. return 0;
  236. }
  237. int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
  238. {
  239. i2c_del_adapter(&(usbvision->i2c_adap));
  240. PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
  241. return 0;
  242. }
  243. static int
  244. usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
  245. char *buf, short len)
  246. {
  247. int rc, retries;
  248. for (retries = 5;;) {
  249. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  250. if (rc < 0)
  251. return rc;
  252. /* Initiate byte read cycle */
  253. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  254. /* d3 0=Wr 1=Rd */
  255. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  256. (len & 0x07) | 0x18);
  257. if (rc < 0)
  258. return rc;
  259. /* Test for Busy and ACK */
  260. do {
  261. /* USBVISION_SER_CONT -> d4 == 0 busy */
  262. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  263. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  264. if (rc < 0)
  265. return rc;
  266. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  267. if ((rc & 0x20) == 0) /* Ack? */
  268. break;
  269. /* I2C abort */
  270. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  271. if (rc < 0)
  272. return rc;
  273. if (--retries < 0)
  274. return -1;
  275. }
  276. switch (len) {
  277. case 4:
  278. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  279. case 3:
  280. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  281. case 2:
  282. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  283. case 1:
  284. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  285. break;
  286. default:
  287. printk(KERN_ERR
  288. "usbvision_i2c_read_max4: buffer length > 4\n");
  289. }
  290. if (i2c_debug & DBG_I2C) {
  291. int idx;
  292. for (idx = 0; idx < len; idx++) {
  293. PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
  294. }
  295. }
  296. return len;
  297. }
  298. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  299. unsigned char addr, const char *buf,
  300. short len)
  301. {
  302. int rc, retries;
  303. int i;
  304. unsigned char value[6];
  305. unsigned char ser_cont;
  306. ser_cont = (len & 0x07) | 0x10;
  307. value[0] = addr;
  308. value[1] = ser_cont;
  309. for (i = 0; i < len; i++)
  310. value[i + 2] = buf[i];
  311. for (retries = 5;;) {
  312. rc = usb_control_msg(usbvision->dev,
  313. usb_sndctrlpipe(usbvision->dev, 1),
  314. USBVISION_OP_CODE,
  315. USB_DIR_OUT | USB_TYPE_VENDOR |
  316. USB_RECIP_ENDPOINT, 0,
  317. (__u16) USBVISION_SER_ADRS, value,
  318. len + 2, HZ);
  319. if (rc < 0)
  320. return rc;
  321. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  322. (len & 0x07) | 0x10);
  323. if (rc < 0)
  324. return rc;
  325. /* Test for Busy and ACK */
  326. do {
  327. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  328. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  329. if (rc < 0)
  330. return rc;
  331. if ((rc & 0x20) == 0) /* Ack? */
  332. break;
  333. /* I2C abort */
  334. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  335. if (--retries < 0)
  336. return -1;
  337. }
  338. if (i2c_debug & DBG_I2C) {
  339. int idx;
  340. for (idx = 0; idx < len; idx++) {
  341. PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
  342. }
  343. }
  344. return len;
  345. }
  346. static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  347. short len)
  348. {
  349. char *bufPtr = buf;
  350. int retval;
  351. int wrcount = 0;
  352. int count;
  353. int maxLen = 4;
  354. while (len > 0) {
  355. count = (len > maxLen) ? maxLen : len;
  356. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  357. if (retval > 0) {
  358. len -= count;
  359. bufPtr += count;
  360. wrcount += count;
  361. } else
  362. return (retval < 0) ? retval : -EFAULT;
  363. }
  364. return wrcount;
  365. }
  366. static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
  367. short len)
  368. {
  369. char temp[4];
  370. int retval, i;
  371. int rdcount = 0;
  372. int count;
  373. while (len > 0) {
  374. count = (len > 3) ? 4 : len;
  375. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  376. if (retval > 0) {
  377. for (i = 0; i < len; i++)
  378. buf[rdcount + i] = temp[i];
  379. len -= count;
  380. rdcount += count;
  381. } else
  382. return (retval < 0) ? retval : -EFAULT;
  383. }
  384. return rdcount;
  385. }
  386. static struct i2c_adapter i2c_adap_template = {
  387. .owner = THIS_MODULE,
  388. .name = "usbvision",
  389. };
  390. /*
  391. * Overrides for Emacs so that we follow Linus's tabbing style.
  392. * ---------------------------------------------------------------------------
  393. * Local variables:
  394. * c-basic-offset: 8
  395. * End:
  396. */