i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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-2007 Axis Communications AB, LUND, SWEDEN
  23. *!
  24. *!***************************************************************************/
  25. /****************** INCLUDE FILES SECTION ***********************************/
  26. #include <linux/module.h>
  27. #include <linux/sched.h>
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/fs.h>
  31. #include <linux/string.h>
  32. #include <linux/init.h>
  33. #include <linux/mutex.h>
  34. #include <asm/etraxi2c.h>
  35. #include <asm/io.h>
  36. #include <asm/delay.h>
  37. #include "i2c.h"
  38. /****************** I2C DEFINITION SECTION *************************/
  39. #define D(x)
  40. #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
  41. static DEFINE_MUTEX(i2c_mutex);
  42. static const char i2c_name[] = "i2c";
  43. #define CLOCK_LOW_TIME 8
  44. #define CLOCK_HIGH_TIME 8
  45. #define START_CONDITION_HOLD_TIME 8
  46. #define STOP_CONDITION_HOLD_TIME 8
  47. #define ENABLE_OUTPUT 0x01
  48. #define ENABLE_INPUT 0x00
  49. #define I2C_CLOCK_HIGH 1
  50. #define I2C_CLOCK_LOW 0
  51. #define I2C_DATA_HIGH 1
  52. #define I2C_DATA_LOW 0
  53. #define i2c_enable()
  54. #define i2c_disable()
  55. /* enable or disable output-enable, to select output or input on the i2c bus */
  56. #define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
  57. #define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
  58. /* control the i2c clock and data signals */
  59. #define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
  60. #define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
  61. /* read a bit from the i2c interface */
  62. #define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
  63. #define i2c_delay(usecs) udelay(usecs)
  64. static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
  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. #if 0
  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. #endif
  225. /*
  226. * now wait for ack
  227. */
  228. i2c_delay(CLOCK_HIGH_TIME/2);
  229. /*
  230. * check for ack
  231. */
  232. if (i2c_getbit())
  233. ack = 0;
  234. i2c_delay(CLOCK_HIGH_TIME/2);
  235. if (!ack) {
  236. if (!i2c_getbit()) /* receiver pulld SDA low */
  237. ack = 1;
  238. i2c_delay(CLOCK_HIGH_TIME/2);
  239. }
  240. /*
  241. * our clock is high now, make sure data is low
  242. * before we enable our output. If we keep data high
  243. * and enable output, we would generate a stop condition.
  244. */
  245. #if 0
  246. i2c_data(I2C_DATA_LOW);
  247. /*
  248. * end clock pulse
  249. */
  250. i2c_enable();
  251. i2c_dir_out();
  252. #endif
  253. i2c_clk(I2C_CLOCK_LOW);
  254. i2c_delay(CLOCK_HIGH_TIME/4);
  255. /*
  256. * enable output
  257. */
  258. i2c_dir_out();
  259. /*
  260. * remove ACK clock pulse
  261. */
  262. i2c_data(I2C_DATA_HIGH);
  263. i2c_delay(CLOCK_LOW_TIME/2);
  264. return ack;
  265. }
  266. /*#---------------------------------------------------------------------------
  267. *#
  268. *# FUNCTION NAME: I2C::sendAck
  269. *#
  270. *# DESCRIPTION : Send ACK on received data
  271. *#
  272. *#--------------------------------------------------------------------------*/
  273. void
  274. i2c_sendack(void)
  275. {
  276. /*
  277. * enable output
  278. */
  279. i2c_delay(CLOCK_LOW_TIME);
  280. i2c_dir_out();
  281. /*
  282. * set ack pulse high
  283. */
  284. i2c_data(I2C_DATA_LOW);
  285. /*
  286. * generate clock pulse
  287. */
  288. i2c_delay(CLOCK_HIGH_TIME/6);
  289. i2c_clk(I2C_CLOCK_HIGH);
  290. i2c_delay(CLOCK_HIGH_TIME);
  291. i2c_clk(I2C_CLOCK_LOW);
  292. i2c_delay(CLOCK_LOW_TIME/6);
  293. /*
  294. * reset data out
  295. */
  296. i2c_data(I2C_DATA_HIGH);
  297. i2c_delay(CLOCK_LOW_TIME);
  298. i2c_dir_in();
  299. }
  300. /*#---------------------------------------------------------------------------
  301. *#
  302. *# FUNCTION NAME: i2c_sendnack
  303. *#
  304. *# DESCRIPTION : Sends NACK on received data
  305. *#
  306. *#--------------------------------------------------------------------------*/
  307. void
  308. i2c_sendnack(void)
  309. {
  310. /*
  311. * enable output
  312. */
  313. i2c_delay(CLOCK_LOW_TIME);
  314. i2c_dir_out();
  315. /*
  316. * set data high
  317. */
  318. i2c_data(I2C_DATA_HIGH);
  319. /*
  320. * generate clock pulse
  321. */
  322. i2c_delay(CLOCK_HIGH_TIME/6);
  323. i2c_clk(I2C_CLOCK_HIGH);
  324. i2c_delay(CLOCK_HIGH_TIME);
  325. i2c_clk(I2C_CLOCK_LOW);
  326. i2c_delay(CLOCK_LOW_TIME);
  327. i2c_dir_in();
  328. }
  329. /*#---------------------------------------------------------------------------
  330. *#
  331. *# FUNCTION NAME: i2c_write
  332. *#
  333. *# DESCRIPTION : Writes a value to an I2C device
  334. *#
  335. *#--------------------------------------------------------------------------*/
  336. int
  337. i2c_write(unsigned char theSlave, void *data, size_t nbytes)
  338. {
  339. int error, cntr = 3;
  340. unsigned char bytes_wrote = 0;
  341. unsigned char value;
  342. unsigned long flags;
  343. spin_lock_irqsave(&i2c_lock, flags);
  344. do {
  345. error = 0;
  346. i2c_start();
  347. /*
  348. * send slave address
  349. */
  350. i2c_outbyte((theSlave & 0xfe));
  351. /*
  352. * wait for ack
  353. */
  354. if (!i2c_getack())
  355. error = 1;
  356. /*
  357. * send data
  358. */
  359. for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) {
  360. memcpy(&value, data + bytes_wrote, sizeof value);
  361. i2c_outbyte(value);
  362. /*
  363. * now it's time to wait for ack
  364. */
  365. if (!i2c_getack())
  366. error |= 4;
  367. }
  368. /*
  369. * end byte stream
  370. */
  371. i2c_stop();
  372. } while (error && cntr--);
  373. i2c_delay(CLOCK_LOW_TIME);
  374. spin_unlock_irqrestore(&i2c_lock, flags);
  375. return -error;
  376. }
  377. /*#---------------------------------------------------------------------------
  378. *#
  379. *# FUNCTION NAME: i2c_read
  380. *#
  381. *# DESCRIPTION : Reads a value from an I2C device
  382. *#
  383. *#--------------------------------------------------------------------------*/
  384. int
  385. i2c_read(unsigned char theSlave, void *data, size_t nbytes)
  386. {
  387. unsigned char b = 0;
  388. unsigned char bytes_read = 0;
  389. int error, cntr = 3;
  390. unsigned long flags;
  391. spin_lock_irqsave(&i2c_lock, flags);
  392. do {
  393. error = 0;
  394. memset(data, 0, nbytes);
  395. /*
  396. * generate start condition
  397. */
  398. i2c_start();
  399. /*
  400. * send slave address
  401. */
  402. i2c_outbyte((theSlave | 0x01));
  403. /*
  404. * wait for ack
  405. */
  406. if (!i2c_getack())
  407. error = 1;
  408. /*
  409. * fetch data
  410. */
  411. for (bytes_read = 0; bytes_read < nbytes; bytes_read++) {
  412. b = i2c_inbyte();
  413. memcpy(data + bytes_read, &b, sizeof b);
  414. if (bytes_read < (nbytes - 1))
  415. i2c_sendack();
  416. }
  417. /*
  418. * last received byte needs to be nacked
  419. * instead of acked
  420. */
  421. i2c_sendnack();
  422. /*
  423. * end sequence
  424. */
  425. i2c_stop();
  426. } while (error && cntr--);
  427. spin_unlock_irqrestore(&i2c_lock, flags);
  428. return -error;
  429. }
  430. /*#---------------------------------------------------------------------------
  431. *#
  432. *# FUNCTION NAME: i2c_writereg
  433. *#
  434. *# DESCRIPTION : Writes a value to an I2C device
  435. *#
  436. *#--------------------------------------------------------------------------*/
  437. int
  438. i2c_writereg(unsigned char theSlave, unsigned char theReg,
  439. unsigned char theValue)
  440. {
  441. int error, cntr = 3;
  442. unsigned long flags;
  443. spin_lock_irqsave(&i2c_lock, flags);
  444. do {
  445. error = 0;
  446. i2c_start();
  447. /*
  448. * send slave address
  449. */
  450. i2c_outbyte((theSlave & 0xfe));
  451. /*
  452. * wait for ack
  453. */
  454. if(!i2c_getack())
  455. error = 1;
  456. /*
  457. * now select register
  458. */
  459. i2c_dir_out();
  460. i2c_outbyte(theReg);
  461. /*
  462. * now it's time to wait for ack
  463. */
  464. if(!i2c_getack())
  465. error |= 2;
  466. /*
  467. * send register register data
  468. */
  469. i2c_outbyte(theValue);
  470. /*
  471. * now it's time to wait for ack
  472. */
  473. if(!i2c_getack())
  474. error |= 4;
  475. /*
  476. * end byte stream
  477. */
  478. i2c_stop();
  479. } while(error && cntr--);
  480. i2c_delay(CLOCK_LOW_TIME);
  481. spin_unlock_irqrestore(&i2c_lock, flags);
  482. return -error;
  483. }
  484. /*#---------------------------------------------------------------------------
  485. *#
  486. *# FUNCTION NAME: i2c_readreg
  487. *#
  488. *# DESCRIPTION : Reads a value from the decoder registers.
  489. *#
  490. *#--------------------------------------------------------------------------*/
  491. unsigned char
  492. i2c_readreg(unsigned char theSlave, unsigned char theReg)
  493. {
  494. unsigned char b = 0;
  495. int error, cntr = 3;
  496. unsigned long flags;
  497. spin_lock_irqsave(&i2c_lock, flags);
  498. do {
  499. error = 0;
  500. /*
  501. * generate start condition
  502. */
  503. i2c_start();
  504. /*
  505. * send slave address
  506. */
  507. i2c_outbyte((theSlave & 0xfe));
  508. /*
  509. * wait for ack
  510. */
  511. if(!i2c_getack())
  512. error = 1;
  513. /*
  514. * now select register
  515. */
  516. i2c_dir_out();
  517. i2c_outbyte(theReg);
  518. /*
  519. * now it's time to wait for ack
  520. */
  521. if(!i2c_getack())
  522. error |= 2;
  523. /*
  524. * repeat start condition
  525. */
  526. i2c_delay(CLOCK_LOW_TIME);
  527. i2c_start();
  528. /*
  529. * send slave address
  530. */
  531. i2c_outbyte(theSlave | 0x01);
  532. /*
  533. * wait for ack
  534. */
  535. if(!i2c_getack())
  536. error |= 4;
  537. /*
  538. * fetch register
  539. */
  540. b = i2c_inbyte();
  541. /*
  542. * last received byte needs to be nacked
  543. * instead of acked
  544. */
  545. i2c_sendnack();
  546. /*
  547. * end sequence
  548. */
  549. i2c_stop();
  550. } while(error && cntr--);
  551. spin_unlock_irqrestore(&i2c_lock, flags);
  552. return b;
  553. }
  554. static int
  555. i2c_open(struct inode *inode, struct file *filp)
  556. {
  557. return 0;
  558. }
  559. static int
  560. i2c_release(struct inode *inode, struct file *filp)
  561. {
  562. return 0;
  563. }
  564. /* Main device API. ioctl's to write or read to/from i2c registers.
  565. */
  566. static long
  567. i2c_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  568. {
  569. int ret;
  570. if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
  571. return -ENOTTY;
  572. }
  573. switch (_IOC_NR(cmd)) {
  574. case I2C_WRITEREG:
  575. /* write to an i2c slave */
  576. D(printk("i2cw %d %d %d\n",
  577. I2C_ARGSLAVE(arg),
  578. I2C_ARGREG(arg),
  579. I2C_ARGVALUE(arg)));
  580. mutex_lock(&i2c_mutex);
  581. ret = i2c_writereg(I2C_ARGSLAVE(arg),
  582. I2C_ARGREG(arg),
  583. I2C_ARGVALUE(arg));
  584. mutex_unlock(&i2c_mutex);
  585. return ret;
  586. case I2C_READREG:
  587. {
  588. unsigned char val;
  589. /* read from an i2c slave */
  590. D(printk("i2cr %d %d ",
  591. I2C_ARGSLAVE(arg),
  592. I2C_ARGREG(arg)));
  593. mutex_lock(&i2c_mutex);
  594. val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
  595. mutex_unlock(&i2c_mutex);
  596. D(printk("= %d\n", val));
  597. return val;
  598. }
  599. default:
  600. return -EINVAL;
  601. }
  602. return 0;
  603. }
  604. static const struct file_operations i2c_fops = {
  605. .owner = THIS_MODULE,
  606. .unlocked_ioctl = i2c_ioctl,
  607. .open = i2c_open,
  608. .release = i2c_release,
  609. .llseek = noop_llseek,
  610. };
  611. static int __init i2c_init(void)
  612. {
  613. static int res;
  614. static int first = 1;
  615. if (!first)
  616. return res;
  617. first = 0;
  618. /* Setup and enable the DATA and CLK pins */
  619. res = crisv32_io_get_name(&cris_i2c_data,
  620. CONFIG_ETRAX_V32_I2C_DATA_PORT);
  621. if (res < 0)
  622. return res;
  623. res = crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_V32_I2C_CLK_PORT);
  624. crisv32_io_set_dir(&cris_i2c_clk, crisv32_io_dir_out);
  625. return res;
  626. }
  627. static int __init i2c_register(void)
  628. {
  629. int res;
  630. res = i2c_init();
  631. if (res < 0)
  632. return res;
  633. /* register char device */
  634. res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
  635. if (res < 0) {
  636. printk(KERN_ERR "i2c: couldn't get a major number.\n");
  637. return res;
  638. }
  639. printk(KERN_INFO
  640. "I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");
  641. return 0;
  642. }
  643. /* this makes sure that i2c_init is called during boot */
  644. module_init(i2c_register);
  645. /****************** END OF FILE i2c.c ********************************/