i2c.c 12 KB

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