i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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/slab.h>
  29. #include <linux/errno.h>
  30. #include <linux/kernel.h>
  31. #include <linux/fs.h>
  32. #include <linux/string.h>
  33. #include <linux/init.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 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(&i2c_lock);
  344. do {
  345. error = 0;
  346. /*
  347. * we don't like to be interrupted
  348. */
  349. local_irq_save(flags);
  350. i2c_start();
  351. /*
  352. * send slave address
  353. */
  354. i2c_outbyte((theSlave & 0xfe));
  355. /*
  356. * wait for ack
  357. */
  358. if (!i2c_getack())
  359. error = 1;
  360. /*
  361. * send data
  362. */
  363. for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) {
  364. memcpy(&value, data + bytes_wrote, sizeof value);
  365. i2c_outbyte(value);
  366. /*
  367. * now it's time to wait for ack
  368. */
  369. if (!i2c_getack())
  370. error |= 4;
  371. }
  372. /*
  373. * end byte stream
  374. */
  375. i2c_stop();
  376. /*
  377. * enable interrupt again
  378. */
  379. local_irq_restore(flags);
  380. } while (error && cntr--);
  381. i2c_delay(CLOCK_LOW_TIME);
  382. spin_unlock(&i2c_lock);
  383. return -error;
  384. }
  385. /*#---------------------------------------------------------------------------
  386. *#
  387. *# FUNCTION NAME: i2c_read
  388. *#
  389. *# DESCRIPTION : Reads a value from an I2C device
  390. *#
  391. *#--------------------------------------------------------------------------*/
  392. int
  393. i2c_read(unsigned char theSlave, void *data, size_t nbytes)
  394. {
  395. unsigned char b = 0;
  396. unsigned char bytes_read = 0;
  397. int error, cntr = 3;
  398. unsigned long flags;
  399. spin_lock(&i2c_lock);
  400. do {
  401. error = 0;
  402. memset(data, 0, nbytes);
  403. /*
  404. * we don't like to be interrupted
  405. */
  406. local_irq_save(flags);
  407. /*
  408. * generate start condition
  409. */
  410. i2c_start();
  411. /*
  412. * send slave address
  413. */
  414. i2c_outbyte((theSlave | 0x01));
  415. /*
  416. * wait for ack
  417. */
  418. if (!i2c_getack())
  419. error = 1;
  420. /*
  421. * fetch data
  422. */
  423. for (bytes_read = 0; bytes_read < nbytes; bytes_read++) {
  424. b = i2c_inbyte();
  425. memcpy(data + bytes_read, &b, sizeof b);
  426. if (bytes_read < (nbytes - 1))
  427. i2c_sendack();
  428. }
  429. /*
  430. * last received byte needs to be nacked
  431. * instead of acked
  432. */
  433. i2c_sendnack();
  434. /*
  435. * end sequence
  436. */
  437. i2c_stop();
  438. /*
  439. * enable interrupt again
  440. */
  441. local_irq_restore(flags);
  442. } while (error && cntr--);
  443. spin_unlock(&i2c_lock);
  444. return -error;
  445. }
  446. /*#---------------------------------------------------------------------------
  447. *#
  448. *# FUNCTION NAME: i2c_writereg
  449. *#
  450. *# DESCRIPTION : Writes a value to an I2C device
  451. *#
  452. *#--------------------------------------------------------------------------*/
  453. int
  454. i2c_writereg(unsigned char theSlave, unsigned char theReg,
  455. unsigned char theValue)
  456. {
  457. int error, cntr = 3;
  458. unsigned long flags;
  459. spin_lock(&i2c_lock);
  460. do {
  461. error = 0;
  462. /*
  463. * we don't like to be interrupted
  464. */
  465. local_irq_save(flags);
  466. i2c_start();
  467. /*
  468. * send slave address
  469. */
  470. i2c_outbyte((theSlave & 0xfe));
  471. /*
  472. * wait for ack
  473. */
  474. if(!i2c_getack())
  475. error = 1;
  476. /*
  477. * now select register
  478. */
  479. i2c_dir_out();
  480. i2c_outbyte(theReg);
  481. /*
  482. * now it's time to wait for ack
  483. */
  484. if(!i2c_getack())
  485. error |= 2;
  486. /*
  487. * send register register data
  488. */
  489. i2c_outbyte(theValue);
  490. /*
  491. * now it's time to wait for ack
  492. */
  493. if(!i2c_getack())
  494. error |= 4;
  495. /*
  496. * end byte stream
  497. */
  498. i2c_stop();
  499. /*
  500. * enable interrupt again
  501. */
  502. local_irq_restore(flags);
  503. } while(error && cntr--);
  504. i2c_delay(CLOCK_LOW_TIME);
  505. spin_unlock(&i2c_lock);
  506. return -error;
  507. }
  508. /*#---------------------------------------------------------------------------
  509. *#
  510. *# FUNCTION NAME: i2c_readreg
  511. *#
  512. *# DESCRIPTION : Reads a value from the decoder registers.
  513. *#
  514. *#--------------------------------------------------------------------------*/
  515. unsigned char
  516. i2c_readreg(unsigned char theSlave, unsigned char theReg)
  517. {
  518. unsigned char b = 0;
  519. int error, cntr = 3;
  520. unsigned long flags;
  521. spin_lock(&i2c_lock);
  522. do {
  523. error = 0;
  524. /*
  525. * we don't like to be interrupted
  526. */
  527. local_irq_save(flags);
  528. /*
  529. * generate start condition
  530. */
  531. i2c_start();
  532. /*
  533. * send slave address
  534. */
  535. i2c_outbyte((theSlave & 0xfe));
  536. /*
  537. * wait for ack
  538. */
  539. if(!i2c_getack())
  540. error = 1;
  541. /*
  542. * now select register
  543. */
  544. i2c_dir_out();
  545. i2c_outbyte(theReg);
  546. /*
  547. * now it's time to wait for ack
  548. */
  549. if(!i2c_getack())
  550. error |= 2;
  551. /*
  552. * repeat start condition
  553. */
  554. i2c_delay(CLOCK_LOW_TIME);
  555. i2c_start();
  556. /*
  557. * send slave address
  558. */
  559. i2c_outbyte(theSlave | 0x01);
  560. /*
  561. * wait for ack
  562. */
  563. if(!i2c_getack())
  564. error |= 4;
  565. /*
  566. * fetch register
  567. */
  568. b = i2c_inbyte();
  569. /*
  570. * last received byte needs to be nacked
  571. * instead of acked
  572. */
  573. i2c_sendnack();
  574. /*
  575. * end sequence
  576. */
  577. i2c_stop();
  578. /*
  579. * enable interrupt again
  580. */
  581. local_irq_restore(flags);
  582. } while(error && cntr--);
  583. spin_unlock(&i2c_lock);
  584. return b;
  585. }
  586. static int
  587. i2c_open(struct inode *inode, struct file *filp)
  588. {
  589. return 0;
  590. }
  591. static int
  592. i2c_release(struct inode *inode, struct file *filp)
  593. {
  594. return 0;
  595. }
  596. /* Main device API. ioctl's to write or read to/from i2c registers.
  597. */
  598. static int
  599. i2c_ioctl(struct inode *inode, struct file *file,
  600. unsigned int cmd, unsigned long arg)
  601. {
  602. if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
  603. return -EINVAL;
  604. }
  605. switch (_IOC_NR(cmd)) {
  606. case I2C_WRITEREG:
  607. /* write to an i2c slave */
  608. D(printk("i2cw %d %d %d\n",
  609. I2C_ARGSLAVE(arg),
  610. I2C_ARGREG(arg),
  611. I2C_ARGVALUE(arg)));
  612. return i2c_writereg(I2C_ARGSLAVE(arg),
  613. I2C_ARGREG(arg),
  614. I2C_ARGVALUE(arg));
  615. case I2C_READREG:
  616. {
  617. unsigned char val;
  618. /* read from an i2c slave */
  619. D(printk("i2cr %d %d ",
  620. I2C_ARGSLAVE(arg),
  621. I2C_ARGREG(arg)));
  622. val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
  623. D(printk("= %d\n", val));
  624. return val;
  625. }
  626. default:
  627. return -EINVAL;
  628. }
  629. return 0;
  630. }
  631. static const struct file_operations i2c_fops = {
  632. .owner = THIS_MODULE,
  633. .ioctl = i2c_ioctl,
  634. .open = i2c_open,
  635. .release = i2c_release,
  636. };
  637. int __init
  638. i2c_init(void)
  639. {
  640. static int res;
  641. static int first = 1;
  642. if (!first)
  643. return res;
  644. first = 0;
  645. /* Setup and enable the DATA and CLK pins */
  646. res = crisv32_io_get_name(&cris_i2c_data,
  647. CONFIG_ETRAX_V32_I2C_DATA_PORT);
  648. if (res < 0)
  649. return res;
  650. res = crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_V32_I2C_CLK_PORT);
  651. crisv32_io_set_dir(&cris_i2c_clk, crisv32_io_dir_out);
  652. return res;
  653. }
  654. int __init
  655. i2c_register(void)
  656. {
  657. int res;
  658. res = i2c_init();
  659. if (res < 0)
  660. return res;
  661. /* register char device */
  662. res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
  663. if(res < 0) {
  664. printk(KERN_ERR "i2c: couldn't get a major number.\n");
  665. return res;
  666. }
  667. printk(KERN_INFO
  668. "I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");
  669. return 0;
  670. }
  671. /* this makes sure that i2c_init is called during boot */
  672. module_init(i2c_register);
  673. /****************** END OF FILE i2c.c ********************************/