i2c.c 14 KB

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