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/errno.h>
18 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
23 #include <asm/etraxi2c.h>
25 #include <arch/svinto.h>
27 #include <asm/delay.h>
28 #include <arch/io_interface_mux.h>
32 /****************** I2C DEFINITION SECTION *************************/
36 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
37 static const char i2c_name
[] = "i2c";
39 #define CLOCK_LOW_TIME 8
40 #define CLOCK_HIGH_TIME 8
41 #define START_CONDITION_HOLD_TIME 8
42 #define STOP_CONDITION_HOLD_TIME 8
43 #define ENABLE_OUTPUT 0x01
44 #define ENABLE_INPUT 0x00
45 #define I2C_CLOCK_HIGH 1
46 #define I2C_CLOCK_LOW 0
47 #define I2C_DATA_HIGH 1
48 #define I2C_DATA_LOW 0
50 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
51 /* Use PB and not PB_I2C */
52 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
53 #define CONFIG_ETRAX_I2C_DATA_PORT 0
55 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
56 #define CONFIG_ETRAX_I2C_CLK_PORT 1
59 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
60 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
64 /* enable or disable output-enable, to select output or input on the i2c bus */
66 #define i2c_dir_out() \
67 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
68 #define i2c_dir_in() \
69 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
71 /* control the i2c clock and data signals */
74 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
76 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
78 /* read a bit from the i2c interface */
80 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
83 /* enable or disable the i2c interface */
85 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
86 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
88 /* enable or disable output-enable, to select output or input on the i2c bus */
90 #define i2c_dir_out() \
91 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
92 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
93 #define i2c_dir_in() \
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, 0);
97 /* control the i2c clock and data signals */
100 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
101 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
102 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
104 #define i2c_data(x) \
105 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
106 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
107 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
109 /* read a bit from the i2c interface */
111 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
114 /* use the kernels delay routine */
116 #define i2c_delay(usecs) udelay(usecs)
118 static DEFINE_SPINLOCK(i2c_lock
); /* Protect directions etc */
120 /****************** FUNCTION DEFINITION SECTION *************************/
123 /* generate i2c start condition */
132 i2c_delay(CLOCK_HIGH_TIME
/6);
133 i2c_data(I2C_DATA_HIGH
);
134 i2c_clk(I2C_CLOCK_HIGH
);
135 i2c_delay(CLOCK_HIGH_TIME
);
139 i2c_data(I2C_DATA_LOW
);
140 i2c_delay(START_CONDITION_HOLD_TIME
);
144 i2c_clk(I2C_CLOCK_LOW
);
145 i2c_delay(CLOCK_LOW_TIME
);
148 /* generate i2c stop condition */
158 i2c_clk(I2C_CLOCK_LOW
);
159 i2c_data(I2C_DATA_LOW
);
160 i2c_delay(CLOCK_LOW_TIME
*2);
164 i2c_clk(I2C_CLOCK_HIGH
);
165 i2c_delay(CLOCK_HIGH_TIME
*2);
169 i2c_data(I2C_DATA_HIGH
);
170 i2c_delay(STOP_CONDITION_HOLD_TIME
);
175 /* write a byte to the i2c interface */
178 i2c_outbyte(unsigned char x
)
184 for (i
= 0; i
< 8; i
++) {
186 i2c_data(I2C_DATA_HIGH
);
188 i2c_data(I2C_DATA_LOW
);
191 i2c_delay(CLOCK_LOW_TIME
/2);
192 i2c_clk(I2C_CLOCK_HIGH
);
193 i2c_delay(CLOCK_HIGH_TIME
);
194 i2c_clk(I2C_CLOCK_LOW
);
195 i2c_delay(CLOCK_LOW_TIME
/2);
198 i2c_data(I2C_DATA_LOW
);
199 i2c_delay(CLOCK_LOW_TIME
/2);
207 /* read a byte from the i2c interface */
212 unsigned char aBitByte
= 0;
215 /* Switch off I2C to get bit */
218 i2c_delay(CLOCK_HIGH_TIME
/2);
221 aBitByte
|= i2c_getbit();
225 i2c_delay(CLOCK_LOW_TIME
/2);
227 for (i
= 1; i
< 8; i
++) {
230 i2c_clk(I2C_CLOCK_HIGH
);
231 i2c_delay(CLOCK_HIGH_TIME
);
232 i2c_clk(I2C_CLOCK_LOW
);
233 i2c_delay(CLOCK_LOW_TIME
);
235 /* Switch off I2C to get bit */
238 i2c_delay(CLOCK_HIGH_TIME
/2);
241 aBitByte
|= i2c_getbit();
245 i2c_delay(CLOCK_LOW_TIME
/2);
247 i2c_clk(I2C_CLOCK_HIGH
);
248 i2c_delay(CLOCK_HIGH_TIME
);
251 * we leave the clock low, getbyte is usually followed
252 * by sendack/nack, they assume the clock to be low
254 i2c_clk(I2C_CLOCK_LOW
);
258 /*#---------------------------------------------------------------------------
260 *# FUNCTION NAME: i2c_getack
262 *# DESCRIPTION : checks if ack was received from ic2
264 *#--------------------------------------------------------------------------*/
275 * Release data bus by setting
278 i2c_data(I2C_DATA_HIGH
);
283 i2c_delay(CLOCK_HIGH_TIME
/4);
285 * generate ACK clock pulse
287 i2c_clk(I2C_CLOCK_HIGH
);
289 * Use PORT PB instead of I2C
290 * for input. (I2C not working)
303 i2c_delay(CLOCK_HIGH_TIME
/2);
309 i2c_delay(CLOCK_HIGH_TIME
/2);
311 if(!i2c_getbit()) /* receiver pulld SDA low */
313 i2c_delay(CLOCK_HIGH_TIME
/2);
317 * our clock is high now, make sure data is low
318 * before we enable our output. If we keep data high
319 * and enable output, we would generate a stop condition.
321 i2c_data(I2C_DATA_LOW
);
328 i2c_clk(I2C_CLOCK_LOW
);
329 i2c_delay(CLOCK_HIGH_TIME
/4);
335 * remove ACK clock pulse
337 i2c_data(I2C_DATA_HIGH
);
338 i2c_delay(CLOCK_LOW_TIME
/2);
342 /*#---------------------------------------------------------------------------
344 *# FUNCTION NAME: I2C::sendAck
346 *# DESCRIPTION : Send ACK on received data
348 *#--------------------------------------------------------------------------*/
355 i2c_delay(CLOCK_LOW_TIME
);
360 i2c_data(I2C_DATA_LOW
);
362 * generate clock pulse
364 i2c_delay(CLOCK_HIGH_TIME
/6);
365 i2c_clk(I2C_CLOCK_HIGH
);
366 i2c_delay(CLOCK_HIGH_TIME
);
367 i2c_clk(I2C_CLOCK_LOW
);
368 i2c_delay(CLOCK_LOW_TIME
/6);
372 i2c_data(I2C_DATA_HIGH
);
373 i2c_delay(CLOCK_LOW_TIME
);
378 /*#---------------------------------------------------------------------------
380 *# FUNCTION NAME: i2c_sendnack
382 *# DESCRIPTION : Sends NACK on received data
384 *#--------------------------------------------------------------------------*/
391 i2c_delay(CLOCK_LOW_TIME
);
396 i2c_data(I2C_DATA_HIGH
);
398 * generate clock pulse
400 i2c_delay(CLOCK_HIGH_TIME
/6);
401 i2c_clk(I2C_CLOCK_HIGH
);
402 i2c_delay(CLOCK_HIGH_TIME
);
403 i2c_clk(I2C_CLOCK_LOW
);
404 i2c_delay(CLOCK_LOW_TIME
);
409 /*#---------------------------------------------------------------------------
411 *# FUNCTION NAME: i2c_writereg
413 *# DESCRIPTION : Writes a value to an I2C device
415 *#--------------------------------------------------------------------------*/
417 i2c_writereg(unsigned char theSlave
, unsigned char theReg
,
418 unsigned char theValue
)
423 spin_lock(&i2c_lock
);
428 * we don't like to be interrupted
430 local_irq_save(flags
);
436 i2c_outbyte((theSlave
& 0xfe));
443 * now select register
448 * now it's time to wait for ack
453 * send register register data
455 i2c_outbyte(theValue
);
457 * now it's time to wait for ack
466 * enable interrupt again
468 local_irq_restore(flags
);
470 } while(error
&& cntr
--);
472 i2c_delay(CLOCK_LOW_TIME
);
474 spin_unlock(&i2c_lock
);
479 /*#---------------------------------------------------------------------------
481 *# FUNCTION NAME: i2c_readreg
483 *# DESCRIPTION : Reads a value from the decoder registers.
485 *#--------------------------------------------------------------------------*/
487 i2c_readreg(unsigned char theSlave
, unsigned char theReg
)
493 spin_lock(&i2c_lock
);
498 * we don't like to be interrupted
500 local_irq_save(flags
);
502 * generate start condition
509 i2c_outbyte((theSlave
& 0xfe));
516 * now select register
521 * now it's time to wait for ack
526 * repeat start condition
528 i2c_delay(CLOCK_LOW_TIME
);
533 i2c_outbyte(theSlave
| 0x01);
544 * last received byte needs to be nacked
553 * enable interrupt again
555 local_irq_restore(flags
);
557 } while(error
&& cntr
--);
559 spin_unlock(&i2c_lock
);
565 i2c_open(struct inode
*inode
, struct file
*filp
)
571 i2c_release(struct inode
*inode
, struct file
*filp
)
576 /* Main device API. ioctl's to write or read to/from i2c registers.
579 static long i2c_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
581 if(_IOC_TYPE(cmd
) != ETRAXI2C_IOCTYPE
) {
585 switch (_IOC_NR(cmd
)) {
587 /* write to an i2c slave */
588 D(printk(KERN_DEBUG
"i2cw %d %d %d\n",
593 return i2c_writereg(I2C_ARGSLAVE(arg
),
599 /* read from an i2c slave */
600 D(printk(KERN_DEBUG
"i2cr %d %d ",
603 val
= i2c_readreg(I2C_ARGSLAVE(arg
), I2C_ARGREG(arg
));
604 D(printk(KERN_DEBUG
"= %d\n", val
));
614 static const struct file_operations i2c_fops
= {
615 .owner
= THIS_MODULE
,
616 .unlocked_ioctl
= i2c_ioctl
,
618 .release
= i2c_release
,
619 .llseek
= noop_llseek
,
626 static int first
= 1;
633 /* Setup and enable the Port B I2C interface */
635 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
636 if ((res
= cris_request_io_interface(if_i2c
, "I2C"))) {
637 printk(KERN_CRIT
"i2c_init: Failed to get IO interface\n");
641 *R_PORT_PB_I2C
= port_pb_i2c_shadow
|=
642 IO_STATE(R_PORT_PB_I2C
, i2c_en
, on
) |
643 IO_FIELD(R_PORT_PB_I2C
, i2c_d
, 1) |
644 IO_FIELD(R_PORT_PB_I2C
, i2c_clk
, 1) |
645 IO_STATE(R_PORT_PB_I2C
, i2c_oe_
, enable
);
647 port_pb_dir_shadow
&= ~IO_MASK(R_PORT_PB_DIR
, dir0
);
648 port_pb_dir_shadow
&= ~IO_MASK(R_PORT_PB_DIR
, dir1
);
650 *R_PORT_PB_DIR
= (port_pb_dir_shadow
|=
651 IO_STATE(R_PORT_PB_DIR
, dir0
, input
) |
652 IO_STATE(R_PORT_PB_DIR
, dir1
, output
));
654 if ((res
= cris_io_interface_allocate_pins(if_i2c
,
656 CONFIG_ETRAX_I2C_DATA_PORT
,
657 CONFIG_ETRAX_I2C_DATA_PORT
))) {
658 printk(KERN_WARNING
"i2c_init: Failed to get IO pin for I2C data port\n");
660 } else if ((res
= cris_io_interface_allocate_pins(if_i2c
,
662 CONFIG_ETRAX_I2C_CLK_PORT
,
663 CONFIG_ETRAX_I2C_CLK_PORT
))) {
664 cris_io_interface_free_pins(if_i2c
,
666 CONFIG_ETRAX_I2C_DATA_PORT
,
667 CONFIG_ETRAX_I2C_DATA_PORT
);
668 printk(KERN_WARNING
"i2c_init: Failed to get IO pin for I2C clk port\n");
683 res
= register_chrdev(I2C_MAJOR
, i2c_name
, &i2c_fops
);
685 printk(KERN_ERR
"i2c: couldn't get a major number.\n");
689 printk(KERN_INFO
"I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
694 /* this makes sure that i2c_register is called during boot */
696 module_init(i2c_register
);
698 /****************** END OF FILE i2c.c ********************************/