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,
14 *| June 23 2003 Pieter Grimmerink Added 'i2c_sendnack'. i2c_readreg now
15 *| generates nack on last received byte,
17 *| i2c_getack changed data level while clock
18 *| was high, causing DS75 to see a stop condition
20 *! ---------------------------------------------------------------------------
22 *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
24 *!***************************************************************************/
26 /****************** INCLUDE FILES SECTION ***********************************/
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
33 #include <linux/string.h>
34 #include <linux/init.h>
35 #include <linux/mutex.h>
37 #include <asm/etraxi2c.h>
39 #include <asm/system.h>
41 #include <asm/delay.h>
45 /****************** I2C DEFINITION SECTION *************************/
49 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
50 static DEFINE_MUTEX(i2c_mutex
);
51 static const char i2c_name
[] = "i2c";
53 #define CLOCK_LOW_TIME 8
54 #define CLOCK_HIGH_TIME 8
55 #define START_CONDITION_HOLD_TIME 8
56 #define STOP_CONDITION_HOLD_TIME 8
57 #define ENABLE_OUTPUT 0x01
58 #define ENABLE_INPUT 0x00
59 #define I2C_CLOCK_HIGH 1
60 #define I2C_CLOCK_LOW 0
61 #define I2C_DATA_HIGH 1
62 #define I2C_DATA_LOW 0
67 /* enable or disable output-enable, to select output or input on the i2c bus */
69 #define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
70 #define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
72 /* control the i2c clock and data signals */
74 #define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
75 #define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
77 /* read a bit from the i2c interface */
79 #define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
81 #define i2c_delay(usecs) udelay(usecs)
83 static DEFINE_SPINLOCK(i2c_lock
); /* Protect directions etc */
85 /****************** VARIABLE SECTION ************************************/
87 static struct crisv32_iopin cris_i2c_clk
;
88 static struct crisv32_iopin cris_i2c_data
;
90 /****************** FUNCTION DEFINITION SECTION *************************/
93 /* generate i2c start condition */
102 i2c_delay(CLOCK_HIGH_TIME
/6);
103 i2c_data(I2C_DATA_HIGH
);
104 i2c_clk(I2C_CLOCK_HIGH
);
105 i2c_delay(CLOCK_HIGH_TIME
);
109 i2c_data(I2C_DATA_LOW
);
110 i2c_delay(START_CONDITION_HOLD_TIME
);
114 i2c_clk(I2C_CLOCK_LOW
);
115 i2c_delay(CLOCK_LOW_TIME
);
118 /* generate i2c stop condition */
128 i2c_clk(I2C_CLOCK_LOW
);
129 i2c_data(I2C_DATA_LOW
);
130 i2c_delay(CLOCK_LOW_TIME
*2);
134 i2c_clk(I2C_CLOCK_HIGH
);
135 i2c_delay(CLOCK_HIGH_TIME
*2);
139 i2c_data(I2C_DATA_HIGH
);
140 i2c_delay(STOP_CONDITION_HOLD_TIME
);
145 /* write a byte to the i2c interface */
148 i2c_outbyte(unsigned char x
)
154 for (i
= 0; i
< 8; i
++) {
156 i2c_data(I2C_DATA_HIGH
);
158 i2c_data(I2C_DATA_LOW
);
161 i2c_delay(CLOCK_LOW_TIME
/2);
162 i2c_clk(I2C_CLOCK_HIGH
);
163 i2c_delay(CLOCK_HIGH_TIME
);
164 i2c_clk(I2C_CLOCK_LOW
);
165 i2c_delay(CLOCK_LOW_TIME
/2);
168 i2c_data(I2C_DATA_LOW
);
169 i2c_delay(CLOCK_LOW_TIME
/2);
177 /* read a byte from the i2c interface */
182 unsigned char aBitByte
= 0;
185 /* Switch off I2C to get bit */
188 i2c_delay(CLOCK_HIGH_TIME
/2);
191 aBitByte
|= i2c_getbit();
195 i2c_delay(CLOCK_LOW_TIME
/2);
197 for (i
= 1; i
< 8; i
++) {
200 i2c_clk(I2C_CLOCK_HIGH
);
201 i2c_delay(CLOCK_HIGH_TIME
);
202 i2c_clk(I2C_CLOCK_LOW
);
203 i2c_delay(CLOCK_LOW_TIME
);
205 /* Switch off I2C to get bit */
208 i2c_delay(CLOCK_HIGH_TIME
/2);
211 aBitByte
|= i2c_getbit();
215 i2c_delay(CLOCK_LOW_TIME
/2);
217 i2c_clk(I2C_CLOCK_HIGH
);
218 i2c_delay(CLOCK_HIGH_TIME
);
221 * we leave the clock low, getbyte is usually followed
222 * by sendack/nack, they assume the clock to be low
224 i2c_clk(I2C_CLOCK_LOW
);
228 /*#---------------------------------------------------------------------------
230 *# FUNCTION NAME: i2c_getack
232 *# DESCRIPTION : checks if ack was received from ic2
234 *#--------------------------------------------------------------------------*/
245 * Release data bus by setting
248 i2c_data(I2C_DATA_HIGH
);
253 i2c_delay(CLOCK_HIGH_TIME
/4);
255 * generate ACK clock pulse
257 i2c_clk(I2C_CLOCK_HIGH
);
262 i2c_delay(CLOCK_HIGH_TIME
/2);
268 i2c_delay(CLOCK_HIGH_TIME
/2);
270 if (!i2c_getbit()) /* receiver pulld SDA low */
272 i2c_delay(CLOCK_HIGH_TIME
/2);
276 * our clock is high now, make sure data is low
277 * before we enable our output. If we keep data high
278 * and enable output, we would generate a stop condition.
280 i2c_clk(I2C_CLOCK_LOW
);
281 i2c_delay(CLOCK_HIGH_TIME
/4);
287 * remove ACK clock pulse
289 i2c_data(I2C_DATA_HIGH
);
290 i2c_delay(CLOCK_LOW_TIME
/2);
294 /*#---------------------------------------------------------------------------
296 *# FUNCTION NAME: I2C::sendAck
298 *# DESCRIPTION : Send ACK on received data
300 *#--------------------------------------------------------------------------*/
307 i2c_delay(CLOCK_LOW_TIME
);
312 i2c_data(I2C_DATA_LOW
);
314 * generate clock pulse
316 i2c_delay(CLOCK_HIGH_TIME
/6);
317 i2c_clk(I2C_CLOCK_HIGH
);
318 i2c_delay(CLOCK_HIGH_TIME
);
319 i2c_clk(I2C_CLOCK_LOW
);
320 i2c_delay(CLOCK_LOW_TIME
/6);
324 i2c_data(I2C_DATA_HIGH
);
325 i2c_delay(CLOCK_LOW_TIME
);
330 /*#---------------------------------------------------------------------------
332 *# FUNCTION NAME: i2c_sendnack
334 *# DESCRIPTION : Sends NACK on received data
336 *#--------------------------------------------------------------------------*/
343 i2c_delay(CLOCK_LOW_TIME
);
348 i2c_data(I2C_DATA_HIGH
);
350 * generate clock pulse
352 i2c_delay(CLOCK_HIGH_TIME
/6);
353 i2c_clk(I2C_CLOCK_HIGH
);
354 i2c_delay(CLOCK_HIGH_TIME
);
355 i2c_clk(I2C_CLOCK_LOW
);
356 i2c_delay(CLOCK_LOW_TIME
);
361 /*#---------------------------------------------------------------------------
363 *# FUNCTION NAME: i2c_write
365 *# DESCRIPTION : Writes a value to an I2C device
367 *#--------------------------------------------------------------------------*/
369 i2c_write(unsigned char theSlave
, void *data
, size_t nbytes
)
372 unsigned char bytes_wrote
= 0;
376 spin_lock_irqsave(&i2c_lock
, flags
);
385 i2c_outbyte((theSlave
& 0xfe));
394 for (bytes_wrote
= 0; bytes_wrote
< nbytes
; bytes_wrote
++) {
395 memcpy(&value
, data
+ bytes_wrote
, sizeof value
);
398 * now it's time to wait for ack
408 } while (error
&& cntr
--);
410 i2c_delay(CLOCK_LOW_TIME
);
412 spin_unlock_irqrestore(&i2c_lock
, flags
);
417 /*#---------------------------------------------------------------------------
419 *# FUNCTION NAME: i2c_read
421 *# DESCRIPTION : Reads a value from an I2C device
423 *#--------------------------------------------------------------------------*/
425 i2c_read(unsigned char theSlave
, void *data
, size_t nbytes
)
428 unsigned char bytes_read
= 0;
432 spin_lock_irqsave(&i2c_lock
, flags
);
436 memset(data
, 0, nbytes
);
438 * generate start condition
444 i2c_outbyte((theSlave
| 0x01));
453 for (bytes_read
= 0; bytes_read
< nbytes
; bytes_read
++) {
455 memcpy(data
+ bytes_read
, &b
, sizeof b
);
457 if (bytes_read
< (nbytes
- 1))
461 * last received byte needs to be nacked
469 } while (error
&& cntr
--);
471 spin_unlock_irqrestore(&i2c_lock
, flags
);
476 /*#---------------------------------------------------------------------------
478 *# FUNCTION NAME: i2c_writereg
480 *# DESCRIPTION : Writes a value to an I2C device
482 *#--------------------------------------------------------------------------*/
484 i2c_writereg(unsigned char theSlave
, unsigned char theReg
,
485 unsigned char theValue
)
490 spin_lock_irqsave(&i2c_lock
, flags
);
499 i2c_outbyte((theSlave
& 0xfe));
506 * now select register
511 * now it's time to wait for ack
516 * send register register data
518 i2c_outbyte(theValue
);
520 * now it's time to wait for ack
528 } while(error
&& cntr
--);
530 i2c_delay(CLOCK_LOW_TIME
);
532 spin_unlock_irqrestore(&i2c_lock
, flags
);
537 /*#---------------------------------------------------------------------------
539 *# FUNCTION NAME: i2c_readreg
541 *# DESCRIPTION : Reads a value from the decoder registers.
543 *#--------------------------------------------------------------------------*/
545 i2c_readreg(unsigned char theSlave
, unsigned char theReg
)
551 spin_lock_irqsave(&i2c_lock
, flags
);
556 * generate start condition
563 i2c_outbyte((theSlave
& 0xfe));
570 * now select register
575 * now it's time to wait for ack
580 * repeat start condition
582 i2c_delay(CLOCK_LOW_TIME
);
587 i2c_outbyte(theSlave
| 0x01);
598 * last received byte needs to be nacked
607 } while(error
&& cntr
--);
609 spin_unlock_irqrestore(&i2c_lock
, flags
);
615 i2c_open(struct inode
*inode
, struct file
*filp
)
621 i2c_release(struct inode
*inode
, struct file
*filp
)
626 /* Main device API. ioctl's to write or read to/from i2c registers.
630 i2c_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
633 if(_IOC_TYPE(cmd
) != ETRAXI2C_IOCTYPE
) {
637 switch (_IOC_NR(cmd
)) {
639 /* write to an i2c slave */
640 D(printk("i2cw %d %d %d\n",
645 mutex_lock(&i2c_mutex
);
646 ret
= i2c_writereg(I2C_ARGSLAVE(arg
),
649 mutex_unlock(&i2c_mutex
);
655 /* read from an i2c slave */
656 D(printk("i2cr %d %d ",
659 mutex_lock(&i2c_mutex
);
660 val
= i2c_readreg(I2C_ARGSLAVE(arg
), I2C_ARGREG(arg
));
661 mutex_unlock(&i2c_mutex
);
662 D(printk("= %d\n", val
));
673 static const struct file_operations i2c_fops
= {
674 .owner
= THIS_MODULE
,
675 .unlocked_ioctl
= i2c_ioctl
,
677 .release
= i2c_release
,
680 static int __init
i2c_init(void)
683 static int first
= 1;
690 /* Setup and enable the DATA and CLK pins */
692 res
= crisv32_io_get_name(&cris_i2c_data
,
693 CONFIG_ETRAX_V32_I2C_DATA_PORT
);
697 res
= crisv32_io_get_name(&cris_i2c_clk
, CONFIG_ETRAX_V32_I2C_CLK_PORT
);
698 crisv32_io_set_dir(&cris_i2c_clk
, crisv32_io_dir_out
);
704 static int __init
i2c_register(void)
712 /* register char device */
714 res
= register_chrdev(I2C_MAJOR
, i2c_name
, &i2c_fops
);
716 printk(KERN_ERR
"i2c: couldn't get a major number.\n");
721 "I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");
725 /* this makes sure that i2c_init is called during boot */
726 module_init(i2c_register
);
728 /****************** END OF FILE i2c.c ********************************/