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.4 2002/12/11 13:13:57 starvik
16 *! Added arch/ to v10 specific includes
17 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
19 *! Revision 1.3 2002/11/20 11:56:11 starvik
20 *! Merge of Linux 2.5.48
22 *! Revision 1.2 2002/11/18 13:16:06 starvik
23 *! Linux 2.5 port of latest 2.4 drivers
25 *! Revision 1.9 2002/10/31 15:32:26 starvik
26 *! Update Port B register and shadow even when running with hardware support
27 *! to avoid glitches when reading bits
28 *! Never set direction to out in i2c_inbyte
29 *! Removed incorrect clock togling at end of i2c_inbyte
31 *! Revision 1.8 2002/08/13 06:31:53 starvik
32 *! Made SDA and SCL line configurable
33 *! Modified i2c_inbyte to work with PCF8563
35 *! Revision 1.7 2001/04/04 13:11:36 markusl
36 *! Updated according to review remarks
38 *! Revision 1.6 2001/03/19 12:43:00 markusl
39 *! Made some symbols unstatic (used by the eeprom driver)
41 *! Revision 1.5 2001/02/27 13:52:48 bjornw
44 *! Revision 1.4 2001/02/15 07:17:40 starvik
45 *! Corrected usage if port_pb_i2c_shadow
47 *! Revision 1.3 2001/01/26 17:55:13 bjornw
48 *! * Made I2C_USES_PB_NOT_PB_I2C a CONFIG option instead of assigning it
49 *! magically. Config.in needs to set it for the options that need it, like
50 *! Dallas 1302 support. Actually, it should be default since it screws up
51 *! the PB bits even if you don't use I2C..
52 *! * Include linux/config.h to get the above
54 *! Revision 1.2 2001/01/18 15:49:30 bjornw
55 *! 2.4 port of I2C including some cleanups (untested of course)
57 *! Revision 1.1 2001/01/18 15:35:25 bjornw
58 *! Verbatim copy of the Etrax i2c driver, 2.0 elinux version
61 *! ---------------------------------------------------------------------------
63 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
65 *!***************************************************************************/
66 /* $Id: i2c.c,v 1.4 2002/12/11 13:13:57 starvik Exp $ */
68 /****************** INCLUDE FILES SECTION ***********************************/
70 #include <linux/module.h>
71 #include <linux/sched.h>
72 #include <linux/slab.h>
73 #include <linux/errno.h>
74 #include <linux/kernel.h>
76 #include <linux/string.h>
77 #include <linux/init.h>
78 #include <linux/config.h>
80 #include <asm/etraxi2c.h>
82 #include <asm/system.h>
83 #include <asm/arch/svinto.h>
85 #include <asm/delay.h>
89 /****************** I2C DEFINITION SECTION *************************/
93 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
94 static const char i2c_name[] = "i2c";
96 #define CLOCK_LOW_TIME 8
97 #define CLOCK_HIGH_TIME 8
98 #define START_CONDITION_HOLD_TIME 8
99 #define STOP_CONDITION_HOLD_TIME 8
100 #define ENABLE_OUTPUT 0x01
101 #define ENABLE_INPUT 0x00
102 #define I2C_CLOCK_HIGH 1
103 #define I2C_CLOCK_LOW 0
104 #define I2C_DATA_HIGH 1
105 #define I2C_DATA_LOW 0
108 /* TODO: fix this so the CONFIG_ETRAX_I2C_USES... is set in Config.in instead */
109 #if defined(CONFIG_DS1302) && (CONFIG_DS1302_SDABIT==0) && \
110 (CONFIG_DS1302_SCLBIT == 1)
111 #define CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
115 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
116 /* Use PB and not PB_I2C */
117 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
118 #define CONFIG_ETRAX_I2C_DATA_PORT 0
120 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
121 #define CONFIG_ETRAX_I2C_CLK_PORT 1
124 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
125 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
127 #define i2c_disable()
129 /* 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)
136 /* control the i2c clock and data signals */
139 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
140 #define i2c_data(x) \
141 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
143 /* read a bit from the i2c interface */
145 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
148 /* enable or disable the i2c interface */
150 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
151 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
153 /* enable or disable output-enable, to select output or input on the i2c bus */
155 #define i2c_dir_out() \
156 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
157 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
158 #define i2c_dir_in() \
159 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
160 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
162 /* control the i2c clock and data signals */
165 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
166 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
167 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
169 #define i2c_data(x) \
170 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
171 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
172 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
174 /* read a bit from the i2c interface */
176 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
179 /* use the kernels delay routine */
181 #define i2c_delay(usecs) udelay(usecs)
184 /****************** FUNCTION DEFINITION SECTION *************************/
187 /* generate i2c start condition */
196 i2c_delay(CLOCK_HIGH_TIME/6);
197 i2c_data(I2C_DATA_HIGH);
198 i2c_clk(I2C_CLOCK_HIGH);
199 i2c_delay(CLOCK_HIGH_TIME);
203 i2c_data(I2C_DATA_LOW);
204 i2c_delay(START_CONDITION_HOLD_TIME);
208 i2c_clk(I2C_CLOCK_LOW);
209 i2c_delay(CLOCK_LOW_TIME);
212 /* generate i2c stop condition */
222 i2c_clk(I2C_CLOCK_LOW);
223 i2c_data(I2C_DATA_LOW);
224 i2c_delay(CLOCK_LOW_TIME*2);
228 i2c_clk(I2C_CLOCK_HIGH);
229 i2c_delay(CLOCK_HIGH_TIME*2);
233 i2c_data(I2C_DATA_HIGH);
234 i2c_delay(STOP_CONDITION_HOLD_TIME);
239 /* write a byte to the i2c interface */
242 i2c_outbyte(unsigned char x)
248 for (i = 0; i < 8; i++) {
250 i2c_data(I2C_DATA_HIGH);
252 i2c_data(I2C_DATA_LOW);
255 i2c_delay(CLOCK_LOW_TIME/2);
256 i2c_clk(I2C_CLOCK_HIGH);
257 i2c_delay(CLOCK_HIGH_TIME);
258 i2c_clk(I2C_CLOCK_LOW);
259 i2c_delay(CLOCK_LOW_TIME/2);
262 i2c_data(I2C_DATA_LOW);
263 i2c_delay(CLOCK_LOW_TIME/2);
271 /* read a byte from the i2c interface */
276 unsigned char aBitByte = 0;
279 /* Switch off I2C to get bit */
282 i2c_delay(CLOCK_HIGH_TIME/2);
285 aBitByte |= i2c_getbit();
289 i2c_delay(CLOCK_LOW_TIME/2);
291 for (i = 1; i < 8; i++) {
294 i2c_clk(I2C_CLOCK_HIGH);
295 i2c_delay(CLOCK_HIGH_TIME);
296 i2c_clk(I2C_CLOCK_LOW);
297 i2c_delay(CLOCK_LOW_TIME);
299 /* Switch off I2C to get bit */
302 i2c_delay(CLOCK_HIGH_TIME/2);
305 aBitByte |= i2c_getbit();
309 i2c_delay(CLOCK_LOW_TIME/2);
311 i2c_clk(I2C_CLOCK_HIGH);
312 i2c_delay(CLOCK_HIGH_TIME);
316 /*#---------------------------------------------------------------------------
318 *# FUNCTION NAME: i2c_getack
320 *# DESCRIPTION : checks if ack was received from ic2
322 *#--------------------------------------------------------------------------*/
333 * Release data bus by setting
336 i2c_data(I2C_DATA_HIGH);
341 i2c_delay(CLOCK_HIGH_TIME/4);
343 * generate ACK clock pulse
345 i2c_clk(I2C_CLOCK_HIGH);
347 * Use PORT PB instead of I2C
348 * for input. (I2C not working)
361 i2c_delay(CLOCK_HIGH_TIME/2);
367 i2c_delay(CLOCK_HIGH_TIME/2);
369 if(!i2c_getbit()) /* receiver pulld SDA low */
371 i2c_delay(CLOCK_HIGH_TIME/2);
379 i2c_clk(I2C_CLOCK_LOW);
380 i2c_delay(CLOCK_HIGH_TIME/4);
386 * remove ACK clock pulse
388 i2c_data(I2C_DATA_HIGH);
389 i2c_delay(CLOCK_LOW_TIME/2);
393 /*#---------------------------------------------------------------------------
395 *# FUNCTION NAME: I2C::sendAck
397 *# DESCRIPTION : Send ACK on received data
399 *#--------------------------------------------------------------------------*/
406 i2c_delay(CLOCK_LOW_TIME);
411 i2c_data(I2C_DATA_LOW);
413 * generate clock pulse
415 i2c_delay(CLOCK_HIGH_TIME/6);
416 i2c_clk(I2C_CLOCK_HIGH);
417 i2c_delay(CLOCK_HIGH_TIME);
418 i2c_clk(I2C_CLOCK_LOW);
419 i2c_delay(CLOCK_LOW_TIME/6);
423 i2c_data(I2C_DATA_HIGH);
424 i2c_delay(CLOCK_LOW_TIME);
429 /*#---------------------------------------------------------------------------
431 *# FUNCTION NAME: i2c_writereg
433 *# DESCRIPTION : Writes a value to an I2C device
435 *#--------------------------------------------------------------------------*/
437 i2c_writereg(unsigned char theSlave, unsigned char theReg,
438 unsigned char theValue)
446 * we don't like to be interrupted
448 local_irq_save(flags);
455 i2c_outbyte((theSlave & 0xfe));
462 * now select register
467 * now it's time to wait for ack
472 * send register register data
474 i2c_outbyte(theValue);
476 * now it's time to wait for ack
485 * enable interrupt again
487 local_irq_restore(flags);
489 } while(error && cntr--);
491 i2c_delay(CLOCK_LOW_TIME);
496 /*#---------------------------------------------------------------------------
498 *# FUNCTION NAME: i2c_readreg
500 *# DESCRIPTION : Reads a value from the decoder registers.
502 *#--------------------------------------------------------------------------*/
504 i2c_readreg(unsigned char theSlave, unsigned char theReg)
513 * we don't like to be interrupted
515 local_irq_save(flags);
518 * generate start condition
525 i2c_outbyte((theSlave & 0xfe));
532 * now select register
537 * now it's time to wait for ack
542 * repeat start condition
544 i2c_delay(CLOCK_LOW_TIME);
549 i2c_outbyte(theSlave | 0x01);
568 * enable interrupt again
570 local_irq_restore(flags);
572 } while(error && cntr--);
578 i2c_open(struct inode *inode, struct file *filp)
584 i2c_release(struct inode *inode, struct file *filp)
589 /* Main device API. ioctl's to write or read to/from i2c registers.
593 i2c_ioctl(struct inode *inode, struct file *file,
594 unsigned int cmd, unsigned long arg)
596 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
600 switch (_IOC_NR(cmd)) {
602 /* write to an i2c slave */
603 D(printk("i2cw %d %d %d\n",
608 return i2c_writereg(I2C_ARGSLAVE(arg),
614 /* read from an i2c slave */
615 D(printk("i2cr %d %d ",
618 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
619 D(printk("= %d\n", val));
630 static struct file_operations i2c_fops = {
631 .owner = THIS_MODULE,
634 .release = i2c_release,
642 /* Setup and enable the Port B I2C interface */
644 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
645 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
646 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
647 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
648 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
649 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
652 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
653 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
655 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
656 IO_STATE(R_PORT_PB_DIR, dir0, input) |
657 IO_STATE(R_PORT_PB_DIR, dir1, output));
659 /* register char device */
661 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
663 printk(KERN_ERR "i2c: couldn't get a major number.\n");
667 printk("I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
672 /* this makes sure that i2c_init is called during boot */
674 module_init(i2c_init);
676 /****************** END OF FILE i2c.c ********************************/