1 /*
2  * I2C multiplexer driver for PCA9541 bus master selector
3  *
4  * Copyright (c) 2010 Ericsson AB.
5  *
6  * Author: Guenter Roeck <linux@roeck-us.net>
7  *
8  * Derived from:
9  *  pca954x.c
10  *
11  *  Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
12  *  Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
13  *
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.
17  */
18 
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>
26 
27 #include <linux/i2c/pca954x.h>
28 
29 /*
30  * The PCA9541 is a bus master selector. It supports two I2C masters connected
31  * to a single slave bus.
32  *
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.
38  *
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.
42  */
43 
44 #define PCA9541_CONTROL		0x01
45 #define PCA9541_ISTAT		0x02
46 
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)
54 
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)
61 
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)
66 
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 */
70 
71 /* arbitration retry delays, in us */
72 #define SELECT_DELAY_SHORT	50
73 #define SELECT_DELAY_LONG	1000
74 
75 struct pca9541 {
76 	struct i2c_client *client;
77 	unsigned long select_timeout;
78 	unsigned long arb_timeout;
79 };
80 
81 static const struct i2c_device_id pca9541_id[] = {
82 	{"pca9541", 0},
83 	{}
84 };
85 
86 MODULE_DEVICE_TABLE(i2c, pca9541_id);
87 
88 #ifdef CONFIG_OF
89 static const struct of_device_id pca9541_of_match[] = {
90 	{ .compatible = "nxp,pca9541" },
91 	{}
92 };
93 MODULE_DEVICE_TABLE(of, pca9541_of_match);
94 #endif
95 
96 /*
97  * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
98  * as they will try to lock the adapter a second time.
99  */
100 static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
101 {
102 	struct i2c_adapter *adap = client->adapter;
103 	int ret;
104 
105 	if (adap->algo->master_xfer) {
106 		struct i2c_msg msg;
107 		char buf[2];
108 
109 		msg.addr = client->addr;
110 		msg.flags = 0;
111 		msg.len = 2;
112 		buf[0] = command;
113 		buf[1] = val;
114 		msg.buf = buf;
115 		ret = __i2c_transfer(adap, &msg, 1);
116 	} else {
117 		union i2c_smbus_data data;
118 
119 		data.byte = val;
120 		ret = adap->algo->smbus_xfer(adap, client->addr,
121 					     client->flags,
122 					     I2C_SMBUS_WRITE,
123 					     command,
124 					     I2C_SMBUS_BYTE_DATA, &data);
125 	}
126 
127 	return ret;
128 }
129 
130 /*
131  * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
132  * as they will try to lock adapter a second time.
133  */
134 static int pca9541_reg_read(struct i2c_client *client, u8 command)
135 {
136 	struct i2c_adapter *adap = client->adapter;
137 	int ret;
138 	u8 val;
139 
140 	if (adap->algo->master_xfer) {
141 		struct i2c_msg msg[2] = {
142 			{
143 				.addr = client->addr,
144 				.flags = 0,
145 				.len = 1,
146 				.buf = &command
147 			},
148 			{
149 				.addr = client->addr,
150 				.flags = I2C_M_RD,
151 				.len = 1,
152 				.buf = &val
153 			}
154 		};
155 		ret = __i2c_transfer(adap, msg, 2);
156 		if (ret == 2)
157 			ret = val;
158 		else if (ret >= 0)
159 			ret = -EIO;
160 	} else {
161 		union i2c_smbus_data data;
162 
163 		ret = adap->algo->smbus_xfer(adap, client->addr,
164 					     client->flags,
165 					     I2C_SMBUS_READ,
166 					     command,
167 					     I2C_SMBUS_BYTE_DATA, &data);
168 		if (!ret)
169 			ret = data.byte;
170 	}
171 	return ret;
172 }
173 
174 /*
175  * Arbitration management functions
176  */
177 
178 /* Release bus. Also reset NTESTON and BUSINIT if it was set. */
179 static void pca9541_release_bus(struct i2c_client *client)
180 {
181 	int reg;
182 
183 	reg = pca9541_reg_read(client, PCA9541_CONTROL);
184 	if (reg >= 0 && !busoff(reg) && mybus(reg))
185 		pca9541_reg_write(client, PCA9541_CONTROL,
186 				  (reg & PCA9541_CTL_NBUSON) >> 1);
187 }
188 
189 /*
190  * Arbitration is defined as a two-step process. A bus master can only activate
191  * the slave bus if it owns it; otherwise it has to request ownership first.
192  * This multi-step process ensures that access contention is resolved
193  * gracefully.
194  *
195  * Bus	Ownership	Other master	Action
196  * state		requested access
197  * ----------------------------------------------------
198  * off	-		yes		wait for arbitration timeout or
199  *					for other master to drop request
200  * off	no		no		take ownership
201  * off	yes		no		turn on bus
202  * on	yes		-		done
203  * on	no		-		wait for arbitration timeout or
204  *					for other master to release bus
205  *
206  * The main contention point occurs if the slave bus is off and both masters
207  * request ownership at the same time. In this case, one master will turn on
208  * the slave bus, believing that it owns it. The other master will request
209  * bus ownership. Result is that the bus is turned on, and master which did
210  * _not_ own the slave bus before ends up owning it.
211  */
212 
213 /* Control commands per PCA9541 datasheet */
214 static const u8 pca9541_control[16] = {
215 	4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
216 };
217 
218 /*
219  * Channel arbitration
220  *
221  * Return values:
222  *  <0: error
223  *  0 : bus not acquired
224  *  1 : bus acquired
225  */
226 static int pca9541_arbitrate(struct i2c_client *client)
227 {
228 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
229 	struct pca9541 *data = i2c_mux_priv(muxc);
230 	int reg;
231 
232 	reg = pca9541_reg_read(client, PCA9541_CONTROL);
233 	if (reg < 0)
234 		return reg;
235 
236 	if (busoff(reg)) {
237 		int istat;
238 		/*
239 		 * Bus is off. Request ownership or turn it on unless
240 		 * other master requested ownership.
241 		 */
242 		istat = pca9541_reg_read(client, PCA9541_ISTAT);
243 		if (!(istat & PCA9541_ISTAT_NMYTEST)
244 		    || time_is_before_eq_jiffies(data->arb_timeout)) {
245 			/*
246 			 * Other master did not request ownership,
247 			 * or arbitration timeout expired. Take the bus.
248 			 */
249 			pca9541_reg_write(client,
250 					  PCA9541_CONTROL,
251 					  pca9541_control[reg & 0x0f]
252 					  | PCA9541_CTL_NTESTON);
253 			data->select_timeout = SELECT_DELAY_SHORT;
254 		} else {
255 			/*
256 			 * Other master requested ownership.
257 			 * Set extra long timeout to give it time to acquire it.
258 			 */
259 			data->select_timeout = SELECT_DELAY_LONG * 2;
260 		}
261 	} else if (mybus(reg)) {
262 		/*
263 		 * Bus is on, and we own it. We are done with acquisition.
264 		 * Reset NTESTON and BUSINIT, then return success.
265 		 */
266 		if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
267 			pca9541_reg_write(client,
268 					  PCA9541_CONTROL,
269 					  reg & ~(PCA9541_CTL_NTESTON
270 						  | PCA9541_CTL_BUSINIT));
271 		return 1;
272 	} else {
273 		/*
274 		 * Other master owns the bus.
275 		 * If arbitration timeout has expired, force ownership.
276 		 * Otherwise request it.
277 		 */
278 		data->select_timeout = SELECT_DELAY_LONG;
279 		if (time_is_before_eq_jiffies(data->arb_timeout)) {
280 			/* Time is up, take the bus and reset it. */
281 			pca9541_reg_write(client,
282 					  PCA9541_CONTROL,
283 					  pca9541_control[reg & 0x0f]
284 					  | PCA9541_CTL_BUSINIT
285 					  | PCA9541_CTL_NTESTON);
286 		} else {
287 			/* Request bus ownership if needed */
288 			if (!(reg & PCA9541_CTL_NTESTON))
289 				pca9541_reg_write(client,
290 						  PCA9541_CONTROL,
291 						  reg | PCA9541_CTL_NTESTON);
292 		}
293 	}
294 	return 0;
295 }
296 
297 static int pca9541_select_chan(struct i2c_mux_core *muxc, u32 chan)
298 {
299 	struct pca9541 *data = i2c_mux_priv(muxc);
300 	struct i2c_client *client = data->client;
301 	int ret;
302 	unsigned long timeout = jiffies + ARB2_TIMEOUT;
303 		/* give up after this time */
304 
305 	data->arb_timeout = jiffies + ARB_TIMEOUT;
306 		/* force bus ownership after this time */
307 
308 	do {
309 		ret = pca9541_arbitrate(client);
310 		if (ret)
311 			return ret < 0 ? ret : 0;
312 
313 		if (data->select_timeout == SELECT_DELAY_SHORT)
314 			udelay(data->select_timeout);
315 		else
316 			msleep(data->select_timeout / 1000);
317 	} while (time_is_after_eq_jiffies(timeout));
318 
319 	return -ETIMEDOUT;
320 }
321 
322 static int pca9541_release_chan(struct i2c_mux_core *muxc, u32 chan)
323 {
324 	struct pca9541 *data = i2c_mux_priv(muxc);
325 	struct i2c_client *client = data->client;
326 
327 	pca9541_release_bus(client);
328 	return 0;
329 }
330 
331 /*
332  * I2C init/probing/exit functions
333  */
334 static int pca9541_probe(struct i2c_client *client,
335 			 const struct i2c_device_id *id)
336 {
337 	struct i2c_adapter *adap = client->adapter;
338 	struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
339 	struct i2c_mux_core *muxc;
340 	struct pca9541 *data;
341 	int force;
342 	int ret;
343 
344 	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
345 		return -ENODEV;
346 
347 	/*
348 	 * I2C accesses are unprotected here.
349 	 * We have to lock the adapter before releasing the bus.
350 	 */
351 	i2c_lock_adapter(adap);
352 	pca9541_release_bus(client);
353 	i2c_unlock_adapter(adap);
354 
355 	/* Create mux adapter */
356 
357 	force = 0;
358 	if (pdata)
359 		force = pdata->modes[0].adap_id;
360 	muxc = i2c_mux_alloc(adap, &client->dev, 1, sizeof(*data),
361 			     I2C_MUX_ARBITRATOR,
362 			     pca9541_select_chan, pca9541_release_chan);
363 	if (!muxc)
364 		return -ENOMEM;
365 
366 	data = i2c_mux_priv(muxc);
367 	data->client = client;
368 
369 	i2c_set_clientdata(client, muxc);
370 
371 	ret = i2c_mux_add_adapter(muxc, force, 0, 0);
372 	if (ret) {
373 		dev_err(&client->dev, "failed to register master selector\n");
374 		return ret;
375 	}
376 
377 	dev_info(&client->dev, "registered master selector for I2C %s\n",
378 		 client->name);
379 
380 	return 0;
381 }
382 
383 static int pca9541_remove(struct i2c_client *client)
384 {
385 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
386 
387 	i2c_mux_del_adapters(muxc);
388 	return 0;
389 }
390 
391 static struct i2c_driver pca9541_driver = {
392 	.driver = {
393 		   .name = "pca9541",
394 		   .of_match_table = of_match_ptr(pca9541_of_match),
395 		   },
396 	.probe = pca9541_probe,
397 	.remove = pca9541_remove,
398 	.id_table = pca9541_id,
399 };
400 
401 module_i2c_driver(pca9541_driver);
402 
403 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
404 MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
405 MODULE_LICENSE("GPL v2");
406