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-2002 Axis Communications AB, LUND, SWEDEN
24 *!***************************************************************************/
25 /* $Id: i2c.c,v 1.2 2005/05/09 15:29:49 starvik Exp $ */
26 /****************** INCLUDE FILES SECTION ***********************************/
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/init.h>
36 #include <linux/config.h>
38 #include <asm/etraxi2c.h>
40 #include <asm/system.h>
42 #include <asm/delay.h>
46 /****************** I2C DEFINITION SECTION *************************/
50 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
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 /****************** VARIABLE SECTION ************************************/
85 static struct crisv32_iopin cris_i2c_clk
;
86 static struct crisv32_iopin cris_i2c_data
;
88 /****************** FUNCTION DEFINITION SECTION *************************/
91 /* generate i2c start condition */
100 i2c_delay(CLOCK_HIGH_TIME
/6);
101 i2c_data(I2C_DATA_HIGH
);
102 i2c_clk(I2C_CLOCK_HIGH
);
103 i2c_delay(CLOCK_HIGH_TIME
);
107 i2c_data(I2C_DATA_LOW
);
108 i2c_delay(START_CONDITION_HOLD_TIME
);
112 i2c_clk(I2C_CLOCK_LOW
);
113 i2c_delay(CLOCK_LOW_TIME
);
116 /* generate i2c stop condition */
126 i2c_clk(I2C_CLOCK_LOW
);
127 i2c_data(I2C_DATA_LOW
);
128 i2c_delay(CLOCK_LOW_TIME
*2);
132 i2c_clk(I2C_CLOCK_HIGH
);
133 i2c_delay(CLOCK_HIGH_TIME
*2);
137 i2c_data(I2C_DATA_HIGH
);
138 i2c_delay(STOP_CONDITION_HOLD_TIME
);
143 /* write a byte to the i2c interface */
146 i2c_outbyte(unsigned char x
)
152 for (i
= 0; i
< 8; i
++) {
154 i2c_data(I2C_DATA_HIGH
);
156 i2c_data(I2C_DATA_LOW
);
159 i2c_delay(CLOCK_LOW_TIME
/2);
160 i2c_clk(I2C_CLOCK_HIGH
);
161 i2c_delay(CLOCK_HIGH_TIME
);
162 i2c_clk(I2C_CLOCK_LOW
);
163 i2c_delay(CLOCK_LOW_TIME
/2);
166 i2c_data(I2C_DATA_LOW
);
167 i2c_delay(CLOCK_LOW_TIME
/2);
175 /* read a byte from the i2c interface */
180 unsigned char aBitByte
= 0;
183 /* Switch off I2C to get bit */
186 i2c_delay(CLOCK_HIGH_TIME
/2);
189 aBitByte
|= i2c_getbit();
193 i2c_delay(CLOCK_LOW_TIME
/2);
195 for (i
= 1; i
< 8; i
++) {
198 i2c_clk(I2C_CLOCK_HIGH
);
199 i2c_delay(CLOCK_HIGH_TIME
);
200 i2c_clk(I2C_CLOCK_LOW
);
201 i2c_delay(CLOCK_LOW_TIME
);
203 /* Switch off I2C to get bit */
206 i2c_delay(CLOCK_HIGH_TIME
/2);
209 aBitByte
|= i2c_getbit();
213 i2c_delay(CLOCK_LOW_TIME
/2);
215 i2c_clk(I2C_CLOCK_HIGH
);
216 i2c_delay(CLOCK_HIGH_TIME
);
219 * we leave the clock low, getbyte is usually followed
220 * by sendack/nack, they assume the clock to be low
222 i2c_clk(I2C_CLOCK_LOW
);
226 /*#---------------------------------------------------------------------------
228 *# FUNCTION NAME: i2c_getack
230 *# DESCRIPTION : checks if ack was received from ic2
232 *#--------------------------------------------------------------------------*/
243 * Release data bus by setting
246 i2c_data(I2C_DATA_HIGH
);
251 i2c_delay(CLOCK_HIGH_TIME
/4);
253 * generate ACK clock pulse
255 i2c_clk(I2C_CLOCK_HIGH
);
257 * Use PORT PB instead of I2C
258 * for input. (I2C not working)
271 i2c_delay(CLOCK_HIGH_TIME
/2);
277 i2c_delay(CLOCK_HIGH_TIME
/2);
279 if(!i2c_getbit()) /* receiver pulld SDA low */
281 i2c_delay(CLOCK_HIGH_TIME
/2);
285 * our clock is high now, make sure data is low
286 * before we enable our output. If we keep data high
287 * and enable output, we would generate a stop condition.
289 i2c_data(I2C_DATA_LOW
);
296 i2c_clk(I2C_CLOCK_LOW
);
297 i2c_delay(CLOCK_HIGH_TIME
/4);
303 * remove ACK clock pulse
305 i2c_data(I2C_DATA_HIGH
);
306 i2c_delay(CLOCK_LOW_TIME
/2);
310 /*#---------------------------------------------------------------------------
312 *# FUNCTION NAME: I2C::sendAck
314 *# DESCRIPTION : Send ACK on received data
316 *#--------------------------------------------------------------------------*/
323 i2c_delay(CLOCK_LOW_TIME
);
328 i2c_data(I2C_DATA_LOW
);
330 * generate clock pulse
332 i2c_delay(CLOCK_HIGH_TIME
/6);
333 i2c_clk(I2C_CLOCK_HIGH
);
334 i2c_delay(CLOCK_HIGH_TIME
);
335 i2c_clk(I2C_CLOCK_LOW
);
336 i2c_delay(CLOCK_LOW_TIME
/6);
340 i2c_data(I2C_DATA_HIGH
);
341 i2c_delay(CLOCK_LOW_TIME
);
346 /*#---------------------------------------------------------------------------
348 *# FUNCTION NAME: i2c_sendnack
350 *# DESCRIPTION : Sends NACK on received data
352 *#--------------------------------------------------------------------------*/
359 i2c_delay(CLOCK_LOW_TIME
);
364 i2c_data(I2C_DATA_HIGH
);
366 * generate clock pulse
368 i2c_delay(CLOCK_HIGH_TIME
/6);
369 i2c_clk(I2C_CLOCK_HIGH
);
370 i2c_delay(CLOCK_HIGH_TIME
);
371 i2c_clk(I2C_CLOCK_LOW
);
372 i2c_delay(CLOCK_LOW_TIME
);
377 /*#---------------------------------------------------------------------------
379 *# FUNCTION NAME: i2c_writereg
381 *# DESCRIPTION : Writes a value to an I2C device
383 *#--------------------------------------------------------------------------*/
385 i2c_writereg(unsigned char theSlave
, unsigned char theReg
,
386 unsigned char theValue
)
394 * we don't like to be interrupted
396 local_irq_save(flags
);
402 i2c_outbyte((theSlave
& 0xfe));
409 * now select register
414 * now it's time to wait for ack
419 * send register register data
421 i2c_outbyte(theValue
);
423 * now it's time to wait for ack
432 * enable interrupt again
434 local_irq_restore(flags
);
436 } while(error
&& cntr
--);
438 i2c_delay(CLOCK_LOW_TIME
);
443 /*#---------------------------------------------------------------------------
445 *# FUNCTION NAME: i2c_readreg
447 *# DESCRIPTION : Reads a value from the decoder registers.
449 *#--------------------------------------------------------------------------*/
451 i2c_readreg(unsigned char theSlave
, unsigned char theReg
)
460 * we don't like to be interrupted
462 local_irq_save(flags
);
464 * generate start condition
471 i2c_outbyte((theSlave
& 0xfe));
478 * now select register
483 * now it's time to wait for ack
488 * repeat start condition
490 i2c_delay(CLOCK_LOW_TIME
);
495 i2c_outbyte(theSlave
| 0x01);
506 * last received byte needs to be nacked
515 * enable interrupt again
517 local_irq_restore(flags
);
519 } while(error
&& cntr
--);
525 i2c_open(struct inode
*inode
, struct file
*filp
)
531 i2c_release(struct inode
*inode
, struct file
*filp
)
536 /* Main device API. ioctl's to write or read to/from i2c registers.
540 i2c_ioctl(struct inode
*inode
, struct file
*file
,
541 unsigned int cmd
, unsigned long arg
)
543 if(_IOC_TYPE(cmd
) != ETRAXI2C_IOCTYPE
) {
547 switch (_IOC_NR(cmd
)) {
549 /* write to an i2c slave */
550 D(printk("i2cw %d %d %d\n",
555 return i2c_writereg(I2C_ARGSLAVE(arg
),
561 /* read from an i2c slave */
562 D(printk("i2cr %d %d ",
565 val
= i2c_readreg(I2C_ARGSLAVE(arg
), I2C_ARGREG(arg
));
566 D(printk("= %d\n", val
));
577 static struct file_operations i2c_fops
= {
581 release
: i2c_release
,
589 /* Setup and enable the Port B I2C interface */
591 crisv32_io_get_name(&cris_i2c_data
, CONFIG_ETRAX_I2C_DATA_PORT
);
592 crisv32_io_get_name(&cris_i2c_clk
, CONFIG_ETRAX_I2C_CLK_PORT
);
594 /* register char device */
596 res
= register_chrdev(I2C_MAJOR
, i2c_name
, &i2c_fops
);
598 printk(KERN_ERR
"i2c: couldn't get a major number.\n");
602 printk(KERN_INFO
"I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
607 /* this makes sure that i2c_init is called during boot */
609 module_init(i2c_init
);
611 /****************** END OF FILE i2c.c ********************************/