i2c.c 14 KB

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