Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
0004  *    Copyright (C) 2004 Arcom Control Systems
0005  *    Copyright (C) 2008 Pengutronix
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/delay.h>
0012 #include <linux/jiffies.h>
0013 #include <linux/errno.h>
0014 #include <linux/i2c.h>
0015 #include <linux/i2c-algo-pca.h>
0016 
0017 #define DEB1(fmt, args...) do { if (i2c_debug >= 1)         \
0018                  printk(KERN_DEBUG fmt, ## args); } while (0)
0019 #define DEB2(fmt, args...) do { if (i2c_debug >= 2)         \
0020                  printk(KERN_DEBUG fmt, ## args); } while (0)
0021 #define DEB3(fmt, args...) do { if (i2c_debug >= 3)         \
0022                  printk(KERN_DEBUG fmt, ## args); } while (0)
0023 
0024 static int i2c_debug;
0025 
0026 #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
0027 #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
0028 
0029 #define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
0030 #define pca_clock(adap) adap->i2c_clock
0031 #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
0032 #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
0033 #define pca_wait(adap) adap->wait_for_completion(adap->data)
0034 
0035 static void pca_reset(struct i2c_algo_pca_data *adap)
0036 {
0037     if (adap->chip == I2C_PCA_CHIP_9665) {
0038         /* Ignore the reset function from the module,
0039          * we can use the parallel bus reset.
0040          */
0041         pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IPRESET);
0042         pca_outw(adap, I2C_PCA_IND, 0xA5);
0043         pca_outw(adap, I2C_PCA_IND, 0x5A);
0044 
0045         /*
0046          * After a reset we need to re-apply any configuration
0047          * (calculated in pca_init) to get the bus in a working state.
0048          */
0049         pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IMODE);
0050         pca_outw(adap, I2C_PCA_IND, adap->bus_settings.mode);
0051         pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_ISCLL);
0052         pca_outw(adap, I2C_PCA_IND, adap->bus_settings.tlow);
0053         pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_ISCLH);
0054         pca_outw(adap, I2C_PCA_IND, adap->bus_settings.thi);
0055 
0056         pca_set_con(adap, I2C_PCA_CON_ENSIO);
0057     } else {
0058         adap->reset_chip(adap->data);
0059         pca_set_con(adap, I2C_PCA_CON_ENSIO | adap->bus_settings.clock_freq);
0060     }
0061 }
0062 
0063 /*
0064  * Generate a start condition on the i2c bus.
0065  *
0066  * returns after the start condition has occurred
0067  */
0068 static int pca_start(struct i2c_algo_pca_data *adap)
0069 {
0070     int sta = pca_get_con(adap);
0071     DEB2("=== START\n");
0072     sta |= I2C_PCA_CON_STA;
0073     sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
0074     pca_set_con(adap, sta);
0075     return pca_wait(adap);
0076 }
0077 
0078 /*
0079  * Generate a repeated start condition on the i2c bus
0080  *
0081  * return after the repeated start condition has occurred
0082  */
0083 static int pca_repeated_start(struct i2c_algo_pca_data *adap)
0084 {
0085     int sta = pca_get_con(adap);
0086     DEB2("=== REPEATED START\n");
0087     sta |= I2C_PCA_CON_STA;
0088     sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
0089     pca_set_con(adap, sta);
0090     return pca_wait(adap);
0091 }
0092 
0093 /*
0094  * Generate a stop condition on the i2c bus
0095  *
0096  * returns after the stop condition has been generated
0097  *
0098  * STOPs do not generate an interrupt or set the SI flag, since the
0099  * part returns the idle state (0xf8). Hence we don't need to
0100  * pca_wait here.
0101  */
0102 static void pca_stop(struct i2c_algo_pca_data *adap)
0103 {
0104     int sta = pca_get_con(adap);
0105     DEB2("=== STOP\n");
0106     sta |= I2C_PCA_CON_STO;
0107     sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
0108     pca_set_con(adap, sta);
0109 }
0110 
0111 /*
0112  * Send the slave address and R/W bit
0113  *
0114  * returns after the address has been sent
0115  */
0116 static int pca_address(struct i2c_algo_pca_data *adap,
0117                struct i2c_msg *msg)
0118 {
0119     int sta = pca_get_con(adap);
0120     int addr = i2c_8bit_addr_from_msg(msg);
0121 
0122     DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
0123          msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
0124 
0125     pca_outw(adap, I2C_PCA_DAT, addr);
0126 
0127     sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
0128     pca_set_con(adap, sta);
0129 
0130     return pca_wait(adap);
0131 }
0132 
0133 /*
0134  * Transmit a byte.
0135  *
0136  * Returns after the byte has been transmitted
0137  */
0138 static int pca_tx_byte(struct i2c_algo_pca_data *adap,
0139                __u8 b)
0140 {
0141     int sta = pca_get_con(adap);
0142     DEB2("=== WRITE %#04x\n", b);
0143     pca_outw(adap, I2C_PCA_DAT, b);
0144 
0145     sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
0146     pca_set_con(adap, sta);
0147 
0148     return pca_wait(adap);
0149 }
0150 
0151 /*
0152  * Receive a byte
0153  *
0154  * returns immediately.
0155  */
0156 static void pca_rx_byte(struct i2c_algo_pca_data *adap,
0157             __u8 *b, int ack)
0158 {
0159     *b = pca_inw(adap, I2C_PCA_DAT);
0160     DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
0161 }
0162 
0163 /*
0164  * Setup ACK or NACK for next received byte and wait for it to arrive.
0165  *
0166  * Returns after next byte has arrived.
0167  */
0168 static int pca_rx_ack(struct i2c_algo_pca_data *adap,
0169               int ack)
0170 {
0171     int sta = pca_get_con(adap);
0172 
0173     sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
0174 
0175     if (ack)
0176         sta |= I2C_PCA_CON_AA;
0177 
0178     pca_set_con(adap, sta);
0179     return pca_wait(adap);
0180 }
0181 
0182 static int pca_xfer(struct i2c_adapter *i2c_adap,
0183             struct i2c_msg *msgs,
0184             int num)
0185 {
0186     struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
0187     struct i2c_msg *msg = NULL;
0188     int curmsg;
0189     int numbytes = 0;
0190     int state;
0191     int ret;
0192     int completed = 1;
0193     unsigned long timeout = jiffies + i2c_adap->timeout;
0194 
0195     while ((state = pca_status(adap)) != 0xf8) {
0196         if (time_before(jiffies, timeout)) {
0197             msleep(10);
0198         } else {
0199             dev_dbg(&i2c_adap->dev, "bus is not idle. status is "
0200                 "%#04x\n", state);
0201             return -EBUSY;
0202         }
0203     }
0204 
0205     DEB1("{{{ XFER %d messages\n", num);
0206 
0207     if (i2c_debug >= 2) {
0208         for (curmsg = 0; curmsg < num; curmsg++) {
0209             int addr, i;
0210             msg = &msgs[curmsg];
0211 
0212             addr = (0x7f & msg->addr) ;
0213 
0214             if (msg->flags & I2C_M_RD)
0215                 printk(KERN_INFO "    [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
0216                        curmsg, msg->len, addr, (addr << 1) | 1);
0217             else {
0218                 printk(KERN_INFO "    [%02d] WR %d bytes to %#02x [%#02x%s",
0219                        curmsg, msg->len, addr, addr << 1,
0220                        msg->len == 0 ? "" : ", ");
0221                 for (i = 0; i < msg->len; i++)
0222                     printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
0223                 printk("]\n");
0224             }
0225         }
0226     }
0227 
0228     curmsg = 0;
0229     ret = -EIO;
0230     while (curmsg < num) {
0231         state = pca_status(adap);
0232 
0233         DEB3("STATE is 0x%02x\n", state);
0234         msg = &msgs[curmsg];
0235 
0236         switch (state) {
0237         case 0xf8: /* On reset or stop the bus is idle */
0238             completed = pca_start(adap);
0239             break;
0240 
0241         case 0x08: /* A START condition has been transmitted */
0242         case 0x10: /* A repeated start condition has been transmitted */
0243             completed = pca_address(adap, msg);
0244             break;
0245 
0246         case 0x18: /* SLA+W has been transmitted; ACK has been received */
0247         case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
0248             if (numbytes < msg->len) {
0249                 completed = pca_tx_byte(adap,
0250                             msg->buf[numbytes]);
0251                 numbytes++;
0252                 break;
0253             }
0254             curmsg++; numbytes = 0;
0255             if (curmsg == num)
0256                 pca_stop(adap);
0257             else
0258                 completed = pca_repeated_start(adap);
0259             break;
0260 
0261         case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
0262             DEB2("NOT ACK received after SLA+W\n");
0263             pca_stop(adap);
0264             ret = -ENXIO;
0265             goto out;
0266 
0267         case 0x40: /* SLA+R has been transmitted; ACK has been received */
0268             completed = pca_rx_ack(adap, msg->len > 1);
0269             break;
0270 
0271         case 0x50: /* Data bytes has been received; ACK has been returned */
0272             if (numbytes < msg->len) {
0273                 pca_rx_byte(adap, &msg->buf[numbytes], 1);
0274                 numbytes++;
0275                 completed = pca_rx_ack(adap,
0276                                numbytes < msg->len - 1);
0277                 break;
0278             }
0279             curmsg++; numbytes = 0;
0280             if (curmsg == num)
0281                 pca_stop(adap);
0282             else
0283                 completed = pca_repeated_start(adap);
0284             break;
0285 
0286         case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
0287             DEB2("NOT ACK received after SLA+R\n");
0288             pca_stop(adap);
0289             ret = -ENXIO;
0290             goto out;
0291 
0292         case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
0293             DEB2("NOT ACK received after data byte\n");
0294             pca_stop(adap);
0295             goto out;
0296 
0297         case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
0298             DEB2("Arbitration lost\n");
0299             /*
0300              * The PCA9564 data sheet (2006-09-01) says "A
0301              * START condition will be transmitted when the
0302              * bus becomes free (STOP or SCL and SDA high)"
0303              * when the STA bit is set (p. 11).
0304              *
0305              * In case this won't work, try pca_reset()
0306              * instead.
0307              */
0308             pca_start(adap);
0309             goto out;
0310 
0311         case 0x58: /* Data byte has been received; NOT ACK has been returned */
0312             if (numbytes == msg->len - 1) {
0313                 pca_rx_byte(adap, &msg->buf[numbytes], 0);
0314                 curmsg++; numbytes = 0;
0315                 if (curmsg == num)
0316                     pca_stop(adap);
0317                 else
0318                     completed = pca_repeated_start(adap);
0319             } else {
0320                 DEB2("NOT ACK sent after data byte received. "
0321                      "Not final byte. numbytes %d. len %d\n",
0322                      numbytes, msg->len);
0323                 pca_stop(adap);
0324                 goto out;
0325             }
0326             break;
0327         case 0x70: /* Bus error - SDA stuck low */
0328             DEB2("BUS ERROR - SDA Stuck low\n");
0329             pca_reset(adap);
0330             goto out;
0331         case 0x78: /* Bus error - SCL stuck low (PCA9665) */
0332         case 0x90: /* Bus error - SCL stuck low (PCA9564) */
0333             DEB2("BUS ERROR - SCL Stuck low\n");
0334             pca_reset(adap);
0335             goto out;
0336         case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
0337             DEB2("BUS ERROR - Illegal START or STOP\n");
0338             pca_reset(adap);
0339             goto out;
0340         default:
0341             dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
0342             break;
0343         }
0344 
0345         if (!completed)
0346             goto out;
0347     }
0348 
0349     ret = curmsg;
0350  out:
0351     DEB1("}}} transferred %d/%d messages. "
0352          "status is %#04x. control is %#04x\n",
0353          curmsg, num, pca_status(adap),
0354          pca_get_con(adap));
0355     return ret;
0356 }
0357 
0358 static u32 pca_func(struct i2c_adapter *adap)
0359 {
0360     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0361 }
0362 
0363 static const struct i2c_algorithm pca_algo = {
0364     .master_xfer    = pca_xfer,
0365     .functionality  = pca_func,
0366 };
0367 
0368 static unsigned int pca_probe_chip(struct i2c_adapter *adap)
0369 {
0370     struct i2c_algo_pca_data *pca_data = adap->algo_data;
0371     /* The trick here is to check if there is an indirect register
0372      * available. If there is one, we will read the value we first
0373      * wrote on I2C_PCA_IADR. Otherwise, we will read the last value
0374      * we wrote on I2C_PCA_ADR
0375      */
0376     pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
0377     pca_outw(pca_data, I2C_PCA_IND, 0xAA);
0378     pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ITO);
0379     pca_outw(pca_data, I2C_PCA_IND, 0x00);
0380     pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
0381     if (pca_inw(pca_data, I2C_PCA_IND) == 0xAA) {
0382         printk(KERN_INFO "%s: PCA9665 detected.\n", adap->name);
0383         pca_data->chip = I2C_PCA_CHIP_9665;
0384     } else {
0385         printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name);
0386         pca_data->chip = I2C_PCA_CHIP_9564;
0387     }
0388     return pca_data->chip;
0389 }
0390 
0391 static int pca_init(struct i2c_adapter *adap)
0392 {
0393     struct i2c_algo_pca_data *pca_data = adap->algo_data;
0394 
0395     adap->algo = &pca_algo;
0396 
0397     if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) {
0398         static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};
0399         int clock;
0400 
0401         if (pca_data->i2c_clock > 7) {
0402             switch (pca_data->i2c_clock) {
0403             case 330000:
0404                 pca_data->i2c_clock = I2C_PCA_CON_330kHz;
0405                 break;
0406             case 288000:
0407                 pca_data->i2c_clock = I2C_PCA_CON_288kHz;
0408                 break;
0409             case 217000:
0410                 pca_data->i2c_clock = I2C_PCA_CON_217kHz;
0411                 break;
0412             case 146000:
0413                 pca_data->i2c_clock = I2C_PCA_CON_146kHz;
0414                 break;
0415             case 88000:
0416                 pca_data->i2c_clock = I2C_PCA_CON_88kHz;
0417                 break;
0418             case 59000:
0419                 pca_data->i2c_clock = I2C_PCA_CON_59kHz;
0420                 break;
0421             case 44000:
0422                 pca_data->i2c_clock = I2C_PCA_CON_44kHz;
0423                 break;
0424             case 36000:
0425                 pca_data->i2c_clock = I2C_PCA_CON_36kHz;
0426                 break;
0427             default:
0428                 printk(KERN_WARNING
0429                     "%s: Invalid I2C clock speed selected."
0430                     " Using default 59kHz.\n", adap->name);
0431             pca_data->i2c_clock = I2C_PCA_CON_59kHz;
0432             }
0433         } else {
0434             printk(KERN_WARNING "%s: "
0435                 "Choosing the clock frequency based on "
0436                 "index is deprecated."
0437                 " Use the nominal frequency.\n", adap->name);
0438         }
0439 
0440         clock = pca_clock(pca_data);
0441         printk(KERN_INFO "%s: Clock frequency is %dkHz\n",
0442              adap->name, freqs[clock]);
0443 
0444         /* Store settings as these will be needed when the PCA chip is reset */
0445         pca_data->bus_settings.clock_freq = clock;
0446 
0447         pca_reset(pca_data);
0448     } else {
0449         int clock;
0450         int mode;
0451         int tlow, thi;
0452         /* Values can be found on PCA9665 datasheet section 7.3.2.6 */
0453         int min_tlow, min_thi;
0454         /* These values are the maximum raise and fall values allowed
0455          * by the I2C operation mode (Standard, Fast or Fast+)
0456          * They are used (added) below to calculate the clock dividers
0457          * of PCA9665. Note that they are slightly different of the
0458          * real maximum, to allow the change on mode exactly on the
0459          * maximum clock rate for each mode
0460          */
0461         int raise_fall_time;
0462 
0463         if (pca_data->i2c_clock > 1265800) {
0464             printk(KERN_WARNING "%s: I2C clock speed too high."
0465                 " Using 1265.8kHz.\n", adap->name);
0466             pca_data->i2c_clock = 1265800;
0467         }
0468 
0469         if (pca_data->i2c_clock < 60300) {
0470             printk(KERN_WARNING "%s: I2C clock speed too low."
0471                 " Using 60.3kHz.\n", adap->name);
0472             pca_data->i2c_clock = 60300;
0473         }
0474 
0475         /* To avoid integer overflow, use clock/100 for calculations */
0476         clock = pca_clock(pca_data) / 100;
0477 
0478         if (pca_data->i2c_clock > I2C_MAX_FAST_MODE_PLUS_FREQ) {
0479             mode = I2C_PCA_MODE_TURBO;
0480             min_tlow = 14;
0481             min_thi  = 5;
0482             raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
0483         } else if (pca_data->i2c_clock > I2C_MAX_FAST_MODE_FREQ) {
0484             mode = I2C_PCA_MODE_FASTP;
0485             min_tlow = 17;
0486             min_thi  = 9;
0487             raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
0488         } else if (pca_data->i2c_clock > I2C_MAX_STANDARD_MODE_FREQ) {
0489             mode = I2C_PCA_MODE_FAST;
0490             min_tlow = 44;
0491             min_thi  = 20;
0492             raise_fall_time = 58; /* Raise 29e-8s, Fall 29e-8s */
0493         } else {
0494             mode = I2C_PCA_MODE_STD;
0495             min_tlow = 157;
0496             min_thi  = 134;
0497             raise_fall_time = 127; /* Raise 29e-8s, Fall 98e-8s */
0498         }
0499 
0500         /* The minimum clock that respects the thi/tlow = 134/157 is
0501          * 64800 Hz. Below that, we have to fix the tlow to 255 and
0502          * calculate the thi factor.
0503          */
0504         if (clock < 648) {
0505             tlow = 255;
0506             thi = 1000000 - clock * raise_fall_time;
0507             thi /= (I2C_PCA_OSC_PER * clock) - tlow;
0508         } else {
0509             tlow = (1000000 - clock * raise_fall_time) * min_tlow;
0510             tlow /= I2C_PCA_OSC_PER * clock * (min_thi + min_tlow);
0511             thi = tlow * min_thi / min_tlow;
0512         }
0513 
0514         /* Store settings as these will be needed when the PCA chip is reset */
0515         pca_data->bus_settings.mode = mode;
0516         pca_data->bus_settings.tlow = tlow;
0517         pca_data->bus_settings.thi = thi;
0518 
0519         pca_reset(pca_data);
0520 
0521         printk(KERN_INFO
0522              "%s: Clock frequency is %dHz\n", adap->name, clock * 100);
0523     }
0524     udelay(500); /* 500 us for oscillator to stabilise */
0525 
0526     return 0;
0527 }
0528 
0529 /*
0530  * registering functions to load algorithms at runtime
0531  */
0532 int i2c_pca_add_bus(struct i2c_adapter *adap)
0533 {
0534     int rval;
0535 
0536     rval = pca_init(adap);
0537     if (rval)
0538         return rval;
0539 
0540     return i2c_add_adapter(adap);
0541 }
0542 EXPORT_SYMBOL(i2c_pca_add_bus);
0543 
0544 int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
0545 {
0546     int rval;
0547 
0548     rval = pca_init(adap);
0549     if (rval)
0550         return rval;
0551 
0552     return i2c_add_numbered_adapter(adap);
0553 }
0554 EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
0555 
0556 MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
0557 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
0558 MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");
0559 MODULE_LICENSE("GPL");
0560 
0561 module_param(i2c_debug, int, 0);