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