vfc_i2c.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * drivers/sbus/char/vfc_i2c.c
  3.  *
  4.  * Driver for the Videopix Frame Grabber.
  5.  * 
  6.  * Functions that support the Phillips i2c(I squared C) bus on the vfc
  7.  *  Documentation for the Phillips I2C bus can be found on the 
  8.  *  phillips home page
  9.  *
  10.  * Copyright (C) 1996 Manish Vachharajani (mvachhar@noc.rutgers.edu)
  11.  *
  12.  */
  13. /* NOTE: It seems to me that the documentation regarding the
  14. pcd8584t/pcf8584 does not show the correct way to address the i2c bus.
  15. Based on the information on the I2C bus itself and the remainder of
  16. the Phillips docs the following algorithims apper to be correct.  I am
  17. fairly certain that the flowcharts in the phillips docs are wrong. */
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/errno.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include <linux/delay.h>
  25. #include <asm/openprom.h>
  26. #include <asm/oplib.h>
  27. #include <asm/io.h>
  28. #include <asm/system.h>
  29. #include <asm/sbus.h>
  30. #if 0 
  31. #define VFC_I2C_DEBUG
  32. #endif
  33. #include "vfc.h"
  34. #include "vfc_i2c.h"
  35. #define WRITE_S1(__val) 
  36. sbus_writel(__val, &dev->regs->i2c_s1)
  37. #define WRITE_REG(__val) 
  38. sbus_writel(__val, &dev->regs->i2c_reg)
  39. #define VFC_I2C_READ (0x1)
  40. #define VFC_I2C_WRITE (0x0)
  41.      
  42. /****** 
  43.   The i2c bus controller chip on the VFC is a pcd8584t, but
  44.   phillips claims it doesn't exist.  As far as I can tell it is
  45.   identical to the PCF8584 so I treat it like it is the pcf8584.
  46.   
  47.   NOTE: The pcf8584 only cares
  48.   about the msb of the word you feed it 
  49. *****/
  50. int vfc_pcf8584_init(struct vfc_dev *dev) 
  51. {
  52. /* This will also choose register S0_OWN so we can set it. */
  53. WRITE_S1(RESET);
  54. /* The pcf8584 shifts this value left one bit and uses
  55.  * it as its i2c bus address.
  56.  */
  57. WRITE_REG(0x55000000);
  58. /* This will set the i2c bus at the same speed sun uses,
  59.  * and set another magic bit.
  60.  */
  61. WRITE_S1(SELECT(S2));
  62. WRITE_REG(0x14000000);
  63. /* Enable the serial port, idle the i2c bus and set
  64.  * the data reg to s0.
  65.  */
  66. WRITE_S1(CLEAR_I2C_BUS);
  67. udelay(100);
  68. return 0;
  69. }
  70. void vfc_i2c_delay_wakeup(struct vfc_dev *dev) 
  71. {
  72. /* Used to profile code and eliminate too many delays */
  73. VFC_I2C_DEBUG_PRINTK(("vfc%d: Delayingn", dev->instance));
  74. wake_up(&dev->poll_wait);
  75. }
  76. void vfc_i2c_delay_no_busy(struct vfc_dev *dev, unsigned long usecs) 
  77. {
  78. init_timer(&dev->poll_timer);
  79. dev->poll_timer.expires = jiffies + 
  80. ((unsigned long)usecs*(HZ))/1000000;
  81. dev->poll_timer.data=(unsigned long)dev;
  82. dev->poll_timer.function=(void *)(unsigned long)vfc_i2c_delay_wakeup;
  83. add_timer(&dev->poll_timer);
  84. sleep_on(&dev->poll_wait);
  85. del_timer(&dev->poll_timer);
  86. }
  87. void inline vfc_i2c_delay(struct vfc_dev *dev) 
  88. vfc_i2c_delay_no_busy(dev, 100);
  89. }
  90. int vfc_init_i2c_bus(struct vfc_dev *dev)
  91. {
  92. WRITE_S1(ENABLE_SERIAL | SELECT(S0) | ACK);
  93. vfc_i2c_reset_bus(dev);
  94. return 0;
  95. }
  96. int vfc_i2c_reset_bus(struct vfc_dev *dev) 
  97. {
  98. VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: Resetting the i2c busn",
  99.       dev->instance));
  100. if(dev == NULL)
  101. return -EINVAL;
  102. if(dev->regs == NULL)
  103. return -EINVAL;
  104. WRITE_S1(SEND_I2C_STOP);
  105. WRITE_S1(SEND_I2C_STOP | ACK);
  106. vfc_i2c_delay(dev);
  107. WRITE_S1(CLEAR_I2C_BUS);
  108. VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: I2C status %xn",
  109.       dev->instance,
  110.       sbus_readl(&dev->regs->i2c_s1)));
  111. return 0;
  112. }
  113. int vfc_i2c_wait_for_bus(struct vfc_dev *dev) 
  114. {
  115. int timeout = 1000; 
  116. while(!(sbus_readl(&dev->regs->i2c_s1) & BB)) {
  117. if(!(timeout--))
  118. return -ETIMEDOUT;
  119. vfc_i2c_delay(dev);
  120. }
  121. return 0;
  122. }
  123. int vfc_i2c_wait_for_pin(struct vfc_dev *dev, int ack)
  124. {
  125. int timeout = 1000; 
  126. int s1;
  127. while ((s1 = sbus_readl(&dev->regs->i2c_s1)) & PIN) {
  128. if (!(timeout--))
  129. return -ETIMEDOUT;
  130. vfc_i2c_delay(dev);
  131. }
  132. if (ack == VFC_I2C_ACK_CHECK) {
  133. if(s1 & LRB)
  134. return -EIO; 
  135. }
  136. return 0;
  137. }
  138. #define SHIFT(a) ((a) << 24)
  139. int vfc_i2c_xmit_addr(struct vfc_dev *dev, unsigned char addr, char mode) 
  140. int ret, raddr;
  141. #if 1
  142. WRITE_S1(SEND_I2C_STOP | ACK);
  143. WRITE_S1(SELECT(S0) | ENABLE_SERIAL);
  144. vfc_i2c_delay(dev);
  145. #endif
  146. switch(mode) {
  147. case VFC_I2C_READ:
  148. raddr = SHIFT(((unsigned int)addr | 0x1));
  149. WRITE_REG(raddr);
  150. VFC_I2C_DEBUG_PRINTK(("vfc%d: receiving from i2c addr 0x%xn",
  151.       dev->instance, addr | 0x1));
  152. break;
  153. case VFC_I2C_WRITE:
  154. raddr = SHIFT((unsigned int)addr & ~0x1);
  155. WRITE_REG(raddr);
  156. VFC_I2C_DEBUG_PRINTK(("vfc%d: sending to i2c addr 0x%xn",
  157.       dev->instance, addr & ~0x1));
  158. break;
  159. default:
  160. return -EINVAL;
  161. };
  162. WRITE_S1(SEND_I2C_START);
  163. vfc_i2c_delay(dev);
  164. ret = vfc_i2c_wait_for_pin(dev,VFC_I2C_ACK_CHECK); /* We wait
  165.       for the
  166.       i2c send
  167.       to finish
  168.       here but
  169.       Sun
  170.       doesn't,
  171.       hmm */
  172. if (ret) {
  173. printk(KERN_ERR "vfc%d: VFC xmit addr timed out or no ackn",
  174.        dev->instance);
  175. return ret;
  176. } else if (mode == VFC_I2C_READ) {
  177. if ((ret = sbus_readl(&dev->regs->i2c_reg) & 0xff000000) != raddr) {
  178. printk(KERN_WARNING 
  179.        "vfc%d: returned slave address "
  180.        "mismatch(%x,%x)n",
  181.        dev->instance, raddr, ret);
  182. }
  183. }
  184. return 0;
  185. }
  186. int vfc_i2c_xmit_byte(struct vfc_dev *dev,unsigned char *byte) 
  187. {
  188. int ret;
  189. u32 val = SHIFT((unsigned int)*byte);
  190. WRITE_REG(val);
  191. ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_ACK_CHECK); 
  192. switch(ret) {
  193. case -ETIMEDOUT: 
  194. printk(KERN_ERR "vfc%d: VFC xmit byte timed out or no ackn",
  195.        dev->instance);
  196. break;
  197. case -EIO:
  198. ret = XMIT_LAST_BYTE;
  199. break;
  200. default:
  201. break;
  202. };
  203. return ret;
  204. }
  205. int vfc_i2c_recv_byte(struct vfc_dev *dev, unsigned char *byte, int last) 
  206. {
  207. int ret;
  208. if (last) {
  209. WRITE_REG(NEGATIVE_ACK);
  210. VFC_I2C_DEBUG_PRINTK(("vfc%d: sending negative ackn",
  211.       dev->instance));
  212. } else {
  213. WRITE_S1(ACK);
  214. }
  215. ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_NO_ACK_CHECK);
  216. if(ret) {
  217. printk(KERN_ERR "vfc%d: "
  218.        "VFC recv byte timed outn",
  219.        dev->instance);
  220. }
  221. *byte = (sbus_readl(&dev->regs->i2c_reg)) >> 24;
  222. return ret;
  223. }
  224. int vfc_i2c_recvbuf(struct vfc_dev *dev, unsigned char addr,
  225.     char *buf, int count)
  226. {
  227. int ret, last;
  228. if(!(count && buf && dev && dev->regs) )
  229. return -EINVAL;
  230. if ((ret = vfc_i2c_wait_for_bus(dev))) {
  231. printk(KERN_ERR "vfc%d: VFC I2C bus busyn", dev->instance);
  232. return ret;
  233. }
  234. if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_READ))) {
  235. WRITE_S1(SEND_I2C_STOP);
  236. vfc_i2c_delay(dev);
  237. return ret;
  238. }
  239. last = 0;
  240. while (count--) {
  241. if (!count)
  242. last = 1;
  243. if ((ret = vfc_i2c_recv_byte(dev, buf, last))) {
  244. printk(KERN_ERR "vfc%d: "
  245.        "VFC error while receiving byten",
  246.        dev->instance);
  247. WRITE_S1(SEND_I2C_STOP);
  248. ret = -EINVAL;
  249. }
  250. buf++;
  251. }
  252. WRITE_S1(SEND_I2C_STOP | ACK);
  253. vfc_i2c_delay(dev);
  254. return ret;
  255. }
  256. int vfc_i2c_sendbuf(struct vfc_dev *dev, unsigned char addr, 
  257.     char *buf, int count) 
  258. {
  259. int ret;
  260. if (!(buf && dev && dev->regs))
  261. return -EINVAL;
  262. if ((ret = vfc_i2c_wait_for_bus(dev))) {
  263. printk(KERN_ERR "vfc%d: VFC I2C bus busyn", dev->instance);
  264. return ret;
  265. }
  266. if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_WRITE))) {
  267. WRITE_S1(SEND_I2C_STOP);
  268. vfc_i2c_delay(dev);
  269. return ret;
  270. }
  271. while(count--) {
  272. ret = vfc_i2c_xmit_byte(dev, buf);
  273. switch(ret) {
  274. case XMIT_LAST_BYTE:
  275. VFC_I2C_DEBUG_PRINTK(("vfc%d: "
  276.       "Receiver ended transmission with "
  277.       " %d bytes remainingn",
  278.       dev->instance, count));
  279. ret = 0;
  280. goto done;
  281. break;
  282. case 0:
  283. break;
  284. default:
  285. printk(KERN_ERR "vfc%d: "
  286.        "VFC error while sending byten", dev->instance);
  287. break;
  288. };
  289. buf++;
  290. }
  291. done:
  292. WRITE_S1(SEND_I2C_STOP | ACK);
  293. vfc_i2c_delay(dev);
  294. return ret;
  295. }