usbvision-i2c.c 12 KB

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