i2c.c 14 KB

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