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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* ------------------------------------------------------------------------- */
  2. /* i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters      */
  3. /* ------------------------------------------------------------------------- */
  4. /*   Copyright (C) 1995-1997 Simon G. Vogl
  5.                    1998-2000 Hans Berglund
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.      */
  17. /* ------------------------------------------------------------------------- */
  18. /* With some changes from Ky鰏ti M鋖kki <kmalkki@cc.hut.fi> and 
  19.    Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
  20.    <mbailey@littlefeet-inc.com> */
  21. /* Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
  22.    messages, proper stop/repstart signaling during receive,
  23.    added detect code */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/version.h>
  29. #include <linux/init.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/ioport.h>
  32. #include <linux/errno.h>
  33. #include <linux/sched.h>
  34. #include <linux/i2c.h>
  35. #include <linux/i2c-algo-pcf.h>
  36. #include "i2c-pcf8584.h"
  37. /* ----- global defines ----------------------------------------------- */
  38. #define DEB(x) if (i2c_debug>=1) x
  39. #define DEB2(x) if (i2c_debug>=2) x
  40. #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
  41. #define DEBPROTO(x) if (i2c_debug>=9) x;
  42.   /* debug the protocol by showing transferred bits */
  43. #define DEF_TIMEOUT 16
  44. /* module parameters:
  45.  */
  46. static int i2c_debug=0;
  47. static int pcf_scan=0; /* have a look at what's hanging 'round */
  48. /* --- setting states on the bus with the right timing: --------------- */
  49. #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
  50. #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
  51. #define get_own(adap) adap->getown(adap->data)
  52. #define get_clock(adap) adap->getclock(adap->data)
  53. #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
  54. #define i2c_inb(adap) adap->getpcf(adap->data, 0)
  55. /* --- other auxiliary functions -------------------------------------- */
  56. static void i2c_start(struct i2c_algo_pcf_data *adap) 
  57. {
  58. DEBPROTO(printk("S "));
  59. set_pcf(adap, 1, I2C_PCF_START);
  60. }
  61. static void i2c_repstart(struct i2c_algo_pcf_data *adap) 
  62. {
  63. DEBPROTO(printk(" Sr "));
  64. set_pcf(adap, 1, I2C_PCF_REPSTART);
  65. }
  66. static void i2c_stop(struct i2c_algo_pcf_data *adap) 
  67. {
  68. DEBPROTO(printk("Pn"));
  69. set_pcf(adap, 1, I2C_PCF_STOP);
  70. }
  71. static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
  72. int timeout = DEF_TIMEOUT;
  73. int status;
  74. status = get_pcf(adap, 1);
  75. #ifndef STUB_I2C
  76. while (timeout-- && !(status & I2C_PCF_BB)) {
  77. udelay(100); /* wait for 100 us */
  78. status = get_pcf(adap, 1);
  79. }
  80. #endif
  81. if (timeout <= 0) {
  82. printk("Timeout waiting for Bus Busyn");
  83. }
  84. return (timeout<=0);
  85. }
  86. static inline void pcf_sleep(unsigned long timeout)
  87. {
  88. schedule_timeout( timeout * HZ);
  89. }
  90. static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
  91. int timeout = DEF_TIMEOUT;
  92. *status = get_pcf(adap, 1);
  93. #ifndef STUB_I2C
  94. while (timeout-- && (*status & I2C_PCF_PIN)) {
  95. adap->waitforpin();
  96. *status = get_pcf(adap, 1);
  97. }
  98. #endif
  99. if (timeout <= 0)
  100. return(-1);
  101. else
  102. return(0);
  103. }
  104. /* 
  105.  * This should perform the 'PCF8584 initialization sequence' as described
  106.  * in the Philips IC12 data book (1995, Aug 29).
  107.  * There should be a 30 clock cycle wait after reset, I assume this
  108.  * has been fulfilled.
  109.  * There should be a delay at the end equal to the longest I2C message
  110.  * to synchronize the BB-bit (in multimaster systems). How long is
  111.  * this? I assume 1 second is always long enough.
  112.  *
  113.  * vdovikin: added detect code for PCF8584
  114.  */
  115. static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
  116. {
  117. unsigned char temp;
  118. DEB3(printk("i2c-algo-pcf.o: PCF state 0x%02xn", get_pcf(adap, 1)));
  119. /* S1=0x80: S0 selected, serial interface off */
  120. set_pcf(adap, 1, I2C_PCF_PIN);
  121. /* check to see S1 now used as R/W ctrl -
  122.    PCF8584 does that when ESO is zero */
  123. /* PCF also resets PIN bit */
  124. if ((temp = get_pcf(adap, 1)) != (0)) {
  125. DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).n", temp));
  126. return -ENXIO; /* definetly not PCF8584 */
  127. }
  128. /* load own address in S0, effective address is (own << 1) */
  129. i2c_outb(adap, get_own(adap));
  130. /* check it's realy writen */
  131. if ((temp = i2c_inb(adap)) != get_own(adap)) {
  132. DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).n", temp));
  133. return -ENXIO;
  134. }
  135. /* S1=0xA0, next byte in S2 */
  136. set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
  137. /* check to see S2 now selected */
  138. if ((temp = get_pcf(adap, 1)) != I2C_PCF_ES1) {
  139. DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).n", temp));
  140. return -ENXIO;
  141. }
  142. /* load clock register S2 */
  143. i2c_outb(adap, get_clock(adap));
  144. /* check it's realy writen, the only 5 lowest bits does matter */
  145. if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
  146. DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).n", temp));
  147. return -ENXIO;
  148. }
  149. /* Enable serial interface, idle, S0 selected */
  150. set_pcf(adap, 1, I2C_PCF_IDLE);
  151. /* check to see PCF is realy idled and we can access status register */
  152. if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
  153. DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).n", temp));
  154. return -ENXIO;
  155. }
  156. printk("i2c-algo-pcf.o: deteted and initialized PCF8584.n");
  157. return 0;
  158. }
  159. /* ----- Utility functions
  160.  */
  161. static inline int try_address(struct i2c_algo_pcf_data *adap,
  162.        unsigned char addr, int retries)
  163. {
  164. int i, status, ret = -1;
  165. for (i=0;i<retries;i++) {
  166. i2c_outb(adap, addr);
  167. i2c_start(adap);
  168. status = get_pcf(adap, 1);
  169. if (wait_for_pin(adap, &status) >= 0) {
  170. if ((status & I2C_PCF_LRB) == 0) { 
  171. i2c_stop(adap);
  172. break; /* success! */
  173. }
  174. }
  175. i2c_stop(adap);
  176. udelay(adap->udelay);
  177. }
  178. DEB2(if (i) printk("i2c-algo-pcf.o: needed %d retries for %dn",i,
  179.                    addr));
  180. return ret;
  181. }
  182. static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
  183.                          int count, int last)
  184. {
  185. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  186. int wrcount, status, timeout;
  187.     
  188. for (wrcount=0; wrcount<count; ++wrcount) {
  189. DEB2(printk("i2c-algo-pcf.o: %s i2c_write: writing %2.2Xn",
  190.       i2c_adap->name, buf[wrcount]&0xff));
  191. i2c_outb(adap, buf[wrcount]);
  192. timeout = wait_for_pin(adap, &status);
  193. if (timeout) {
  194. i2c_stop(adap);
  195. printk("i2c-algo-pcf.o: %s i2c_write: "
  196.        "error - timeout.n", i2c_adap->name);
  197. return -EREMOTEIO; /* got a better one ?? */
  198. }
  199. #ifndef STUB_I2C
  200. if (status & I2C_PCF_LRB) {
  201. i2c_stop(adap);
  202. printk("i2c-algo-pcf.o: %s i2c_write: "
  203.        "error - no ack.n", i2c_adap->name);
  204. return -EREMOTEIO; /* got a better one ?? */
  205. }
  206. #endif
  207. }
  208. if (last) {
  209. i2c_stop(adap);
  210. }
  211. else {
  212. i2c_repstart(adap);
  213. }
  214. return (wrcount);
  215. }
  216. static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
  217.                          int count, int last)
  218. {
  219. int i, status;
  220. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  221. /* increment number of bytes to read by one -- read dummy byte */
  222. for (i = 0; i <= count; i++) {
  223. if (wait_for_pin(adap, &status)) {
  224. i2c_stop(adap);
  225. printk("i2c-algo-pcf.o: pcf_readbytes timed out.n");
  226. return (-1);
  227. }
  228. #ifndef STUB_I2C
  229. if ((status & I2C_PCF_LRB) && (i != count)) {
  230. i2c_stop(adap);
  231. printk("i2c-algo-pcf.o: i2c_read: i2c_inb, No ack.n");
  232. return (-1);
  233. }
  234. #endif
  235. if (i == count - 1) {
  236. set_pcf(adap, 1, I2C_PCF_ESO);
  237. } else 
  238. if (i == count) {
  239. if (last) {
  240. i2c_stop(adap);
  241. } else {
  242. i2c_repstart(adap);
  243. }
  244. };
  245. if (i) {
  246. buf[i - 1] = i2c_inb(adap);
  247. } else {
  248. i2c_inb(adap); /* dummy read */
  249. }
  250. }
  251. return (i - 1);
  252. }
  253. static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
  254.                                 struct i2c_msg *msg, int retries) 
  255. {
  256. unsigned short flags = msg->flags;
  257. unsigned char addr;
  258. int ret;
  259. if ( (flags & I2C_M_TEN)  ) { 
  260. /* a ten bit address */
  261. addr = 0xf0 | (( msg->addr >> 7) & 0x03);
  262. DEB2(printk("addr0: %dn",addr));
  263. /* try extended address code...*/
  264. ret = try_address(adap, addr, retries);
  265. if (ret!=1) {
  266. printk("died at extended address code.n");
  267. return -EREMOTEIO;
  268. }
  269. /* the remaining 8 bit address */
  270. i2c_outb(adap,msg->addr & 0x7f);
  271. /* Status check comes here */
  272. if (ret != 1) {
  273. printk("died at 2nd address code.n");
  274. return -EREMOTEIO;
  275. }
  276. if ( flags & I2C_M_RD ) {
  277. i2c_repstart(adap);
  278. /* okay, now switch into reading mode */
  279. addr |= 0x01;
  280. ret = try_address(adap, addr, retries);
  281. if (ret!=1) {
  282. printk("died at extended address code.n");
  283. return -EREMOTEIO;
  284. }
  285. }
  286. } else { /* normal 7bit address */
  287. addr = ( msg->addr << 1 );
  288. if (flags & I2C_M_RD )
  289. addr |= 1;
  290. if (flags & I2C_M_REV_DIR_ADDR )
  291. addr ^= 1;
  292. i2c_outb(adap, addr);
  293. }
  294. return 0;
  295. }
  296. static int pcf_xfer(struct i2c_adapter *i2c_adap,
  297.     struct i2c_msg msgs[], 
  298.     int num)
  299. {
  300. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  301. struct i2c_msg *pmsg;
  302. int i;
  303. int ret=0, timeout, status;
  304.     
  305. /* Check for bus busy */
  306. timeout = wait_for_bb(adap);
  307. if (timeout) {
  308. DEB2(printk("i2c-algo-pcf.o: "
  309.             "Timeout waiting for BB in pcf_xfern");)
  310. return -EIO;
  311. }
  312. for (i = 0;ret >= 0 && i < num; i++) {
  313. pmsg = &msgs[i];
  314. DEB2(printk("i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messagesn",
  315.      pmsg->flags & I2C_M_RD ? "read" : "write",
  316.                      pmsg->len, pmsg->addr, i + 1, num);)
  317.     
  318. ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
  319. /* Send START */
  320. if (i == 0) {
  321. i2c_start(adap); 
  322. }
  323.     
  324. /* Wait for PIN (pending interrupt NOT) */
  325. timeout = wait_for_pin(adap, &status);
  326. if (timeout) {
  327. i2c_stop(adap);
  328. DEB2(printk("i2c-algo-pcf.o: Timeout waiting "
  329.     "for PIN(1) in pcf_xfern");)
  330. return (-EREMOTEIO);
  331. }
  332.     
  333. #ifndef STUB_I2C
  334. /* Check LRB (last rcvd bit - slave ack) */
  335. if (status & I2C_PCF_LRB) {
  336. i2c_stop(adap);
  337. DEB2(printk("i2c-algo-pcf.o: No LRB(1) in pcf_xfern");)
  338. return (-EREMOTEIO);
  339. }
  340. #endif
  341.     
  342. DEB3(printk("i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%dn",
  343.     i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
  344.     
  345. /* Read */
  346. if (pmsg->flags & I2C_M_RD) {
  347. /* read bytes into buffer*/
  348. ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
  349.                                             (i + 1 == num));
  350.         
  351. if (ret != pmsg->len) {
  352. DEB2(printk("i2c-algo-pcf.o: fail: "
  353.     "only read %d bytes.n",ret));
  354. } else {
  355. DEB2(printk("i2c-algo-pcf.o: read %d bytes.n",ret));
  356. }
  357. } else { /* Write */
  358. ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
  359.                                             (i + 1 == num));
  360.         
  361. if (ret != pmsg->len) {
  362. DEB2(printk("i2c-algo-pcf.o: fail: "
  363.     "only wrote %d bytes.n",ret));
  364. } else {
  365. DEB2(printk("i2c-algo-pcf.o: wrote %d bytes.n",ret));
  366. }
  367. }
  368. }
  369. return (i);
  370. }
  371. static int algo_control(struct i2c_adapter *adapter, 
  372. unsigned int cmd, unsigned long arg)
  373. {
  374. return 0;
  375. }
  376. static u32 pcf_func(struct i2c_adapter *adap)
  377. {
  378. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | 
  379.        I2C_FUNC_PROTOCOL_MANGLING; 
  380. }
  381. /* -----exported algorithm data: ------------------------------------- */
  382. static struct i2c_algorithm pcf_algo = {
  383. "PCF8584 algorithm",
  384. I2C_ALGO_PCF,
  385. pcf_xfer,
  386. NULL,
  387. NULL, /* slave_xmit */
  388. NULL, /* slave_recv */
  389. algo_control, /* ioctl */
  390. pcf_func, /* functionality */
  391. };
  392. /* 
  393.  * registering functions to load algorithms at runtime 
  394.  */
  395. int i2c_pcf_add_bus(struct i2c_adapter *adap)
  396. {
  397. int i, status;
  398. struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
  399. DEB2(printk("i2c-algo-pcf.o: hw routines for %s registered.n",
  400.             adap->name));
  401. /* register new adapter to i2c module... */
  402. adap->id |= pcf_algo.id;
  403. adap->algo = &pcf_algo;
  404. adap->timeout = 100; /* default values, should */
  405. adap->retries = 3; /* be replaced by defines */
  406. if ((i = pcf_init_8584(pcf_adap))) {
  407. return i;
  408. }
  409. #ifdef MODULE
  410. MOD_INC_USE_COUNT;
  411. #endif
  412. i2c_add_adapter(adap);
  413. /* scan bus */
  414. if (pcf_scan) {
  415. printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s.n",
  416.        adap->name);
  417. for (i = 0x00; i < 0xff; i+=2) {
  418. if (wait_for_bb(pcf_adap)) {
  419.      printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s - TIMEOUTed.n",
  420.            adap->name);
  421.     break;
  422. }
  423. i2c_outb(pcf_adap, i);
  424. i2c_start(pcf_adap);
  425. if ((wait_for_pin(pcf_adap, &status) >= 0) && 
  426.     ((status & I2C_PCF_LRB) == 0)) { 
  427. printk("(%02x)",i>>1); 
  428. } else {
  429. printk("."); 
  430. }
  431. i2c_stop(pcf_adap);
  432. udelay(pcf_adap->udelay);
  433. }
  434. printk("n");
  435. }
  436. return 0;
  437. }
  438. int i2c_pcf_del_bus(struct i2c_adapter *adap)
  439. {
  440. int res;
  441. if ((res = i2c_del_adapter(adap)) < 0)
  442. return res;
  443. DEB2(printk("i2c-algo-pcf.o: adapter unregistered: %sn",adap->name));
  444. #ifdef MODULE
  445. MOD_DEC_USE_COUNT;
  446. #endif
  447. return 0;
  448. }
  449. int __init i2c_algo_pcf_init (void)
  450. {
  451. printk("i2c-algo-pcf.o: i2c pcf8584 algorithm modulen");
  452. return 0;
  453. }
  454. EXPORT_SYMBOL(i2c_pcf_add_bus);
  455. EXPORT_SYMBOL(i2c_pcf_del_bus);
  456. #ifdef MODULE
  457. MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
  458. MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
  459. MODULE_LICENSE("GPL");
  460. MODULE_PARM(pcf_scan, "i");
  461. MODULE_PARM(i2c_debug,"i");
  462. MODULE_PARM_DESC(pcf_scan, "Scan for active chips on the bus");
  463. MODULE_PARM_DESC(i2c_debug,
  464.         "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
  465. int init_module(void) 
  466. {
  467. return i2c_algo_pcf_init();
  468. }
  469. void cleanup_module(void) 
  470. {
  471. }
  472. #endif