19 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
25 #include <linux/i2c.h>
45 #define PCA9541_CONTROL 0x01
46 #define PCA9541_ISTAT 0x02
48 #define PCA9541_CTL_MYBUS (1 << 0)
49 #define PCA9541_CTL_NMYBUS (1 << 1)
50 #define PCA9541_CTL_BUSON (1 << 2)
51 #define PCA9541_CTL_NBUSON (1 << 3)
52 #define PCA9541_CTL_BUSINIT (1 << 4)
53 #define PCA9541_CTL_TESTON (1 << 6)
54 #define PCA9541_CTL_NTESTON (1 << 7)
56 #define PCA9541_ISTAT_INTIN (1 << 0)
57 #define PCA9541_ISTAT_BUSINIT (1 << 1)
58 #define PCA9541_ISTAT_BUSOK (1 << 2)
59 #define PCA9541_ISTAT_BUSLOST (1 << 3)
60 #define PCA9541_ISTAT_MYTEST (1 << 6)
61 #define PCA9541_ISTAT_NMYTEST (1 << 7)
63 #define BUSON (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON)
64 #define MYBUS (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS)
65 #define mybus(x) (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS)
66 #define busoff(x) (!((x) & BUSON) || ((x) & BUSON) == BUSON)
69 #define ARB_TIMEOUT (HZ / 8)
70 #define ARB2_TIMEOUT (HZ / 4)
73 #define SELECT_DELAY_SHORT 50
74 #define SELECT_DELAY_LONG 1000
98 if (adap->
algo->master_xfer) {
108 ret = adap->
algo->master_xfer(adap, &
msg, 1);
113 ret = adap->
algo->smbus_xfer(adap, client->
addr,
127 static int pca9541_reg_read(
struct i2c_client *client,
u8 command)
133 if (adap->
algo->master_xfer) {
136 .addr = client->
addr,
142 .addr = client->
addr,
148 ret = adap->
algo->master_xfer(adap, msg, 2);
156 ret = adap->
algo->smbus_xfer(adap, client->
addr,
172 static void pca9541_release_bus(
struct i2c_client *client)
207 static const u8 pca9541_control[16] = {
208 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
219 static int pca9541_arbitrate(
struct i2c_client *client)
241 pca9541_reg_write(client,
243 pca9541_control[reg & 0x0f]
253 }
else if (
mybus(reg)) {
259 pca9541_reg_write(client,
273 pca9541_reg_write(client,
275 pca9541_control[reg & 0x0f]
281 pca9541_reg_write(client,
283 reg | PCA9541_CTL_NTESTON);
291 struct pca9541 *data = i2c_get_clientdata(client);
300 ret = pca9541_arbitrate(client);
302 return ret < 0 ? ret : 0;
313 static int pca9541_release_chan(
struct i2c_adapter *adap,
314 void *client,
u32 chan)
316 pca9541_release_bus(client);
323 static int pca9541_probe(
struct i2c_client *client,
341 i2c_set_clientdata(client, data);
348 pca9541_release_bus(client);
355 force = pdata->
modes[0].adap_id;
359 pca9541_release_chan);
362 dev_err(&client->
dev,
"failed to register master selector\n");
366 dev_info(&client->
dev,
"registered master selector for I2C %s\n",
377 static int pca9541_remove(
struct i2c_client *client)
379 struct pca9541 *data = i2c_get_clientdata(client);
392 .probe = pca9541_probe,
393 .remove = pca9541_remove,
394 .id_table = pca9541_id,