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/slab.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/init.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 const char i2c_name[] = "i2c";
52 #define CLOCK_LOW_TIME 8
53 #define CLOCK_HIGH_TIME 8
54 #define START_CONDITION_HOLD_TIME 8
55 #define STOP_CONDITION_HOLD_TIME 8
56 #define ENABLE_OUTPUT 0x01
57 #define ENABLE_INPUT 0x00
58 #define I2C_CLOCK_HIGH 1
59 #define I2C_CLOCK_LOW 0
60 #define I2C_DATA_HIGH 1
61 #define I2C_DATA_LOW 0
66 /* enable or disable output-enable, to select output or input on the i2c bus */
68 #define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
69 #define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
71 /* control the i2c clock and data signals */
73 #define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
74 #define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
76 /* read a bit from the i2c interface */
78 #define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
80 #define i2c_delay(usecs) udelay(usecs)
82 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
84 /****************** VARIABLE SECTION ************************************/
86 static struct crisv32_iopin cris_i2c_clk;
87 static struct crisv32_iopin cris_i2c_data;
89 /****************** FUNCTION DEFINITION SECTION *************************/
92 /* generate i2c start condition */
101 i2c_delay(CLOCK_HIGH_TIME/6);
102 i2c_data(I2C_DATA_HIGH);
103 i2c_clk(I2C_CLOCK_HIGH);
104 i2c_delay(CLOCK_HIGH_TIME);
108 i2c_data(I2C_DATA_LOW);
109 i2c_delay(START_CONDITION_HOLD_TIME);
113 i2c_clk(I2C_CLOCK_LOW);
114 i2c_delay(CLOCK_LOW_TIME);
117 /* generate i2c stop condition */
127 i2c_clk(I2C_CLOCK_LOW);
128 i2c_data(I2C_DATA_LOW);
129 i2c_delay(CLOCK_LOW_TIME*2);
133 i2c_clk(I2C_CLOCK_HIGH);
134 i2c_delay(CLOCK_HIGH_TIME*2);
138 i2c_data(I2C_DATA_HIGH);
139 i2c_delay(STOP_CONDITION_HOLD_TIME);
144 /* write a byte to the i2c interface */
147 i2c_outbyte(unsigned char x)
153 for (i = 0; i < 8; i++) {
155 i2c_data(I2C_DATA_HIGH);
157 i2c_data(I2C_DATA_LOW);
160 i2c_delay(CLOCK_LOW_TIME/2);
161 i2c_clk(I2C_CLOCK_HIGH);
162 i2c_delay(CLOCK_HIGH_TIME);
163 i2c_clk(I2C_CLOCK_LOW);
164 i2c_delay(CLOCK_LOW_TIME/2);
167 i2c_data(I2C_DATA_LOW);
168 i2c_delay(CLOCK_LOW_TIME/2);
176 /* read a byte from the i2c interface */
181 unsigned char aBitByte = 0;
184 /* Switch off I2C to get bit */
187 i2c_delay(CLOCK_HIGH_TIME/2);
190 aBitByte |= i2c_getbit();
194 i2c_delay(CLOCK_LOW_TIME/2);
196 for (i = 1; i < 8; i++) {
199 i2c_clk(I2C_CLOCK_HIGH);
200 i2c_delay(CLOCK_HIGH_TIME);
201 i2c_clk(I2C_CLOCK_LOW);
202 i2c_delay(CLOCK_LOW_TIME);
204 /* Switch off I2C to get bit */
207 i2c_delay(CLOCK_HIGH_TIME/2);
210 aBitByte |= i2c_getbit();
214 i2c_delay(CLOCK_LOW_TIME/2);
216 i2c_clk(I2C_CLOCK_HIGH);
217 i2c_delay(CLOCK_HIGH_TIME);
220 * we leave the clock low, getbyte is usually followed
221 * by sendack/nack, they assume the clock to be low
223 i2c_clk(I2C_CLOCK_LOW);
227 /*#---------------------------------------------------------------------------
229 *# FUNCTION NAME: i2c_getack
231 *# DESCRIPTION : checks if ack was received from ic2
233 *#--------------------------------------------------------------------------*/
244 * Release data bus by setting
247 i2c_data(I2C_DATA_HIGH);
252 i2c_delay(CLOCK_HIGH_TIME/4);
254 * generate ACK clock pulse
256 i2c_clk(I2C_CLOCK_HIGH);
259 * Use PORT PB instead of I2C
260 * for input. (I2C not working)
275 i2c_delay(CLOCK_HIGH_TIME/2);
281 i2c_delay(CLOCK_HIGH_TIME/2);
283 if (!i2c_getbit()) /* receiver pulld SDA low */
285 i2c_delay(CLOCK_HIGH_TIME/2);
289 * our clock is high now, make sure data is low
290 * before we enable our output. If we keep data high
291 * and enable output, we would generate a stop condition.
294 i2c_data(I2C_DATA_LOW);
302 i2c_clk(I2C_CLOCK_LOW);
303 i2c_delay(CLOCK_HIGH_TIME/4);
309 * remove ACK clock pulse
311 i2c_data(I2C_DATA_HIGH);
312 i2c_delay(CLOCK_LOW_TIME/2);
316 /*#---------------------------------------------------------------------------
318 *# FUNCTION NAME: I2C::sendAck
320 *# DESCRIPTION : Send ACK on received data
322 *#--------------------------------------------------------------------------*/
329 i2c_delay(CLOCK_LOW_TIME);
334 i2c_data(I2C_DATA_LOW);
336 * generate clock pulse
338 i2c_delay(CLOCK_HIGH_TIME/6);
339 i2c_clk(I2C_CLOCK_HIGH);
340 i2c_delay(CLOCK_HIGH_TIME);
341 i2c_clk(I2C_CLOCK_LOW);
342 i2c_delay(CLOCK_LOW_TIME/6);
346 i2c_data(I2C_DATA_HIGH);
347 i2c_delay(CLOCK_LOW_TIME);
352 /*#---------------------------------------------------------------------------
354 *# FUNCTION NAME: i2c_sendnack
356 *# DESCRIPTION : Sends NACK on received data
358 *#--------------------------------------------------------------------------*/
365 i2c_delay(CLOCK_LOW_TIME);
370 i2c_data(I2C_DATA_HIGH);
372 * generate clock pulse
374 i2c_delay(CLOCK_HIGH_TIME/6);
375 i2c_clk(I2C_CLOCK_HIGH);
376 i2c_delay(CLOCK_HIGH_TIME);
377 i2c_clk(I2C_CLOCK_LOW);
378 i2c_delay(CLOCK_LOW_TIME);
383 /*#---------------------------------------------------------------------------
385 *# FUNCTION NAME: i2c_write
387 *# DESCRIPTION : Writes a value to an I2C device
389 *#--------------------------------------------------------------------------*/
391 i2c_write(unsigned char theSlave, void *data, size_t nbytes)
394 unsigned char bytes_wrote = 0;
398 spin_lock_irqsave(&i2c_lock, flags);
407 i2c_outbyte((theSlave & 0xfe));
416 for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) {
417 memcpy(&value, data + bytes_wrote, sizeof value);
420 * now it's time to wait for ack
430 } while (error && cntr--);
432 i2c_delay(CLOCK_LOW_TIME);
434 spin_unlock_irqrestore(&i2c_lock, flags);
439 /*#---------------------------------------------------------------------------
441 *# FUNCTION NAME: i2c_read
443 *# DESCRIPTION : Reads a value from an I2C device
445 *#--------------------------------------------------------------------------*/
447 i2c_read(unsigned char theSlave, void *data, size_t nbytes)
450 unsigned char bytes_read = 0;
454 spin_lock_irqsave(&i2c_lock, flags);
458 memset(data, 0, nbytes);
460 * generate start condition
466 i2c_outbyte((theSlave | 0x01));
475 for (bytes_read = 0; bytes_read < nbytes; bytes_read++) {
477 memcpy(data + bytes_read, &b, sizeof b);
479 if (bytes_read < (nbytes - 1))
483 * last received byte needs to be nacked
491 } while (error && cntr--);
493 spin_unlock_irqrestore(&i2c_lock, flags);
498 /*#---------------------------------------------------------------------------
500 *# FUNCTION NAME: i2c_writereg
502 *# DESCRIPTION : Writes a value to an I2C device
504 *#--------------------------------------------------------------------------*/
506 i2c_writereg(unsigned char theSlave, unsigned char theReg,
507 unsigned char theValue)
512 spin_lock_irqsave(&i2c_lock, flags);
521 i2c_outbyte((theSlave & 0xfe));
528 * now select register
533 * now it's time to wait for ack
538 * send register register data
540 i2c_outbyte(theValue);
542 * now it's time to wait for ack
550 } while(error && cntr--);
552 i2c_delay(CLOCK_LOW_TIME);
554 spin_unlock_irqrestore(&i2c_lock, flags);
559 /*#---------------------------------------------------------------------------
561 *# FUNCTION NAME: i2c_readreg
563 *# DESCRIPTION : Reads a value from the decoder registers.
565 *#--------------------------------------------------------------------------*/
567 i2c_readreg(unsigned char theSlave, unsigned char theReg)
573 spin_lock_irqsave(&i2c_lock, flags);
578 * generate start condition
585 i2c_outbyte((theSlave & 0xfe));
592 * now select register
597 * now it's time to wait for ack
602 * repeat start condition
604 i2c_delay(CLOCK_LOW_TIME);
609 i2c_outbyte(theSlave | 0x01);
620 * last received byte needs to be nacked
629 } while(error && cntr--);
631 spin_unlock_irqrestore(&i2c_lock, flags);
637 i2c_open(struct inode *inode, struct file *filp)
643 i2c_release(struct inode *inode, struct file *filp)
648 /* Main device API. ioctl's to write or read to/from i2c registers.
652 i2c_ioctl(struct inode *inode, struct file *file,
653 unsigned int cmd, unsigned long arg)
655 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
659 switch (_IOC_NR(cmd)) {
661 /* write to an i2c slave */
662 D(printk("i2cw %d %d %d\n",
667 return i2c_writereg(I2C_ARGSLAVE(arg),
673 /* read from an i2c slave */
674 D(printk("i2cr %d %d ",
677 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
678 D(printk("= %d\n", val));
689 static const struct file_operations i2c_fops = {
690 .owner = THIS_MODULE,
693 .release = i2c_release,
696 static int __init i2c_init(void)
699 static int first = 1;
706 /* Setup and enable the DATA and CLK pins */
708 res = crisv32_io_get_name(&cris_i2c_data,
709 CONFIG_ETRAX_V32_I2C_DATA_PORT);
713 res = crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_V32_I2C_CLK_PORT);
714 crisv32_io_set_dir(&cris_i2c_clk, crisv32_io_dir_out);
720 static int __init i2c_register(void)
728 /* register char device */
730 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
732 printk(KERN_ERR "i2c: couldn't get a major number.\n");
737 "I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");
741 /* this makes sure that i2c_init is called during boot */
742 module_init(i2c_register);
744 /****************** END OF FILE i2c.c ********************************/