usbvision-i2c.c 11 KB

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