1 /*!***************************************************************************
5 *! DESCRIPTION: implements an interface for IIC/I2C, both directly from other
6 *! kernel modules (i2c_writereg/readreg) and from userspace using
9 *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
11 *!***************************************************************************/
13 /****************** INCLUDE FILES SECTION ***********************************/
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/smp_lock.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
25 #include <asm/etraxi2c.h>
27 #include <asm/system.h>
28 #include <asm/arch/svinto.h>
30 #include <asm/delay.h>
31 #include <asm/arch/io_interface_mux.h>
35 /****************** I2C DEFINITION SECTION *************************/
39 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
40 static const char i2c_name[] = "i2c";
42 #define CLOCK_LOW_TIME 8
43 #define CLOCK_HIGH_TIME 8
44 #define START_CONDITION_HOLD_TIME 8
45 #define STOP_CONDITION_HOLD_TIME 8
46 #define ENABLE_OUTPUT 0x01
47 #define ENABLE_INPUT 0x00
48 #define I2C_CLOCK_HIGH 1
49 #define I2C_CLOCK_LOW 0
50 #define I2C_DATA_HIGH 1
51 #define I2C_DATA_LOW 0
53 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
54 /* Use PB and not PB_I2C */
55 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
56 #define CONFIG_ETRAX_I2C_DATA_PORT 0
58 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
59 #define CONFIG_ETRAX_I2C_CLK_PORT 1
62 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
63 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
67 /* enable or disable output-enable, to select output or input on the i2c bus */
69 #define i2c_dir_out() \
70 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
71 #define i2c_dir_in() \
72 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
74 /* control the i2c clock and data signals */
77 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
79 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
81 /* read a bit from the i2c interface */
83 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
86 /* enable or disable the i2c interface */
88 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
89 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
91 /* enable or disable output-enable, to select output or input on the i2c bus */
93 #define i2c_dir_out() \
94 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
95 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
96 #define i2c_dir_in() \
97 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
98 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
100 /* control the i2c clock and data signals */
103 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
104 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
105 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
107 #define i2c_data(x) \
108 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
109 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
110 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
112 /* read a bit from the i2c interface */
114 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
117 /* use the kernels delay routine */
119 #define i2c_delay(usecs) udelay(usecs)
121 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
123 /****************** FUNCTION DEFINITION SECTION *************************/
126 /* generate i2c start condition */
135 i2c_delay(CLOCK_HIGH_TIME/6);
136 i2c_data(I2C_DATA_HIGH);
137 i2c_clk(I2C_CLOCK_HIGH);
138 i2c_delay(CLOCK_HIGH_TIME);
142 i2c_data(I2C_DATA_LOW);
143 i2c_delay(START_CONDITION_HOLD_TIME);
147 i2c_clk(I2C_CLOCK_LOW);
148 i2c_delay(CLOCK_LOW_TIME);
151 /* generate i2c stop condition */
161 i2c_clk(I2C_CLOCK_LOW);
162 i2c_data(I2C_DATA_LOW);
163 i2c_delay(CLOCK_LOW_TIME*2);
167 i2c_clk(I2C_CLOCK_HIGH);
168 i2c_delay(CLOCK_HIGH_TIME*2);
172 i2c_data(I2C_DATA_HIGH);
173 i2c_delay(STOP_CONDITION_HOLD_TIME);
178 /* write a byte to the i2c interface */
181 i2c_outbyte(unsigned char x)
187 for (i = 0; i < 8; i++) {
189 i2c_data(I2C_DATA_HIGH);
191 i2c_data(I2C_DATA_LOW);
194 i2c_delay(CLOCK_LOW_TIME/2);
195 i2c_clk(I2C_CLOCK_HIGH);
196 i2c_delay(CLOCK_HIGH_TIME);
197 i2c_clk(I2C_CLOCK_LOW);
198 i2c_delay(CLOCK_LOW_TIME/2);
201 i2c_data(I2C_DATA_LOW);
202 i2c_delay(CLOCK_LOW_TIME/2);
210 /* read a byte from the i2c interface */
215 unsigned char aBitByte = 0;
218 /* Switch off I2C to get bit */
221 i2c_delay(CLOCK_HIGH_TIME/2);
224 aBitByte |= i2c_getbit();
228 i2c_delay(CLOCK_LOW_TIME/2);
230 for (i = 1; i < 8; i++) {
233 i2c_clk(I2C_CLOCK_HIGH);
234 i2c_delay(CLOCK_HIGH_TIME);
235 i2c_clk(I2C_CLOCK_LOW);
236 i2c_delay(CLOCK_LOW_TIME);
238 /* Switch off I2C to get bit */
241 i2c_delay(CLOCK_HIGH_TIME/2);
244 aBitByte |= i2c_getbit();
248 i2c_delay(CLOCK_LOW_TIME/2);
250 i2c_clk(I2C_CLOCK_HIGH);
251 i2c_delay(CLOCK_HIGH_TIME);
254 * we leave the clock low, getbyte is usually followed
255 * by sendack/nack, they assume the clock to be low
257 i2c_clk(I2C_CLOCK_LOW);
261 /*#---------------------------------------------------------------------------
263 *# FUNCTION NAME: i2c_getack
265 *# DESCRIPTION : checks if ack was received from ic2
267 *#--------------------------------------------------------------------------*/
278 * Release data bus by setting
281 i2c_data(I2C_DATA_HIGH);
286 i2c_delay(CLOCK_HIGH_TIME/4);
288 * generate ACK clock pulse
290 i2c_clk(I2C_CLOCK_HIGH);
292 * Use PORT PB instead of I2C
293 * for input. (I2C not working)
306 i2c_delay(CLOCK_HIGH_TIME/2);
312 i2c_delay(CLOCK_HIGH_TIME/2);
314 if(!i2c_getbit()) /* receiver pulld SDA low */
316 i2c_delay(CLOCK_HIGH_TIME/2);
320 * our clock is high now, make sure data is low
321 * before we enable our output. If we keep data high
322 * and enable output, we would generate a stop condition.
324 i2c_data(I2C_DATA_LOW);
331 i2c_clk(I2C_CLOCK_LOW);
332 i2c_delay(CLOCK_HIGH_TIME/4);
338 * remove ACK clock pulse
340 i2c_data(I2C_DATA_HIGH);
341 i2c_delay(CLOCK_LOW_TIME/2);
345 /*#---------------------------------------------------------------------------
347 *# FUNCTION NAME: I2C::sendAck
349 *# DESCRIPTION : Send ACK on received data
351 *#--------------------------------------------------------------------------*/
358 i2c_delay(CLOCK_LOW_TIME);
363 i2c_data(I2C_DATA_LOW);
365 * generate clock pulse
367 i2c_delay(CLOCK_HIGH_TIME/6);
368 i2c_clk(I2C_CLOCK_HIGH);
369 i2c_delay(CLOCK_HIGH_TIME);
370 i2c_clk(I2C_CLOCK_LOW);
371 i2c_delay(CLOCK_LOW_TIME/6);
375 i2c_data(I2C_DATA_HIGH);
376 i2c_delay(CLOCK_LOW_TIME);
381 /*#---------------------------------------------------------------------------
383 *# FUNCTION NAME: i2c_sendnack
385 *# DESCRIPTION : Sends NACK on received data
387 *#--------------------------------------------------------------------------*/
394 i2c_delay(CLOCK_LOW_TIME);
399 i2c_data(I2C_DATA_HIGH);
401 * generate clock pulse
403 i2c_delay(CLOCK_HIGH_TIME/6);
404 i2c_clk(I2C_CLOCK_HIGH);
405 i2c_delay(CLOCK_HIGH_TIME);
406 i2c_clk(I2C_CLOCK_LOW);
407 i2c_delay(CLOCK_LOW_TIME);
412 /*#---------------------------------------------------------------------------
414 *# FUNCTION NAME: i2c_writereg
416 *# DESCRIPTION : Writes a value to an I2C device
418 *#--------------------------------------------------------------------------*/
420 i2c_writereg(unsigned char theSlave, unsigned char theReg,
421 unsigned char theValue)
426 spin_lock(&i2c_lock);
431 * we don't like to be interrupted
433 local_irq_save(flags);
439 i2c_outbyte((theSlave & 0xfe));
446 * now select register
451 * now it's time to wait for ack
456 * send register register data
458 i2c_outbyte(theValue);
460 * now it's time to wait for ack
469 * enable interrupt again
471 local_irq_restore(flags);
473 } while(error && cntr--);
475 i2c_delay(CLOCK_LOW_TIME);
477 spin_unlock(&i2c_lock);
482 /*#---------------------------------------------------------------------------
484 *# FUNCTION NAME: i2c_readreg
486 *# DESCRIPTION : Reads a value from the decoder registers.
488 *#--------------------------------------------------------------------------*/
490 i2c_readreg(unsigned char theSlave, unsigned char theReg)
496 spin_lock(&i2c_lock);
501 * we don't like to be interrupted
503 local_irq_save(flags);
505 * generate start condition
512 i2c_outbyte((theSlave & 0xfe));
519 * now select register
524 * now it's time to wait for ack
529 * repeat start condition
531 i2c_delay(CLOCK_LOW_TIME);
536 i2c_outbyte(theSlave | 0x01);
547 * last received byte needs to be nacked
556 * enable interrupt again
558 local_irq_restore(flags);
560 } while(error && cntr--);
562 spin_unlock(&i2c_lock);
568 i2c_open(struct inode *inode, struct file *filp)
575 i2c_release(struct inode *inode, struct file *filp)
580 /* Main device API. ioctl's to write or read to/from i2c registers.
584 i2c_ioctl(struct inode *inode, struct file *file,
585 unsigned int cmd, unsigned long arg)
587 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
591 switch (_IOC_NR(cmd)) {
593 /* write to an i2c slave */
594 D(printk("i2cw %d %d %d\n",
599 return i2c_writereg(I2C_ARGSLAVE(arg),
605 /* read from an i2c slave */
606 D(printk("i2cr %d %d ",
609 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
610 D(printk("= %d\n", val));
621 static const struct file_operations i2c_fops = {
622 .owner = THIS_MODULE,
625 .release = i2c_release,
632 static int first = 1;
639 /* Setup and enable the Port B I2C interface */
641 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
642 if ((res = cris_request_io_interface(if_i2c, "I2C"))) {
643 printk(KERN_CRIT "i2c_init: Failed to get IO interface\n");
647 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
648 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
649 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
650 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
651 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
653 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
654 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
656 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
657 IO_STATE(R_PORT_PB_DIR, dir0, input) |
658 IO_STATE(R_PORT_PB_DIR, dir1, output));
660 if ((res = cris_io_interface_allocate_pins(if_i2c,
662 CONFIG_ETRAX_I2C_DATA_PORT,
663 CONFIG_ETRAX_I2C_DATA_PORT))) {
664 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n");
666 } else if ((res = cris_io_interface_allocate_pins(if_i2c,
668 CONFIG_ETRAX_I2C_CLK_PORT,
669 CONFIG_ETRAX_I2C_CLK_PORT))) {
670 cris_io_interface_free_pins(if_i2c,
672 CONFIG_ETRAX_I2C_DATA_PORT,
673 CONFIG_ETRAX_I2C_DATA_PORT);
674 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n");
689 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
691 printk(KERN_ERR "i2c: couldn't get a major number.\n");
695 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
700 /* this makes sure that i2c_register is called during boot */
702 module_init(i2c_register);
704 /****************** END OF FILE i2c.c ********************************/