i2c-algo-ite.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. -------------------------------------------------------------------------
  3. i2c-algo-ite.c i2c driver algorithms for ITE adapters
  4. Hai-Pao Fan, MontaVista Software, Inc.
  5. hpfan@mvista.com or source@mvista.com
  6. Copyright 2000 MontaVista Software Inc.
  7. ---------------------------------------------------------------------------
  8. This file was highly leveraged from i2c-algo-pcf.c, which was created
  9. by Simon G. Vogl and Hans Berglund:
  10. Copyright (C) 1995-1997 Simon G. Vogl
  11. 1998-2000 Hans Berglund
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  23. /* ------------------------------------------------------------------------- */
  24. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
  25. Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
  26. <mbailey@littlefeet-inc.com> */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/ioport.h>
  34. #include <linux/errno.h>
  35. #include <linux/sched.h>
  36. #include <linux/i2c.h>
  37. #include <linux/i2c-algo-ite.h>
  38. #include "i2c-algo-ite.h"
  39. #define PM_DSR IT8172_PCI_IO_BASE + IT_PM_DSR
  40. #define PM_IBSR IT8172_PCI_IO_BASE + IT_PM_DSR + 0x04
  41. #define GPIO_CCR IT8172_PCI_IO_BASE + IT_GPCCR
  42. #define DEB2(x) if (i2c_debug>=2) x
  43. #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
  44. #define DEF_TIMEOUT 16
  45. /* module parameters:
  46. */
  47. static int i2c_debug;
  48. static int iic_test; /* see if the line-setting functions work */
  49. /* --- setting states on the bus with the right timing: --------------- */
  50. #define get_clock(adap) adap->getclock(adap->data)
  51. #define iic_outw(adap, reg, val) adap->setiic(adap->data, reg, val)
  52. #define iic_inw(adap, reg) adap->getiic(adap->data, reg)
  53. /* --- other auxiliary functions -------------------------------------- */
  54. static void iic_start(struct i2c_algo_iic_data *adap)
  55. {
  56. iic_outw(adap,ITE_I2CHCR,ITE_CMD);
  57. }
  58. static void iic_stop(struct i2c_algo_iic_data *adap)
  59. {
  60. iic_outw(adap,ITE_I2CHCR,0);
  61. iic_outw(adap,ITE_I2CHSR,ITE_I2CHSR_TDI);
  62. }
  63. static void iic_reset(struct i2c_algo_iic_data *adap)
  64. {
  65. iic_outw(adap, PM_IBSR, iic_inw(adap, PM_IBSR) | 0x80);
  66. }
  67. static int wait_for_bb(struct i2c_algo_iic_data *adap)
  68. {
  69. int timeout = DEF_TIMEOUT;
  70. short status;
  71. status = iic_inw(adap, ITE_I2CHSR);
  72. #ifndef STUB_I2C
  73. while (timeout-- && (status & ITE_I2CHSR_HB)) {
  74. udelay(1000); /* How much is this? */
  75. status = iic_inw(adap, ITE_I2CHSR);
  76. }
  77. #endif
  78. if (timeout<=0) {
  79. printk(KERN_ERR "Timeout, host is busy\n");
  80. iic_reset(adap);
  81. }
  82. return(timeout<=0);
  83. }
  84. /* After we issue a transaction on the IIC bus, this function
  85. * is called. It puts this process to sleep until we get an interrupt from
  86. * from the controller telling us that the transaction we requested in complete.
  87. */
  88. static int wait_for_pin(struct i2c_algo_iic_data *adap, short *status) {
  89. int timeout = DEF_TIMEOUT;
  90. timeout = wait_for_bb(adap);
  91. if (timeout) {
  92. DEB2(printk("Timeout waiting for host not busy\n");)
  93. return -EIO;
  94. }
  95. timeout = DEF_TIMEOUT;
  96. *status = iic_inw(adap, ITE_I2CHSR);
  97. #ifndef STUB_I2C
  98. while (timeout-- && !(*status & ITE_I2CHSR_TDI)) {
  99. adap->waitforpin();
  100. *status = iic_inw(adap, ITE_I2CHSR);
  101. }
  102. #endif
  103. if (timeout <= 0)
  104. return(-1);
  105. else
  106. return(0);
  107. }
  108. static int wait_for_fe(struct i2c_algo_iic_data *adap, short *status)
  109. {
  110. int timeout = DEF_TIMEOUT;
  111. *status = iic_inw(adap, ITE_I2CFSR);
  112. #ifndef STUB_I2C
  113. while (timeout-- && (*status & ITE_I2CFSR_FE)) {
  114. udelay(1000);
  115. iic_inw(adap, ITE_I2CFSR);
  116. }
  117. #endif
  118. if (timeout <= 0)
  119. return(-1);
  120. else
  121. return(0);
  122. }
  123. static int iic_init (struct i2c_algo_iic_data *adap)
  124. {
  125. short i;
  126. /* Clear bit 7 to set I2C to normal operation mode */
  127. i=iic_inw(adap, PM_DSR)& 0xff7f;
  128. iic_outw(adap, PM_DSR, i);
  129. /* set IT_GPCCR port C bit 2&3 as function 2 */
  130. i = iic_inw(adap, GPIO_CCR) & 0xfc0f;
  131. iic_outw(adap,GPIO_CCR,i);
  132. /* Clear slave address/sub-address */
  133. iic_outw(adap,ITE_I2CSAR, 0);
  134. iic_outw(adap,ITE_I2CSSAR, 0);
  135. /* Set clock counter register */
  136. iic_outw(adap,ITE_I2CCKCNT, get_clock(adap));
  137. /* Set START/reSTART/STOP time registers */
  138. iic_outw(adap,ITE_I2CSHDR, 0x0a);
  139. iic_outw(adap,ITE_I2CRSUR, 0x0a);
  140. iic_outw(adap,ITE_I2CPSUR, 0x0a);
  141. /* Enable interrupts on completing the current transaction */
  142. iic_outw(adap,ITE_I2CHCR, ITE_I2CHCR_IE | ITE_I2CHCR_HCE);
  143. /* Clear transfer count */
  144. iic_outw(adap,ITE_I2CFBCR, 0x0);
  145. DEB2(printk("iic_init: Initialized IIC on ITE 0x%x\n",
  146. iic_inw(adap, ITE_I2CHSR)));
  147. return 0;
  148. }
  149. /*
  150. * Sanity check for the adapter hardware - check the reaction of
  151. * the bus lines only if it seems to be idle.
  152. */
  153. static int test_bus(struct i2c_algo_iic_data *adap, char *name) {
  154. #if 0
  155. int scl,sda;
  156. sda=getsda(adap);
  157. if (adap->getscl==NULL) {
  158. printk("test_bus: Warning: Adapter can't read from clock line - skipping test.\n");
  159. return 0;
  160. }
  161. scl=getscl(adap);
  162. printk("test_bus: Adapter: %s scl: %d sda: %d -- testing...\n",
  163. name,getscl(adap),getsda(adap));
  164. if (!scl || !sda ) {
  165. printk("test_bus: %s seems to be busy.\n",adap->name);
  166. goto bailout;
  167. }
  168. sdalo(adap);
  169. printk("test_bus:1 scl: %d sda: %d\n", getscl(adap),
  170. getsda(adap));
  171. if ( 0 != getsda(adap) ) {
  172. printk("test_bus: %s SDA stuck high!\n",name);
  173. sdahi(adap);
  174. goto bailout;
  175. }
  176. if ( 0 == getscl(adap) ) {
  177. printk("test_bus: %s SCL unexpected low while pulling SDA low!\n",
  178. name);
  179. goto bailout;
  180. }
  181. sdahi(adap);
  182. printk("test_bus:2 scl: %d sda: %d\n", getscl(adap),
  183. getsda(adap));
  184. if ( 0 == getsda(adap) ) {
  185. printk("test_bus: %s SDA stuck low!\n",name);
  186. sdahi(adap);
  187. goto bailout;
  188. }
  189. if ( 0 == getscl(adap) ) {
  190. printk("test_bus: %s SCL unexpected low while SDA high!\n",
  191. adap->name);
  192. goto bailout;
  193. }
  194. scllo(adap);
  195. printk("test_bus:3 scl: %d sda: %d\n", getscl(adap),
  196. getsda(adap));
  197. if ( 0 != getscl(adap) ) {
  198. sclhi(adap);
  199. goto bailout;
  200. }
  201. if ( 0 == getsda(adap) ) {
  202. printk("test_bus: %s SDA unexpected low while pulling SCL low!\n",
  203. name);
  204. goto bailout;
  205. }
  206. sclhi(adap);
  207. printk("test_bus:4 scl: %d sda: %d\n", getscl(adap),
  208. getsda(adap));
  209. if ( 0 == getscl(adap) ) {
  210. printk("test_bus: %s SCL stuck low!\n",name);
  211. sclhi(adap);
  212. goto bailout;
  213. }
  214. if ( 0 == getsda(adap) ) {
  215. printk("test_bus: %s SDA unexpected low while SCL high!\n",
  216. name);
  217. goto bailout;
  218. }
  219. printk("test_bus: %s passed test.\n",name);
  220. return 0;
  221. bailout:
  222. sdahi(adap);
  223. sclhi(adap);
  224. return -ENODEV;
  225. #endif
  226. return (0);
  227. }
  228. /* ----- Utility functions
  229. */
  230. /* Verify the device we want to talk to on the IIC bus really exists. */
  231. static inline int try_address(struct i2c_algo_iic_data *adap,
  232. unsigned int addr, int retries)
  233. {
  234. int i, ret = -1;
  235. short status;
  236. for (i=0;i<retries;i++) {
  237. iic_outw(adap, ITE_I2CSAR, addr);
  238. iic_start(adap);
  239. if (wait_for_pin(adap, &status) == 0) {
  240. if ((status & ITE_I2CHSR_DNE) == 0) {
  241. iic_stop(adap);
  242. iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
  243. ret=1;
  244. break; /* success! */
  245. }
  246. }
  247. iic_stop(adap);
  248. udelay(adap->udelay);
  249. }
  250. DEB2(if (i) printk("try_address: needed %d retries for 0x%x\n",i,
  251. addr));
  252. return ret;
  253. }
  254. static int iic_sendbytes(struct i2c_adapter *i2c_adap,const char *buf,
  255. int count)
  256. {
  257. struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
  258. int wrcount=0, timeout;
  259. short status;
  260. int loops, remainder, i, j;
  261. union {
  262. char byte[2];
  263. unsigned short word;
  264. } tmp;
  265. iic_outw(adap, ITE_I2CSSAR, (unsigned short)buf[wrcount++]);
  266. count--;
  267. if (count == 0)
  268. return -EIO;
  269. loops = count / 32; /* 32-byte FIFO */
  270. remainder = count % 32;
  271. if(loops) {
  272. for(i=0; i<loops; i++) {
  273. iic_outw(adap, ITE_I2CFBCR, 32);
  274. for(j=0; j<32/2; j++) {
  275. tmp.byte[1] = buf[wrcount++];
  276. tmp.byte[0] = buf[wrcount++];
  277. iic_outw(adap, ITE_I2CFDR, tmp.word);
  278. }
  279. /* status FIFO overrun */
  280. iic_inw(adap, ITE_I2CFSR);
  281. iic_inw(adap, ITE_I2CFBCR);
  282. iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
  283. /* Wait for transmission to complete */
  284. timeout = wait_for_pin(adap, &status);
  285. if(timeout) {
  286. iic_stop(adap);
  287. printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
  288. return -EREMOTEIO; /* got a better one ?? */
  289. }
  290. if (status & ITE_I2CHSR_DB) {
  291. iic_stop(adap);
  292. printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
  293. return -EREMOTEIO; /* got a better one ?? */
  294. }
  295. }
  296. }
  297. if(remainder) {
  298. iic_outw(adap, ITE_I2CFBCR, remainder);
  299. for(i=0; i<remainder/2; i++) {
  300. tmp.byte[1] = buf[wrcount++];
  301. tmp.byte[0] = buf[wrcount++];
  302. iic_outw(adap, ITE_I2CFDR, tmp.word);
  303. }
  304. /* status FIFO overrun */
  305. iic_inw(adap, ITE_I2CFSR);
  306. iic_inw(adap, ITE_I2CFBCR);
  307. iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
  308. timeout = wait_for_pin(adap, &status);
  309. if(timeout) {
  310. iic_stop(adap);
  311. printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
  312. return -EREMOTEIO; /* got a better one ?? */
  313. }
  314. #ifndef STUB_I2C
  315. if (status & ITE_I2CHSR_DB) {
  316. iic_stop(adap);
  317. printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
  318. return -EREMOTEIO; /* got a better one ?? */
  319. }
  320. #endif
  321. }
  322. iic_stop(adap);
  323. return wrcount;
  324. }
  325. static int iic_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count,
  326. int sread)
  327. {
  328. int rdcount=0, i, timeout;
  329. short status;
  330. struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
  331. int loops, remainder, j;
  332. union {
  333. char byte[2];
  334. unsigned short word;
  335. } tmp;
  336. loops = count / 32; /* 32-byte FIFO */
  337. remainder = count % 32;
  338. if(loops) {
  339. for(i=0; i<loops; i++) {
  340. iic_outw(adap, ITE_I2CFBCR, 32);
  341. if (sread)
  342. iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
  343. else
  344. iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
  345. timeout = wait_for_pin(adap, &status);
  346. if(timeout) {
  347. iic_stop(adap);
  348. printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
  349. return (-1);
  350. }
  351. #ifndef STUB_I2C
  352. if (status & ITE_I2CHSR_DB) {
  353. iic_stop(adap);
  354. printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
  355. return (-1);
  356. }
  357. #endif
  358. timeout = wait_for_fe(adap, &status);
  359. if(timeout) {
  360. iic_stop(adap);
  361. printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
  362. return (-1);
  363. }
  364. for(j=0; j<32/2; j++) {
  365. tmp.word = iic_inw(adap, ITE_I2CFDR);
  366. buf[rdcount++] = tmp.byte[1];
  367. buf[rdcount++] = tmp.byte[0];
  368. }
  369. /* status FIFO underrun */
  370. iic_inw(adap, ITE_I2CFSR);
  371. }
  372. }
  373. if(remainder) {
  374. remainder=(remainder+1)/2 * 2;
  375. iic_outw(adap, ITE_I2CFBCR, remainder);
  376. if (sread)
  377. iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
  378. else
  379. iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
  380. timeout = wait_for_pin(adap, &status);
  381. if(timeout) {
  382. iic_stop(adap);
  383. printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
  384. return (-1);
  385. }
  386. #ifndef STUB_I2C
  387. if (status & ITE_I2CHSR_DB) {
  388. iic_stop(adap);
  389. printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
  390. return (-1);
  391. }
  392. #endif
  393. timeout = wait_for_fe(adap, &status);
  394. if(timeout) {
  395. iic_stop(adap);
  396. printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
  397. return (-1);
  398. }
  399. for(i=0; i<(remainder+1)/2; i++) {
  400. tmp.word = iic_inw(adap, ITE_I2CFDR);
  401. buf[rdcount++] = tmp.byte[1];
  402. buf[rdcount++] = tmp.byte[0];
  403. }
  404. /* status FIFO underrun */
  405. iic_inw(adap, ITE_I2CFSR);
  406. }
  407. iic_stop(adap);
  408. return rdcount;
  409. }
  410. /* This function implements combined transactions. Combined
  411. * transactions consist of combinations of reading and writing blocks of data.
  412. * Each transfer (i.e. a read or a write) is separated by a repeated start
  413. * condition.
  414. */
  415. #if 0
  416. static int iic_combined_transaction(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  417. {
  418. int i;
  419. struct i2c_msg *pmsg;
  420. int ret;
  421. DEB2(printk("Beginning combined transaction\n"));
  422. for(i=0; i<(num-1); i++) {
  423. pmsg = &msgs[i];
  424. if(pmsg->flags & I2C_M_RD) {
  425. DEB2(printk(" This one is a read\n"));
  426. ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
  427. }
  428. else if(!(pmsg->flags & I2C_M_RD)) {
  429. DEB2(printk("This one is a write\n"));
  430. ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
  431. }
  432. }
  433. /* Last read or write segment needs to be terminated with a stop */
  434. pmsg = &msgs[i];
  435. if(pmsg->flags & I2C_M_RD) {
  436. DEB2(printk("Doing the last read\n"));
  437. ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
  438. }
  439. else if(!(pmsg->flags & I2C_M_RD)) {
  440. DEB2(printk("Doing the last write\n"));
  441. ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
  442. }
  443. return ret;
  444. }
  445. #endif
  446. /* Whenever we initiate a transaction, the first byte clocked
  447. * onto the bus after the start condition is the address (7 bit) of the
  448. * device we want to talk to. This function manipulates the address specified
  449. * so that it makes sense to the hardware when written to the IIC peripheral.
  450. *
  451. * Note: 10 bit addresses are not supported in this driver, although they are
  452. * supported by the hardware. This functionality needs to be implemented.
  453. */
  454. static inline int iic_doAddress(struct i2c_algo_iic_data *adap,
  455. struct i2c_msg *msg, int retries)
  456. {
  457. unsigned short flags = msg->flags;
  458. unsigned int addr;
  459. int ret;
  460. /* Ten bit addresses not supported right now */
  461. if ( (flags & I2C_M_TEN) ) {
  462. #if 0
  463. addr = 0xf0 | (( msg->addr >> 7) & 0x03);
  464. DEB2(printk("addr0: %d\n",addr));
  465. ret = try_address(adap, addr, retries);
  466. if (ret!=1) {
  467. printk("iic_doAddress: died at extended address code.\n");
  468. return -EREMOTEIO;
  469. }
  470. iic_outw(adap,msg->addr & 0x7f);
  471. if (ret != 1) {
  472. printk("iic_doAddress: died at 2nd address code.\n");
  473. return -EREMOTEIO;
  474. }
  475. if ( flags & I2C_M_RD ) {
  476. i2c_repstart(adap);
  477. addr |= 0x01;
  478. ret = try_address(adap, addr, retries);
  479. if (ret!=1) {
  480. printk("iic_doAddress: died at extended address code.\n");
  481. return -EREMOTEIO;
  482. }
  483. }
  484. #endif
  485. } else {
  486. addr = ( msg->addr << 1 );
  487. #if 0
  488. if (flags & I2C_M_RD )
  489. addr |= 1;
  490. if (flags & I2C_M_REV_DIR_ADDR )
  491. addr ^= 1;
  492. #endif
  493. if (iic_inw(adap, ITE_I2CSAR) != addr) {
  494. iic_outw(adap, ITE_I2CSAR, addr);
  495. ret = try_address(adap, addr, retries);
  496. if (ret!=1) {
  497. printk("iic_doAddress: died at address code.\n");
  498. return -EREMOTEIO;
  499. }
  500. }
  501. }
  502. return 0;
  503. }
  504. /* Description: Prepares the controller for a transaction (clearing status
  505. * registers, data buffers, etc), and then calls either iic_readbytes or
  506. * iic_sendbytes to do the actual transaction.
  507. *
  508. * still to be done: Before we issue a transaction, we should
  509. * verify that the bus is not busy or in some unknown state.
  510. */
  511. static int iic_xfer(struct i2c_adapter *i2c_adap,
  512. struct i2c_msg *msgs,
  513. int num)
  514. {
  515. struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
  516. struct i2c_msg *pmsg;
  517. int i = 0;
  518. int ret, timeout;
  519. pmsg = &msgs[i];
  520. if(!pmsg->len) {
  521. DEB2(printk("iic_xfer: read/write length is 0\n");)
  522. return -EIO;
  523. }
  524. if(!(pmsg->flags & I2C_M_RD) && (!(pmsg->len)%2) ) {
  525. DEB2(printk("iic_xfer: write buffer length is not odd\n");)
  526. return -EIO;
  527. }
  528. /* Wait for any pending transfers to complete */
  529. timeout = wait_for_bb(adap);
  530. if (timeout) {
  531. DEB2(printk("iic_xfer: Timeout waiting for host not busy\n");)
  532. return -EIO;
  533. }
  534. /* Flush FIFO */
  535. iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
  536. /* Load address */
  537. ret = iic_doAddress(adap, pmsg, i2c_adap->retries);
  538. if (ret)
  539. return -EIO;
  540. #if 0
  541. /* Combined transaction (read and write) */
  542. if(num > 1) {
  543. DEB2(printk("iic_xfer: Call combined transaction\n"));
  544. ret = iic_combined_transaction(i2c_adap, msgs, num);
  545. }
  546. #endif
  547. DEB3(printk("iic_xfer: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
  548. i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
  549. if(pmsg->flags & I2C_M_RD) /* Read */
  550. ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, 0);
  551. else { /* Write */
  552. udelay(1000);
  553. ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len);
  554. }
  555. if (ret != pmsg->len)
  556. DEB3(printk("iic_xfer: error or fail on read/write %d bytes.\n",ret));
  557. else
  558. DEB3(printk("iic_xfer: read/write %d bytes.\n",ret));
  559. return ret;
  560. }
  561. /* Implements device specific ioctls. Higher level ioctls can
  562. * be found in i2c-core.c and are typical of any i2c controller (specifying
  563. * slave address, timeouts, etc). These ioctls take advantage of any hardware
  564. * features built into the controller for which this algorithm-adapter set
  565. * was written. These ioctls allow you to take control of the data and clock
  566. * lines and set the either high or low,
  567. * similar to a GPIO pin.
  568. */
  569. static int algo_control(struct i2c_adapter *adapter,
  570. unsigned int cmd, unsigned long arg)
  571. {
  572. struct i2c_algo_iic_data *adap = adapter->algo_data;
  573. struct i2c_iic_msg s_msg;
  574. char *buf;
  575. int ret;
  576. if (cmd == I2C_SREAD) {
  577. if(copy_from_user(&s_msg, (struct i2c_iic_msg *)arg,
  578. sizeof(struct i2c_iic_msg)))
  579. return -EFAULT;
  580. buf = kmalloc(s_msg.len, GFP_KERNEL);
  581. if (buf== NULL)
  582. return -ENOMEM;
  583. /* Flush FIFO */
  584. iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
  585. /* Load address */
  586. iic_outw(adap, ITE_I2CSAR,s_msg.addr<<1);
  587. iic_outw(adap, ITE_I2CSSAR,s_msg.waddr & 0xff);
  588. ret = iic_readbytes(adapter, buf, s_msg.len, 1);
  589. if (ret>=0) {
  590. if(copy_to_user( s_msg.buf, buf, s_msg.len) )
  591. ret = -EFAULT;
  592. }
  593. kfree(buf);
  594. }
  595. return 0;
  596. }
  597. static u32 iic_func(struct i2c_adapter *adap)
  598. {
  599. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
  600. I2C_FUNC_PROTOCOL_MANGLING;
  601. }
  602. /* -----exported algorithm data: ------------------------------------- */
  603. static struct i2c_algorithm iic_algo = {
  604. .master_xfer = iic_xfer,
  605. .algo_control = algo_control, /* ioctl */
  606. .functionality = iic_func,
  607. };
  608. /*
  609. * registering functions to load algorithms at runtime
  610. */
  611. int i2c_iic_add_bus(struct i2c_adapter *adap)
  612. {
  613. struct i2c_algo_iic_data *iic_adap = adap->algo_data;
  614. if (iic_test) {
  615. int ret = test_bus(iic_adap, adap->name);
  616. if (ret<0)
  617. return -ENODEV;
  618. }
  619. DEB2(printk("i2c-algo-ite: hw routines for %s registered.\n",
  620. adap->name));
  621. /* register new adapter to i2c module... */
  622. adap->algo = &iic_algo;
  623. adap->timeout = 100; /* default values, should */
  624. adap->retries = 3; /* be replaced by defines */
  625. adap->flags = 0;
  626. i2c_add_adapter(adap);
  627. iic_init(iic_adap);
  628. return 0;
  629. }
  630. int i2c_iic_del_bus(struct i2c_adapter *adap)
  631. {
  632. int res;
  633. if ((res = i2c_del_adapter(adap)) < 0)
  634. return res;
  635. DEB2(printk("i2c-algo-ite: adapter unregistered: %s\n",adap->name));
  636. return 0;
  637. }
  638. int __init i2c_algo_iic_init (void)
  639. {
  640. printk(KERN_INFO "ITE iic (i2c) algorithm module\n");
  641. return 0;
  642. }
  643. void i2c_algo_iic_exit(void)
  644. {
  645. return;
  646. }
  647. EXPORT_SYMBOL(i2c_iic_add_bus);
  648. EXPORT_SYMBOL(i2c_iic_del_bus);
  649. /* The MODULE_* macros resolve to nothing if MODULES is not defined
  650. * when this file is compiled.
  651. */
  652. MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
  653. MODULE_DESCRIPTION("ITE iic algorithm");
  654. MODULE_LICENSE("GPL");
  655. module_param(iic_test, bool, 0);
  656. module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
  657. MODULE_PARM_DESC(iic_test, "Test if the I2C bus is available");
  658. MODULE_PARM_DESC(i2c_debug,
  659. "debug level - 0 off; 1 normal; 2,3 more verbose; 9 iic-protocol");
  660. /* This function resolves to init_module (the function invoked when a module
  661. * is loaded via insmod) when this file is compiled with MODULES defined.
  662. * Otherwise (i.e. if you want this driver statically linked to the kernel),
  663. * a pointer to this function is stored in a table and called
  664. * during the initialization of the kernel (in do_basic_setup in /init/main.c)
  665. *
  666. * All this functionality is complements of the macros defined in linux/init.h
  667. */
  668. module_init(i2c_algo_iic_init);
  669. /* If MODULES is defined when this file is compiled, then this function will
  670. * resolved to cleanup_module.
  671. */
  672. module_exit(i2c_algo_iic_exit);