i2c.c 14 KB

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