Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * I2C multiplexer driver for PCA9541 bus master selector
0003  *
0004  * Copyright (c) 2010 Ericsson AB.
0005  *
0006  * Author: Guenter Roeck <linux@roeck-us.net>
0007  *
0008  * Derived from:
0009  *  pca954x.c
0010  *
0011  *  Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
0012  *  Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
0013  *
0014  * This file is licensed under the terms of the GNU General Public
0015  * License version 2. This program is licensed "as is" without any
0016  * warranty of any kind, whether express or implied.
0017  */
0018 
0019 #include <linux/bitops.h>
0020 #include <linux/delay.h>
0021 #include <linux/device.h>
0022 #include <linux/i2c.h>
0023 #include <linux/i2c-mux.h>
0024 #include <linux/jiffies.h>
0025 #include <linux/module.h>
0026 #include <linux/slab.h>
0027 
0028 /*
0029  * The PCA9541 is a bus master selector. It supports two I2C masters connected
0030  * to a single slave bus.
0031  *
0032  * Before each bus transaction, a master has to acquire bus ownership. After the
0033  * transaction is complete, bus ownership has to be released. This fits well
0034  * into the I2C multiplexer framework, which provides select and release
0035  * functions for this purpose. For this reason, this driver is modeled as
0036  * single-channel I2C bus multiplexer.
0037  *
0038  * This driver assumes that the two bus masters are controlled by two different
0039  * hosts. If a single host controls both masters, platform code has to ensure
0040  * that only one of the masters is instantiated at any given time.
0041  */
0042 
0043 #define PCA9541_CONTROL     0x01
0044 #define PCA9541_ISTAT       0x02
0045 
0046 #define PCA9541_CTL_MYBUS   BIT(0)
0047 #define PCA9541_CTL_NMYBUS  BIT(1)
0048 #define PCA9541_CTL_BUSON   BIT(2)
0049 #define PCA9541_CTL_NBUSON  BIT(3)
0050 #define PCA9541_CTL_BUSINIT BIT(4)
0051 #define PCA9541_CTL_TESTON  BIT(6)
0052 #define PCA9541_CTL_NTESTON BIT(7)
0053 
0054 #define PCA9541_ISTAT_INTIN BIT(0)
0055 #define PCA9541_ISTAT_BUSINIT   BIT(1)
0056 #define PCA9541_ISTAT_BUSOK BIT(2)
0057 #define PCA9541_ISTAT_BUSLOST   BIT(3)
0058 #define PCA9541_ISTAT_MYTEST    BIT(6)
0059 #define PCA9541_ISTAT_NMYTEST   BIT(7)
0060 
0061 #define BUSON       (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON)
0062 #define MYBUS       (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS)
0063 #define mybus(x)    (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS)
0064 #define busoff(x)   (!((x) & BUSON) || ((x) & BUSON) == BUSON)
0065 
0066 /* arbitration timeouts, in jiffies */
0067 #define ARB_TIMEOUT (HZ / 8)    /* 125 ms until forcing bus ownership */
0068 #define ARB2_TIMEOUT    (HZ / 4)    /* 250 ms until acquisition failure */
0069 
0070 /* arbitration retry delays, in us */
0071 #define SELECT_DELAY_SHORT  50
0072 #define SELECT_DELAY_LONG   1000
0073 
0074 struct pca9541 {
0075     struct i2c_client *client;
0076     unsigned long select_timeout;
0077     unsigned long arb_timeout;
0078 };
0079 
0080 static const struct i2c_device_id pca9541_id[] = {
0081     {"pca9541", 0},
0082     {}
0083 };
0084 
0085 MODULE_DEVICE_TABLE(i2c, pca9541_id);
0086 
0087 #ifdef CONFIG_OF
0088 static const struct of_device_id pca9541_of_match[] = {
0089     { .compatible = "nxp,pca9541" },
0090     {}
0091 };
0092 MODULE_DEVICE_TABLE(of, pca9541_of_match);
0093 #endif
0094 
0095 /*
0096  * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
0097  * as they will try to lock the adapter a second time.
0098  */
0099 static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
0100 {
0101     struct i2c_adapter *adap = client->adapter;
0102     union i2c_smbus_data data = { .byte = val };
0103 
0104     return __i2c_smbus_xfer(adap, client->addr, client->flags,
0105                 I2C_SMBUS_WRITE, command,
0106                 I2C_SMBUS_BYTE_DATA, &data);
0107 }
0108 
0109 /*
0110  * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
0111  * as they will try to lock adapter a second time.
0112  */
0113 static int pca9541_reg_read(struct i2c_client *client, u8 command)
0114 {
0115     struct i2c_adapter *adap = client->adapter;
0116     union i2c_smbus_data data;
0117     int ret;
0118 
0119     ret = __i2c_smbus_xfer(adap, client->addr, client->flags,
0120                    I2C_SMBUS_READ, command,
0121                    I2C_SMBUS_BYTE_DATA, &data);
0122 
0123     return ret ?: data.byte;
0124 }
0125 
0126 /*
0127  * Arbitration management functions
0128  */
0129 
0130 /* Release bus. Also reset NTESTON and BUSINIT if it was set. */
0131 static void pca9541_release_bus(struct i2c_client *client)
0132 {
0133     int reg;
0134 
0135     reg = pca9541_reg_read(client, PCA9541_CONTROL);
0136     if (reg >= 0 && !busoff(reg) && mybus(reg))
0137         pca9541_reg_write(client, PCA9541_CONTROL,
0138                   (reg & PCA9541_CTL_NBUSON) >> 1);
0139 }
0140 
0141 /*
0142  * Arbitration is defined as a two-step process. A bus master can only activate
0143  * the slave bus if it owns it; otherwise it has to request ownership first.
0144  * This multi-step process ensures that access contention is resolved
0145  * gracefully.
0146  *
0147  * Bus  Ownership   Other master    Action
0148  * state        requested access
0149  * ----------------------------------------------------
0150  * off  -       yes     wait for arbitration timeout or
0151  *                  for other master to drop request
0152  * off  no      no      take ownership
0153  * off  yes     no      turn on bus
0154  * on   yes     -       done
0155  * on   no      -       wait for arbitration timeout or
0156  *                  for other master to release bus
0157  *
0158  * The main contention point occurs if the slave bus is off and both masters
0159  * request ownership at the same time. In this case, one master will turn on
0160  * the slave bus, believing that it owns it. The other master will request
0161  * bus ownership. Result is that the bus is turned on, and master which did
0162  * _not_ own the slave bus before ends up owning it.
0163  */
0164 
0165 /* Control commands per PCA9541 datasheet */
0166 static const u8 pca9541_control[16] = {
0167     4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
0168 };
0169 
0170 /*
0171  * Channel arbitration
0172  *
0173  * Return values:
0174  *  <0: error
0175  *  0 : bus not acquired
0176  *  1 : bus acquired
0177  */
0178 static int pca9541_arbitrate(struct i2c_client *client)
0179 {
0180     struct i2c_mux_core *muxc = i2c_get_clientdata(client);
0181     struct pca9541 *data = i2c_mux_priv(muxc);
0182     int reg;
0183 
0184     reg = pca9541_reg_read(client, PCA9541_CONTROL);
0185     if (reg < 0)
0186         return reg;
0187 
0188     if (busoff(reg)) {
0189         int istat;
0190         /*
0191          * Bus is off. Request ownership or turn it on unless
0192          * other master requested ownership.
0193          */
0194         istat = pca9541_reg_read(client, PCA9541_ISTAT);
0195         if (!(istat & PCA9541_ISTAT_NMYTEST)
0196             || time_is_before_eq_jiffies(data->arb_timeout)) {
0197             /*
0198              * Other master did not request ownership,
0199              * or arbitration timeout expired. Take the bus.
0200              */
0201             pca9541_reg_write(client,
0202                       PCA9541_CONTROL,
0203                       pca9541_control[reg & 0x0f]
0204                       | PCA9541_CTL_NTESTON);
0205             data->select_timeout = SELECT_DELAY_SHORT;
0206         } else {
0207             /*
0208              * Other master requested ownership.
0209              * Set extra long timeout to give it time to acquire it.
0210              */
0211             data->select_timeout = SELECT_DELAY_LONG * 2;
0212         }
0213     } else if (mybus(reg)) {
0214         /*
0215          * Bus is on, and we own it. We are done with acquisition.
0216          * Reset NTESTON and BUSINIT, then return success.
0217          */
0218         if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
0219             pca9541_reg_write(client,
0220                       PCA9541_CONTROL,
0221                       reg & ~(PCA9541_CTL_NTESTON
0222                           | PCA9541_CTL_BUSINIT));
0223         return 1;
0224     } else {
0225         /*
0226          * Other master owns the bus.
0227          * If arbitration timeout has expired, force ownership.
0228          * Otherwise request it.
0229          */
0230         data->select_timeout = SELECT_DELAY_LONG;
0231         if (time_is_before_eq_jiffies(data->arb_timeout)) {
0232             /* Time is up, take the bus and reset it. */
0233             pca9541_reg_write(client,
0234                       PCA9541_CONTROL,
0235                       pca9541_control[reg & 0x0f]
0236                       | PCA9541_CTL_BUSINIT
0237                       | PCA9541_CTL_NTESTON);
0238         } else {
0239             /* Request bus ownership if needed */
0240             if (!(reg & PCA9541_CTL_NTESTON))
0241                 pca9541_reg_write(client,
0242                           PCA9541_CONTROL,
0243                           reg | PCA9541_CTL_NTESTON);
0244         }
0245     }
0246     return 0;
0247 }
0248 
0249 static int pca9541_select_chan(struct i2c_mux_core *muxc, u32 chan)
0250 {
0251     struct pca9541 *data = i2c_mux_priv(muxc);
0252     struct i2c_client *client = data->client;
0253     int ret;
0254     unsigned long timeout = jiffies + ARB2_TIMEOUT;
0255         /* give up after this time */
0256 
0257     data->arb_timeout = jiffies + ARB_TIMEOUT;
0258         /* force bus ownership after this time */
0259 
0260     do {
0261         ret = pca9541_arbitrate(client);
0262         if (ret)
0263             return ret < 0 ? ret : 0;
0264 
0265         if (data->select_timeout == SELECT_DELAY_SHORT)
0266             udelay(data->select_timeout);
0267         else
0268             msleep(data->select_timeout / 1000);
0269     } while (time_is_after_eq_jiffies(timeout));
0270 
0271     return -ETIMEDOUT;
0272 }
0273 
0274 static int pca9541_release_chan(struct i2c_mux_core *muxc, u32 chan)
0275 {
0276     struct pca9541 *data = i2c_mux_priv(muxc);
0277     struct i2c_client *client = data->client;
0278 
0279     pca9541_release_bus(client);
0280     return 0;
0281 }
0282 
0283 /*
0284  * I2C init/probing/exit functions
0285  */
0286 static int pca9541_probe(struct i2c_client *client,
0287              const struct i2c_device_id *id)
0288 {
0289     struct i2c_adapter *adap = client->adapter;
0290     struct i2c_mux_core *muxc;
0291     struct pca9541 *data;
0292     int ret;
0293 
0294     if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
0295         return -ENODEV;
0296 
0297     /*
0298      * I2C accesses are unprotected here.
0299      * We have to lock the I2C segment before releasing the bus.
0300      */
0301     i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
0302     pca9541_release_bus(client);
0303     i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
0304 
0305     /* Create mux adapter */
0306 
0307     muxc = i2c_mux_alloc(adap, &client->dev, 1, sizeof(*data),
0308                  I2C_MUX_ARBITRATOR,
0309                  pca9541_select_chan, pca9541_release_chan);
0310     if (!muxc)
0311         return -ENOMEM;
0312 
0313     data = i2c_mux_priv(muxc);
0314     data->client = client;
0315 
0316     i2c_set_clientdata(client, muxc);
0317 
0318     ret = i2c_mux_add_adapter(muxc, 0, 0, 0);
0319     if (ret)
0320         return ret;
0321 
0322     dev_info(&client->dev, "registered master selector for I2C %s\n",
0323          client->name);
0324 
0325     return 0;
0326 }
0327 
0328 static int pca9541_remove(struct i2c_client *client)
0329 {
0330     struct i2c_mux_core *muxc = i2c_get_clientdata(client);
0331 
0332     i2c_mux_del_adapters(muxc);
0333     return 0;
0334 }
0335 
0336 static struct i2c_driver pca9541_driver = {
0337     .driver = {
0338            .name = "pca9541",
0339            .of_match_table = of_match_ptr(pca9541_of_match),
0340            },
0341     .probe = pca9541_probe,
0342     .remove = pca9541_remove,
0343     .id_table = pca9541_id,
0344 };
0345 
0346 module_i2c_driver(pca9541_driver);
0347 
0348 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
0349 MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
0350 MODULE_LICENSE("GPL v2");