0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
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
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
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
0067 #define ARB_TIMEOUT (HZ / 8)
0068 #define ARB2_TIMEOUT (HZ / 4)
0069
0070
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
0097
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
0111
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
0128
0129
0130
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
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
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
0172
0173
0174
0175
0176
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
0192
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
0199
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
0209
0210
0211 data->select_timeout = SELECT_DELAY_LONG * 2;
0212 }
0213 } else if (mybus(reg)) {
0214
0215
0216
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
0227
0228
0229
0230 data->select_timeout = SELECT_DELAY_LONG;
0231 if (time_is_before_eq_jiffies(data->arb_timeout)) {
0232
0233 pca9541_reg_write(client,
0234 PCA9541_CONTROL,
0235 pca9541_control[reg & 0x0f]
0236 | PCA9541_CTL_BUSINIT
0237 | PCA9541_CTL_NTESTON);
0238 } else {
0239
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
0256
0257 data->arb_timeout = jiffies + ARB_TIMEOUT;
0258
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
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
0299
0300
0301 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
0302 pca9541_release_bus(client);
0303 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
0304
0305
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");