usbvision-i2c.c 12 KB

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