xref: /linux/drivers/i2c/i2c-mux.c (revision 25083d5c01ec56046acfa84b2db084aec0275810)
1 /*
2  * Multiplexed I2C bus driver.
3  *
4  * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
5  * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
6  * Copyright (c) 2009-2010 NSN GmbH & Co KG <michael.lawnick.ext@nsn.com>
7  *
8  * Simplifies access to complex multiplexed I2C bus topologies, by presenting
9  * each multiplexed bus segment as an additional I2C adapter.
10  * Supports multi-level mux'ing (mux behind a mux).
11  *
12  * Based on:
13  *	i2c-virt.c from Kumar Gala <galak@kernel.crashing.org>
14  *	i2c-virtual.c from Ken Harrenstien, Copyright (c) 2004 Google, Inc.
15  *	i2c-virtual.c from Brian Kuschak <bkuschak@yahoo.com>
16  *
17  * This file is licensed under the terms of the GNU General Public
18  * License version 2. This program is licensed "as is" without any
19  * warranty of any kind, whether express or implied.
20  */
21 
22 #include <linux/acpi.h>
23 #include <linux/i2c.h>
24 #include <linux/i2c-mux.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/property.h>
28 #include <linux/slab.h>
29 #include <linux/sysfs.h>
30 
31 /* multiplexer per channel data */
32 struct i2c_mux_priv {
33 	struct i2c_adapter adap;
34 	struct i2c_algorithm algo;
35 	struct i2c_mux_core *muxc;
36 	struct fwnode_handle *channel_node;
37 	u32 chan_id;
38 };
39 
40 static int __i2c_mux_master_xfer(struct i2c_adapter *adap,
41 				 struct i2c_msg msgs[], int num)
42 {
43 	struct i2c_mux_priv *priv = adap->algo_data;
44 	struct i2c_mux_core *muxc = priv->muxc;
45 	struct i2c_adapter *parent = muxc->parent;
46 	int ret;
47 
48 	/* Switch to the right mux port and perform the transfer. */
49 
50 	ret = muxc->select(muxc, priv->chan_id);
51 	if (ret >= 0)
52 		ret = __i2c_transfer(parent, msgs, num);
53 	if (muxc->deselect)
54 		muxc->deselect(muxc, priv->chan_id);
55 
56 	return ret;
57 }
58 
59 static int i2c_mux_master_xfer(struct i2c_adapter *adap,
60 			       struct i2c_msg msgs[], int num)
61 {
62 	struct i2c_mux_priv *priv = adap->algo_data;
63 	struct i2c_mux_core *muxc = priv->muxc;
64 	struct i2c_adapter *parent = muxc->parent;
65 	int ret;
66 
67 	/* Switch to the right mux port and perform the transfer. */
68 
69 	ret = muxc->select(muxc, priv->chan_id);
70 	if (ret >= 0)
71 		ret = i2c_transfer(parent, msgs, num);
72 	if (muxc->deselect)
73 		muxc->deselect(muxc, priv->chan_id);
74 
75 	return ret;
76 }
77 
78 static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
79 				u16 addr, unsigned short flags,
80 				char read_write, u8 command,
81 				int size, union i2c_smbus_data *data)
82 {
83 	struct i2c_mux_priv *priv = adap->algo_data;
84 	struct i2c_mux_core *muxc = priv->muxc;
85 	struct i2c_adapter *parent = muxc->parent;
86 	int ret;
87 
88 	/* Select the right mux port and perform the transfer. */
89 
90 	ret = muxc->select(muxc, priv->chan_id);
91 	if (ret >= 0)
92 		ret = __i2c_smbus_xfer(parent, addr, flags,
93 				       read_write, command, size, data);
94 	if (muxc->deselect)
95 		muxc->deselect(muxc, priv->chan_id);
96 
97 	return ret;
98 }
99 
100 static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
101 			      u16 addr, unsigned short flags,
102 			      char read_write, u8 command,
103 			      int size, union i2c_smbus_data *data)
104 {
105 	struct i2c_mux_priv *priv = adap->algo_data;
106 	struct i2c_mux_core *muxc = priv->muxc;
107 	struct i2c_adapter *parent = muxc->parent;
108 	int ret;
109 
110 	/* Select the right mux port and perform the transfer. */
111 
112 	ret = muxc->select(muxc, priv->chan_id);
113 	if (ret >= 0)
114 		ret = i2c_smbus_xfer(parent, addr, flags,
115 				     read_write, command, size, data);
116 	if (muxc->deselect)
117 		muxc->deselect(muxc, priv->chan_id);
118 
119 	return ret;
120 }
121 
122 /* Return the parent's functionality */
123 static u32 i2c_mux_functionality(struct i2c_adapter *adap)
124 {
125 	struct i2c_mux_priv *priv = adap->algo_data;
126 	struct i2c_adapter *parent = priv->muxc->parent;
127 
128 	return parent->algo->functionality(parent);
129 }
130 
131 static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
132 {
133 	struct i2c_mux_priv *priv = adapter->algo_data;
134 	struct i2c_adapter *parent = priv->muxc->parent;
135 
136 	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
137 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
138 		return;
139 	i2c_lock_bus(parent, flags);
140 }
141 
142 static int i2c_mux_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
143 {
144 	struct i2c_mux_priv *priv = adapter->algo_data;
145 	struct i2c_adapter *parent = priv->muxc->parent;
146 
147 	if (!rt_mutex_trylock(&parent->mux_lock))
148 		return 0;	/* mux_lock not locked, failure */
149 	if (!(flags & I2C_LOCK_ROOT_ADAPTER))
150 		return 1;	/* we only want mux_lock, success */
151 	if (i2c_trylock_bus(parent, flags))
152 		return 1;	/* parent locked too, success */
153 	rt_mutex_unlock(&parent->mux_lock);
154 	return 0;		/* parent not locked, failure */
155 }
156 
157 static void i2c_mux_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
158 {
159 	struct i2c_mux_priv *priv = adapter->algo_data;
160 	struct i2c_adapter *parent = priv->muxc->parent;
161 
162 	if (flags & I2C_LOCK_ROOT_ADAPTER)
163 		i2c_unlock_bus(parent, flags);
164 	rt_mutex_unlock(&parent->mux_lock);
165 }
166 
167 static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
168 				unsigned int flags)
169 {
170 	struct i2c_mux_priv *priv = adapter->algo_data;
171 	struct i2c_adapter *parent = priv->muxc->parent;
172 
173 	rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
174 	i2c_lock_bus(parent, flags);
175 }
176 
177 static int i2c_parent_trylock_bus(struct i2c_adapter *adapter,
178 				  unsigned int flags)
179 {
180 	struct i2c_mux_priv *priv = adapter->algo_data;
181 	struct i2c_adapter *parent = priv->muxc->parent;
182 
183 	if (!rt_mutex_trylock(&parent->mux_lock))
184 		return 0;	/* mux_lock not locked, failure */
185 	if (i2c_trylock_bus(parent, flags))
186 		return 1;	/* parent locked too, success */
187 	rt_mutex_unlock(&parent->mux_lock);
188 	return 0;		/* parent not locked, failure */
189 }
190 
191 static void i2c_parent_unlock_bus(struct i2c_adapter *adapter,
192 				  unsigned int flags)
193 {
194 	struct i2c_mux_priv *priv = adapter->algo_data;
195 	struct i2c_adapter *parent = priv->muxc->parent;
196 
197 	i2c_unlock_bus(parent, flags);
198 	rt_mutex_unlock(&parent->mux_lock);
199 }
200 
201 struct i2c_adapter *i2c_root_adapter(struct device *dev)
202 {
203 	struct device *i2c;
204 	struct i2c_adapter *i2c_root;
205 
206 	/*
207 	 * Walk up the device tree to find an i2c adapter, indicating
208 	 * that this is an i2c client device. Check all ancestors to
209 	 * handle mfd devices etc.
210 	 */
211 	for (i2c = dev; i2c; i2c = i2c->parent) {
212 		if (i2c->type == &i2c_adapter_type)
213 			break;
214 	}
215 	if (!i2c)
216 		return NULL;
217 
218 	/* Continue up the tree to find the root i2c adapter */
219 	i2c_root = to_i2c_adapter(i2c);
220 	while (i2c_parent_is_i2c_adapter(i2c_root))
221 		i2c_root = i2c_parent_is_i2c_adapter(i2c_root);
222 
223 	return i2c_root;
224 }
225 EXPORT_SYMBOL_GPL(i2c_root_adapter);
226 
227 struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
228 				   struct device *dev, int max_adapters,
229 				   int sizeof_priv, u32 flags,
230 				   int (*select)(struct i2c_mux_core *, u32),
231 				   int (*deselect)(struct i2c_mux_core *, u32))
232 {
233 	struct i2c_mux_core *muxc;
234 	size_t mux_size;
235 
236 	mux_size = struct_size(muxc, adapter, max_adapters);
237 	muxc = devm_kzalloc(dev, size_add(mux_size, sizeof_priv), GFP_KERNEL);
238 	if (!muxc)
239 		return NULL;
240 	if (sizeof_priv)
241 		muxc->priv = &muxc->adapter[max_adapters];
242 
243 	muxc->parent = parent;
244 	muxc->dev = dev;
245 	muxc->mux_locked = !!(flags & I2C_MUX_LOCKED);
246 	muxc->arbitrator = !!(flags & I2C_MUX_ARBITRATOR);
247 	muxc->gate = !!(flags & I2C_MUX_GATE);
248 	muxc->select = select;
249 	muxc->deselect = deselect;
250 	muxc->max_adapters = max_adapters;
251 
252 	return muxc;
253 }
254 EXPORT_SYMBOL_GPL(i2c_mux_alloc);
255 
256 static const struct i2c_lock_operations i2c_mux_lock_ops = {
257 	.lock_bus =    i2c_mux_lock_bus,
258 	.trylock_bus = i2c_mux_trylock_bus,
259 	.unlock_bus =  i2c_mux_unlock_bus,
260 };
261 
262 static const struct i2c_lock_operations i2c_parent_lock_ops = {
263 	.lock_bus =    i2c_parent_lock_bus,
264 	.trylock_bus = i2c_parent_trylock_bus,
265 	.unlock_bus =  i2c_parent_unlock_bus,
266 };
267 
268 static struct fwnode_handle *
269 i2c_mux_get_channel_node(struct i2c_mux_core *muxc, u32 chan_id)
270 {
271 	struct fwnode_handle *dev_node;
272 	struct fwnode_handle *mux_node;
273 	struct fwnode_handle *child;
274 	u32 reg;
275 	int ret;
276 
277 	dev_node = dev_fwnode(muxc->dev);
278 	if (!dev_node)
279 		return NULL;
280 
281 	if (muxc->arbitrator)
282 		mux_node = fwnode_get_named_child_node(dev_node, "i2c-arb");
283 	else if (muxc->gate)
284 		mux_node = fwnode_get_named_child_node(dev_node, "i2c-gate");
285 	else
286 		mux_node = fwnode_get_named_child_node(dev_node, "i2c-mux");
287 
288 	if (mux_node) {
289 		/* A "reg" property indicates an old-style firmware entry. */
290 		if (!fwnode_property_read_u32(mux_node, "reg", &reg)) {
291 			fwnode_handle_put(mux_node);
292 			mux_node = NULL;
293 		}
294 	}
295 
296 	if (!mux_node)
297 		mux_node = fwnode_handle_get(dev_node);
298 	else if (muxc->arbitrator || muxc->gate)
299 		return mux_node;
300 
301 	fwnode_for_each_child_node(mux_node, child) {
302 		ret = fwnode_property_read_u32(child, "reg", &reg);
303 		if (ret)
304 			continue;
305 		if (chan_id == reg)
306 			break;
307 	}
308 
309 	fwnode_handle_put(mux_node);
310 	return child;
311 }
312 
313 int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
314 			u32 force_nr, u32 chan_id)
315 {
316 	struct i2c_adapter *parent = muxc->parent;
317 	struct i2c_mux_priv *priv;
318 	char symlink_name[20];
319 	int ret;
320 
321 	if (muxc->num_adapters >= muxc->max_adapters) {
322 		dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
323 		return -EINVAL;
324 	}
325 
326 	priv = kzalloc_obj(*priv);
327 	if (!priv)
328 		return -ENOMEM;
329 
330 	/* Set up private adapter data */
331 	priv->muxc = muxc;
332 	priv->chan_id = chan_id;
333 
334 	/* Need to do algo dynamically because we don't know ahead
335 	 * of time what sort of physical adapter we'll be dealing with.
336 	 */
337 	if (parent->algo->master_xfer) {
338 		if (muxc->mux_locked)
339 			priv->algo.xfer = i2c_mux_master_xfer;
340 		else
341 			priv->algo.xfer = __i2c_mux_master_xfer;
342 	}
343 	if (parent->algo->master_xfer_atomic)
344 		priv->algo.xfer_atomic = priv->algo.master_xfer;
345 
346 	if (parent->algo->smbus_xfer) {
347 		if (muxc->mux_locked)
348 			priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
349 		else
350 			priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
351 	}
352 	if (parent->algo->smbus_xfer_atomic)
353 		priv->algo.smbus_xfer_atomic = priv->algo.smbus_xfer;
354 
355 	priv->algo.functionality = i2c_mux_functionality;
356 
357 	/* Now fill out new adapter structure */
358 	snprintf(priv->adap.name, sizeof(priv->adap.name),
359 		 "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
360 	priv->adap.owner = THIS_MODULE;
361 	priv->adap.algo = &priv->algo;
362 	priv->adap.algo_data = priv;
363 	priv->adap.dev.parent = &parent->dev;
364 	priv->adap.retries = parent->retries;
365 	priv->adap.timeout = parent->timeout;
366 	priv->adap.quirks = parent->quirks;
367 	if (muxc->mux_locked)
368 		priv->adap.lock_ops = &i2c_mux_lock_ops;
369 	else
370 		priv->adap.lock_ops = &i2c_parent_lock_ops;
371 
372 	/* Associate the mux adapter with its channel firmware node. */
373 	priv->channel_node = i2c_mux_get_channel_node(muxc, chan_id);
374 	device_set_node(&priv->adap.dev, priv->channel_node);
375 
376 	/*
377 	 * Associate the mux channel with an ACPI node.
378 	 */
379 	if (has_acpi_companion(muxc->dev))
380 		acpi_preset_companion(&priv->adap.dev,
381 				      ACPI_COMPANION(muxc->dev),
382 				      chan_id);
383 
384 	if (force_nr) {
385 		priv->adap.nr = force_nr;
386 		ret = i2c_add_numbered_adapter(&priv->adap);
387 		if (ret < 0) {
388 			dev_err(&parent->dev,
389 				"failed to add mux-adapter %u as bus %u (error=%d)\n",
390 				chan_id, force_nr, ret);
391 			goto err_free_priv;
392 		}
393 	} else {
394 		ret = i2c_add_adapter(&priv->adap);
395 		if (ret < 0) {
396 			dev_err(&parent->dev,
397 				"failed to add mux-adapter %u (error=%d)\n",
398 				chan_id, ret);
399 			goto err_free_priv;
400 		}
401 	}
402 
403 	WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
404 			       "mux_device"),
405 	     "can't create symlink to mux device\n");
406 
407 	snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
408 	WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
409 			       symlink_name),
410 	     "can't create symlink to channel %u\n", chan_id);
411 	dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
412 		 i2c_adapter_id(&priv->adap));
413 
414 	muxc->adapter[muxc->num_adapters++] = &priv->adap;
415 	return 0;
416 
417 err_free_priv:
418 	fwnode_handle_put(priv->channel_node);
419 	kfree(priv);
420 	return ret;
421 }
422 EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
423 
424 void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
425 {
426 	char symlink_name[20];
427 
428 	while (muxc->num_adapters) {
429 		struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
430 		struct i2c_mux_priv *priv = adap->algo_data;
431 
432 		muxc->adapter[muxc->num_adapters] = NULL;
433 
434 		snprintf(symlink_name, sizeof(symlink_name),
435 			 "channel-%u", priv->chan_id);
436 		sysfs_remove_link(&muxc->dev->kobj, symlink_name);
437 
438 		sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
439 		i2c_del_adapter(adap);
440 		fwnode_handle_put(priv->channel_node);
441 		kfree(priv);
442 	}
443 }
444 EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
445 
446 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
447 MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
448 MODULE_LICENSE("GPL v2");
449