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/errno.h>
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
24 #include <asm/etraxi2c.h>
26 #include <asm/system.h>
27 #include <asm/arch/svinto.h>
29 #include <asm/delay.h>
30 #include <asm/arch/io_interface_mux.h>
34 /****************** I2C DEFINITION SECTION *************************/
38 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
39 static const char i2c_name
[] = "i2c";
41 #define CLOCK_LOW_TIME 8
42 #define CLOCK_HIGH_TIME 8
43 #define START_CONDITION_HOLD_TIME 8
44 #define STOP_CONDITION_HOLD_TIME 8
45 #define ENABLE_OUTPUT 0x01
46 #define ENABLE_INPUT 0x00
47 #define I2C_CLOCK_HIGH 1
48 #define I2C_CLOCK_LOW 0
49 #define I2C_DATA_HIGH 1
50 #define I2C_DATA_LOW 0
52 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
53 /* Use PB and not PB_I2C */
54 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
55 #define CONFIG_ETRAX_I2C_DATA_PORT 0
57 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
58 #define CONFIG_ETRAX_I2C_CLK_PORT 1
61 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
62 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
66 /* enable or disable output-enable, to select output or input on the i2c bus */
68 #define i2c_dir_out() \
69 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
70 #define i2c_dir_in() \
71 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
73 /* control the i2c clock and data signals */
76 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
78 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
80 /* read a bit from the i2c interface */
82 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
85 /* enable or disable the i2c interface */
87 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
88 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
90 /* enable or disable output-enable, to select output or input on the i2c bus */
92 #define i2c_dir_out() \
93 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
94 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
95 #define i2c_dir_in() \
96 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
97 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
99 /* control the i2c clock and data signals */
102 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
103 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
104 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
106 #define i2c_data(x) \
107 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
108 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
109 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
111 /* read a bit from the i2c interface */
113 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
116 /* use the kernels delay routine */
118 #define i2c_delay(usecs) udelay(usecs)
120 static DEFINE_SPINLOCK(i2c_lock
); /* Protect directions etc */
122 /****************** FUNCTION DEFINITION SECTION *************************/
125 /* generate i2c start condition */
134 i2c_delay(CLOCK_HIGH_TIME
/6);
135 i2c_data(I2C_DATA_HIGH
);
136 i2c_clk(I2C_CLOCK_HIGH
);
137 i2c_delay(CLOCK_HIGH_TIME
);
141 i2c_data(I2C_DATA_LOW
);
142 i2c_delay(START_CONDITION_HOLD_TIME
);
146 i2c_clk(I2C_CLOCK_LOW
);
147 i2c_delay(CLOCK_LOW_TIME
);
150 /* generate i2c stop condition */
160 i2c_clk(I2C_CLOCK_LOW
);
161 i2c_data(I2C_DATA_LOW
);
162 i2c_delay(CLOCK_LOW_TIME
*2);
166 i2c_clk(I2C_CLOCK_HIGH
);
167 i2c_delay(CLOCK_HIGH_TIME
*2);
171 i2c_data(I2C_DATA_HIGH
);
172 i2c_delay(STOP_CONDITION_HOLD_TIME
);
177 /* write a byte to the i2c interface */
180 i2c_outbyte(unsigned char x
)
186 for (i
= 0; i
< 8; i
++) {
188 i2c_data(I2C_DATA_HIGH
);
190 i2c_data(I2C_DATA_LOW
);
193 i2c_delay(CLOCK_LOW_TIME
/2);
194 i2c_clk(I2C_CLOCK_HIGH
);
195 i2c_delay(CLOCK_HIGH_TIME
);
196 i2c_clk(I2C_CLOCK_LOW
);
197 i2c_delay(CLOCK_LOW_TIME
/2);
200 i2c_data(I2C_DATA_LOW
);
201 i2c_delay(CLOCK_LOW_TIME
/2);
209 /* read a byte from the i2c interface */
214 unsigned char aBitByte
= 0;
217 /* Switch off I2C to get bit */
220 i2c_delay(CLOCK_HIGH_TIME
/2);
223 aBitByte
|= i2c_getbit();
227 i2c_delay(CLOCK_LOW_TIME
/2);
229 for (i
= 1; i
< 8; i
++) {
232 i2c_clk(I2C_CLOCK_HIGH
);
233 i2c_delay(CLOCK_HIGH_TIME
);
234 i2c_clk(I2C_CLOCK_LOW
);
235 i2c_delay(CLOCK_LOW_TIME
);
237 /* Switch off I2C to get bit */
240 i2c_delay(CLOCK_HIGH_TIME
/2);
243 aBitByte
|= i2c_getbit();
247 i2c_delay(CLOCK_LOW_TIME
/2);
249 i2c_clk(I2C_CLOCK_HIGH
);
250 i2c_delay(CLOCK_HIGH_TIME
);
253 * we leave the clock low, getbyte is usually followed
254 * by sendack/nack, they assume the clock to be low
256 i2c_clk(I2C_CLOCK_LOW
);
260 /*#---------------------------------------------------------------------------
262 *# FUNCTION NAME: i2c_getack
264 *# DESCRIPTION : checks if ack was received from ic2
266 *#--------------------------------------------------------------------------*/
277 * Release data bus by setting
280 i2c_data(I2C_DATA_HIGH
);
285 i2c_delay(CLOCK_HIGH_TIME
/4);
287 * generate ACK clock pulse
289 i2c_clk(I2C_CLOCK_HIGH
);
291 * Use PORT PB instead of I2C
292 * for input. (I2C not working)
305 i2c_delay(CLOCK_HIGH_TIME
/2);
311 i2c_delay(CLOCK_HIGH_TIME
/2);
313 if(!i2c_getbit()) /* receiver pulld SDA low */
315 i2c_delay(CLOCK_HIGH_TIME
/2);
319 * our clock is high now, make sure data is low
320 * before we enable our output. If we keep data high
321 * and enable output, we would generate a stop condition.
323 i2c_data(I2C_DATA_LOW
);
330 i2c_clk(I2C_CLOCK_LOW
);
331 i2c_delay(CLOCK_HIGH_TIME
/4);
337 * remove ACK clock pulse
339 i2c_data(I2C_DATA_HIGH
);
340 i2c_delay(CLOCK_LOW_TIME
/2);
344 /*#---------------------------------------------------------------------------
346 *# FUNCTION NAME: I2C::sendAck
348 *# DESCRIPTION : Send ACK on received data
350 *#--------------------------------------------------------------------------*/
357 i2c_delay(CLOCK_LOW_TIME
);
362 i2c_data(I2C_DATA_LOW
);
364 * generate clock pulse
366 i2c_delay(CLOCK_HIGH_TIME
/6);
367 i2c_clk(I2C_CLOCK_HIGH
);
368 i2c_delay(CLOCK_HIGH_TIME
);
369 i2c_clk(I2C_CLOCK_LOW
);
370 i2c_delay(CLOCK_LOW_TIME
/6);
374 i2c_data(I2C_DATA_HIGH
);
375 i2c_delay(CLOCK_LOW_TIME
);
380 /*#---------------------------------------------------------------------------
382 *# FUNCTION NAME: i2c_sendnack
384 *# DESCRIPTION : Sends NACK on received data
386 *#--------------------------------------------------------------------------*/
393 i2c_delay(CLOCK_LOW_TIME
);
398 i2c_data(I2C_DATA_HIGH
);
400 * generate clock pulse
402 i2c_delay(CLOCK_HIGH_TIME
/6);
403 i2c_clk(I2C_CLOCK_HIGH
);
404 i2c_delay(CLOCK_HIGH_TIME
);
405 i2c_clk(I2C_CLOCK_LOW
);
406 i2c_delay(CLOCK_LOW_TIME
);
411 /*#---------------------------------------------------------------------------
413 *# FUNCTION NAME: i2c_writereg
415 *# DESCRIPTION : Writes a value to an I2C device
417 *#--------------------------------------------------------------------------*/
419 i2c_writereg(unsigned char theSlave
, unsigned char theReg
,
420 unsigned char theValue
)
425 spin_lock(&i2c_lock
);
430 * we don't like to be interrupted
432 local_irq_save(flags
);
438 i2c_outbyte((theSlave
& 0xfe));
445 * now select register
450 * now it's time to wait for ack
455 * send register register data
457 i2c_outbyte(theValue
);
459 * now it's time to wait for ack
468 * enable interrupt again
470 local_irq_restore(flags
);
472 } while(error
&& cntr
--);
474 i2c_delay(CLOCK_LOW_TIME
);
476 spin_unlock(&i2c_lock
);
481 /*#---------------------------------------------------------------------------
483 *# FUNCTION NAME: i2c_readreg
485 *# DESCRIPTION : Reads a value from the decoder registers.
487 *#--------------------------------------------------------------------------*/
489 i2c_readreg(unsigned char theSlave
, unsigned char theReg
)
495 spin_lock(&i2c_lock
);
500 * we don't like to be interrupted
502 local_irq_save(flags
);
504 * generate start condition
511 i2c_outbyte((theSlave
& 0xfe));
518 * now select register
523 * now it's time to wait for ack
528 * repeat start condition
530 i2c_delay(CLOCK_LOW_TIME
);
535 i2c_outbyte(theSlave
| 0x01);
546 * last received byte needs to be nacked
555 * enable interrupt again
557 local_irq_restore(flags
);
559 } while(error
&& cntr
--);
561 spin_unlock(&i2c_lock
);
567 i2c_open(struct inode
*inode
, struct file
*filp
)
573 i2c_release(struct inode
*inode
, struct file
*filp
)
578 /* Main device API. ioctl's to write or read to/from i2c registers.
582 i2c_ioctl(struct inode
*inode
, struct file
*file
,
583 unsigned int cmd
, unsigned long arg
)
585 if(_IOC_TYPE(cmd
) != ETRAXI2C_IOCTYPE
) {
589 switch (_IOC_NR(cmd
)) {
591 /* write to an i2c slave */
592 D(printk("i2cw %d %d %d\n",
597 return i2c_writereg(I2C_ARGSLAVE(arg
),
603 /* read from an i2c slave */
604 D(printk("i2cr %d %d ",
607 val
= i2c_readreg(I2C_ARGSLAVE(arg
), I2C_ARGREG(arg
));
608 D(printk("= %d\n", val
));
619 static const struct file_operations i2c_fops
= {
620 .owner
= THIS_MODULE
,
623 .release
= i2c_release
,
630 static int first
= 1;
637 /* Setup and enable the Port B I2C interface */
639 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
640 if ((res
= cris_request_io_interface(if_i2c
, "I2C"))) {
641 printk(KERN_CRIT
"i2c_init: Failed to get IO interface\n");
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
);
651 port_pb_dir_shadow
&= ~IO_MASK(R_PORT_PB_DIR
, dir0
);
652 port_pb_dir_shadow
&= ~IO_MASK(R_PORT_PB_DIR
, dir1
);
654 *R_PORT_PB_DIR
= (port_pb_dir_shadow
|=
655 IO_STATE(R_PORT_PB_DIR
, dir0
, input
) |
656 IO_STATE(R_PORT_PB_DIR
, dir1
, output
));
658 if ((res
= cris_io_interface_allocate_pins(if_i2c
,
660 CONFIG_ETRAX_I2C_DATA_PORT
,
661 CONFIG_ETRAX_I2C_DATA_PORT
))) {
662 printk(KERN_WARNING
"i2c_init: Failed to get IO pin for I2C data port\n");
664 } else if ((res
= cris_io_interface_allocate_pins(if_i2c
,
666 CONFIG_ETRAX_I2C_CLK_PORT
,
667 CONFIG_ETRAX_I2C_CLK_PORT
))) {
668 cris_io_interface_free_pins(if_i2c
,
670 CONFIG_ETRAX_I2C_DATA_PORT
,
671 CONFIG_ETRAX_I2C_DATA_PORT
);
672 printk(KERN_WARNING
"i2c_init: Failed to get IO pin for I2C clk port\n");
687 res
= register_chrdev(I2C_MAJOR
, i2c_name
, &i2c_fops
);
689 printk(KERN_ERR
"i2c: couldn't get a major number.\n");
693 printk(KERN_INFO
"I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
698 /* this makes sure that i2c_register is called during boot */
700 module_init(i2c_register
);
702 /****************** END OF FILE i2c.c ********************************/