Path: blob/master/drivers/i2c/algos/i2c-algo-pca.c
17533 views
/*1* i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters2* Copyright (C) 2004 Arcom Control Systems3* Copyright (C) 2008 Pengutronix4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18*/1920#include <linux/kernel.h>21#include <linux/module.h>22#include <linux/moduleparam.h>23#include <linux/delay.h>24#include <linux/jiffies.h>25#include <linux/init.h>26#include <linux/errno.h>27#include <linux/i2c.h>28#include <linux/i2c-algo-pca.h>2930#define DEB1(fmt, args...) do { if (i2c_debug >= 1) \31printk(KERN_DEBUG fmt, ## args); } while (0)32#define DEB2(fmt, args...) do { if (i2c_debug >= 2) \33printk(KERN_DEBUG fmt, ## args); } while (0)34#define DEB3(fmt, args...) do { if (i2c_debug >= 3) \35printk(KERN_DEBUG fmt, ## args); } while (0)3637static int i2c_debug;3839#define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)40#define pca_inw(adap, reg) adap->read_byte(adap->data, reg)4142#define pca_status(adap) pca_inw(adap, I2C_PCA_STA)43#define pca_clock(adap) adap->i2c_clock44#define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)45#define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)46#define pca_wait(adap) adap->wait_for_completion(adap->data)47#define pca_reset(adap) adap->reset_chip(adap->data)4849static void pca9665_reset(void *pd)50{51struct i2c_algo_pca_data *adap = pd;52pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IPRESET);53pca_outw(adap, I2C_PCA_IND, 0xA5);54pca_outw(adap, I2C_PCA_IND, 0x5A);55}5657/*58* Generate a start condition on the i2c bus.59*60* returns after the start condition has occurred61*/62static int pca_start(struct i2c_algo_pca_data *adap)63{64int sta = pca_get_con(adap);65DEB2("=== START\n");66sta |= I2C_PCA_CON_STA;67sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);68pca_set_con(adap, sta);69return pca_wait(adap);70}7172/*73* Generate a repeated start condition on the i2c bus74*75* return after the repeated start condition has occurred76*/77static int pca_repeated_start(struct i2c_algo_pca_data *adap)78{79int sta = pca_get_con(adap);80DEB2("=== REPEATED START\n");81sta |= I2C_PCA_CON_STA;82sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);83pca_set_con(adap, sta);84return pca_wait(adap);85}8687/*88* Generate a stop condition on the i2c bus89*90* returns after the stop condition has been generated91*92* STOPs do not generate an interrupt or set the SI flag, since the93* part returns the idle state (0xf8). Hence we don't need to94* pca_wait here.95*/96static void pca_stop(struct i2c_algo_pca_data *adap)97{98int sta = pca_get_con(adap);99DEB2("=== STOP\n");100sta |= I2C_PCA_CON_STO;101sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);102pca_set_con(adap, sta);103}104105/*106* Send the slave address and R/W bit107*108* returns after the address has been sent109*/110static int pca_address(struct i2c_algo_pca_data *adap,111struct i2c_msg *msg)112{113int sta = pca_get_con(adap);114int addr;115116addr = ((0x7f & msg->addr) << 1);117if (msg->flags & I2C_M_RD)118addr |= 1;119DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",120msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);121122pca_outw(adap, I2C_PCA_DAT, addr);123124sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);125pca_set_con(adap, sta);126127return pca_wait(adap);128}129130/*131* Transmit a byte.132*133* Returns after the byte has been transmitted134*/135static int pca_tx_byte(struct i2c_algo_pca_data *adap,136__u8 b)137{138int sta = pca_get_con(adap);139DEB2("=== WRITE %#04x\n", b);140pca_outw(adap, I2C_PCA_DAT, b);141142sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);143pca_set_con(adap, sta);144145return pca_wait(adap);146}147148/*149* Receive a byte150*151* returns immediately.152*/153static void pca_rx_byte(struct i2c_algo_pca_data *adap,154__u8 *b, int ack)155{156*b = pca_inw(adap, I2C_PCA_DAT);157DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");158}159160/*161* Setup ACK or NACK for next received byte and wait for it to arrive.162*163* Returns after next byte has arrived.164*/165static int pca_rx_ack(struct i2c_algo_pca_data *adap,166int ack)167{168int sta = pca_get_con(adap);169170sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);171172if (ack)173sta |= I2C_PCA_CON_AA;174175pca_set_con(adap, sta);176return pca_wait(adap);177}178179static int pca_xfer(struct i2c_adapter *i2c_adap,180struct i2c_msg *msgs,181int num)182{183struct i2c_algo_pca_data *adap = i2c_adap->algo_data;184struct i2c_msg *msg = NULL;185int curmsg;186int numbytes = 0;187int state;188int ret;189int completed = 1;190unsigned long timeout = jiffies + i2c_adap->timeout;191192while ((state = pca_status(adap)) != 0xf8) {193if (time_before(jiffies, timeout)) {194msleep(10);195} else {196dev_dbg(&i2c_adap->dev, "bus is not idle. status is "197"%#04x\n", state);198return -EAGAIN;199}200}201202DEB1("{{{ XFER %d messages\n", num);203204if (i2c_debug >= 2) {205for (curmsg = 0; curmsg < num; curmsg++) {206int addr, i;207msg = &msgs[curmsg];208209addr = (0x7f & msg->addr) ;210211if (msg->flags & I2C_M_RD)212printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",213curmsg, msg->len, addr, (addr << 1) | 1);214else {215printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",216curmsg, msg->len, addr, addr << 1,217msg->len == 0 ? "" : ", ");218for (i = 0; i < msg->len; i++)219printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");220printk("]\n");221}222}223}224225curmsg = 0;226ret = -EREMOTEIO;227while (curmsg < num) {228state = pca_status(adap);229230DEB3("STATE is 0x%02x\n", state);231msg = &msgs[curmsg];232233switch (state) {234case 0xf8: /* On reset or stop the bus is idle */235completed = pca_start(adap);236break;237238case 0x08: /* A START condition has been transmitted */239case 0x10: /* A repeated start condition has been transmitted */240completed = pca_address(adap, msg);241break;242243case 0x18: /* SLA+W has been transmitted; ACK has been received */244case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */245if (numbytes < msg->len) {246completed = pca_tx_byte(adap,247msg->buf[numbytes]);248numbytes++;249break;250}251curmsg++; numbytes = 0;252if (curmsg == num)253pca_stop(adap);254else255completed = pca_repeated_start(adap);256break;257258case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */259DEB2("NOT ACK received after SLA+W\n");260pca_stop(adap);261goto out;262263case 0x40: /* SLA+R has been transmitted; ACK has been received */264completed = pca_rx_ack(adap, msg->len > 1);265break;266267case 0x50: /* Data bytes has been received; ACK has been returned */268if (numbytes < msg->len) {269pca_rx_byte(adap, &msg->buf[numbytes], 1);270numbytes++;271completed = pca_rx_ack(adap,272numbytes < msg->len - 1);273break;274}275curmsg++; numbytes = 0;276if (curmsg == num)277pca_stop(adap);278else279completed = pca_repeated_start(adap);280break;281282case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */283DEB2("NOT ACK received after SLA+R\n");284pca_stop(adap);285goto out;286287case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */288DEB2("NOT ACK received after data byte\n");289pca_stop(adap);290goto out;291292case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */293DEB2("Arbitration lost\n");294/*295* The PCA9564 data sheet (2006-09-01) says "A296* START condition will be transmitted when the297* bus becomes free (STOP or SCL and SDA high)"298* when the STA bit is set (p. 11).299*300* In case this won't work, try pca_reset()301* instead.302*/303pca_start(adap);304goto out;305306case 0x58: /* Data byte has been received; NOT ACK has been returned */307if (numbytes == msg->len - 1) {308pca_rx_byte(adap, &msg->buf[numbytes], 0);309curmsg++; numbytes = 0;310if (curmsg == num)311pca_stop(adap);312else313completed = pca_repeated_start(adap);314} else {315DEB2("NOT ACK sent after data byte received. "316"Not final byte. numbytes %d. len %d\n",317numbytes, msg->len);318pca_stop(adap);319goto out;320}321break;322case 0x70: /* Bus error - SDA stuck low */323DEB2("BUS ERROR - SDA Stuck low\n");324pca_reset(adap);325goto out;326case 0x90: /* Bus error - SCL stuck low */327DEB2("BUS ERROR - SCL Stuck low\n");328pca_reset(adap);329goto out;330case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */331DEB2("BUS ERROR - Illegal START or STOP\n");332pca_reset(adap);333goto out;334default:335dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);336break;337}338339if (!completed)340goto out;341}342343ret = curmsg;344out:345DEB1("}}} transferred %d/%d messages. "346"status is %#04x. control is %#04x\n",347curmsg, num, pca_status(adap),348pca_get_con(adap));349return ret;350}351352static u32 pca_func(struct i2c_adapter *adap)353{354return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;355}356357static const struct i2c_algorithm pca_algo = {358.master_xfer = pca_xfer,359.functionality = pca_func,360};361362static unsigned int pca_probe_chip(struct i2c_adapter *adap)363{364struct i2c_algo_pca_data *pca_data = adap->algo_data;365/* The trick here is to check if there is an indirect register366* available. If there is one, we will read the value we first367* wrote on I2C_PCA_IADR. Otherwise, we will read the last value368* we wrote on I2C_PCA_ADR369*/370pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);371pca_outw(pca_data, I2C_PCA_IND, 0xAA);372pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ITO);373pca_outw(pca_data, I2C_PCA_IND, 0x00);374pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);375if (pca_inw(pca_data, I2C_PCA_IND) == 0xAA) {376printk(KERN_INFO "%s: PCA9665 detected.\n", adap->name);377return I2C_PCA_CHIP_9665;378} else {379printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name);380return I2C_PCA_CHIP_9564;381}382}383384static int pca_init(struct i2c_adapter *adap)385{386struct i2c_algo_pca_data *pca_data = adap->algo_data;387388adap->algo = &pca_algo;389390if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) {391static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};392int clock;393394if (pca_data->i2c_clock > 7) {395switch (pca_data->i2c_clock) {396case 330000:397pca_data->i2c_clock = I2C_PCA_CON_330kHz;398break;399case 288000:400pca_data->i2c_clock = I2C_PCA_CON_288kHz;401break;402case 217000:403pca_data->i2c_clock = I2C_PCA_CON_217kHz;404break;405case 146000:406pca_data->i2c_clock = I2C_PCA_CON_146kHz;407break;408case 88000:409pca_data->i2c_clock = I2C_PCA_CON_88kHz;410break;411case 59000:412pca_data->i2c_clock = I2C_PCA_CON_59kHz;413break;414case 44000:415pca_data->i2c_clock = I2C_PCA_CON_44kHz;416break;417case 36000:418pca_data->i2c_clock = I2C_PCA_CON_36kHz;419break;420default:421printk(KERN_WARNING422"%s: Invalid I2C clock speed selected."423" Using default 59kHz.\n", adap->name);424pca_data->i2c_clock = I2C_PCA_CON_59kHz;425}426} else {427printk(KERN_WARNING "%s: "428"Choosing the clock frequency based on "429"index is deprecated."430" Use the nominal frequency.\n", adap->name);431}432433pca_reset(pca_data);434435clock = pca_clock(pca_data);436printk(KERN_INFO "%s: Clock frequency is %dkHz\n",437adap->name, freqs[clock]);438439pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);440} else {441int clock;442int mode;443int tlow, thi;444/* Values can be found on PCA9665 datasheet section 7.3.2.6 */445int min_tlow, min_thi;446/* These values are the maximum raise and fall values allowed447* by the I2C operation mode (Standard, Fast or Fast+)448* They are used (added) below to calculate the clock dividers449* of PCA9665. Note that they are slightly different of the450* real maximum, to allow the change on mode exactly on the451* maximum clock rate for each mode452*/453int raise_fall_time;454455/* Ignore the reset function from the module,456* we can use the parallel bus reset457*/458pca_data->reset_chip = pca9665_reset;459460if (pca_data->i2c_clock > 1265800) {461printk(KERN_WARNING "%s: I2C clock speed too high."462" Using 1265.8kHz.\n", adap->name);463pca_data->i2c_clock = 1265800;464}465466if (pca_data->i2c_clock < 60300) {467printk(KERN_WARNING "%s: I2C clock speed too low."468" Using 60.3kHz.\n", adap->name);469pca_data->i2c_clock = 60300;470}471472/* To avoid integer overflow, use clock/100 for calculations */473clock = pca_clock(pca_data) / 100;474475if (pca_data->i2c_clock > 10000) {476mode = I2C_PCA_MODE_TURBO;477min_tlow = 14;478min_thi = 5;479raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */480} else if (pca_data->i2c_clock > 4000) {481mode = I2C_PCA_MODE_FASTP;482min_tlow = 17;483min_thi = 9;484raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */485} else if (pca_data->i2c_clock > 1000) {486mode = I2C_PCA_MODE_FAST;487min_tlow = 44;488min_thi = 20;489raise_fall_time = 58; /* Raise 29e-8s, Fall 29e-8s */490} else {491mode = I2C_PCA_MODE_STD;492min_tlow = 157;493min_thi = 134;494raise_fall_time = 127; /* Raise 29e-8s, Fall 98e-8s */495}496497/* The minimum clock that respects the thi/tlow = 134/157 is498* 64800 Hz. Below that, we have to fix the tlow to 255 and499* calculate the thi factor.500*/501if (clock < 648) {502tlow = 255;503thi = 1000000 - clock * raise_fall_time;504thi /= (I2C_PCA_OSC_PER * clock) - tlow;505} else {506tlow = (1000000 - clock * raise_fall_time) * min_tlow;507tlow /= I2C_PCA_OSC_PER * clock * (min_thi + min_tlow);508thi = tlow * min_thi / min_tlow;509}510511pca_reset(pca_data);512513printk(KERN_INFO514"%s: Clock frequency is %dHz\n", adap->name, clock * 100);515516pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IMODE);517pca_outw(pca_data, I2C_PCA_IND, mode);518pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLL);519pca_outw(pca_data, I2C_PCA_IND, tlow);520pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLH);521pca_outw(pca_data, I2C_PCA_IND, thi);522523pca_set_con(pca_data, I2C_PCA_CON_ENSIO);524}525udelay(500); /* 500 us for oscilator to stabilise */526527return 0;528}529530/*531* registering functions to load algorithms at runtime532*/533int i2c_pca_add_bus(struct i2c_adapter *adap)534{535int rval;536537rval = pca_init(adap);538if (rval)539return rval;540541return i2c_add_adapter(adap);542}543EXPORT_SYMBOL(i2c_pca_add_bus);544545int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)546{547int rval;548549rval = pca_init(adap);550if (rval)551return rval;552553return i2c_add_numbered_adapter(adap);554}555EXPORT_SYMBOL(i2c_pca_add_numbered_bus);556557MODULE_AUTHOR("Ian Campbell <[email protected]>, "558"Wolfram Sang <[email protected]>");559MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");560MODULE_LICENSE("GPL");561562module_param(i2c_debug, int, 0);563564565