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.7 2004/05/28 09:26:59 starvik
16 *! Modified I2C initialization to work in 2.6.
18 *! Revision 1.6 2004/05/14 07:58:03 starvik
19 *! Merge of changes from 2.4
21 *! Revision 1.4 2002/12/11 13:13:57 starvik
22 *! Added arch/ to v10 specific includes
23 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
25 *! Revision 1.3 2002/11/20 11:56:11 starvik
26 *! Merge of Linux 2.5.48
28 *! Revision 1.2 2002/11/18 13:16:06 starvik
29 *! Linux 2.5 port of latest 2.4 drivers
31 *! Revision 1.9 2002/10/31 15:32:26 starvik
32 *! Update Port B register and shadow even when running with hardware support
33 *! to avoid glitches when reading bits
34 *! Never set direction to out in i2c_inbyte
35 *! Removed incorrect clock togling at end of i2c_inbyte
37 *! Revision 1.8 2002/08/13 06:31:53 starvik
38 *! Made SDA and SCL line configurable
39 *! Modified i2c_inbyte to work with PCF8563
41 *! Revision 1.7 2001/04/04 13:11:36 markusl
42 *! Updated according to review remarks
44 *! Revision 1.6 2001/03/19 12:43:00 markusl
45 *! Made some symbols unstatic (used by the eeprom driver)
47 *! Revision 1.5 2001/02/27 13:52:48 bjornw
50 *! Revision 1.4 2001/02/15 07:17:40 starvik
51 *! Corrected usage if port_pb_i2c_shadow
53 *! Revision 1.3 2001/01/26 17:55:13 bjornw
54 *! * Made I2C_USES_PB_NOT_PB_I2C a CONFIG option instead of assigning it
55 *! magically. Config.in needs to set it for the options that need it, like
56 *! Dallas 1302 support. Actually, it should be default since it screws up
57 *! the PB bits even if you don't use I2C..
58 *! * Include linux/config.h to get the above
60 *! Revision 1.2 2001/01/18 15:49:30 bjornw
61 *! 2.4 port of I2C including some cleanups (untested of course)
63 *! Revision 1.1 2001/01/18 15:35:25 bjornw
64 *! Verbatim copy of the Etrax i2c driver, 2.0 elinux version
67 *! ---------------------------------------------------------------------------
69 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
71 *!***************************************************************************/
72 /* $Id: i2c.c,v 1.7 2004/05/28 09:26:59 starvik Exp $ */
74 /****************** INCLUDE FILES SECTION ***********************************/
76 #include <linux/module.h>
77 #include <linux/sched.h>
78 #include <linux/slab.h>
79 #include <linux/errno.h>
80 #include <linux/kernel.h>
82 #include <linux/string.h>
83 #include <linux/init.h>
84 #include <linux/config.h>
86 #include <asm/etraxi2c.h>
88 #include <asm/system.h>
89 #include <asm/arch/svinto.h>
91 #include <asm/delay.h>
95 /****************** I2C DEFINITION SECTION *************************/
99 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
100 static const char i2c_name[] = "i2c";
102 #define CLOCK_LOW_TIME 8
103 #define CLOCK_HIGH_TIME 8
104 #define START_CONDITION_HOLD_TIME 8
105 #define STOP_CONDITION_HOLD_TIME 8
106 #define ENABLE_OUTPUT 0x01
107 #define ENABLE_INPUT 0x00
108 #define I2C_CLOCK_HIGH 1
109 #define I2C_CLOCK_LOW 0
110 #define I2C_DATA_HIGH 1
111 #define I2C_DATA_LOW 0
114 /* TODO: fix this so the CONFIG_ETRAX_I2C_USES... is set in Config.in instead */
115 #if defined(CONFIG_DS1302) && (CONFIG_DS1302_SDABIT==0) && \
116 (CONFIG_DS1302_SCLBIT == 1)
117 #define CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
121 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
122 /* Use PB and not PB_I2C */
123 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
124 #define CONFIG_ETRAX_I2C_DATA_PORT 0
126 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
127 #define CONFIG_ETRAX_I2C_CLK_PORT 1
130 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
131 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
133 #define i2c_disable()
135 /* enable or disable output-enable, to select output or input on the i2c bus */
137 #define i2c_dir_out() \
138 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
139 #define i2c_dir_in() \
140 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
142 /* control the i2c clock and data signals */
145 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
146 #define i2c_data(x) \
147 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
149 /* read a bit from the i2c interface */
151 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
154 /* enable or disable the i2c interface */
156 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
157 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
159 /* enable or disable output-enable, to select output or input on the i2c bus */
161 #define i2c_dir_out() \
162 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
163 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
164 #define i2c_dir_in() \
165 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
166 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
168 /* control the i2c clock and data signals */
171 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
172 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
173 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
175 #define i2c_data(x) \
176 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
177 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
178 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
180 /* read a bit from the i2c interface */
182 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
185 /* use the kernels delay routine */
187 #define i2c_delay(usecs) udelay(usecs)
190 /****************** FUNCTION DEFINITION SECTION *************************/
193 /* generate i2c start condition */
202 i2c_delay(CLOCK_HIGH_TIME/6);
203 i2c_data(I2C_DATA_HIGH);
204 i2c_clk(I2C_CLOCK_HIGH);
205 i2c_delay(CLOCK_HIGH_TIME);
209 i2c_data(I2C_DATA_LOW);
210 i2c_delay(START_CONDITION_HOLD_TIME);
214 i2c_clk(I2C_CLOCK_LOW);
215 i2c_delay(CLOCK_LOW_TIME);
218 /* generate i2c stop condition */
228 i2c_clk(I2C_CLOCK_LOW);
229 i2c_data(I2C_DATA_LOW);
230 i2c_delay(CLOCK_LOW_TIME*2);
234 i2c_clk(I2C_CLOCK_HIGH);
235 i2c_delay(CLOCK_HIGH_TIME*2);
239 i2c_data(I2C_DATA_HIGH);
240 i2c_delay(STOP_CONDITION_HOLD_TIME);
245 /* write a byte to the i2c interface */
248 i2c_outbyte(unsigned char x)
254 for (i = 0; i < 8; i++) {
256 i2c_data(I2C_DATA_HIGH);
258 i2c_data(I2C_DATA_LOW);
261 i2c_delay(CLOCK_LOW_TIME/2);
262 i2c_clk(I2C_CLOCK_HIGH);
263 i2c_delay(CLOCK_HIGH_TIME);
264 i2c_clk(I2C_CLOCK_LOW);
265 i2c_delay(CLOCK_LOW_TIME/2);
268 i2c_data(I2C_DATA_LOW);
269 i2c_delay(CLOCK_LOW_TIME/2);
277 /* read a byte from the i2c interface */
282 unsigned char aBitByte = 0;
285 /* Switch off I2C to get bit */
288 i2c_delay(CLOCK_HIGH_TIME/2);
291 aBitByte |= i2c_getbit();
295 i2c_delay(CLOCK_LOW_TIME/2);
297 for (i = 1; i < 8; i++) {
300 i2c_clk(I2C_CLOCK_HIGH);
301 i2c_delay(CLOCK_HIGH_TIME);
302 i2c_clk(I2C_CLOCK_LOW);
303 i2c_delay(CLOCK_LOW_TIME);
305 /* Switch off I2C to get bit */
308 i2c_delay(CLOCK_HIGH_TIME/2);
311 aBitByte |= i2c_getbit();
315 i2c_delay(CLOCK_LOW_TIME/2);
317 i2c_clk(I2C_CLOCK_HIGH);
318 i2c_delay(CLOCK_HIGH_TIME);
321 * we leave the clock low, getbyte is usually followed
322 * by sendack/nack, they assume the clock to be low
324 i2c_clk(I2C_CLOCK_LOW);
328 /*#---------------------------------------------------------------------------
330 *# FUNCTION NAME: i2c_getack
332 *# DESCRIPTION : checks if ack was received from ic2
334 *#--------------------------------------------------------------------------*/
345 * Release data bus by setting
348 i2c_data(I2C_DATA_HIGH);
353 i2c_delay(CLOCK_HIGH_TIME/4);
355 * generate ACK clock pulse
357 i2c_clk(I2C_CLOCK_HIGH);
359 * Use PORT PB instead of I2C
360 * for input. (I2C not working)
373 i2c_delay(CLOCK_HIGH_TIME/2);
379 i2c_delay(CLOCK_HIGH_TIME/2);
381 if(!i2c_getbit()) /* receiver pulld SDA low */
383 i2c_delay(CLOCK_HIGH_TIME/2);
387 * our clock is high now, make sure data is low
388 * before we enable our output. If we keep data high
389 * and enable output, we would generate a stop condition.
391 i2c_data(I2C_DATA_LOW);
398 i2c_clk(I2C_CLOCK_LOW);
399 i2c_delay(CLOCK_HIGH_TIME/4);
405 * remove ACK clock pulse
407 i2c_data(I2C_DATA_HIGH);
408 i2c_delay(CLOCK_LOW_TIME/2);
412 /*#---------------------------------------------------------------------------
414 *# FUNCTION NAME: I2C::sendAck
416 *# DESCRIPTION : Send ACK on received data
418 *#--------------------------------------------------------------------------*/
425 i2c_delay(CLOCK_LOW_TIME);
430 i2c_data(I2C_DATA_LOW);
432 * generate clock pulse
434 i2c_delay(CLOCK_HIGH_TIME/6);
435 i2c_clk(I2C_CLOCK_HIGH);
436 i2c_delay(CLOCK_HIGH_TIME);
437 i2c_clk(I2C_CLOCK_LOW);
438 i2c_delay(CLOCK_LOW_TIME/6);
442 i2c_data(I2C_DATA_HIGH);
443 i2c_delay(CLOCK_LOW_TIME);
448 /*#---------------------------------------------------------------------------
450 *# FUNCTION NAME: i2c_sendnack
452 *# DESCRIPTION : Sends NACK on received data
454 *#--------------------------------------------------------------------------*/
461 i2c_delay(CLOCK_LOW_TIME);
466 i2c_data(I2C_DATA_HIGH);
468 * generate clock pulse
470 i2c_delay(CLOCK_HIGH_TIME/6);
471 i2c_clk(I2C_CLOCK_HIGH);
472 i2c_delay(CLOCK_HIGH_TIME);
473 i2c_clk(I2C_CLOCK_LOW);
474 i2c_delay(CLOCK_LOW_TIME);
479 /*#---------------------------------------------------------------------------
481 *# FUNCTION NAME: i2c_writereg
483 *# DESCRIPTION : Writes a value to an I2C device
485 *#--------------------------------------------------------------------------*/
487 i2c_writereg(unsigned char theSlave, unsigned char theReg,
488 unsigned char theValue)
496 * we don't like to be interrupted
498 local_irq_save(flags);
505 i2c_outbyte((theSlave & 0xfe));
512 * now select register
517 * now it's time to wait for ack
522 * send register register data
524 i2c_outbyte(theValue);
526 * now it's time to wait for ack
535 * enable interrupt again
537 local_irq_restore(flags);
539 } while(error && cntr--);
541 i2c_delay(CLOCK_LOW_TIME);
546 /*#---------------------------------------------------------------------------
548 *# FUNCTION NAME: i2c_readreg
550 *# DESCRIPTION : Reads a value from the decoder registers.
552 *#--------------------------------------------------------------------------*/
554 i2c_readreg(unsigned char theSlave, unsigned char theReg)
563 * we don't like to be interrupted
565 local_irq_save(flags);
568 * generate start condition
575 i2c_outbyte((theSlave & 0xfe));
582 * now select register
587 * now it's time to wait for ack
592 * repeat start condition
594 i2c_delay(CLOCK_LOW_TIME);
599 i2c_outbyte(theSlave | 0x01);
610 * last received byte needs to be nacked
619 * enable interrupt again
621 local_irq_restore(flags);
623 } while(error && cntr--);
629 i2c_open(struct inode *inode, struct file *filp)
635 i2c_release(struct inode *inode, struct file *filp)
640 /* Main device API. ioctl's to write or read to/from i2c registers.
644 i2c_ioctl(struct inode *inode, struct file *file,
645 unsigned int cmd, unsigned long arg)
647 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
651 switch (_IOC_NR(cmd)) {
653 /* write to an i2c slave */
654 D(printk("i2cw %d %d %d\n",
659 return i2c_writereg(I2C_ARGSLAVE(arg),
665 /* read from an i2c slave */
666 D(printk("i2cr %d %d ",
669 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
670 D(printk("= %d\n", val));
681 static struct file_operations i2c_fops = {
682 .owner = THIS_MODULE,
685 .release = i2c_release,
691 /* Setup and enable the Port B I2C interface */
693 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
694 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
695 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
696 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
697 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
698 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
701 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
702 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
704 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
705 IO_STATE(R_PORT_PB_DIR, dir0, input) |
706 IO_STATE(R_PORT_PB_DIR, dir1, output));
717 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
719 printk(KERN_ERR "i2c: couldn't get a major number.\n");
723 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
728 /* this makes sure that i2c_register is called during boot */
730 module_init(i2c_register);
732 /****************** END OF FILE i2c.c ********************************/