i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*!***************************************************************************
  2. *!
  3. *! FILE NAME : i2c.c
  4. *!
  5. *! DESCRIPTION: implements an interface for IIC/I2C, both directly from other
  6. *! kernel modules (i2c_writereg/readreg) and from userspace using
  7. *! ioctl()'s
  8. *!
  9. *! Nov 30 1998 Torbjorn Eliasson Initial version.
  10. *! Bjorn Wesen Elinux kernel version.
  11. *! Jan 14 2000 Johan Adolfsson Fixed PB shadow register stuff -
  12. *! don't use PB_I2C if DS1302 uses same bits,
  13. *! use PB.
  14. *| June 23 2003 Pieter Grimmerink Added 'i2c_sendnack'. i2c_readreg now
  15. *| generates nack on last received byte,
  16. *| instead of ack.
  17. *| i2c_getack changed data level while clock
  18. *| was high, causing DS75 to see a stop condition
  19. *!
  20. *! ---------------------------------------------------------------------------
  21. *!
  22. *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
  23. *!
  24. *!***************************************************************************/
  25. /* $Id: i2c.c,v 1.2 2005/05/09 15:29:49 starvik Exp $ */
  26. /****************** INCLUDE FILES SECTION ***********************************/
  27. #include <linux/module.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/errno.h>
  31. #include <linux/kernel.h>
  32. #include <linux/fs.h>
  33. #include <linux/string.h>
  34. #include <linux/init.h>
  35. #include <asm/etraxi2c.h>
  36. #include <asm/system.h>
  37. #include <asm/io.h>
  38. #include <asm/delay.h>
  39. #include "i2c.h"
  40. /****************** I2C DEFINITION SECTION *************************/
  41. #define D(x)
  42. #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
  43. static const char i2c_name[] = "i2c";
  44. #define CLOCK_LOW_TIME 8
  45. #define CLOCK_HIGH_TIME 8
  46. #define START_CONDITION_HOLD_TIME 8
  47. #define STOP_CONDITION_HOLD_TIME 8
  48. #define ENABLE_OUTPUT 0x01
  49. #define ENABLE_INPUT 0x00
  50. #define I2C_CLOCK_HIGH 1
  51. #define I2C_CLOCK_LOW 0
  52. #define I2C_DATA_HIGH 1
  53. #define I2C_DATA_LOW 0
  54. #define i2c_enable()
  55. #define i2c_disable()
  56. /* enable or disable output-enable, to select output or input on the i2c bus */
  57. #define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
  58. #define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
  59. /* control the i2c clock and data signals */
  60. #define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
  61. #define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
  62. /* read a bit from the i2c interface */
  63. #define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
  64. #define i2c_delay(usecs) udelay(usecs)
  65. /****************** VARIABLE SECTION ************************************/
  66. static struct crisv32_iopin cris_i2c_clk;
  67. static struct crisv32_iopin cris_i2c_data;
  68. /****************** FUNCTION DEFINITION SECTION *************************/
  69. /* generate i2c start condition */
  70. void
  71. i2c_start(void)
  72. {
  73. /*
  74. * SCL=1 SDA=1
  75. */
  76. i2c_dir_out();
  77. i2c_delay(CLOCK_HIGH_TIME/6);
  78. i2c_data(I2C_DATA_HIGH);
  79. i2c_clk(I2C_CLOCK_HIGH);
  80. i2c_delay(CLOCK_HIGH_TIME);
  81. /*
  82. * SCL=1 SDA=0
  83. */
  84. i2c_data(I2C_DATA_LOW);
  85. i2c_delay(START_CONDITION_HOLD_TIME);
  86. /*
  87. * SCL=0 SDA=0
  88. */
  89. i2c_clk(I2C_CLOCK_LOW);
  90. i2c_delay(CLOCK_LOW_TIME);
  91. }
  92. /* generate i2c stop condition */
  93. void
  94. i2c_stop(void)
  95. {
  96. i2c_dir_out();
  97. /*
  98. * SCL=0 SDA=0
  99. */
  100. i2c_clk(I2C_CLOCK_LOW);
  101. i2c_data(I2C_DATA_LOW);
  102. i2c_delay(CLOCK_LOW_TIME*2);
  103. /*
  104. * SCL=1 SDA=0
  105. */
  106. i2c_clk(I2C_CLOCK_HIGH);
  107. i2c_delay(CLOCK_HIGH_TIME*2);
  108. /*
  109. * SCL=1 SDA=1
  110. */
  111. i2c_data(I2C_DATA_HIGH);
  112. i2c_delay(STOP_CONDITION_HOLD_TIME);
  113. i2c_dir_in();
  114. }
  115. /* write a byte to the i2c interface */
  116. void
  117. i2c_outbyte(unsigned char x)
  118. {
  119. int i;
  120. i2c_dir_out();
  121. for (i = 0; i < 8; i++) {
  122. if (x & 0x80) {
  123. i2c_data(I2C_DATA_HIGH);
  124. } else {
  125. i2c_data(I2C_DATA_LOW);
  126. }
  127. i2c_delay(CLOCK_LOW_TIME/2);
  128. i2c_clk(I2C_CLOCK_HIGH);
  129. i2c_delay(CLOCK_HIGH_TIME);
  130. i2c_clk(I2C_CLOCK_LOW);
  131. i2c_delay(CLOCK_LOW_TIME/2);
  132. x <<= 1;
  133. }
  134. i2c_data(I2C_DATA_LOW);
  135. i2c_delay(CLOCK_LOW_TIME/2);
  136. /*
  137. * enable input
  138. */
  139. i2c_dir_in();
  140. }
  141. /* read a byte from the i2c interface */
  142. unsigned char
  143. i2c_inbyte(void)
  144. {
  145. unsigned char aBitByte = 0;
  146. int i;
  147. /* Switch off I2C to get bit */
  148. i2c_disable();
  149. i2c_dir_in();
  150. i2c_delay(CLOCK_HIGH_TIME/2);
  151. /* Get bit */
  152. aBitByte |= i2c_getbit();
  153. /* Enable I2C */
  154. i2c_enable();
  155. i2c_delay(CLOCK_LOW_TIME/2);
  156. for (i = 1; i < 8; i++) {
  157. aBitByte <<= 1;
  158. /* Clock pulse */
  159. i2c_clk(I2C_CLOCK_HIGH);
  160. i2c_delay(CLOCK_HIGH_TIME);
  161. i2c_clk(I2C_CLOCK_LOW);
  162. i2c_delay(CLOCK_LOW_TIME);
  163. /* Switch off I2C to get bit */
  164. i2c_disable();
  165. i2c_dir_in();
  166. i2c_delay(CLOCK_HIGH_TIME/2);
  167. /* Get bit */
  168. aBitByte |= i2c_getbit();
  169. /* Enable I2C */
  170. i2c_enable();
  171. i2c_delay(CLOCK_LOW_TIME/2);
  172. }
  173. i2c_clk(I2C_CLOCK_HIGH);
  174. i2c_delay(CLOCK_HIGH_TIME);
  175. /*
  176. * we leave the clock low, getbyte is usually followed
  177. * by sendack/nack, they assume the clock to be low
  178. */
  179. i2c_clk(I2C_CLOCK_LOW);
  180. return aBitByte;
  181. }
  182. /*#---------------------------------------------------------------------------
  183. *#
  184. *# FUNCTION NAME: i2c_getack
  185. *#
  186. *# DESCRIPTION : checks if ack was received from ic2
  187. *#
  188. *#--------------------------------------------------------------------------*/
  189. int
  190. i2c_getack(void)
  191. {
  192. int ack = 1;
  193. /*
  194. * enable output
  195. */
  196. i2c_dir_out();
  197. /*
  198. * Release data bus by setting
  199. * data high
  200. */
  201. i2c_data(I2C_DATA_HIGH);
  202. /*
  203. * enable input
  204. */
  205. i2c_dir_in();
  206. i2c_delay(CLOCK_HIGH_TIME/4);
  207. /*
  208. * generate ACK clock pulse
  209. */
  210. i2c_clk(I2C_CLOCK_HIGH);
  211. /*
  212. * Use PORT PB instead of I2C
  213. * for input. (I2C not working)
  214. */
  215. i2c_clk(1);
  216. i2c_data(1);
  217. /*
  218. * switch off I2C
  219. */
  220. i2c_data(1);
  221. i2c_disable();
  222. i2c_dir_in();
  223. /*
  224. * now wait for ack
  225. */
  226. i2c_delay(CLOCK_HIGH_TIME/2);
  227. /*
  228. * check for ack
  229. */
  230. if(i2c_getbit())
  231. ack = 0;
  232. i2c_delay(CLOCK_HIGH_TIME/2);
  233. if(!ack){
  234. if(!i2c_getbit()) /* receiver pulld SDA low */
  235. ack = 1;
  236. i2c_delay(CLOCK_HIGH_TIME/2);
  237. }
  238. /*
  239. * our clock is high now, make sure data is low
  240. * before we enable our output. If we keep data high
  241. * and enable output, we would generate a stop condition.
  242. */
  243. i2c_data(I2C_DATA_LOW);
  244. /*
  245. * end clock pulse
  246. */
  247. i2c_enable();
  248. i2c_dir_out();
  249. i2c_clk(I2C_CLOCK_LOW);
  250. i2c_delay(CLOCK_HIGH_TIME/4);
  251. /*
  252. * enable output
  253. */
  254. i2c_dir_out();
  255. /*
  256. * remove ACK clock pulse
  257. */
  258. i2c_data(I2C_DATA_HIGH);
  259. i2c_delay(CLOCK_LOW_TIME/2);
  260. return ack;
  261. }
  262. /*#---------------------------------------------------------------------------
  263. *#
  264. *# FUNCTION NAME: I2C::sendAck
  265. *#
  266. *# DESCRIPTION : Send ACK on received data
  267. *#
  268. *#--------------------------------------------------------------------------*/
  269. void
  270. i2c_sendack(void)
  271. {
  272. /*
  273. * enable output
  274. */
  275. i2c_delay(CLOCK_LOW_TIME);
  276. i2c_dir_out();
  277. /*
  278. * set ack pulse high
  279. */
  280. i2c_data(I2C_DATA_LOW);
  281. /*
  282. * generate clock pulse
  283. */
  284. i2c_delay(CLOCK_HIGH_TIME/6);
  285. i2c_clk(I2C_CLOCK_HIGH);
  286. i2c_delay(CLOCK_HIGH_TIME);
  287. i2c_clk(I2C_CLOCK_LOW);
  288. i2c_delay(CLOCK_LOW_TIME/6);
  289. /*
  290. * reset data out
  291. */
  292. i2c_data(I2C_DATA_HIGH);
  293. i2c_delay(CLOCK_LOW_TIME);
  294. i2c_dir_in();
  295. }
  296. /*#---------------------------------------------------------------------------
  297. *#
  298. *# FUNCTION NAME: i2c_sendnack
  299. *#
  300. *# DESCRIPTION : Sends NACK on received data
  301. *#
  302. *#--------------------------------------------------------------------------*/
  303. void
  304. i2c_sendnack(void)
  305. {
  306. /*
  307. * enable output
  308. */
  309. i2c_delay(CLOCK_LOW_TIME);
  310. i2c_dir_out();
  311. /*
  312. * set data high
  313. */
  314. i2c_data(I2C_DATA_HIGH);
  315. /*
  316. * generate clock pulse
  317. */
  318. i2c_delay(CLOCK_HIGH_TIME/6);
  319. i2c_clk(I2C_CLOCK_HIGH);
  320. i2c_delay(CLOCK_HIGH_TIME);
  321. i2c_clk(I2C_CLOCK_LOW);
  322. i2c_delay(CLOCK_LOW_TIME);
  323. i2c_dir_in();
  324. }
  325. /*#---------------------------------------------------------------------------
  326. *#
  327. *# FUNCTION NAME: i2c_writereg
  328. *#
  329. *# DESCRIPTION : Writes a value to an I2C device
  330. *#
  331. *#--------------------------------------------------------------------------*/
  332. int
  333. i2c_writereg(unsigned char theSlave, unsigned char theReg,
  334. unsigned char theValue)
  335. {
  336. int error, cntr = 3;
  337. unsigned long flags;
  338. do {
  339. error = 0;
  340. /*
  341. * we don't like to be interrupted
  342. */
  343. local_irq_save(flags);
  344. i2c_start();
  345. /*
  346. * send slave address
  347. */
  348. i2c_outbyte((theSlave & 0xfe));
  349. /*
  350. * wait for ack
  351. */
  352. if(!i2c_getack())
  353. error = 1;
  354. /*
  355. * now select register
  356. */
  357. i2c_dir_out();
  358. i2c_outbyte(theReg);
  359. /*
  360. * now it's time to wait for ack
  361. */
  362. if(!i2c_getack())
  363. error |= 2;
  364. /*
  365. * send register register data
  366. */
  367. i2c_outbyte(theValue);
  368. /*
  369. * now it's time to wait for ack
  370. */
  371. if(!i2c_getack())
  372. error |= 4;
  373. /*
  374. * end byte stream
  375. */
  376. i2c_stop();
  377. /*
  378. * enable interrupt again
  379. */
  380. local_irq_restore(flags);
  381. } while(error && cntr--);
  382. i2c_delay(CLOCK_LOW_TIME);
  383. return -error;
  384. }
  385. /*#---------------------------------------------------------------------------
  386. *#
  387. *# FUNCTION NAME: i2c_readreg
  388. *#
  389. *# DESCRIPTION : Reads a value from the decoder registers.
  390. *#
  391. *#--------------------------------------------------------------------------*/
  392. unsigned char
  393. i2c_readreg(unsigned char theSlave, unsigned char theReg)
  394. {
  395. unsigned char b = 0;
  396. int error, cntr = 3;
  397. unsigned long flags;
  398. do {
  399. error = 0;
  400. /*
  401. * we don't like to be interrupted
  402. */
  403. local_irq_save(flags);
  404. /*
  405. * generate start condition
  406. */
  407. i2c_start();
  408. /*
  409. * send slave address
  410. */
  411. i2c_outbyte((theSlave & 0xfe));
  412. /*
  413. * wait for ack
  414. */
  415. if(!i2c_getack())
  416. error = 1;
  417. /*
  418. * now select register
  419. */
  420. i2c_dir_out();
  421. i2c_outbyte(theReg);
  422. /*
  423. * now it's time to wait for ack
  424. */
  425. if(!i2c_getack())
  426. error = 1;
  427. /*
  428. * repeat start condition
  429. */
  430. i2c_delay(CLOCK_LOW_TIME);
  431. i2c_start();
  432. /*
  433. * send slave address
  434. */
  435. i2c_outbyte(theSlave | 0x01);
  436. /*
  437. * wait for ack
  438. */
  439. if(!i2c_getack())
  440. error = 1;
  441. /*
  442. * fetch register
  443. */
  444. b = i2c_inbyte();
  445. /*
  446. * last received byte needs to be nacked
  447. * instead of acked
  448. */
  449. i2c_sendnack();
  450. /*
  451. * end sequence
  452. */
  453. i2c_stop();
  454. /*
  455. * enable interrupt again
  456. */
  457. local_irq_restore(flags);
  458. } while(error && cntr--);
  459. return b;
  460. }
  461. static int
  462. i2c_open(struct inode *inode, struct file *filp)
  463. {
  464. return 0;
  465. }
  466. static int
  467. i2c_release(struct inode *inode, struct file *filp)
  468. {
  469. return 0;
  470. }
  471. /* Main device API. ioctl's to write or read to/from i2c registers.
  472. */
  473. static int
  474. i2c_ioctl(struct inode *inode, struct file *file,
  475. unsigned int cmd, unsigned long arg)
  476. {
  477. if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
  478. return -EINVAL;
  479. }
  480. switch (_IOC_NR(cmd)) {
  481. case I2C_WRITEREG:
  482. /* write to an i2c slave */
  483. D(printk("i2cw %d %d %d\n",
  484. I2C_ARGSLAVE(arg),
  485. I2C_ARGREG(arg),
  486. I2C_ARGVALUE(arg)));
  487. return i2c_writereg(I2C_ARGSLAVE(arg),
  488. I2C_ARGREG(arg),
  489. I2C_ARGVALUE(arg));
  490. case I2C_READREG:
  491. {
  492. unsigned char val;
  493. /* read from an i2c slave */
  494. D(printk("i2cr %d %d ",
  495. I2C_ARGSLAVE(arg),
  496. I2C_ARGREG(arg)));
  497. val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
  498. D(printk("= %d\n", val));
  499. return val;
  500. }
  501. default:
  502. return -EINVAL;
  503. }
  504. return 0;
  505. }
  506. static struct file_operations i2c_fops = {
  507. owner: THIS_MODULE,
  508. ioctl: i2c_ioctl,
  509. open: i2c_open,
  510. release: i2c_release,
  511. };
  512. int __init
  513. i2c_init(void)
  514. {
  515. int res;
  516. /* Setup and enable the Port B I2C interface */
  517. crisv32_io_get_name(&cris_i2c_data, CONFIG_ETRAX_I2C_DATA_PORT);
  518. crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_I2C_CLK_PORT);
  519. /* register char device */
  520. res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
  521. if(res < 0) {
  522. printk(KERN_ERR "i2c: couldn't get a major number.\n");
  523. return res;
  524. }
  525. printk(KERN_INFO "I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
  526. return 0;
  527. }
  528. /* this makes sure that i2c_init is called during boot */
  529. module_init(i2c_init);
  530. /****************** END OF FILE i2c.c ********************************/