i2c.c 17 KB

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