File Index
Files and directories
Sign in
FILE

i2c-mux-pca9541.c

File preview with syntax highlighting.

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