1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * I2C multiplexer driver for PCA9541 bus master selector
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2010 Ericsson AB.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Author: Guenter Roeck <linux@roeck-us.net>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Derived from:
9*4882a593Smuzhiyun * pca954x.c
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
12*4882a593Smuzhiyun * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * This file is licensed under the terms of the GNU General Public
15*4882a593Smuzhiyun * License version 2. This program is licensed "as is" without any
16*4882a593Smuzhiyun * warranty of any kind, whether express or implied.
17*4882a593Smuzhiyun */
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <linux/bitops.h>
20*4882a593Smuzhiyun #include <linux/delay.h>
21*4882a593Smuzhiyun #include <linux/device.h>
22*4882a593Smuzhiyun #include <linux/i2c.h>
23*4882a593Smuzhiyun #include <linux/i2c-mux.h>
24*4882a593Smuzhiyun #include <linux/jiffies.h>
25*4882a593Smuzhiyun #include <linux/module.h>
26*4882a593Smuzhiyun #include <linux/slab.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * The PCA9541 is a bus master selector. It supports two I2C masters connected
30*4882a593Smuzhiyun * to a single slave bus.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * Before each bus transaction, a master has to acquire bus ownership. After the
33*4882a593Smuzhiyun * transaction is complete, bus ownership has to be released. This fits well
34*4882a593Smuzhiyun * into the I2C multiplexer framework, which provides select and release
35*4882a593Smuzhiyun * functions for this purpose. For this reason, this driver is modeled as
36*4882a593Smuzhiyun * single-channel I2C bus multiplexer.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * This driver assumes that the two bus masters are controlled by two different
39*4882a593Smuzhiyun * hosts. If a single host controls both masters, platform code has to ensure
40*4882a593Smuzhiyun * that only one of the masters is instantiated at any given time.
41*4882a593Smuzhiyun */
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #define PCA9541_CONTROL 0x01
44*4882a593Smuzhiyun #define PCA9541_ISTAT 0x02
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #define PCA9541_CTL_MYBUS BIT(0)
47*4882a593Smuzhiyun #define PCA9541_CTL_NMYBUS BIT(1)
48*4882a593Smuzhiyun #define PCA9541_CTL_BUSON BIT(2)
49*4882a593Smuzhiyun #define PCA9541_CTL_NBUSON BIT(3)
50*4882a593Smuzhiyun #define PCA9541_CTL_BUSINIT BIT(4)
51*4882a593Smuzhiyun #define PCA9541_CTL_TESTON BIT(6)
52*4882a593Smuzhiyun #define PCA9541_CTL_NTESTON BIT(7)
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #define PCA9541_ISTAT_INTIN BIT(0)
55*4882a593Smuzhiyun #define PCA9541_ISTAT_BUSINIT BIT(1)
56*4882a593Smuzhiyun #define PCA9541_ISTAT_BUSOK BIT(2)
57*4882a593Smuzhiyun #define PCA9541_ISTAT_BUSLOST BIT(3)
58*4882a593Smuzhiyun #define PCA9541_ISTAT_MYTEST BIT(6)
59*4882a593Smuzhiyun #define PCA9541_ISTAT_NMYTEST BIT(7)
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define BUSON (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON)
62*4882a593Smuzhiyun #define MYBUS (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS)
63*4882a593Smuzhiyun #define mybus(x) (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS)
64*4882a593Smuzhiyun #define busoff(x) (!((x) & BUSON) || ((x) & BUSON) == BUSON)
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* arbitration timeouts, in jiffies */
67*4882a593Smuzhiyun #define ARB_TIMEOUT (HZ / 8) /* 125 ms until forcing bus ownership */
68*4882a593Smuzhiyun #define ARB2_TIMEOUT (HZ / 4) /* 250 ms until acquisition failure */
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* arbitration retry delays, in us */
71*4882a593Smuzhiyun #define SELECT_DELAY_SHORT 50
72*4882a593Smuzhiyun #define SELECT_DELAY_LONG 1000
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun struct pca9541 {
75*4882a593Smuzhiyun struct i2c_client *client;
76*4882a593Smuzhiyun unsigned long select_timeout;
77*4882a593Smuzhiyun unsigned long arb_timeout;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun static const struct i2c_device_id pca9541_id[] = {
81*4882a593Smuzhiyun {"pca9541", 0},
82*4882a593Smuzhiyun {}
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, pca9541_id);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun #ifdef CONFIG_OF
88*4882a593Smuzhiyun static const struct of_device_id pca9541_of_match[] = {
89*4882a593Smuzhiyun { .compatible = "nxp,pca9541" },
90*4882a593Smuzhiyun {}
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, pca9541_of_match);
93*4882a593Smuzhiyun #endif
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
97*4882a593Smuzhiyun * as they will try to lock the adapter a second time.
98*4882a593Smuzhiyun */
pca9541_reg_write(struct i2c_client * client,u8 command,u8 val)99*4882a593Smuzhiyun static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun struct i2c_adapter *adap = client->adapter;
102*4882a593Smuzhiyun union i2c_smbus_data data = { .byte = val };
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun return __i2c_smbus_xfer(adap, client->addr, client->flags,
105*4882a593Smuzhiyun I2C_SMBUS_WRITE, command,
106*4882a593Smuzhiyun I2C_SMBUS_BYTE_DATA, &data);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
111*4882a593Smuzhiyun * as they will try to lock adapter a second time.
112*4882a593Smuzhiyun */
pca9541_reg_read(struct i2c_client * client,u8 command)113*4882a593Smuzhiyun static int pca9541_reg_read(struct i2c_client *client, u8 command)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct i2c_adapter *adap = client->adapter;
116*4882a593Smuzhiyun union i2c_smbus_data data;
117*4882a593Smuzhiyun int ret;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun ret = __i2c_smbus_xfer(adap, client->addr, client->flags,
120*4882a593Smuzhiyun I2C_SMBUS_READ, command,
121*4882a593Smuzhiyun I2C_SMBUS_BYTE_DATA, &data);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun return ret ?: data.byte;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun * Arbitration management functions
128*4882a593Smuzhiyun */
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /* Release bus. Also reset NTESTON and BUSINIT if it was set. */
pca9541_release_bus(struct i2c_client * client)131*4882a593Smuzhiyun static void pca9541_release_bus(struct i2c_client *client)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun int reg;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun reg = pca9541_reg_read(client, PCA9541_CONTROL);
136*4882a593Smuzhiyun if (reg >= 0 && !busoff(reg) && mybus(reg))
137*4882a593Smuzhiyun pca9541_reg_write(client, PCA9541_CONTROL,
138*4882a593Smuzhiyun (reg & PCA9541_CTL_NBUSON) >> 1);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * Arbitration is defined as a two-step process. A bus master can only activate
143*4882a593Smuzhiyun * the slave bus if it owns it; otherwise it has to request ownership first.
144*4882a593Smuzhiyun * This multi-step process ensures that access contention is resolved
145*4882a593Smuzhiyun * gracefully.
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun * Bus Ownership Other master Action
148*4882a593Smuzhiyun * state requested access
149*4882a593Smuzhiyun * ----------------------------------------------------
150*4882a593Smuzhiyun * off - yes wait for arbitration timeout or
151*4882a593Smuzhiyun * for other master to drop request
152*4882a593Smuzhiyun * off no no take ownership
153*4882a593Smuzhiyun * off yes no turn on bus
154*4882a593Smuzhiyun * on yes - done
155*4882a593Smuzhiyun * on no - wait for arbitration timeout or
156*4882a593Smuzhiyun * for other master to release bus
157*4882a593Smuzhiyun *
158*4882a593Smuzhiyun * The main contention point occurs if the slave bus is off and both masters
159*4882a593Smuzhiyun * request ownership at the same time. In this case, one master will turn on
160*4882a593Smuzhiyun * the slave bus, believing that it owns it. The other master will request
161*4882a593Smuzhiyun * bus ownership. Result is that the bus is turned on, and master which did
162*4882a593Smuzhiyun * _not_ own the slave bus before ends up owning it.
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* Control commands per PCA9541 datasheet */
166*4882a593Smuzhiyun static const u8 pca9541_control[16] = {
167*4882a593Smuzhiyun 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * Channel arbitration
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * Return values:
174*4882a593Smuzhiyun * <0: error
175*4882a593Smuzhiyun * 0 : bus not acquired
176*4882a593Smuzhiyun * 1 : bus acquired
177*4882a593Smuzhiyun */
pca9541_arbitrate(struct i2c_client * client)178*4882a593Smuzhiyun static int pca9541_arbitrate(struct i2c_client *client)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct i2c_mux_core *muxc = i2c_get_clientdata(client);
181*4882a593Smuzhiyun struct pca9541 *data = i2c_mux_priv(muxc);
182*4882a593Smuzhiyun int reg;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun reg = pca9541_reg_read(client, PCA9541_CONTROL);
185*4882a593Smuzhiyun if (reg < 0)
186*4882a593Smuzhiyun return reg;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun if (busoff(reg)) {
189*4882a593Smuzhiyun int istat;
190*4882a593Smuzhiyun /*
191*4882a593Smuzhiyun * Bus is off. Request ownership or turn it on unless
192*4882a593Smuzhiyun * other master requested ownership.
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun istat = pca9541_reg_read(client, PCA9541_ISTAT);
195*4882a593Smuzhiyun if (!(istat & PCA9541_ISTAT_NMYTEST)
196*4882a593Smuzhiyun || time_is_before_eq_jiffies(data->arb_timeout)) {
197*4882a593Smuzhiyun /*
198*4882a593Smuzhiyun * Other master did not request ownership,
199*4882a593Smuzhiyun * or arbitration timeout expired. Take the bus.
200*4882a593Smuzhiyun */
201*4882a593Smuzhiyun pca9541_reg_write(client,
202*4882a593Smuzhiyun PCA9541_CONTROL,
203*4882a593Smuzhiyun pca9541_control[reg & 0x0f]
204*4882a593Smuzhiyun | PCA9541_CTL_NTESTON);
205*4882a593Smuzhiyun data->select_timeout = SELECT_DELAY_SHORT;
206*4882a593Smuzhiyun } else {
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun * Other master requested ownership.
209*4882a593Smuzhiyun * Set extra long timeout to give it time to acquire it.
210*4882a593Smuzhiyun */
211*4882a593Smuzhiyun data->select_timeout = SELECT_DELAY_LONG * 2;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun } else if (mybus(reg)) {
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun * Bus is on, and we own it. We are done with acquisition.
216*4882a593Smuzhiyun * Reset NTESTON and BUSINIT, then return success.
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
219*4882a593Smuzhiyun pca9541_reg_write(client,
220*4882a593Smuzhiyun PCA9541_CONTROL,
221*4882a593Smuzhiyun reg & ~(PCA9541_CTL_NTESTON
222*4882a593Smuzhiyun | PCA9541_CTL_BUSINIT));
223*4882a593Smuzhiyun return 1;
224*4882a593Smuzhiyun } else {
225*4882a593Smuzhiyun /*
226*4882a593Smuzhiyun * Other master owns the bus.
227*4882a593Smuzhiyun * If arbitration timeout has expired, force ownership.
228*4882a593Smuzhiyun * Otherwise request it.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun data->select_timeout = SELECT_DELAY_LONG;
231*4882a593Smuzhiyun if (time_is_before_eq_jiffies(data->arb_timeout)) {
232*4882a593Smuzhiyun /* Time is up, take the bus and reset it. */
233*4882a593Smuzhiyun pca9541_reg_write(client,
234*4882a593Smuzhiyun PCA9541_CONTROL,
235*4882a593Smuzhiyun pca9541_control[reg & 0x0f]
236*4882a593Smuzhiyun | PCA9541_CTL_BUSINIT
237*4882a593Smuzhiyun | PCA9541_CTL_NTESTON);
238*4882a593Smuzhiyun } else {
239*4882a593Smuzhiyun /* Request bus ownership if needed */
240*4882a593Smuzhiyun if (!(reg & PCA9541_CTL_NTESTON))
241*4882a593Smuzhiyun pca9541_reg_write(client,
242*4882a593Smuzhiyun PCA9541_CONTROL,
243*4882a593Smuzhiyun reg | PCA9541_CTL_NTESTON);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun return 0;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
pca9541_select_chan(struct i2c_mux_core * muxc,u32 chan)249*4882a593Smuzhiyun static int pca9541_select_chan(struct i2c_mux_core *muxc, u32 chan)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun struct pca9541 *data = i2c_mux_priv(muxc);
252*4882a593Smuzhiyun struct i2c_client *client = data->client;
253*4882a593Smuzhiyun int ret;
254*4882a593Smuzhiyun unsigned long timeout = jiffies + ARB2_TIMEOUT;
255*4882a593Smuzhiyun /* give up after this time */
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun data->arb_timeout = jiffies + ARB_TIMEOUT;
258*4882a593Smuzhiyun /* force bus ownership after this time */
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun do {
261*4882a593Smuzhiyun ret = pca9541_arbitrate(client);
262*4882a593Smuzhiyun if (ret)
263*4882a593Smuzhiyun return ret < 0 ? ret : 0;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun if (data->select_timeout == SELECT_DELAY_SHORT)
266*4882a593Smuzhiyun udelay(data->select_timeout);
267*4882a593Smuzhiyun else
268*4882a593Smuzhiyun msleep(data->select_timeout / 1000);
269*4882a593Smuzhiyun } while (time_is_after_eq_jiffies(timeout));
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun return -ETIMEDOUT;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
pca9541_release_chan(struct i2c_mux_core * muxc,u32 chan)274*4882a593Smuzhiyun static int pca9541_release_chan(struct i2c_mux_core *muxc, u32 chan)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun struct pca9541 *data = i2c_mux_priv(muxc);
277*4882a593Smuzhiyun struct i2c_client *client = data->client;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun pca9541_release_bus(client);
280*4882a593Smuzhiyun return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * I2C init/probing/exit functions
285*4882a593Smuzhiyun */
pca9541_probe(struct i2c_client * client,const struct i2c_device_id * id)286*4882a593Smuzhiyun static int pca9541_probe(struct i2c_client *client,
287*4882a593Smuzhiyun const struct i2c_device_id *id)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun struct i2c_adapter *adap = client->adapter;
290*4882a593Smuzhiyun struct i2c_mux_core *muxc;
291*4882a593Smuzhiyun struct pca9541 *data;
292*4882a593Smuzhiyun int ret;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
295*4882a593Smuzhiyun return -ENODEV;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /*
298*4882a593Smuzhiyun * I2C accesses are unprotected here.
299*4882a593Smuzhiyun * We have to lock the I2C segment before releasing the bus.
300*4882a593Smuzhiyun */
301*4882a593Smuzhiyun i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
302*4882a593Smuzhiyun pca9541_release_bus(client);
303*4882a593Smuzhiyun i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /* Create mux adapter */
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun muxc = i2c_mux_alloc(adap, &client->dev, 1, sizeof(*data),
308*4882a593Smuzhiyun I2C_MUX_ARBITRATOR,
309*4882a593Smuzhiyun pca9541_select_chan, pca9541_release_chan);
310*4882a593Smuzhiyun if (!muxc)
311*4882a593Smuzhiyun return -ENOMEM;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun data = i2c_mux_priv(muxc);
314*4882a593Smuzhiyun data->client = client;
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun i2c_set_clientdata(client, muxc);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun ret = i2c_mux_add_adapter(muxc, 0, 0, 0);
319*4882a593Smuzhiyun if (ret)
320*4882a593Smuzhiyun return ret;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun dev_info(&client->dev, "registered master selector for I2C %s\n",
323*4882a593Smuzhiyun client->name);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun return 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
pca9541_remove(struct i2c_client * client)328*4882a593Smuzhiyun static int pca9541_remove(struct i2c_client *client)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun struct i2c_mux_core *muxc = i2c_get_clientdata(client);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun i2c_mux_del_adapters(muxc);
333*4882a593Smuzhiyun return 0;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun static struct i2c_driver pca9541_driver = {
337*4882a593Smuzhiyun .driver = {
338*4882a593Smuzhiyun .name = "pca9541",
339*4882a593Smuzhiyun .of_match_table = of_match_ptr(pca9541_of_match),
340*4882a593Smuzhiyun },
341*4882a593Smuzhiyun .probe = pca9541_probe,
342*4882a593Smuzhiyun .remove = pca9541_remove,
343*4882a593Smuzhiyun .id_table = pca9541_id,
344*4882a593Smuzhiyun };
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun module_i2c_driver(pca9541_driver);
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
349*4882a593Smuzhiyun MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
350*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
351