Lines Matching +full:i2c +full:- +full:gate
2 * Multiplexed I2C bus driver.
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>
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).
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>
23 #include <linux/i2c.h>
24 #include <linux/i2c-mux.h>
42 struct i2c_mux_priv *priv = adap->algo_data; in __i2c_mux_master_xfer()
43 struct i2c_mux_core *muxc = priv->muxc; in __i2c_mux_master_xfer()
44 struct i2c_adapter *parent = muxc->parent; in __i2c_mux_master_xfer()
49 ret = muxc->select(muxc, priv->chan_id); in __i2c_mux_master_xfer()
52 if (muxc->deselect) in __i2c_mux_master_xfer()
53 muxc->deselect(muxc, priv->chan_id); in __i2c_mux_master_xfer()
61 struct i2c_mux_priv *priv = adap->algo_data; in i2c_mux_master_xfer()
62 struct i2c_mux_core *muxc = priv->muxc; in i2c_mux_master_xfer()
63 struct i2c_adapter *parent = muxc->parent; in i2c_mux_master_xfer()
68 ret = muxc->select(muxc, priv->chan_id); in i2c_mux_master_xfer()
71 if (muxc->deselect) in i2c_mux_master_xfer()
72 muxc->deselect(muxc, priv->chan_id); in i2c_mux_master_xfer()
82 struct i2c_mux_priv *priv = adap->algo_data; in __i2c_mux_smbus_xfer()
83 struct i2c_mux_core *muxc = priv->muxc; in __i2c_mux_smbus_xfer()
84 struct i2c_adapter *parent = muxc->parent; in __i2c_mux_smbus_xfer()
89 ret = muxc->select(muxc, priv->chan_id); in __i2c_mux_smbus_xfer()
93 if (muxc->deselect) in __i2c_mux_smbus_xfer()
94 muxc->deselect(muxc, priv->chan_id); in __i2c_mux_smbus_xfer()
104 struct i2c_mux_priv *priv = adap->algo_data; in i2c_mux_smbus_xfer()
105 struct i2c_mux_core *muxc = priv->muxc; in i2c_mux_smbus_xfer()
106 struct i2c_adapter *parent = muxc->parent; in i2c_mux_smbus_xfer()
111 ret = muxc->select(muxc, priv->chan_id); in i2c_mux_smbus_xfer()
115 if (muxc->deselect) in i2c_mux_smbus_xfer()
116 muxc->deselect(muxc, priv->chan_id); in i2c_mux_smbus_xfer()
124 struct i2c_mux_priv *priv = adap->algo_data; in i2c_mux_functionality()
125 struct i2c_adapter *parent = priv->muxc->parent; in i2c_mux_functionality()
127 return parent->algo->functionality(parent); in i2c_mux_functionality()
136 class |= parent->class; in i2c_mux_parent_classes()
145 struct i2c_mux_priv *priv = adapter->algo_data; in i2c_mux_lock_bus()
146 struct i2c_adapter *parent = priv->muxc->parent; in i2c_mux_lock_bus()
148 rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter)); in i2c_mux_lock_bus()
156 struct i2c_mux_priv *priv = adapter->algo_data; in i2c_mux_trylock_bus()
157 struct i2c_adapter *parent = priv->muxc->parent; in i2c_mux_trylock_bus()
159 if (!rt_mutex_trylock(&parent->mux_lock)) in i2c_mux_trylock_bus()
165 rt_mutex_unlock(&parent->mux_lock); in i2c_mux_trylock_bus()
171 struct i2c_mux_priv *priv = adapter->algo_data; in i2c_mux_unlock_bus()
172 struct i2c_adapter *parent = priv->muxc->parent; in i2c_mux_unlock_bus()
176 rt_mutex_unlock(&parent->mux_lock); in i2c_mux_unlock_bus()
182 struct i2c_mux_priv *priv = adapter->algo_data; in i2c_parent_lock_bus()
183 struct i2c_adapter *parent = priv->muxc->parent; in i2c_parent_lock_bus()
185 rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter)); in i2c_parent_lock_bus()
192 struct i2c_mux_priv *priv = adapter->algo_data; in i2c_parent_trylock_bus()
193 struct i2c_adapter *parent = priv->muxc->parent; in i2c_parent_trylock_bus()
195 if (!rt_mutex_trylock(&parent->mux_lock)) in i2c_parent_trylock_bus()
199 rt_mutex_unlock(&parent->mux_lock); in i2c_parent_trylock_bus()
206 struct i2c_mux_priv *priv = adapter->algo_data; in i2c_parent_unlock_bus()
207 struct i2c_adapter *parent = priv->muxc->parent; in i2c_parent_unlock_bus()
210 rt_mutex_unlock(&parent->mux_lock); in i2c_parent_unlock_bus()
215 struct device *i2c; in i2c_root_adapter() local
219 * Walk up the device tree to find an i2c adapter, indicating in i2c_root_adapter()
220 * that this is an i2c client device. Check all ancestors to in i2c_root_adapter()
223 for (i2c = dev; i2c; i2c = i2c->parent) { in i2c_root_adapter()
224 if (i2c->type == &i2c_adapter_type) in i2c_root_adapter()
227 if (!i2c) in i2c_root_adapter()
230 /* Continue up the tree to find the root i2c adapter */ in i2c_root_adapter()
231 i2c_root = to_i2c_adapter(i2c); in i2c_root_adapter()
253 muxc->priv = &muxc->adapter[max_adapters]; in i2c_mux_alloc()
255 muxc->parent = parent; in i2c_mux_alloc()
256 muxc->dev = dev; in i2c_mux_alloc()
258 muxc->mux_locked = true; in i2c_mux_alloc()
260 muxc->arbitrator = true; in i2c_mux_alloc()
262 muxc->gate = true; in i2c_mux_alloc()
263 muxc->select = select; in i2c_mux_alloc()
264 muxc->deselect = deselect; in i2c_mux_alloc()
265 muxc->max_adapters = max_adapters; in i2c_mux_alloc()
287 struct i2c_adapter *parent = muxc->parent; in i2c_mux_add_adapter()
292 if (muxc->num_adapters >= muxc->max_adapters) { in i2c_mux_add_adapter()
293 dev_err(muxc->dev, "No room for more i2c-mux adapters\n"); in i2c_mux_add_adapter()
294 return -EINVAL; in i2c_mux_add_adapter()
299 return -ENOMEM; in i2c_mux_add_adapter()
302 priv->muxc = muxc; in i2c_mux_add_adapter()
303 priv->chan_id = chan_id; in i2c_mux_add_adapter()
308 if (parent->algo->master_xfer) { in i2c_mux_add_adapter()
309 if (muxc->mux_locked) in i2c_mux_add_adapter()
310 priv->algo.master_xfer = i2c_mux_master_xfer; in i2c_mux_add_adapter()
312 priv->algo.master_xfer = __i2c_mux_master_xfer; in i2c_mux_add_adapter()
314 if (parent->algo->master_xfer_atomic) in i2c_mux_add_adapter()
315 priv->algo.master_xfer_atomic = priv->algo.master_xfer; in i2c_mux_add_adapter()
317 if (parent->algo->smbus_xfer) { in i2c_mux_add_adapter()
318 if (muxc->mux_locked) in i2c_mux_add_adapter()
319 priv->algo.smbus_xfer = i2c_mux_smbus_xfer; in i2c_mux_add_adapter()
321 priv->algo.smbus_xfer = __i2c_mux_smbus_xfer; in i2c_mux_add_adapter()
323 if (parent->algo->smbus_xfer_atomic) in i2c_mux_add_adapter()
324 priv->algo.smbus_xfer_atomic = priv->algo.smbus_xfer; in i2c_mux_add_adapter()
326 priv->algo.functionality = i2c_mux_functionality; in i2c_mux_add_adapter()
329 snprintf(priv->adap.name, sizeof(priv->adap.name), in i2c_mux_add_adapter()
330 "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id); in i2c_mux_add_adapter()
331 priv->adap.owner = THIS_MODULE; in i2c_mux_add_adapter()
332 priv->adap.algo = &priv->algo; in i2c_mux_add_adapter()
333 priv->adap.algo_data = priv; in i2c_mux_add_adapter()
334 priv->adap.dev.parent = &parent->dev; in i2c_mux_add_adapter()
335 priv->adap.retries = parent->retries; in i2c_mux_add_adapter()
336 priv->adap.timeout = parent->timeout; in i2c_mux_add_adapter()
337 priv->adap.quirks = parent->quirks; in i2c_mux_add_adapter()
338 if (muxc->mux_locked) in i2c_mux_add_adapter()
339 priv->adap.lock_ops = &i2c_mux_lock_ops; in i2c_mux_add_adapter()
341 priv->adap.lock_ops = &i2c_parent_lock_ops; in i2c_mux_add_adapter()
345 dev_err(&parent->dev, in i2c_mux_add_adapter()
349 priv->adap.class = class; in i2c_mux_add_adapter()
355 if (muxc->dev->of_node) { in i2c_mux_add_adapter()
356 struct device_node *dev_node = muxc->dev->of_node; in i2c_mux_add_adapter()
360 if (muxc->arbitrator) in i2c_mux_add_adapter()
361 mux_node = of_get_child_by_name(dev_node, "i2c-arb"); in i2c_mux_add_adapter()
362 else if (muxc->gate) in i2c_mux_add_adapter()
363 mux_node = of_get_child_by_name(dev_node, "i2c-gate"); in i2c_mux_add_adapter()
365 mux_node = of_get_child_by_name(dev_node, "i2c-mux"); in i2c_mux_add_adapter()
368 /* A "reg" property indicates an old-style DT entry */ in i2c_mux_add_adapter()
377 else if (muxc->arbitrator || muxc->gate) in i2c_mux_add_adapter()
390 priv->adap.dev.of_node = child; in i2c_mux_add_adapter()
397 if (has_acpi_companion(muxc->dev)) in i2c_mux_add_adapter()
398 acpi_preset_companion(&priv->adap.dev, in i2c_mux_add_adapter()
399 ACPI_COMPANION(muxc->dev), in i2c_mux_add_adapter()
403 priv->adap.nr = force_nr; in i2c_mux_add_adapter()
404 ret = i2c_add_numbered_adapter(&priv->adap); in i2c_mux_add_adapter()
406 dev_err(&parent->dev, in i2c_mux_add_adapter()
407 "failed to add mux-adapter %u as bus %u (error=%d)\n", in i2c_mux_add_adapter()
412 ret = i2c_add_adapter(&priv->adap); in i2c_mux_add_adapter()
414 dev_err(&parent->dev, in i2c_mux_add_adapter()
415 "failed to add mux-adapter %u (error=%d)\n", in i2c_mux_add_adapter()
421 WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj, in i2c_mux_add_adapter()
425 snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id); in i2c_mux_add_adapter()
426 WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj, in i2c_mux_add_adapter()
429 dev_info(&parent->dev, "Added multiplexed i2c bus %d\n", in i2c_mux_add_adapter()
430 i2c_adapter_id(&priv->adap)); in i2c_mux_add_adapter()
432 muxc->adapter[muxc->num_adapters++] = &priv->adap; in i2c_mux_add_adapter()
445 while (muxc->num_adapters) { in i2c_mux_del_adapters()
446 struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters]; in i2c_mux_del_adapters()
447 struct i2c_mux_priv *priv = adap->algo_data; in i2c_mux_del_adapters()
448 struct device_node *np = adap->dev.of_node; in i2c_mux_del_adapters()
450 muxc->adapter[muxc->num_adapters] = NULL; in i2c_mux_del_adapters()
453 "channel-%u", priv->chan_id); in i2c_mux_del_adapters()
454 sysfs_remove_link(&muxc->dev->kobj, symlink_name); in i2c_mux_del_adapters()
456 sysfs_remove_link(&priv->adap.dev.kobj, "mux_device"); in i2c_mux_del_adapters()
465 MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");