Path: blob/master/drivers/i2c/algos/i2c-algo-bit.c
17531 views
/* -------------------------------------------------------------------------1* i2c-algo-bit.c i2c driver algorithms for bit-shift adapters2* -------------------------------------------------------------------------3* Copyright (C) 1995-2000 Simon G. Vogl45This program is free software; you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation; either version 2 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program; if not, write to the Free Software17Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18* ------------------------------------------------------------------------- */1920/* With some changes from Frodo Looijaard <[email protected]>, Kyösti Mälkki21<[email protected]> and Jean Delvare <[email protected]> */2223#include <linux/kernel.h>24#include <linux/module.h>25#include <linux/delay.h>26#include <linux/init.h>27#include <linux/errno.h>28#include <linux/sched.h>29#include <linux/i2c.h>30#include <linux/i2c-algo-bit.h>313233/* ----- global defines ----------------------------------------------- */3435#ifdef DEBUG36#define bit_dbg(level, dev, format, args...) \37do { \38if (i2c_debug >= level) \39dev_dbg(dev, format, ##args); \40} while (0)41#else42#define bit_dbg(level, dev, format, args...) \43do {} while (0)44#endif /* DEBUG */4546/* ----- global variables --------------------------------------------- */4748static int bit_test; /* see if the line-setting functions work */49module_param(bit_test, bool, 0);50MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");5152#ifdef DEBUG53static int i2c_debug = 1;54module_param(i2c_debug, int, S_IRUGO | S_IWUSR);55MODULE_PARM_DESC(i2c_debug,56"debug level - 0 off; 1 normal; 2 verbose; 3 very verbose");57#endif5859/* --- setting states on the bus with the right timing: --------------- */6061#define setsda(adap, val) adap->setsda(adap->data, val)62#define setscl(adap, val) adap->setscl(adap->data, val)63#define getsda(adap) adap->getsda(adap->data)64#define getscl(adap) adap->getscl(adap->data)6566static inline void sdalo(struct i2c_algo_bit_data *adap)67{68setsda(adap, 0);69udelay((adap->udelay + 1) / 2);70}7172static inline void sdahi(struct i2c_algo_bit_data *adap)73{74setsda(adap, 1);75udelay((adap->udelay + 1) / 2);76}7778static inline void scllo(struct i2c_algo_bit_data *adap)79{80setscl(adap, 0);81udelay(adap->udelay / 2);82}8384/*85* Raise scl line, and do checking for delays. This is necessary for slower86* devices.87*/88static int sclhi(struct i2c_algo_bit_data *adap)89{90unsigned long start;9192setscl(adap, 1);9394/* Not all adapters have scl sense line... */95if (!adap->getscl)96goto done;9798start = jiffies;99while (!getscl(adap)) {100/* This hw knows how to read the clock line, so we wait101* until it actually gets high. This is safer as some102* chips may hold it low ("clock stretching") while they103* are processing data internally.104*/105if (time_after(jiffies, start + adap->timeout))106return -ETIMEDOUT;107cond_resched();108}109#ifdef DEBUG110if (jiffies != start && i2c_debug >= 3)111pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go "112"high\n", jiffies - start);113#endif114115done:116udelay(adap->udelay);117return 0;118}119120121/* --- other auxiliary functions -------------------------------------- */122static void i2c_start(struct i2c_algo_bit_data *adap)123{124/* assert: scl, sda are high */125setsda(adap, 0);126udelay(adap->udelay);127scllo(adap);128}129130static void i2c_repstart(struct i2c_algo_bit_data *adap)131{132/* assert: scl is low */133sdahi(adap);134sclhi(adap);135setsda(adap, 0);136udelay(adap->udelay);137scllo(adap);138}139140141static void i2c_stop(struct i2c_algo_bit_data *adap)142{143/* assert: scl is low */144sdalo(adap);145sclhi(adap);146setsda(adap, 1);147udelay(adap->udelay);148}149150151152/* send a byte without start cond., look for arbitration,153check ackn. from slave */154/* returns:155* 1 if the device acknowledged156* 0 if the device did not ack157* -ETIMEDOUT if an error occurred (while raising the scl line)158*/159static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c)160{161int i;162int sb;163int ack;164struct i2c_algo_bit_data *adap = i2c_adap->algo_data;165166/* assert: scl is low */167for (i = 7; i >= 0; i--) {168sb = (c >> i) & 1;169setsda(adap, sb);170udelay((adap->udelay + 1) / 2);171if (sclhi(adap) < 0) { /* timed out */172bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "173"timeout at bit #%d\n", (int)c, i);174return -ETIMEDOUT;175}176/* FIXME do arbitration here:177* if (sb && !getsda(adap)) -> ouch! Get out of here.178*179* Report a unique code, so higher level code can retry180* the whole (combined) message and *NOT* issue STOP.181*/182scllo(adap);183}184sdahi(adap);185if (sclhi(adap) < 0) { /* timeout */186bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "187"timeout at ack\n", (int)c);188return -ETIMEDOUT;189}190191/* read ack: SDA should be pulled down by slave, or it may192* NAK (usually to report problems with the data we wrote).193*/194ack = !getsda(adap); /* ack: sda is pulled low -> success */195bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c,196ack ? "A" : "NA");197198scllo(adap);199return ack;200/* assert: scl is low (sda undef) */201}202203204static int i2c_inb(struct i2c_adapter *i2c_adap)205{206/* read byte via i2c port, without start/stop sequence */207/* acknowledge is sent in i2c_read. */208int i;209unsigned char indata = 0;210struct i2c_algo_bit_data *adap = i2c_adap->algo_data;211212/* assert: scl is low */213sdahi(adap);214for (i = 0; i < 8; i++) {215if (sclhi(adap) < 0) { /* timeout */216bit_dbg(1, &i2c_adap->dev, "i2c_inb: timeout at bit "217"#%d\n", 7 - i);218return -ETIMEDOUT;219}220indata *= 2;221if (getsda(adap))222indata |= 0x01;223setscl(adap, 0);224udelay(i == 7 ? adap->udelay / 2 : adap->udelay);225}226/* assert: scl is low */227return indata;228}229230/*231* Sanity check for the adapter hardware - check the reaction of232* the bus lines only if it seems to be idle.233*/234static int test_bus(struct i2c_adapter *i2c_adap)235{236struct i2c_algo_bit_data *adap = i2c_adap->algo_data;237const char *name = i2c_adap->name;238int scl, sda, ret;239240if (adap->pre_xfer) {241ret = adap->pre_xfer(i2c_adap);242if (ret < 0)243return -ENODEV;244}245246if (adap->getscl == NULL)247pr_info("%s: Testing SDA only, SCL is not readable\n", name);248249sda = getsda(adap);250scl = (adap->getscl == NULL) ? 1 : getscl(adap);251if (!scl || !sda) {252printk(KERN_WARNING "%s: bus seems to be busy\n", name);253goto bailout;254}255256sdalo(adap);257sda = getsda(adap);258scl = (adap->getscl == NULL) ? 1 : getscl(adap);259if (sda) {260printk(KERN_WARNING "%s: SDA stuck high!\n", name);261goto bailout;262}263if (!scl) {264printk(KERN_WARNING "%s: SCL unexpected low "265"while pulling SDA low!\n", name);266goto bailout;267}268269sdahi(adap);270sda = getsda(adap);271scl = (adap->getscl == NULL) ? 1 : getscl(adap);272if (!sda) {273printk(KERN_WARNING "%s: SDA stuck low!\n", name);274goto bailout;275}276if (!scl) {277printk(KERN_WARNING "%s: SCL unexpected low "278"while pulling SDA high!\n", name);279goto bailout;280}281282scllo(adap);283sda = getsda(adap);284scl = (adap->getscl == NULL) ? 0 : getscl(adap);285if (scl) {286printk(KERN_WARNING "%s: SCL stuck high!\n", name);287goto bailout;288}289if (!sda) {290printk(KERN_WARNING "%s: SDA unexpected low "291"while pulling SCL low!\n", name);292goto bailout;293}294295sclhi(adap);296sda = getsda(adap);297scl = (adap->getscl == NULL) ? 1 : getscl(adap);298if (!scl) {299printk(KERN_WARNING "%s: SCL stuck low!\n", name);300goto bailout;301}302if (!sda) {303printk(KERN_WARNING "%s: SDA unexpected low "304"while pulling SCL high!\n", name);305goto bailout;306}307308if (adap->post_xfer)309adap->post_xfer(i2c_adap);310311pr_info("%s: Test OK\n", name);312return 0;313bailout:314sdahi(adap);315sclhi(adap);316317if (adap->post_xfer)318adap->post_xfer(i2c_adap);319320return -ENODEV;321}322323/* ----- Utility functions324*/325326/* try_address tries to contact a chip for a number of327* times before it gives up.328* return values:329* 1 chip answered330* 0 chip did not answer331* -x transmission error332*/333static int try_address(struct i2c_adapter *i2c_adap,334unsigned char addr, int retries)335{336struct i2c_algo_bit_data *adap = i2c_adap->algo_data;337int i, ret = 0;338339for (i = 0; i <= retries; i++) {340ret = i2c_outb(i2c_adap, addr);341if (ret == 1 || i == retries)342break;343bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");344i2c_stop(adap);345udelay(adap->udelay);346yield();347bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");348i2c_start(adap);349}350if (i && ret)351bit_dbg(1, &i2c_adap->dev, "Used %d tries to %s client at "352"0x%02x: %s\n", i + 1,353addr & 1 ? "read from" : "write to", addr >> 1,354ret == 1 ? "success" : "failed, timeout?");355return ret;356}357358static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)359{360const unsigned char *temp = msg->buf;361int count = msg->len;362unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;363int retval;364int wrcount = 0;365366while (count > 0) {367retval = i2c_outb(i2c_adap, *temp);368369/* OK/ACK; or ignored NAK */370if ((retval > 0) || (nak_ok && (retval == 0))) {371count--;372temp++;373wrcount++;374375/* A slave NAKing the master means the slave didn't like376* something about the data it saw. For example, maybe377* the SMBus PEC was wrong.378*/379} else if (retval == 0) {380dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");381return -EIO;382383/* Timeout; or (someday) lost arbitration384*385* FIXME Lost ARB implies retrying the transaction from386* the first message, after the "winning" master issues387* its STOP. As a rule, upper layer code has no reason388* to know or care about this ... it is *NOT* an error.389*/390} else {391dev_err(&i2c_adap->dev, "sendbytes: error %d\n",392retval);393return retval;394}395}396return wrcount;397}398399static int acknak(struct i2c_adapter *i2c_adap, int is_ack)400{401struct i2c_algo_bit_data *adap = i2c_adap->algo_data;402403/* assert: sda is high */404if (is_ack) /* send ack */405setsda(adap, 0);406udelay((adap->udelay + 1) / 2);407if (sclhi(adap) < 0) { /* timeout */408dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");409return -ETIMEDOUT;410}411scllo(adap);412return 0;413}414415static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)416{417int inval;418int rdcount = 0; /* counts bytes read */419unsigned char *temp = msg->buf;420int count = msg->len;421const unsigned flags = msg->flags;422423while (count > 0) {424inval = i2c_inb(i2c_adap);425if (inval >= 0) {426*temp = inval;427rdcount++;428} else { /* read timed out */429break;430}431432temp++;433count--;434435/* Some SMBus transactions require that we receive the436transaction length as the first read byte. */437if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {438if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {439if (!(flags & I2C_M_NO_RD_ACK))440acknak(i2c_adap, 0);441dev_err(&i2c_adap->dev, "readbytes: invalid "442"block length (%d)\n", inval);443return -EREMOTEIO;444}445/* The original count value accounts for the extra446bytes, that is, either 1 for a regular transaction,447or 2 for a PEC transaction. */448count += inval;449msg->len += inval;450}451452bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",453inval,454(flags & I2C_M_NO_RD_ACK)455? "(no ack/nak)"456: (count ? "A" : "NA"));457458if (!(flags & I2C_M_NO_RD_ACK)) {459inval = acknak(i2c_adap, count);460if (inval < 0)461return inval;462}463}464return rdcount;465}466467/* doAddress initiates the transfer by generating the start condition (in468* try_address) and transmits the address in the necessary format to handle469* reads, writes as well as 10bit-addresses.470* returns:471* 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set472* -x an error occurred (like: -EREMOTEIO if the device did not answer, or473* -ETIMEDOUT, for example if the lines are stuck...)474*/475static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)476{477unsigned short flags = msg->flags;478unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;479struct i2c_algo_bit_data *adap = i2c_adap->algo_data;480481unsigned char addr;482int ret, retries;483484retries = nak_ok ? 0 : i2c_adap->retries;485486if (flags & I2C_M_TEN) {487/* a ten bit address */488addr = 0xf0 | ((msg->addr >> 7) & 0x03);489bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);490/* try extended address code...*/491ret = try_address(i2c_adap, addr, retries);492if ((ret != 1) && !nak_ok) {493dev_err(&i2c_adap->dev,494"died at extended address code\n");495return -EREMOTEIO;496}497/* the remaining 8 bit address */498ret = i2c_outb(i2c_adap, msg->addr & 0x7f);499if ((ret != 1) && !nak_ok) {500/* the chip did not ack / xmission error occurred */501dev_err(&i2c_adap->dev, "died at 2nd address code\n");502return -EREMOTEIO;503}504if (flags & I2C_M_RD) {505bit_dbg(3, &i2c_adap->dev, "emitting repeated "506"start condition\n");507i2c_repstart(adap);508/* okay, now switch into reading mode */509addr |= 0x01;510ret = try_address(i2c_adap, addr, retries);511if ((ret != 1) && !nak_ok) {512dev_err(&i2c_adap->dev,513"died at repeated address code\n");514return -EREMOTEIO;515}516}517} else { /* normal 7bit address */518addr = msg->addr << 1;519if (flags & I2C_M_RD)520addr |= 1;521if (flags & I2C_M_REV_DIR_ADDR)522addr ^= 1;523ret = try_address(i2c_adap, addr, retries);524if ((ret != 1) && !nak_ok)525return -ENXIO;526}527528return 0;529}530531static int bit_xfer(struct i2c_adapter *i2c_adap,532struct i2c_msg msgs[], int num)533{534struct i2c_msg *pmsg;535struct i2c_algo_bit_data *adap = i2c_adap->algo_data;536int i, ret;537unsigned short nak_ok;538539if (adap->pre_xfer) {540ret = adap->pre_xfer(i2c_adap);541if (ret < 0)542return ret;543}544545bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");546i2c_start(adap);547for (i = 0; i < num; i++) {548pmsg = &msgs[i];549nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;550if (!(pmsg->flags & I2C_M_NOSTART)) {551if (i) {552bit_dbg(3, &i2c_adap->dev, "emitting "553"repeated start condition\n");554i2c_repstart(adap);555}556ret = bit_doAddress(i2c_adap, pmsg);557if ((ret != 0) && !nak_ok) {558bit_dbg(1, &i2c_adap->dev, "NAK from "559"device addr 0x%02x msg #%d\n",560msgs[i].addr, i);561goto bailout;562}563}564if (pmsg->flags & I2C_M_RD) {565/* read bytes into buffer*/566ret = readbytes(i2c_adap, pmsg);567if (ret >= 1)568bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",569ret, ret == 1 ? "" : "s");570if (ret < pmsg->len) {571if (ret >= 0)572ret = -EREMOTEIO;573goto bailout;574}575} else {576/* write bytes from buffer */577ret = sendbytes(i2c_adap, pmsg);578if (ret >= 1)579bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",580ret, ret == 1 ? "" : "s");581if (ret < pmsg->len) {582if (ret >= 0)583ret = -EREMOTEIO;584goto bailout;585}586}587}588ret = i;589590bailout:591bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");592i2c_stop(adap);593594if (adap->post_xfer)595adap->post_xfer(i2c_adap);596return ret;597}598599static u32 bit_func(struct i2c_adapter *adap)600{601return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |602I2C_FUNC_SMBUS_READ_BLOCK_DATA |603I2C_FUNC_SMBUS_BLOCK_PROC_CALL |604I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;605}606607608/* -----exported algorithm data: ------------------------------------- */609610static const struct i2c_algorithm i2c_bit_algo = {611.master_xfer = bit_xfer,612.functionality = bit_func,613};614615/*616* registering functions to load algorithms at runtime617*/618static int __i2c_bit_add_bus(struct i2c_adapter *adap,619int (*add_adapter)(struct i2c_adapter *))620{621struct i2c_algo_bit_data *bit_adap = adap->algo_data;622int ret;623624if (bit_test) {625ret = test_bus(adap);626if (ret < 0)627return -ENODEV;628}629630/* register new adapter to i2c module... */631adap->algo = &i2c_bit_algo;632adap->retries = 3;633634ret = add_adapter(adap);635if (ret < 0)636return ret;637638/* Complain if SCL can't be read */639if (bit_adap->getscl == NULL) {640dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n");641dev_warn(&adap->dev, "Bus may be unreliable\n");642}643return 0;644}645646int i2c_bit_add_bus(struct i2c_adapter *adap)647{648return __i2c_bit_add_bus(adap, i2c_add_adapter);649}650EXPORT_SYMBOL(i2c_bit_add_bus);651652int i2c_bit_add_numbered_bus(struct i2c_adapter *adap)653{654return __i2c_bit_add_bus(adap, i2c_add_numbered_adapter);655}656EXPORT_SYMBOL(i2c_bit_add_numbered_bus);657658MODULE_AUTHOR("Simon G. Vogl <[email protected]>");659MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");660MODULE_LICENSE("GPL");661662663