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
75struct pca9541 {
76	struct i2c_adapter *mux_adap;
77	unsigned long select_timeout;
78	unsigned long arb_timeout;
79};
80
81static const struct i2c_device_id pca9541_id[] = {
82	{"pca9541", 0},
83	{}
84};
85
86MODULE_DEVICE_TABLE(i2c, pca9541_id);
87
88/*
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.
91 */
92static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
93{
94	struct i2c_adapter *adap = client->adapter;
95	int ret;
96
97	if (adap->algo->master_xfer) {
98		struct i2c_msg msg;
99		char buf[2];
100
101		msg.addr = client->addr;
102		msg.flags = 0;
103		msg.len = 2;
104		buf[0] = command;
105		buf[1] = val;
106		msg.buf = buf;
107		ret = __i2c_transfer(adap, &msg, 1);
108	} else {
109		union i2c_smbus_data data;
110
111		data.byte = val;
112		ret = adap->algo->smbus_xfer(adap, client->addr,
113					     client->flags,
114					     I2C_SMBUS_WRITE,
115					     command,
116					     I2C_SMBUS_BYTE_DATA, &data);
117	}
118
119	return ret;
120}
121
122/*
123 * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
124 * as they will try to lock adapter a second time.
125 */
126static int pca9541_reg_read(struct i2c_client *client, u8 command)
127{
128	struct i2c_adapter *adap = client->adapter;
129	int ret;
130	u8 val;
131
132	if (adap->algo->master_xfer) {
133		struct i2c_msg msg[2] = {
134			{
135				.addr = client->addr,
136				.flags = 0,
137				.len = 1,
138				.buf = &command
139			},
140			{
141				.addr = client->addr,
142				.flags = I2C_M_RD,
143				.len = 1,
144				.buf = &val
145			}
146		};
147		ret = __i2c_transfer(adap, msg, 2);
148		if (ret == 2)
149			ret = val;
150		else if (ret >= 0)
151			ret = -EIO;
152	} else {
153		union i2c_smbus_data data;
154
155		ret = adap->algo->smbus_xfer(adap, client->addr,
156					     client->flags,
157					     I2C_SMBUS_READ,
158					     command,
159					     I2C_SMBUS_BYTE_DATA, &data);
160		if (!ret)
161			ret = data.byte;
162	}
163	return ret;
164}
165
166/*
167 * Arbitration management functions
168 */
169
170/* Release bus. Also reset NTESTON and BUSINIT if it was set. */
171static void pca9541_release_bus(struct i2c_client *client)
172{
173	int reg;
174
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);
179}
180
181/*
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
185 * gracefully.
186 *
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
194 * on	yes		-		done
195 * on	no		-		wait for arbitration timeout or
196 *					for other master to release bus
197 *
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.
203 */
204
205/* Control commands per PCA9541 datasheet */
206static const u8 pca9541_control[16] = {
207	4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
208};
209
210/*
211 * Channel arbitration
212 *
213 * Return values:
214 *  <0: error
215 *  0 : bus not acquired
216 *  1 : bus acquired
217 */
218static int pca9541_arbitrate(struct i2c_client *client)
219{
220	struct pca9541 *data = i2c_get_clientdata(client);
221	int reg;
222
223	reg = pca9541_reg_read(client, PCA9541_CONTROL);
224	if (reg < 0)
225		return reg;
226
227	if (busoff(reg)) {
228		int istat;
229		/*
230		 * Bus is off. Request ownership or turn it on unless
231		 * other master requested ownership.
232		 */
233		istat = pca9541_reg_read(client, PCA9541_ISTAT);
234		if (!(istat & PCA9541_ISTAT_NMYTEST)
235		    || time_is_before_eq_jiffies(data->arb_timeout)) {
236			/*
237			 * Other master did not request ownership,
238			 * or arbitration timeout expired. Take the bus.
239			 */
240			pca9541_reg_write(client,
241					  PCA9541_CONTROL,
242					  pca9541_control[reg & 0x0f]
243					  | PCA9541_CTL_NTESTON);
244			data->select_timeout = SELECT_DELAY_SHORT;
245		} else {
246			/*
247			 * Other master requested ownership.
248			 * Set extra long timeout to give it time to acquire it.
249			 */
250			data->select_timeout = SELECT_DELAY_LONG * 2;
251		}
252	} else if (mybus(reg)) {
253		/*
254		 * Bus is on, and we own it. We are done with acquisition.
255		 * Reset NTESTON and BUSINIT, then return success.
256		 */
257		if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
258			pca9541_reg_write(client,
259					  PCA9541_CONTROL,
260					  reg & ~(PCA9541_CTL_NTESTON
261						  | PCA9541_CTL_BUSINIT));
262		return 1;
263	} else {
264		/*
265		 * Other master owns the bus.
266		 * If arbitration timeout has expired, force ownership.
267		 * Otherwise request it.
268		 */
269		data->select_timeout = SELECT_DELAY_LONG;
270		if (time_is_before_eq_jiffies(data->arb_timeout)) {
271			/* Time is up, take the bus and reset it. */
272			pca9541_reg_write(client,
273					  PCA9541_CONTROL,
274					  pca9541_control[reg & 0x0f]
275					  | PCA9541_CTL_BUSINIT
276					  | PCA9541_CTL_NTESTON);
277		} else {
278			/* Request bus ownership if needed */
279			if (!(reg & PCA9541_CTL_NTESTON))
280				pca9541_reg_write(client,
281						  PCA9541_CONTROL,
282						  reg | PCA9541_CTL_NTESTON);
283		}
284	}
285	return 0;
286}
287
288static int pca9541_select_chan(struct i2c_adapter *adap, void *client, u32 chan)
289{
290	struct pca9541 *data = i2c_get_clientdata(client);
291	int ret;
292	unsigned long timeout = jiffies + ARB2_TIMEOUT;
293		/* give up after this time */
294
295	data->arb_timeout = jiffies + ARB_TIMEOUT;
296		/* force bus ownership after this time */
297
298	do {
299		ret = pca9541_arbitrate(client);
300		if (ret)
301			return ret < 0 ? ret : 0;
302
303		if (data->select_timeout == SELECT_DELAY_SHORT)
304			udelay(data->select_timeout);
305		else
306			msleep(data->select_timeout / 1000);
307	} while (time_is_after_eq_jiffies(timeout));
308
309	return -ETIMEDOUT;
310}
311
312static int pca9541_release_chan(struct i2c_adapter *adap,
313				void *client, u32 chan)
314{
315	pca9541_release_bus(client);
316	return 0;
317}
318
319/*
320 * I2C init/probing/exit functions
321 */
322static int pca9541_probe(struct i2c_client *client,
323			 const struct i2c_device_id *id)
324{
325	struct i2c_adapter *adap = client->adapter;
326	struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
327	struct pca9541 *data;
328	int force;
329	int ret = -ENODEV;
330
331	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
332		goto err;
333
334	data = kzalloc(sizeof(struct pca9541), GFP_KERNEL);
335	if (!data) {
336		ret = -ENOMEM;
337		goto err;
338	}
339
340	i2c_set_clientdata(client, data);
341
342	/*
343	 * I2C accesses are unprotected here.
344	 * We have to lock the adapter before releasing the bus.
345	 */
346	i2c_lock_adapter(adap);
347	pca9541_release_bus(client);
348	i2c_unlock_adapter(adap);
349
350	/* Create mux adapter */
351
352	force = 0;
353	if (pdata)
354		force = pdata->modes[0].adap_id;
355	data->mux_adap = i2c_add_mux_adapter(adap, &client->dev, client,
356					     force, 0, 0,
357					     pca9541_select_chan,
358					     pca9541_release_chan);
359
360	if (data->mux_adap == NULL) {
361		dev_err(&client->dev, "failed to register master selector\n");
362		goto exit_free;
363	}
364
365	dev_info(&client->dev, "registered master selector for I2C %s\n",
366		 client->name);
367
368	return 0;
369
370exit_free:
371	kfree(data);
372err:
373	return ret;
374}
375
376static int pca9541_remove(struct i2c_client *client)
377{
378	struct pca9541 *data = i2c_get_clientdata(client);
379
380	i2c_del_mux_adapter(data->mux_adap);
381
382	kfree(data);
383	return 0;
384}
385
386static struct i2c_driver pca9541_driver = {
387	.driver = {
388		   .name = "pca9541",
389		   },
390	.probe = pca9541_probe,
391	.remove = pca9541_remove,
392	.id_table = pca9541_id,
393};
394
395module_i2c_driver(pca9541_driver);
396
397MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
398MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
399MODULE_LICENSE("GPL v2");
400