i2c.c 17 KB

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