1 /*!***************************************************************************
5 *! DESCRIPTION: implements an interface for IIC/I2C, both directly from other
6 *! kernel modules (i2c_writereg/readreg) and from userspace using
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,
15 *! Revision 1.9 2004/08/24 06:49:14 starvik
18 *! Revision 1.8 2004/06/08 08:48:26 starvik
19 *! Removed unused code
21 *! Revision 1.7 2004/05/28 09:26:59 starvik
22 *! Modified I2C initialization to work in 2.6.
24 *! Revision 1.6 2004/05/14 07:58:03 starvik
25 *! Merge of changes from 2.4
27 *! Revision 1.4 2002/12/11 13:13:57 starvik
28 *! Added arch/ to v10 specific includes
29 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
31 *! Revision 1.3 2002/11/20 11:56:11 starvik
32 *! Merge of Linux 2.5.48
34 *! Revision 1.2 2002/11/18 13:16:06 starvik
35 *! Linux 2.5 port of latest 2.4 drivers
37 *! Revision 1.9 2002/10/31 15:32:26 starvik
38 *! Update Port B register and shadow even when running with hardware support
39 *! to avoid glitches when reading bits
40 *! Never set direction to out in i2c_inbyte
41 *! Removed incorrect clock togling at end of i2c_inbyte
43 *! Revision 1.8 2002/08/13 06:31:53 starvik
44 *! Made SDA and SCL line configurable
45 *! Modified i2c_inbyte to work with PCF8563
47 *! Revision 1.7 2001/04/04 13:11:36 markusl
48 *! Updated according to review remarks
50 *! Revision 1.6 2001/03/19 12:43:00 markusl
51 *! Made some symbols unstatic (used by the eeprom driver)
53 *! Revision 1.5 2001/02/27 13:52:48 bjornw
56 *! Revision 1.4 2001/02/15 07:17:40 starvik
57 *! Corrected usage if port_pb_i2c_shadow
59 *! Revision 1.3 2001/01/26 17:55:13 bjornw
60 *! * Made I2C_USES_PB_NOT_PB_I2C a CONFIG option instead of assigning it
61 *! magically. Config.in needs to set it for the options that need it, like
62 *! Dallas 1302 support. Actually, it should be default since it screws up
63 *! the PB bits even if you don't use I2C..
64 *! * Include linux/config.h to get the above
66 *! Revision 1.2 2001/01/18 15:49:30 bjornw
67 *! 2.4 port of I2C including some cleanups (untested of course)
69 *! Revision 1.1 2001/01/18 15:35:25 bjornw
70 *! Verbatim copy of the Etrax i2c driver, 2.0 elinux version
73 *! ---------------------------------------------------------------------------
75 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
77 *!***************************************************************************/
78 /* $Id: i2c.c,v 1.9 2004/08/24 06:49:14 starvik Exp $ */
80 /****************** INCLUDE FILES SECTION ***********************************/
82 #include <linux/module.h>
83 #include <linux/sched.h>
84 #include <linux/slab.h>
85 #include <linux/errno.h>
86 #include <linux/kernel.h>
88 #include <linux/string.h>
89 #include <linux/init.h>
90 #include <linux/config.h>
92 #include <asm/etraxi2c.h>
94 #include <asm/system.h>
95 #include <asm/arch/svinto.h>
97 #include <asm/delay.h>
101 /****************** I2C DEFINITION SECTION *************************/
105 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
106 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
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
124 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
125 #define CONFIG_ETRAX_I2C_CLK_PORT 1
128 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
129 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
131 #define i2c_disable()
133 /* enable or disable output-enable, to select output or input on the i2c bus */
135 #define i2c_dir_out() \
136 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
137 #define i2c_dir_in() \
138 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
140 /* control the i2c clock and data signals */
143 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
144 #define i2c_data(x) \
145 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
147 /* read a bit from the i2c interface */
149 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
152 /* enable or disable the i2c interface */
154 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
155 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
157 /* enable or disable output-enable, to select output or input on the i2c bus */
159 #define i2c_dir_out() \
160 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
161 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
162 #define i2c_dir_in() \
163 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
164 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
166 /* control the i2c clock and data signals */
169 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
170 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
171 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
173 #define i2c_data(x) \
174 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
175 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
176 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
178 /* read a bit from the i2c interface */
180 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
183 /* use the kernels delay routine */
185 #define i2c_delay(usecs) udelay(usecs)
188 /****************** FUNCTION DEFINITION SECTION *************************/
191 /* generate i2c start condition */
200 i2c_delay(CLOCK_HIGH_TIME
/6);
201 i2c_data(I2C_DATA_HIGH
);
202 i2c_clk(I2C_CLOCK_HIGH
);
203 i2c_delay(CLOCK_HIGH_TIME
);
207 i2c_data(I2C_DATA_LOW
);
208 i2c_delay(START_CONDITION_HOLD_TIME
);
212 i2c_clk(I2C_CLOCK_LOW
);
213 i2c_delay(CLOCK_LOW_TIME
);
216 /* generate i2c stop condition */
226 i2c_clk(I2C_CLOCK_LOW
);
227 i2c_data(I2C_DATA_LOW
);
228 i2c_delay(CLOCK_LOW_TIME
*2);
232 i2c_clk(I2C_CLOCK_HIGH
);
233 i2c_delay(CLOCK_HIGH_TIME
*2);
237 i2c_data(I2C_DATA_HIGH
);
238 i2c_delay(STOP_CONDITION_HOLD_TIME
);
243 /* write a byte to the i2c interface */
246 i2c_outbyte(unsigned char x
)
252 for (i
= 0; i
< 8; i
++) {
254 i2c_data(I2C_DATA_HIGH
);
256 i2c_data(I2C_DATA_LOW
);
259 i2c_delay(CLOCK_LOW_TIME
/2);
260 i2c_clk(I2C_CLOCK_HIGH
);
261 i2c_delay(CLOCK_HIGH_TIME
);
262 i2c_clk(I2C_CLOCK_LOW
);
263 i2c_delay(CLOCK_LOW_TIME
/2);
266 i2c_data(I2C_DATA_LOW
);
267 i2c_delay(CLOCK_LOW_TIME
/2);
275 /* read a byte from the i2c interface */
280 unsigned char aBitByte
= 0;
283 /* Switch off I2C to get bit */
286 i2c_delay(CLOCK_HIGH_TIME
/2);
289 aBitByte
|= i2c_getbit();
293 i2c_delay(CLOCK_LOW_TIME
/2);
295 for (i
= 1; i
< 8; i
++) {
298 i2c_clk(I2C_CLOCK_HIGH
);
299 i2c_delay(CLOCK_HIGH_TIME
);
300 i2c_clk(I2C_CLOCK_LOW
);
301 i2c_delay(CLOCK_LOW_TIME
);
303 /* Switch off I2C to get bit */
306 i2c_delay(CLOCK_HIGH_TIME
/2);
309 aBitByte
|= i2c_getbit();
313 i2c_delay(CLOCK_LOW_TIME
/2);
315 i2c_clk(I2C_CLOCK_HIGH
);
316 i2c_delay(CLOCK_HIGH_TIME
);
319 * we leave the clock low, getbyte is usually followed
320 * by sendack/nack, they assume the clock to be low
322 i2c_clk(I2C_CLOCK_LOW
);
326 /*#---------------------------------------------------------------------------
328 *# FUNCTION NAME: i2c_getack
330 *# DESCRIPTION : checks if ack was received from ic2
332 *#--------------------------------------------------------------------------*/
343 * Release data bus by setting
346 i2c_data(I2C_DATA_HIGH
);
351 i2c_delay(CLOCK_HIGH_TIME
/4);
353 * generate ACK clock pulse
355 i2c_clk(I2C_CLOCK_HIGH
);
357 * Use PORT PB instead of I2C
358 * for input. (I2C not working)
371 i2c_delay(CLOCK_HIGH_TIME
/2);
377 i2c_delay(CLOCK_HIGH_TIME
/2);
379 if(!i2c_getbit()) /* receiver pulld SDA low */
381 i2c_delay(CLOCK_HIGH_TIME
/2);
385 * our clock is high now, make sure data is low
386 * before we enable our output. If we keep data high
387 * and enable output, we would generate a stop condition.
389 i2c_data(I2C_DATA_LOW
);
396 i2c_clk(I2C_CLOCK_LOW
);
397 i2c_delay(CLOCK_HIGH_TIME
/4);
403 * remove ACK clock pulse
405 i2c_data(I2C_DATA_HIGH
);
406 i2c_delay(CLOCK_LOW_TIME
/2);
410 /*#---------------------------------------------------------------------------
412 *# FUNCTION NAME: I2C::sendAck
414 *# DESCRIPTION : Send ACK on received data
416 *#--------------------------------------------------------------------------*/
423 i2c_delay(CLOCK_LOW_TIME
);
428 i2c_data(I2C_DATA_LOW
);
430 * generate clock pulse
432 i2c_delay(CLOCK_HIGH_TIME
/6);
433 i2c_clk(I2C_CLOCK_HIGH
);
434 i2c_delay(CLOCK_HIGH_TIME
);
435 i2c_clk(I2C_CLOCK_LOW
);
436 i2c_delay(CLOCK_LOW_TIME
/6);
440 i2c_data(I2C_DATA_HIGH
);
441 i2c_delay(CLOCK_LOW_TIME
);
446 /*#---------------------------------------------------------------------------
448 *# FUNCTION NAME: i2c_sendnack
450 *# DESCRIPTION : Sends NACK on received data
452 *#--------------------------------------------------------------------------*/
459 i2c_delay(CLOCK_LOW_TIME
);
464 i2c_data(I2C_DATA_HIGH
);
466 * generate clock pulse
468 i2c_delay(CLOCK_HIGH_TIME
/6);
469 i2c_clk(I2C_CLOCK_HIGH
);
470 i2c_delay(CLOCK_HIGH_TIME
);
471 i2c_clk(I2C_CLOCK_LOW
);
472 i2c_delay(CLOCK_LOW_TIME
);
477 /*#---------------------------------------------------------------------------
479 *# FUNCTION NAME: i2c_writereg
481 *# DESCRIPTION : Writes a value to an I2C device
483 *#--------------------------------------------------------------------------*/
485 i2c_writereg(unsigned char theSlave
, unsigned char theReg
,
486 unsigned char theValue
)
494 * we don't like to be interrupted
496 local_irq_save(flags
);
503 i2c_outbyte((theSlave
& 0xfe));
510 * now select register
515 * now it's time to wait for ack
520 * send register register data
522 i2c_outbyte(theValue
);
524 * now it's time to wait for ack
533 * enable interrupt again
535 local_irq_restore(flags
);
537 } while(error
&& cntr
--);
539 i2c_delay(CLOCK_LOW_TIME
);
544 /*#---------------------------------------------------------------------------
546 *# FUNCTION NAME: i2c_readreg
548 *# DESCRIPTION : Reads a value from the decoder registers.
550 *#--------------------------------------------------------------------------*/
552 i2c_readreg(unsigned char theSlave
, unsigned char theReg
)
561 * we don't like to be interrupted
563 local_irq_save(flags
);
566 * generate start condition
573 i2c_outbyte((theSlave
& 0xfe));
580 * now select register
585 * now it's time to wait for ack
590 * repeat start condition
592 i2c_delay(CLOCK_LOW_TIME
);
597 i2c_outbyte(theSlave
| 0x01);
608 * last received byte needs to be nacked
617 * enable interrupt again
619 local_irq_restore(flags
);
621 } while(error
&& cntr
--);
627 i2c_open(struct inode
*inode
, struct file
*filp
)
633 i2c_release(struct inode
*inode
, struct file
*filp
)
638 /* Main device API. ioctl's to write or read to/from i2c registers.
642 i2c_ioctl(struct inode
*inode
, struct file
*file
,
643 unsigned int cmd
, unsigned long arg
)
645 if(_IOC_TYPE(cmd
) != ETRAXI2C_IOCTYPE
) {
649 switch (_IOC_NR(cmd
)) {
651 /* write to an i2c slave */
652 D(printk("i2cw %d %d %d\n",
657 return i2c_writereg(I2C_ARGSLAVE(arg
),
663 /* read from an i2c slave */
664 D(printk("i2cr %d %d ",
667 val
= i2c_readreg(I2C_ARGSLAVE(arg
), I2C_ARGREG(arg
));
668 D(printk("= %d\n", val
));
679 static struct file_operations i2c_fops
= {
680 .owner
= THIS_MODULE
,
683 .release
= i2c_release
,
689 /* Setup and enable the Port B I2C interface */
691 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
692 *R_PORT_PB_I2C
= port_pb_i2c_shadow
|=
693 IO_STATE(R_PORT_PB_I2C
, i2c_en
, on
) |
694 IO_FIELD(R_PORT_PB_I2C
, i2c_d
, 1) |
695 IO_FIELD(R_PORT_PB_I2C
, i2c_clk
, 1) |
696 IO_STATE(R_PORT_PB_I2C
, i2c_oe_
, enable
);
699 port_pb_dir_shadow
&= ~IO_MASK(R_PORT_PB_DIR
, dir0
);
700 port_pb_dir_shadow
&= ~IO_MASK(R_PORT_PB_DIR
, dir1
);
702 *R_PORT_PB_DIR
= (port_pb_dir_shadow
|=
703 IO_STATE(R_PORT_PB_DIR
, dir0
, input
) |
704 IO_STATE(R_PORT_PB_DIR
, dir1
, output
));
715 res
= register_chrdev(I2C_MAJOR
, i2c_name
, &i2c_fops
);
717 printk(KERN_ERR
"i2c: couldn't get a major number.\n");
721 printk(KERN_INFO
"I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
726 /* this makes sure that i2c_register is called during boot */
728 module_init(i2c_register
);
730 /****************** END OF FILE i2c.c ********************************/