2 * I2C multiplexer driver for PCA9541 bus master selector
4 * Copyright (c) 2010 Ericsson AB.
6 * Author: Guenter Roeck <guenter.roeck@ericsson.com>
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/init.h>
21 #include <linux/jiffies.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-mux.h>
28 #include <linux/i2c/pca954x.h>
31 * The PCA9541 is a bus master selector. It supports two I2C masters connected
32 * to a single slave bus.
34 * Before each bus transaction, a master has to acquire bus ownership. After the
35 * transaction is complete, bus ownership has to be released. This fits well
36 * into the I2C multiplexer framework, which provides select and release
37 * functions for this purpose. For this reason, this driver is modeled as
38 * single-channel I2C bus multiplexer.
40 * This driver assumes that the two bus masters are controlled by two different
41 * hosts. If a single host controls both masters, platform code has to ensure
42 * that only one of the masters is instantiated at any given time.
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)
68 /* arbitration timeouts, in jiffies */
69 #define ARB_TIMEOUT (HZ / 8) /* 125 ms until forcing bus ownership */
70 #define ARB2_TIMEOUT (HZ / 4) /* 250 ms until acquisition failure */
72 /* arbitration retry delays, in us */
73 #define SELECT_DELAY_SHORT 50
74 #define SELECT_DELAY_LONG 1000
77 struct i2c_adapter
*mux_adap
;
78 unsigned long select_timeout
;
79 unsigned long arb_timeout
;
82 static const struct i2c_device_id pca9541_id
[] = {
87 MODULE_DEVICE_TABLE(i2c
, pca9541_id
);
90 * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
91 * as they will try to lock the adapter a second time.
93 static int pca9541_reg_write(struct i2c_client
*client
, u8 command
, u8 val
)
95 struct i2c_adapter
*adap
= client
->adapter
;
98 if (adap
->algo
->master_xfer
) {
102 msg
.addr
= client
->addr
;
108 ret
= adap
->algo
->master_xfer(adap
, &msg
, 1);
110 union i2c_smbus_data data
;
113 ret
= adap
->algo
->smbus_xfer(adap
, client
->addr
,
117 I2C_SMBUS_BYTE_DATA
, &data
);
124 * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
125 * as they will try to lock adapter a second time.
127 static int pca9541_reg_read(struct i2c_client
*client
, u8 command
)
129 struct i2c_adapter
*adap
= client
->adapter
;
133 if (adap
->algo
->master_xfer
) {
134 struct i2c_msg msg
[2] = {
136 .addr
= client
->addr
,
142 .addr
= client
->addr
,
148 ret
= adap
->algo
->master_xfer(adap
, msg
, 2);
154 union i2c_smbus_data data
;
156 ret
= adap
->algo
->smbus_xfer(adap
, client
->addr
,
160 I2C_SMBUS_BYTE_DATA
, &data
);
168 * Arbitration management functions
171 /* Release bus. Also reset NTESTON and BUSINIT if it was set. */
172 static void pca9541_release_bus(struct i2c_client
*client
)
176 reg
= pca9541_reg_read(client
, PCA9541_CONTROL
);
177 if (reg
>= 0 && !busoff(reg
) && mybus(reg
))
178 pca9541_reg_write(client
, PCA9541_CONTROL
,
179 (reg
& PCA9541_CTL_NBUSON
) >> 1);
183 * Arbitration is defined as a two-step process. A bus master can only activate
184 * the slave bus if it owns it; otherwise it has to request ownership first.
185 * This multi-step process ensures that access contention is resolved
188 * Bus Ownership Other master Action
189 * state requested access
190 * ----------------------------------------------------
191 * off - yes wait for arbitration timeout or
192 * for other master to drop request
193 * off no no take ownership
194 * off yes no turn on bus
196 * on no - wait for arbitration timeout or
197 * for other master to release bus
199 * The main contention point occurs if the slave bus is off and both masters
200 * request ownership at the same time. In this case, one master will turn on
201 * the slave bus, believing that it owns it. The other master will request
202 * bus ownership. Result is that the bus is turned on, and master which did
203 * _not_ own the slave bus before ends up owning it.
206 /* Control commands per PCA9541 datasheet */
207 static const u8 pca9541_control
[16] = {
208 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
212 * Channel arbitration
216 * 0 : bus not acquired
219 static int pca9541_arbitrate(struct i2c_client
*client
)
221 struct pca9541
*data
= i2c_get_clientdata(client
);
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_adapter
*adap
, void *client
, u32 chan
)
291 struct pca9541
*data
= i2c_get_clientdata(client
);
293 unsigned long timeout
= jiffies
+ ARB2_TIMEOUT
;
294 /* give up after this time */
296 data
->arb_timeout
= jiffies
+ ARB_TIMEOUT
;
297 /* force bus ownership after this time */
300 ret
= pca9541_arbitrate(client
);
302 return ret
< 0 ? ret
: 0;
304 if (data
->select_timeout
== SELECT_DELAY_SHORT
)
305 udelay(data
->select_timeout
);
307 msleep(data
->select_timeout
/ 1000);
308 } while (time_is_after_eq_jiffies(timeout
));
313 static int pca9541_release_chan(struct i2c_adapter
*adap
,
314 void *client
, u32 chan
)
316 pca9541_release_bus(client
);
321 * I2C init/probing/exit functions
323 static int pca9541_probe(struct i2c_client
*client
,
324 const struct i2c_device_id
*id
)
326 struct i2c_adapter
*adap
= client
->adapter
;
327 struct pca954x_platform_data
*pdata
= client
->dev
.platform_data
;
328 struct pca9541
*data
;
332 if (!i2c_check_functionality(adap
, I2C_FUNC_SMBUS_BYTE_DATA
))
335 data
= kzalloc(sizeof(struct pca9541
), GFP_KERNEL
);
341 i2c_set_clientdata(client
, data
);
344 * I2C accesses are unprotected here.
345 * We have to lock the adapter before releasing the bus.
347 i2c_lock_adapter(adap
);
348 pca9541_release_bus(client
);
349 i2c_unlock_adapter(adap
);
351 /* Create mux adapter */
355 force
= pdata
->modes
[0].adap_id
;
356 data
->mux_adap
= i2c_add_mux_adapter(adap
, client
, force
, 0,
358 pca9541_release_chan
);
360 if (data
->mux_adap
== NULL
) {
361 dev_err(&client
->dev
, "failed to register master selector\n");
365 dev_info(&client
->dev
, "registered master selector for I2C %s\n",
376 static int pca9541_remove(struct i2c_client
*client
)
378 struct pca9541
*data
= i2c_get_clientdata(client
);
380 i2c_del_mux_adapter(data
->mux_adap
);
386 static struct i2c_driver pca9541_driver
= {
389 .owner
= THIS_MODULE
,
391 .probe
= pca9541_probe
,
392 .remove
= pca9541_remove
,
393 .id_table
= pca9541_id
,
396 static int __init
pca9541_init(void)
398 return i2c_add_driver(&pca9541_driver
);
401 static void __exit
pca9541_exit(void)
403 i2c_del_driver(&pca9541_driver
);
406 module_init(pca9541_init
);
407 module_exit(pca9541_exit
);
409 MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
410 MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
411 MODULE_LICENSE("GPL v2");