Path: blob/master/arch/cris/arch-v32/drivers/i2c.c
15126 views
/*!***************************************************************************1*!2*! FILE NAME : i2c.c3*!4*! DESCRIPTION: implements an interface for IIC/I2C, both directly from other5*! kernel modules (i2c_writereg/readreg) and from userspace using6*! ioctl()'s7*!8*! Nov 30 1998 Torbjorn Eliasson Initial version.9*! Bjorn Wesen Elinux kernel version.10*! Jan 14 2000 Johan Adolfsson Fixed PB shadow register stuff -11*! don't use PB_I2C if DS1302 uses same bits,12*! use PB.13*| June 23 2003 Pieter Grimmerink Added 'i2c_sendnack'. i2c_readreg now14*| generates nack on last received byte,15*| instead of ack.16*| i2c_getack changed data level while clock17*| was high, causing DS75 to see a stop condition18*!19*! ---------------------------------------------------------------------------20*!21*! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN22*!23*!***************************************************************************/2425/****************** INCLUDE FILES SECTION ***********************************/2627#include <linux/module.h>28#include <linux/sched.h>29#include <linux/errno.h>30#include <linux/kernel.h>31#include <linux/fs.h>32#include <linux/string.h>33#include <linux/init.h>34#include <linux/mutex.h>3536#include <asm/etraxi2c.h>3738#include <asm/system.h>39#include <asm/io.h>40#include <asm/delay.h>4142#include "i2c.h"4344/****************** I2C DEFINITION SECTION *************************/4546#define D(x)4748#define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */49static DEFINE_MUTEX(i2c_mutex);50static const char i2c_name[] = "i2c";5152#define CLOCK_LOW_TIME 853#define CLOCK_HIGH_TIME 854#define START_CONDITION_HOLD_TIME 855#define STOP_CONDITION_HOLD_TIME 856#define ENABLE_OUTPUT 0x0157#define ENABLE_INPUT 0x0058#define I2C_CLOCK_HIGH 159#define I2C_CLOCK_LOW 060#define I2C_DATA_HIGH 161#define I2C_DATA_LOW 06263#define i2c_enable()64#define i2c_disable()6566/* enable or disable output-enable, to select output or input on the i2c bus */6768#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)7071/* control the i2c clock and data signals */7273#define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)74#define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)7576/* read a bit from the i2c interface */7778#define i2c_getbit() crisv32_io_rd(&cris_i2c_data)7980#define i2c_delay(usecs) udelay(usecs)8182static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */8384/****************** VARIABLE SECTION ************************************/8586static struct crisv32_iopin cris_i2c_clk;87static struct crisv32_iopin cris_i2c_data;8889/****************** FUNCTION DEFINITION SECTION *************************/909192/* generate i2c start condition */9394void95i2c_start(void)96{97/*98* SCL=1 SDA=199*/100i2c_dir_out();101i2c_delay(CLOCK_HIGH_TIME/6);102i2c_data(I2C_DATA_HIGH);103i2c_clk(I2C_CLOCK_HIGH);104i2c_delay(CLOCK_HIGH_TIME);105/*106* SCL=1 SDA=0107*/108i2c_data(I2C_DATA_LOW);109i2c_delay(START_CONDITION_HOLD_TIME);110/*111* SCL=0 SDA=0112*/113i2c_clk(I2C_CLOCK_LOW);114i2c_delay(CLOCK_LOW_TIME);115}116117/* generate i2c stop condition */118119void120i2c_stop(void)121{122i2c_dir_out();123124/*125* SCL=0 SDA=0126*/127i2c_clk(I2C_CLOCK_LOW);128i2c_data(I2C_DATA_LOW);129i2c_delay(CLOCK_LOW_TIME*2);130/*131* SCL=1 SDA=0132*/133i2c_clk(I2C_CLOCK_HIGH);134i2c_delay(CLOCK_HIGH_TIME*2);135/*136* SCL=1 SDA=1137*/138i2c_data(I2C_DATA_HIGH);139i2c_delay(STOP_CONDITION_HOLD_TIME);140141i2c_dir_in();142}143144/* write a byte to the i2c interface */145146void147i2c_outbyte(unsigned char x)148{149int i;150151i2c_dir_out();152153for (i = 0; i < 8; i++) {154if (x & 0x80) {155i2c_data(I2C_DATA_HIGH);156} else {157i2c_data(I2C_DATA_LOW);158}159160i2c_delay(CLOCK_LOW_TIME/2);161i2c_clk(I2C_CLOCK_HIGH);162i2c_delay(CLOCK_HIGH_TIME);163i2c_clk(I2C_CLOCK_LOW);164i2c_delay(CLOCK_LOW_TIME/2);165x <<= 1;166}167i2c_data(I2C_DATA_LOW);168i2c_delay(CLOCK_LOW_TIME/2);169170/*171* enable input172*/173i2c_dir_in();174}175176/* read a byte from the i2c interface */177178unsigned char179i2c_inbyte(void)180{181unsigned char aBitByte = 0;182int i;183184/* Switch off I2C to get bit */185i2c_disable();186i2c_dir_in();187i2c_delay(CLOCK_HIGH_TIME/2);188189/* Get bit */190aBitByte |= i2c_getbit();191192/* Enable I2C */193i2c_enable();194i2c_delay(CLOCK_LOW_TIME/2);195196for (i = 1; i < 8; i++) {197aBitByte <<= 1;198/* Clock pulse */199i2c_clk(I2C_CLOCK_HIGH);200i2c_delay(CLOCK_HIGH_TIME);201i2c_clk(I2C_CLOCK_LOW);202i2c_delay(CLOCK_LOW_TIME);203204/* Switch off I2C to get bit */205i2c_disable();206i2c_dir_in();207i2c_delay(CLOCK_HIGH_TIME/2);208209/* Get bit */210aBitByte |= i2c_getbit();211212/* Enable I2C */213i2c_enable();214i2c_delay(CLOCK_LOW_TIME/2);215}216i2c_clk(I2C_CLOCK_HIGH);217i2c_delay(CLOCK_HIGH_TIME);218219/*220* we leave the clock low, getbyte is usually followed221* by sendack/nack, they assume the clock to be low222*/223i2c_clk(I2C_CLOCK_LOW);224return aBitByte;225}226227/*#---------------------------------------------------------------------------228*#229*# FUNCTION NAME: i2c_getack230*#231*# DESCRIPTION : checks if ack was received from ic2232*#233*#--------------------------------------------------------------------------*/234235int236i2c_getack(void)237{238int ack = 1;239/*240* enable output241*/242i2c_dir_out();243/*244* Release data bus by setting245* data high246*/247i2c_data(I2C_DATA_HIGH);248/*249* enable input250*/251i2c_dir_in();252i2c_delay(CLOCK_HIGH_TIME/4);253/*254* generate ACK clock pulse255*/256i2c_clk(I2C_CLOCK_HIGH);257#if 0258/*259* Use PORT PB instead of I2C260* for input. (I2C not working)261*/262i2c_clk(1);263i2c_data(1);264/*265* switch off I2C266*/267i2c_data(1);268i2c_disable();269i2c_dir_in();270#endif271272/*273* now wait for ack274*/275i2c_delay(CLOCK_HIGH_TIME/2);276/*277* check for ack278*/279if (i2c_getbit())280ack = 0;281i2c_delay(CLOCK_HIGH_TIME/2);282if (!ack) {283if (!i2c_getbit()) /* receiver pulld SDA low */284ack = 1;285i2c_delay(CLOCK_HIGH_TIME/2);286}287288/*289* our clock is high now, make sure data is low290* before we enable our output. If we keep data high291* and enable output, we would generate a stop condition.292*/293#if 0294i2c_data(I2C_DATA_LOW);295296/*297* end clock pulse298*/299i2c_enable();300i2c_dir_out();301#endif302i2c_clk(I2C_CLOCK_LOW);303i2c_delay(CLOCK_HIGH_TIME/4);304/*305* enable output306*/307i2c_dir_out();308/*309* remove ACK clock pulse310*/311i2c_data(I2C_DATA_HIGH);312i2c_delay(CLOCK_LOW_TIME/2);313return ack;314}315316/*#---------------------------------------------------------------------------317*#318*# FUNCTION NAME: I2C::sendAck319*#320*# DESCRIPTION : Send ACK on received data321*#322*#--------------------------------------------------------------------------*/323void324i2c_sendack(void)325{326/*327* enable output328*/329i2c_delay(CLOCK_LOW_TIME);330i2c_dir_out();331/*332* set ack pulse high333*/334i2c_data(I2C_DATA_LOW);335/*336* generate clock pulse337*/338i2c_delay(CLOCK_HIGH_TIME/6);339i2c_clk(I2C_CLOCK_HIGH);340i2c_delay(CLOCK_HIGH_TIME);341i2c_clk(I2C_CLOCK_LOW);342i2c_delay(CLOCK_LOW_TIME/6);343/*344* reset data out345*/346i2c_data(I2C_DATA_HIGH);347i2c_delay(CLOCK_LOW_TIME);348349i2c_dir_in();350}351352/*#---------------------------------------------------------------------------353*#354*# FUNCTION NAME: i2c_sendnack355*#356*# DESCRIPTION : Sends NACK on received data357*#358*#--------------------------------------------------------------------------*/359void360i2c_sendnack(void)361{362/*363* enable output364*/365i2c_delay(CLOCK_LOW_TIME);366i2c_dir_out();367/*368* set data high369*/370i2c_data(I2C_DATA_HIGH);371/*372* generate clock pulse373*/374i2c_delay(CLOCK_HIGH_TIME/6);375i2c_clk(I2C_CLOCK_HIGH);376i2c_delay(CLOCK_HIGH_TIME);377i2c_clk(I2C_CLOCK_LOW);378i2c_delay(CLOCK_LOW_TIME);379380i2c_dir_in();381}382383/*#---------------------------------------------------------------------------384*#385*# FUNCTION NAME: i2c_write386*#387*# DESCRIPTION : Writes a value to an I2C device388*#389*#--------------------------------------------------------------------------*/390int391i2c_write(unsigned char theSlave, void *data, size_t nbytes)392{393int error, cntr = 3;394unsigned char bytes_wrote = 0;395unsigned char value;396unsigned long flags;397398spin_lock_irqsave(&i2c_lock, flags);399400do {401error = 0;402403i2c_start();404/*405* send slave address406*/407i2c_outbyte((theSlave & 0xfe));408/*409* wait for ack410*/411if (!i2c_getack())412error = 1;413/*414* send data415*/416for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) {417memcpy(&value, data + bytes_wrote, sizeof value);418i2c_outbyte(value);419/*420* now it's time to wait for ack421*/422if (!i2c_getack())423error |= 4;424}425/*426* end byte stream427*/428i2c_stop();429430} while (error && cntr--);431432i2c_delay(CLOCK_LOW_TIME);433434spin_unlock_irqrestore(&i2c_lock, flags);435436return -error;437}438439/*#---------------------------------------------------------------------------440*#441*# FUNCTION NAME: i2c_read442*#443*# DESCRIPTION : Reads a value from an I2C device444*#445*#--------------------------------------------------------------------------*/446int447i2c_read(unsigned char theSlave, void *data, size_t nbytes)448{449unsigned char b = 0;450unsigned char bytes_read = 0;451int error, cntr = 3;452unsigned long flags;453454spin_lock_irqsave(&i2c_lock, flags);455456do {457error = 0;458memset(data, 0, nbytes);459/*460* generate start condition461*/462i2c_start();463/*464* send slave address465*/466i2c_outbyte((theSlave | 0x01));467/*468* wait for ack469*/470if (!i2c_getack())471error = 1;472/*473* fetch data474*/475for (bytes_read = 0; bytes_read < nbytes; bytes_read++) {476b = i2c_inbyte();477memcpy(data + bytes_read, &b, sizeof b);478479if (bytes_read < (nbytes - 1))480i2c_sendack();481}482/*483* last received byte needs to be nacked484* instead of acked485*/486i2c_sendnack();487/*488* end sequence489*/490i2c_stop();491} while (error && cntr--);492493spin_unlock_irqrestore(&i2c_lock, flags);494495return -error;496}497498/*#---------------------------------------------------------------------------499*#500*# FUNCTION NAME: i2c_writereg501*#502*# DESCRIPTION : Writes a value to an I2C device503*#504*#--------------------------------------------------------------------------*/505int506i2c_writereg(unsigned char theSlave, unsigned char theReg,507unsigned char theValue)508{509int error, cntr = 3;510unsigned long flags;511512spin_lock_irqsave(&i2c_lock, flags);513514do {515error = 0;516517i2c_start();518/*519* send slave address520*/521i2c_outbyte((theSlave & 0xfe));522/*523* wait for ack524*/525if(!i2c_getack())526error = 1;527/*528* now select register529*/530i2c_dir_out();531i2c_outbyte(theReg);532/*533* now it's time to wait for ack534*/535if(!i2c_getack())536error |= 2;537/*538* send register register data539*/540i2c_outbyte(theValue);541/*542* now it's time to wait for ack543*/544if(!i2c_getack())545error |= 4;546/*547* end byte stream548*/549i2c_stop();550} while(error && cntr--);551552i2c_delay(CLOCK_LOW_TIME);553554spin_unlock_irqrestore(&i2c_lock, flags);555556return -error;557}558559/*#---------------------------------------------------------------------------560*#561*# FUNCTION NAME: i2c_readreg562*#563*# DESCRIPTION : Reads a value from the decoder registers.564*#565*#--------------------------------------------------------------------------*/566unsigned char567i2c_readreg(unsigned char theSlave, unsigned char theReg)568{569unsigned char b = 0;570int error, cntr = 3;571unsigned long flags;572573spin_lock_irqsave(&i2c_lock, flags);574575do {576error = 0;577/*578* generate start condition579*/580i2c_start();581582/*583* send slave address584*/585i2c_outbyte((theSlave & 0xfe));586/*587* wait for ack588*/589if(!i2c_getack())590error = 1;591/*592* now select register593*/594i2c_dir_out();595i2c_outbyte(theReg);596/*597* now it's time to wait for ack598*/599if(!i2c_getack())600error |= 2;601/*602* repeat start condition603*/604i2c_delay(CLOCK_LOW_TIME);605i2c_start();606/*607* send slave address608*/609i2c_outbyte(theSlave | 0x01);610/*611* wait for ack612*/613if(!i2c_getack())614error |= 4;615/*616* fetch register617*/618b = i2c_inbyte();619/*620* last received byte needs to be nacked621* instead of acked622*/623i2c_sendnack();624/*625* end sequence626*/627i2c_stop();628629} while(error && cntr--);630631spin_unlock_irqrestore(&i2c_lock, flags);632633return b;634}635636static int637i2c_open(struct inode *inode, struct file *filp)638{639return 0;640}641642static int643i2c_release(struct inode *inode, struct file *filp)644{645return 0;646}647648/* Main device API. ioctl's to write or read to/from i2c registers.649*/650651static long652i2c_ioctl(struct file *file, unsigned int cmd, unsigned long arg)653{654int ret;655if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {656return -ENOTTY;657}658659switch (_IOC_NR(cmd)) {660case I2C_WRITEREG:661/* write to an i2c slave */662D(printk("i2cw %d %d %d\n",663I2C_ARGSLAVE(arg),664I2C_ARGREG(arg),665I2C_ARGVALUE(arg)));666667mutex_lock(&i2c_mutex);668ret = i2c_writereg(I2C_ARGSLAVE(arg),669I2C_ARGREG(arg),670I2C_ARGVALUE(arg));671mutex_unlock(&i2c_mutex);672return ret;673674case I2C_READREG:675{676unsigned char val;677/* read from an i2c slave */678D(printk("i2cr %d %d ",679I2C_ARGSLAVE(arg),680I2C_ARGREG(arg)));681mutex_lock(&i2c_mutex);682val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));683mutex_unlock(&i2c_mutex);684D(printk("= %d\n", val));685return val;686}687default:688return -EINVAL;689690}691692return 0;693}694695static const struct file_operations i2c_fops = {696.owner = THIS_MODULE,697.unlocked_ioctl = i2c_ioctl,698.open = i2c_open,699.release = i2c_release,700.llseek = noop_llseek,701};702703static int __init i2c_init(void)704{705static int res;706static int first = 1;707708if (!first)709return res;710711first = 0;712713/* Setup and enable the DATA and CLK pins */714715res = crisv32_io_get_name(&cris_i2c_data,716CONFIG_ETRAX_V32_I2C_DATA_PORT);717if (res < 0)718return res;719720res = crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_V32_I2C_CLK_PORT);721crisv32_io_set_dir(&cris_i2c_clk, crisv32_io_dir_out);722723return res;724}725726727static int __init i2c_register(void)728{729int res;730731res = i2c_init();732if (res < 0)733return res;734735/* register char device */736737res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);738if (res < 0) {739printk(KERN_ERR "i2c: couldn't get a major number.\n");740return res;741}742743printk(KERN_INFO744"I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");745746return 0;747}748/* this makes sure that i2c_init is called during boot */749module_init(i2c_register);750751/****************** END OF FILE i2c.c ********************************/752753754