123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- /*!***************************************************************************
- *!
- *! FILE NAME : i2c.c
- *!
- *! DESCRIPTION: implements an interface for IIC/I2C, both directly from other
- *! kernel modules (i2c_writereg/readreg) and from userspace using
- *! ioctl()'s
- *!
- *! Nov 30 1998 Torbjorn Eliasson Initial version.
- *! Bjorn Wesen Elinux kernel version.
- *! Jan 14 2000 Johan Adolfsson Fixed PB shadow register stuff -
- *! don't use PB_I2C if DS1302 uses same bits,
- *! use PB.
- *| June 23 2003 Pieter Grimmerink Added 'i2c_sendnack'. i2c_readreg now
- *| generates nack on last received byte,
- *| instead of ack.
- *| i2c_getack changed data level while clock
- *| was high, causing DS75 to see a stop condition
- *!
- *! ---------------------------------------------------------------------------
- *!
- *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
- *!
- *!***************************************************************************/
- /****************** INCLUDE FILES SECTION ***********************************/
- #include <linux/module.h>
- #include <linux/sched.h>
- #include <linux/errno.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/string.h>
- #include <linux/init.h>
- #include <linux/mutex.h>
- #include <asm/etraxi2c.h>
- #include <asm/system.h>
- #include <asm/io.h>
- #include <asm/delay.h>
- #include "i2c.h"
- /****************** I2C DEFINITION SECTION *************************/
- #define D(x)
- #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
- static DEFINE_MUTEX(i2c_mutex);
- static const char i2c_name[] = "i2c";
- #define CLOCK_LOW_TIME 8
- #define CLOCK_HIGH_TIME 8
- #define START_CONDITION_HOLD_TIME 8
- #define STOP_CONDITION_HOLD_TIME 8
- #define ENABLE_OUTPUT 0x01
- #define ENABLE_INPUT 0x00
- #define I2C_CLOCK_HIGH 1
- #define I2C_CLOCK_LOW 0
- #define I2C_DATA_HIGH 1
- #define I2C_DATA_LOW 0
- #define i2c_enable()
- #define i2c_disable()
- /* enable or disable output-enable, to select output or input on the i2c bus */
- #define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
- #define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
- /* control the i2c clock and data signals */
- #define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
- #define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
- /* read a bit from the i2c interface */
- #define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
- #define i2c_delay(usecs) udelay(usecs)
- static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
- /****************** VARIABLE SECTION ************************************/
- static struct crisv32_iopin cris_i2c_clk;
- static struct crisv32_iopin cris_i2c_data;
- /****************** FUNCTION DEFINITION SECTION *************************/
- /* generate i2c start condition */
- void
- i2c_start(void)
- {
- /*
- * SCL=1 SDA=1
- */
- i2c_dir_out();
- i2c_delay(CLOCK_HIGH_TIME/6);
- i2c_data(I2C_DATA_HIGH);
- i2c_clk(I2C_CLOCK_HIGH);
- i2c_delay(CLOCK_HIGH_TIME);
- /*
- * SCL=1 SDA=0
- */
- i2c_data(I2C_DATA_LOW);
- i2c_delay(START_CONDITION_HOLD_TIME);
- /*
- * SCL=0 SDA=0
- */
- i2c_clk(I2C_CLOCK_LOW);
- i2c_delay(CLOCK_LOW_TIME);
- }
- /* generate i2c stop condition */
- void
- i2c_stop(void)
- {
- i2c_dir_out();
- /*
- * SCL=0 SDA=0
- */
- i2c_clk(I2C_CLOCK_LOW);
- i2c_data(I2C_DATA_LOW);
- i2c_delay(CLOCK_LOW_TIME*2);
- /*
- * SCL=1 SDA=0
- */
- i2c_clk(I2C_CLOCK_HIGH);
- i2c_delay(CLOCK_HIGH_TIME*2);
- /*
- * SCL=1 SDA=1
- */
- i2c_data(I2C_DATA_HIGH);
- i2c_delay(STOP_CONDITION_HOLD_TIME);
- i2c_dir_in();
- }
- /* write a byte to the i2c interface */
- void
- i2c_outbyte(unsigned char x)
- {
- int i;
- i2c_dir_out();
- for (i = 0; i < 8; i++) {
- if (x & 0x80) {
- i2c_data(I2C_DATA_HIGH);
- } else {
- i2c_data(I2C_DATA_LOW);
- }
- i2c_delay(CLOCK_LOW_TIME/2);
- i2c_clk(I2C_CLOCK_HIGH);
- i2c_delay(CLOCK_HIGH_TIME);
- i2c_clk(I2C_CLOCK_LOW);
- i2c_delay(CLOCK_LOW_TIME/2);
- x <<= 1;
- }
- i2c_data(I2C_DATA_LOW);
- i2c_delay(CLOCK_LOW_TIME/2);
- /*
- * enable input
- */
- i2c_dir_in();
- }
- /* read a byte from the i2c interface */
- unsigned char
- i2c_inbyte(void)
- {
- unsigned char aBitByte = 0;
- int i;
- /* Switch off I2C to get bit */
- i2c_disable();
- i2c_dir_in();
- i2c_delay(CLOCK_HIGH_TIME/2);
- /* Get bit */
- aBitByte |= i2c_getbit();
- /* Enable I2C */
- i2c_enable();
- i2c_delay(CLOCK_LOW_TIME/2);
- for (i = 1; i < 8; i++) {
- aBitByte <<= 1;
- /* Clock pulse */
- i2c_clk(I2C_CLOCK_HIGH);
- i2c_delay(CLOCK_HIGH_TIME);
- i2c_clk(I2C_CLOCK_LOW);
- i2c_delay(CLOCK_LOW_TIME);
- /* Switch off I2C to get bit */
- i2c_disable();
- i2c_dir_in();
- i2c_delay(CLOCK_HIGH_TIME/2);
- /* Get bit */
- aBitByte |= i2c_getbit();
- /* Enable I2C */
- i2c_enable();
- i2c_delay(CLOCK_LOW_TIME/2);
- }
- i2c_clk(I2C_CLOCK_HIGH);
- i2c_delay(CLOCK_HIGH_TIME);
- /*
- * we leave the clock low, getbyte is usually followed
- * by sendack/nack, they assume the clock to be low
- */
- i2c_clk(I2C_CLOCK_LOW);
- return aBitByte;
- }
- /*#---------------------------------------------------------------------------
- *#
- *# FUNCTION NAME: i2c_getack
- *#
- *# DESCRIPTION : checks if ack was received from ic2
- *#
- *#--------------------------------------------------------------------------*/
- int
- i2c_getack(void)
- {
- int ack = 1;
- /*
- * enable output
- */
- i2c_dir_out();
- /*
- * Release data bus by setting
- * data high
- */
- i2c_data(I2C_DATA_HIGH);
- /*
- * enable input
- */
- i2c_dir_in();
- i2c_delay(CLOCK_HIGH_TIME/4);
- /*
- * generate ACK clock pulse
- */
- i2c_clk(I2C_CLOCK_HIGH);
- #if 0
- /*
- * Use PORT PB instead of I2C
- * for input. (I2C not working)
- */
- i2c_clk(1);
- i2c_data(1);
- /*
- * switch off I2C
- */
- i2c_data(1);
- i2c_disable();
- i2c_dir_in();
- #endif
- /*
- * now wait for ack
- */
- i2c_delay(CLOCK_HIGH_TIME/2);
- /*
- * check for ack
- */
- if (i2c_getbit())
- ack = 0;
- i2c_delay(CLOCK_HIGH_TIME/2);
- if (!ack) {
- if (!i2c_getbit()) /* receiver pulld SDA low */
- ack = 1;
- i2c_delay(CLOCK_HIGH_TIME/2);
- }
- /*
- * our clock is high now, make sure data is low
- * before we enable our output. If we keep data high
- * and enable output, we would generate a stop condition.
- */
- #if 0
- i2c_data(I2C_DATA_LOW);
- /*
- * end clock pulse
- */
- i2c_enable();
- i2c_dir_out();
- #endif
- i2c_clk(I2C_CLOCK_LOW);
- i2c_delay(CLOCK_HIGH_TIME/4);
- /*
- * enable output
- */
- i2c_dir_out();
- /*
- * remove ACK clock pulse
- */
- i2c_data(I2C_DATA_HIGH);
- i2c_delay(CLOCK_LOW_TIME/2);
- return ack;
- }
- /*#---------------------------------------------------------------------------
- *#
- *# FUNCTION NAME: I2C::sendAck
- *#
- *# DESCRIPTION : Send ACK on received data
- *#
- *#--------------------------------------------------------------------------*/
- void
- i2c_sendack(void)
- {
- /*
- * enable output
- */
- i2c_delay(CLOCK_LOW_TIME);
- i2c_dir_out();
- /*
- * set ack pulse high
- */
- i2c_data(I2C_DATA_LOW);
- /*
- * generate clock pulse
- */
- i2c_delay(CLOCK_HIGH_TIME/6);
- i2c_clk(I2C_CLOCK_HIGH);
- i2c_delay(CLOCK_HIGH_TIME);
- i2c_clk(I2C_CLOCK_LOW);
- i2c_delay(CLOCK_LOW_TIME/6);
- /*
- * reset data out
- */
- i2c_data(I2C_DATA_HIGH);
- i2c_delay(CLOCK_LOW_TIME);
- i2c_dir_in();
- }
- /*#---------------------------------------------------------------------------
- *#
- *# FUNCTION NAME: i2c_sendnack
- *#
- *# DESCRIPTION : Sends NACK on received data
- *#
- *#--------------------------------------------------------------------------*/
- void
- i2c_sendnack(void)
- {
- /*
- * enable output
- */
- i2c_delay(CLOCK_LOW_TIME);
- i2c_dir_out();
- /*
- * set data high
- */
- i2c_data(I2C_DATA_HIGH);
- /*
- * generate clock pulse
- */
- i2c_delay(CLOCK_HIGH_TIME/6);
- i2c_clk(I2C_CLOCK_HIGH);
- i2c_delay(CLOCK_HIGH_TIME);
- i2c_clk(I2C_CLOCK_LOW);
- i2c_delay(CLOCK_LOW_TIME);
- i2c_dir_in();
- }
- /*#---------------------------------------------------------------------------
- *#
- *# FUNCTION NAME: i2c_write
- *#
- *# DESCRIPTION : Writes a value to an I2C device
- *#
- *#--------------------------------------------------------------------------*/
- int
- i2c_write(unsigned char theSlave, void *data, size_t nbytes)
- {
- int error, cntr = 3;
- unsigned char bytes_wrote = 0;
- unsigned char value;
- unsigned long flags;
- spin_lock_irqsave(&i2c_lock, flags);
- do {
- error = 0;
- i2c_start();
- /*
- * send slave address
- */
- i2c_outbyte((theSlave & 0xfe));
- /*
- * wait for ack
- */
- if (!i2c_getack())
- error = 1;
- /*
- * send data
- */
- for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) {
- memcpy(&value, data + bytes_wrote, sizeof value);
- i2c_outbyte(value);
- /*
- * now it's time to wait for ack
- */
- if (!i2c_getack())
- error |= 4;
- }
- /*
- * end byte stream
- */
- i2c_stop();
- } while (error && cntr--);
- i2c_delay(CLOCK_LOW_TIME);
- spin_unlock_irqrestore(&i2c_lock, flags);
- return -error;
- }
- /*#---------------------------------------------------------------------------
- *#
- *# FUNCTION NAME: i2c_read
- *#
- *# DESCRIPTION : Reads a value from an I2C device
- *#
- *#--------------------------------------------------------------------------*/
- int
- i2c_read(unsigned char theSlave, void *data, size_t nbytes)
- {
- unsigned char b = 0;
- unsigned char bytes_read = 0;
- int error, cntr = 3;
- unsigned long flags;
- spin_lock_irqsave(&i2c_lock, flags);
- do {
- error = 0;
- memset(data, 0, nbytes);
- /*
- * generate start condition
- */
- i2c_start();
- /*
- * send slave address
- */
- i2c_outbyte((theSlave | 0x01));
- /*
- * wait for ack
- */
- if (!i2c_getack())
- error = 1;
- /*
- * fetch data
- */
- for (bytes_read = 0; bytes_read < nbytes; bytes_read++) {
- b = i2c_inbyte();
- memcpy(data + bytes_read, &b, sizeof b);
- if (bytes_read < (nbytes - 1))
- i2c_sendack();
- }
- /*
- * last received byte needs to be nacked
- * instead of acked
- */
- i2c_sendnack();
- /*
- * end sequence
- */
- i2c_stop();
- } while (error && cntr--);
- spin_unlock_irqrestore(&i2c_lock, flags);
- return -error;
- }
- /*#---------------------------------------------------------------------------
- *#
- *# FUNCTION NAME: i2c_writereg
- *#
- *# DESCRIPTION : Writes a value to an I2C device
- *#
- *#--------------------------------------------------------------------------*/
- int
- i2c_writereg(unsigned char theSlave, unsigned char theReg,
- unsigned char theValue)
- {
- int error, cntr = 3;
- unsigned long flags;
- spin_lock_irqsave(&i2c_lock, flags);
- do {
- error = 0;
- i2c_start();
- /*
- * send slave address
- */
- i2c_outbyte((theSlave & 0xfe));
- /*
- * wait for ack
- */
- if(!i2c_getack())
- error = 1;
- /*
- * now select register
- */
- i2c_dir_out();
- i2c_outbyte(theReg);
- /*
- * now it's time to wait for ack
- */
- if(!i2c_getack())
- error |= 2;
- /*
- * send register register data
- */
- i2c_outbyte(theValue);
- /*
- * now it's time to wait for ack
- */
- if(!i2c_getack())
- error |= 4;
- /*
- * end byte stream
- */
- i2c_stop();
- } while(error && cntr--);
- i2c_delay(CLOCK_LOW_TIME);
- spin_unlock_irqrestore(&i2c_lock, flags);
- return -error;
- }
- /*#---------------------------------------------------------------------------
- *#
- *# FUNCTION NAME: i2c_readreg
- *#
- *# DESCRIPTION : Reads a value from the decoder registers.
- *#
- *#--------------------------------------------------------------------------*/
- unsigned char
- i2c_readreg(unsigned char theSlave, unsigned char theReg)
- {
- unsigned char b = 0;
- int error, cntr = 3;
- unsigned long flags;
- spin_lock_irqsave(&i2c_lock, flags);
- do {
- error = 0;
- /*
- * generate start condition
- */
- i2c_start();
- /*
- * send slave address
- */
- i2c_outbyte((theSlave & 0xfe));
- /*
- * wait for ack
- */
- if(!i2c_getack())
- error = 1;
- /*
- * now select register
- */
- i2c_dir_out();
- i2c_outbyte(theReg);
- /*
- * now it's time to wait for ack
- */
- if(!i2c_getack())
- error |= 2;
- /*
- * repeat start condition
- */
- i2c_delay(CLOCK_LOW_TIME);
- i2c_start();
- /*
- * send slave address
- */
- i2c_outbyte(theSlave | 0x01);
- /*
- * wait for ack
- */
- if(!i2c_getack())
- error |= 4;
- /*
- * fetch register
- */
- b = i2c_inbyte();
- /*
- * last received byte needs to be nacked
- * instead of acked
- */
- i2c_sendnack();
- /*
- * end sequence
- */
- i2c_stop();
- } while(error && cntr--);
- spin_unlock_irqrestore(&i2c_lock, flags);
- return b;
- }
- static int
- i2c_open(struct inode *inode, struct file *filp)
- {
- return 0;
- }
- static int
- i2c_release(struct inode *inode, struct file *filp)
- {
- return 0;
- }
- /* Main device API. ioctl's to write or read to/from i2c registers.
- */
- static long
- i2c_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- {
- int ret;
- if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
- return -ENOTTY;
- }
- switch (_IOC_NR(cmd)) {
- case I2C_WRITEREG:
- /* write to an i2c slave */
- D(printk("i2cw %d %d %d\n",
- I2C_ARGSLAVE(arg),
- I2C_ARGREG(arg),
- I2C_ARGVALUE(arg)));
- mutex_lock(&i2c_mutex);
- ret = i2c_writereg(I2C_ARGSLAVE(arg),
- I2C_ARGREG(arg),
- I2C_ARGVALUE(arg));
- mutex_unlock(&i2c_mutex);
- return ret;
- case I2C_READREG:
- {
- unsigned char val;
- /* read from an i2c slave */
- D(printk("i2cr %d %d ",
- I2C_ARGSLAVE(arg),
- I2C_ARGREG(arg)));
- mutex_lock(&i2c_mutex);
- val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
- mutex_unlock(&i2c_mutex);
- D(printk("= %d\n", val));
- return val;
- }
- default:
- return -EINVAL;
- }
- return 0;
- }
- static const struct file_operations i2c_fops = {
- .owner = THIS_MODULE,
- .unlocked_ioctl = i2c_ioctl,
- .open = i2c_open,
- .release = i2c_release,
- .llseek = noop_llseek,
- };
- static int __init i2c_init(void)
- {
- static int res;
- static int first = 1;
- if (!first)
- return res;
- first = 0;
- /* Setup and enable the DATA and CLK pins */
- res = crisv32_io_get_name(&cris_i2c_data,
- CONFIG_ETRAX_V32_I2C_DATA_PORT);
- if (res < 0)
- return res;
- res = crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_V32_I2C_CLK_PORT);
- crisv32_io_set_dir(&cris_i2c_clk, crisv32_io_dir_out);
- return res;
- }
- static int __init i2c_register(void)
- {
- int res;
- res = i2c_init();
- if (res < 0)
- return res;
- /* register char device */
- res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
- if (res < 0) {
- printk(KERN_ERR "i2c: couldn't get a major number.\n");
- return res;
- }
- printk(KERN_INFO
- "I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");
- return 0;
- }
- /* this makes sure that i2c_init is called during boot */
- module_init(i2c_register);
- /****************** END OF FILE i2c.c ********************************/
|