2 * I2C multiplexer driver for PCA9541 bus master selector
4 * Copyright (c) 2010 Ericsson AB.
6 * Author: Guenter Roeck <linux@roeck-us.net>
11 * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
12 * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
19 #include <linux/module.h>
20 #include <linux/jiffies.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/device.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-mux.h>
27 #include <linux/i2c/pca954x.h>
30 * The PCA9541 is a bus master selector. It supports two I2C masters connected
31 * to a single slave bus.
33 * Before each bus transaction, a master has to acquire bus ownership. After the
34 * transaction is complete, bus ownership has to be released. This fits well
35 * into the I2C multiplexer framework, which provides select and release
36 * functions for this purpose. For this reason, this driver is modeled as
37 * single-channel I2C bus multiplexer.
39 * This driver assumes that the two bus masters are controlled by two different
40 * hosts. If a single host controls both masters, platform code has to ensure
41 * that only one of the masters is instantiated at any given time.
44 #define PCA9541_CONTROL 0x01
45 #define PCA9541_ISTAT 0x02
47 #define PCA9541_CTL_MYBUS (1 << 0)
48 #define PCA9541_CTL_NMYBUS (1 << 1)
49 #define PCA9541_CTL_BUSON (1 << 2)
50 #define PCA9541_CTL_NBUSON (1 << 3)
51 #define PCA9541_CTL_BUSINIT (1 << 4)
52 #define PCA9541_CTL_TESTON (1 << 6)
53 #define PCA9541_CTL_NTESTON (1 << 7)
55 #define PCA9541_ISTAT_INTIN (1 << 0)
56 #define PCA9541_ISTAT_BUSINIT (1 << 1)
57 #define PCA9541_ISTAT_BUSOK (1 << 2)
58 #define PCA9541_ISTAT_BUSLOST (1 << 3)
59 #define PCA9541_ISTAT_MYTEST (1 << 6)
60 #define PCA9541_ISTAT_NMYTEST (1 << 7)
62 #define BUSON (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON)
63 #define MYBUS (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS)
64 #define mybus(x) (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS)
65 #define busoff(x) (!((x) & BUSON) || ((x) & BUSON) == BUSON)
67 /* arbitration timeouts, in jiffies */
68 #define ARB_TIMEOUT (HZ / 8) /* 125 ms until forcing bus ownership */
69 #define ARB2_TIMEOUT (HZ / 4) /* 250 ms until acquisition failure */
71 /* arbitration retry delays, in us */
72 #define SELECT_DELAY_SHORT 50
73 #define SELECT_DELAY_LONG 1000
76 struct i2c_client
*client
;
77 unsigned long select_timeout
;
78 unsigned long arb_timeout
;
81 static const struct i2c_device_id pca9541_id
[] = {
86 MODULE_DEVICE_TABLE(i2c
, pca9541_id
);
89 * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
90 * as they will try to lock the adapter a second time.
92 static int pca9541_reg_write(struct i2c_client
*client
, u8 command
, u8 val
)
94 struct i2c_adapter
*adap
= client
->adapter
;
97 if (adap
->algo
->master_xfer
) {
101 msg
.addr
= client
->addr
;
107 ret
= __i2c_transfer(adap
, &msg
, 1);
109 union i2c_smbus_data data
;
112 ret
= adap
->algo
->smbus_xfer(adap
, client
->addr
,
116 I2C_SMBUS_BYTE_DATA
, &data
);
123 * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
124 * as they will try to lock adapter a second time.
126 static int pca9541_reg_read(struct i2c_client
*client
, u8 command
)
128 struct i2c_adapter
*adap
= client
->adapter
;
132 if (adap
->algo
->master_xfer
) {
133 struct i2c_msg msg
[2] = {
135 .addr
= client
->addr
,
141 .addr
= client
->addr
,
147 ret
= __i2c_transfer(adap
, msg
, 2);
153 union i2c_smbus_data data
;
155 ret
= adap
->algo
->smbus_xfer(adap
, client
->addr
,
159 I2C_SMBUS_BYTE_DATA
, &data
);
167 * Arbitration management functions
170 /* Release bus. Also reset NTESTON and BUSINIT if it was set. */
171 static void pca9541_release_bus(struct i2c_client
*client
)
175 reg
= pca9541_reg_read(client
, PCA9541_CONTROL
);
176 if (reg
>= 0 && !busoff(reg
) && mybus(reg
))
177 pca9541_reg_write(client
, PCA9541_CONTROL
,
178 (reg
& PCA9541_CTL_NBUSON
) >> 1);
182 * Arbitration is defined as a two-step process. A bus master can only activate
183 * the slave bus if it owns it; otherwise it has to request ownership first.
184 * This multi-step process ensures that access contention is resolved
187 * Bus Ownership Other master Action
188 * state requested access
189 * ----------------------------------------------------
190 * off - yes wait for arbitration timeout or
191 * for other master to drop request
192 * off no no take ownership
193 * off yes no turn on bus
195 * on no - wait for arbitration timeout or
196 * for other master to release bus
198 * The main contention point occurs if the slave bus is off and both masters
199 * request ownership at the same time. In this case, one master will turn on
200 * the slave bus, believing that it owns it. The other master will request
201 * bus ownership. Result is that the bus is turned on, and master which did
202 * _not_ own the slave bus before ends up owning it.
205 /* Control commands per PCA9541 datasheet */
206 static const u8 pca9541_control
[16] = {
207 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
211 * Channel arbitration
215 * 0 : bus not acquired
218 static int pca9541_arbitrate(struct i2c_client
*client
)
220 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
221 struct pca9541
*data
= i2c_mux_priv(muxc
);
224 reg
= pca9541_reg_read(client
, PCA9541_CONTROL
);
231 * Bus is off. Request ownership or turn it on unless
232 * other master requested ownership.
234 istat
= pca9541_reg_read(client
, PCA9541_ISTAT
);
235 if (!(istat
& PCA9541_ISTAT_NMYTEST
)
236 || time_is_before_eq_jiffies(data
->arb_timeout
)) {
238 * Other master did not request ownership,
239 * or arbitration timeout expired. Take the bus.
241 pca9541_reg_write(client
,
243 pca9541_control
[reg
& 0x0f]
244 | PCA9541_CTL_NTESTON
);
245 data
->select_timeout
= SELECT_DELAY_SHORT
;
248 * Other master requested ownership.
249 * Set extra long timeout to give it time to acquire it.
251 data
->select_timeout
= SELECT_DELAY_LONG
* 2;
253 } else if (mybus(reg
)) {
255 * Bus is on, and we own it. We are done with acquisition.
256 * Reset NTESTON and BUSINIT, then return success.
258 if (reg
& (PCA9541_CTL_NTESTON
| PCA9541_CTL_BUSINIT
))
259 pca9541_reg_write(client
,
261 reg
& ~(PCA9541_CTL_NTESTON
262 | PCA9541_CTL_BUSINIT
));
266 * Other master owns the bus.
267 * If arbitration timeout has expired, force ownership.
268 * Otherwise request it.
270 data
->select_timeout
= SELECT_DELAY_LONG
;
271 if (time_is_before_eq_jiffies(data
->arb_timeout
)) {
272 /* Time is up, take the bus and reset it. */
273 pca9541_reg_write(client
,
275 pca9541_control
[reg
& 0x0f]
276 | PCA9541_CTL_BUSINIT
277 | PCA9541_CTL_NTESTON
);
279 /* Request bus ownership if needed */
280 if (!(reg
& PCA9541_CTL_NTESTON
))
281 pca9541_reg_write(client
,
283 reg
| PCA9541_CTL_NTESTON
);
289 static int pca9541_select_chan(struct i2c_mux_core
*muxc
, u32 chan
)
291 struct pca9541
*data
= i2c_mux_priv(muxc
);
292 struct i2c_client
*client
= data
->client
;
294 unsigned long timeout
= jiffies
+ ARB2_TIMEOUT
;
295 /* give up after this time */
297 data
->arb_timeout
= jiffies
+ ARB_TIMEOUT
;
298 /* force bus ownership after this time */
301 ret
= pca9541_arbitrate(client
);
303 return ret
< 0 ? ret
: 0;
305 if (data
->select_timeout
== SELECT_DELAY_SHORT
)
306 udelay(data
->select_timeout
);
308 msleep(data
->select_timeout
/ 1000);
309 } while (time_is_after_eq_jiffies(timeout
));
314 static int pca9541_release_chan(struct i2c_mux_core
*muxc
, u32 chan
)
316 struct pca9541
*data
= i2c_mux_priv(muxc
);
317 struct i2c_client
*client
= data
->client
;
319 pca9541_release_bus(client
);
324 * I2C init/probing/exit functions
326 static int pca9541_probe(struct i2c_client
*client
,
327 const struct i2c_device_id
*id
)
329 struct i2c_adapter
*adap
= client
->adapter
;
330 struct pca954x_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
331 struct i2c_mux_core
*muxc
;
332 struct pca9541
*data
;
336 if (!i2c_check_functionality(adap
, I2C_FUNC_SMBUS_BYTE_DATA
))
340 * I2C accesses are unprotected here.
341 * We have to lock the adapter before releasing the bus.
343 i2c_lock_adapter(adap
);
344 pca9541_release_bus(client
);
345 i2c_unlock_adapter(adap
);
347 /* Create mux adapter */
351 force
= pdata
->modes
[0].adap_id
;
352 muxc
= i2c_mux_alloc(adap
, &client
->dev
, 1, sizeof(*data
), 0,
353 pca9541_select_chan
, pca9541_release_chan
);
357 data
= i2c_mux_priv(muxc
);
358 data
->client
= client
;
360 i2c_set_clientdata(client
, muxc
);
362 ret
= i2c_mux_add_adapter(muxc
, force
, 0, 0);
364 dev_err(&client
->dev
, "failed to register master selector\n");
368 dev_info(&client
->dev
, "registered master selector for I2C %s\n",
374 static int pca9541_remove(struct i2c_client
*client
)
376 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
378 i2c_mux_del_adapters(muxc
);
382 static struct i2c_driver pca9541_driver
= {
386 .probe
= pca9541_probe
,
387 .remove
= pca9541_remove
,
388 .id_table
= pca9541_id
,
391 module_i2c_driver(pca9541_driver
);
393 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
394 MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
395 MODULE_LICENSE("GPL v2");