0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <linux/acpi.h>
0023 #include <linux/i2c.h>
0024 #include <linux/i2c-mux.h>
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/of.h>
0028 #include <linux/slab.h>
0029 #include <linux/sysfs.h>
0030
0031
0032 struct i2c_mux_priv {
0033 struct i2c_adapter adap;
0034 struct i2c_algorithm algo;
0035 struct i2c_mux_core *muxc;
0036 u32 chan_id;
0037 };
0038
0039 static int __i2c_mux_master_xfer(struct i2c_adapter *adap,
0040 struct i2c_msg msgs[], int num)
0041 {
0042 struct i2c_mux_priv *priv = adap->algo_data;
0043 struct i2c_mux_core *muxc = priv->muxc;
0044 struct i2c_adapter *parent = muxc->parent;
0045 int ret;
0046
0047
0048
0049 ret = muxc->select(muxc, priv->chan_id);
0050 if (ret >= 0)
0051 ret = __i2c_transfer(parent, msgs, num);
0052 if (muxc->deselect)
0053 muxc->deselect(muxc, priv->chan_id);
0054
0055 return ret;
0056 }
0057
0058 static int i2c_mux_master_xfer(struct i2c_adapter *adap,
0059 struct i2c_msg msgs[], int num)
0060 {
0061 struct i2c_mux_priv *priv = adap->algo_data;
0062 struct i2c_mux_core *muxc = priv->muxc;
0063 struct i2c_adapter *parent = muxc->parent;
0064 int ret;
0065
0066
0067
0068 ret = muxc->select(muxc, priv->chan_id);
0069 if (ret >= 0)
0070 ret = i2c_transfer(parent, msgs, num);
0071 if (muxc->deselect)
0072 muxc->deselect(muxc, priv->chan_id);
0073
0074 return ret;
0075 }
0076
0077 static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
0078 u16 addr, unsigned short flags,
0079 char read_write, u8 command,
0080 int size, union i2c_smbus_data *data)
0081 {
0082 struct i2c_mux_priv *priv = adap->algo_data;
0083 struct i2c_mux_core *muxc = priv->muxc;
0084 struct i2c_adapter *parent = muxc->parent;
0085 int ret;
0086
0087
0088
0089 ret = muxc->select(muxc, priv->chan_id);
0090 if (ret >= 0)
0091 ret = __i2c_smbus_xfer(parent, addr, flags,
0092 read_write, command, size, data);
0093 if (muxc->deselect)
0094 muxc->deselect(muxc, priv->chan_id);
0095
0096 return ret;
0097 }
0098
0099 static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
0100 u16 addr, unsigned short flags,
0101 char read_write, u8 command,
0102 int size, union i2c_smbus_data *data)
0103 {
0104 struct i2c_mux_priv *priv = adap->algo_data;
0105 struct i2c_mux_core *muxc = priv->muxc;
0106 struct i2c_adapter *parent = muxc->parent;
0107 int ret;
0108
0109
0110
0111 ret = muxc->select(muxc, priv->chan_id);
0112 if (ret >= 0)
0113 ret = i2c_smbus_xfer(parent, addr, flags,
0114 read_write, command, size, data);
0115 if (muxc->deselect)
0116 muxc->deselect(muxc, priv->chan_id);
0117
0118 return ret;
0119 }
0120
0121
0122 static u32 i2c_mux_functionality(struct i2c_adapter *adap)
0123 {
0124 struct i2c_mux_priv *priv = adap->algo_data;
0125 struct i2c_adapter *parent = priv->muxc->parent;
0126
0127 return parent->algo->functionality(parent);
0128 }
0129
0130
0131 static unsigned int i2c_mux_parent_classes(struct i2c_adapter *parent)
0132 {
0133 unsigned int class = 0;
0134
0135 do {
0136 class |= parent->class;
0137 parent = i2c_parent_is_i2c_adapter(parent);
0138 } while (parent);
0139
0140 return class;
0141 }
0142
0143 static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
0144 {
0145 struct i2c_mux_priv *priv = adapter->algo_data;
0146 struct i2c_adapter *parent = priv->muxc->parent;
0147
0148 rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
0149 if (!(flags & I2C_LOCK_ROOT_ADAPTER))
0150 return;
0151 i2c_lock_bus(parent, flags);
0152 }
0153
0154 static int i2c_mux_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
0155 {
0156 struct i2c_mux_priv *priv = adapter->algo_data;
0157 struct i2c_adapter *parent = priv->muxc->parent;
0158
0159 if (!rt_mutex_trylock(&parent->mux_lock))
0160 return 0;
0161 if (!(flags & I2C_LOCK_ROOT_ADAPTER))
0162 return 1;
0163 if (i2c_trylock_bus(parent, flags))
0164 return 1;
0165 rt_mutex_unlock(&parent->mux_lock);
0166 return 0;
0167 }
0168
0169 static void i2c_mux_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
0170 {
0171 struct i2c_mux_priv *priv = adapter->algo_data;
0172 struct i2c_adapter *parent = priv->muxc->parent;
0173
0174 if (flags & I2C_LOCK_ROOT_ADAPTER)
0175 i2c_unlock_bus(parent, flags);
0176 rt_mutex_unlock(&parent->mux_lock);
0177 }
0178
0179 static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
0180 unsigned int flags)
0181 {
0182 struct i2c_mux_priv *priv = adapter->algo_data;
0183 struct i2c_adapter *parent = priv->muxc->parent;
0184
0185 rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
0186 i2c_lock_bus(parent, flags);
0187 }
0188
0189 static int i2c_parent_trylock_bus(struct i2c_adapter *adapter,
0190 unsigned int flags)
0191 {
0192 struct i2c_mux_priv *priv = adapter->algo_data;
0193 struct i2c_adapter *parent = priv->muxc->parent;
0194
0195 if (!rt_mutex_trylock(&parent->mux_lock))
0196 return 0;
0197 if (i2c_trylock_bus(parent, flags))
0198 return 1;
0199 rt_mutex_unlock(&parent->mux_lock);
0200 return 0;
0201 }
0202
0203 static void i2c_parent_unlock_bus(struct i2c_adapter *adapter,
0204 unsigned int flags)
0205 {
0206 struct i2c_mux_priv *priv = adapter->algo_data;
0207 struct i2c_adapter *parent = priv->muxc->parent;
0208
0209 i2c_unlock_bus(parent, flags);
0210 rt_mutex_unlock(&parent->mux_lock);
0211 }
0212
0213 struct i2c_adapter *i2c_root_adapter(struct device *dev)
0214 {
0215 struct device *i2c;
0216 struct i2c_adapter *i2c_root;
0217
0218
0219
0220
0221
0222
0223 for (i2c = dev; i2c; i2c = i2c->parent) {
0224 if (i2c->type == &i2c_adapter_type)
0225 break;
0226 }
0227 if (!i2c)
0228 return NULL;
0229
0230
0231 i2c_root = to_i2c_adapter(i2c);
0232 while (i2c_parent_is_i2c_adapter(i2c_root))
0233 i2c_root = i2c_parent_is_i2c_adapter(i2c_root);
0234
0235 return i2c_root;
0236 }
0237 EXPORT_SYMBOL_GPL(i2c_root_adapter);
0238
0239 struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
0240 struct device *dev, int max_adapters,
0241 int sizeof_priv, u32 flags,
0242 int (*select)(struct i2c_mux_core *, u32),
0243 int (*deselect)(struct i2c_mux_core *, u32))
0244 {
0245 struct i2c_mux_core *muxc;
0246 size_t mux_size;
0247
0248 mux_size = struct_size(muxc, adapter, max_adapters);
0249 muxc = devm_kzalloc(dev, size_add(mux_size, sizeof_priv), GFP_KERNEL);
0250 if (!muxc)
0251 return NULL;
0252 if (sizeof_priv)
0253 muxc->priv = &muxc->adapter[max_adapters];
0254
0255 muxc->parent = parent;
0256 muxc->dev = dev;
0257 if (flags & I2C_MUX_LOCKED)
0258 muxc->mux_locked = true;
0259 if (flags & I2C_MUX_ARBITRATOR)
0260 muxc->arbitrator = true;
0261 if (flags & I2C_MUX_GATE)
0262 muxc->gate = true;
0263 muxc->select = select;
0264 muxc->deselect = deselect;
0265 muxc->max_adapters = max_adapters;
0266
0267 return muxc;
0268 }
0269 EXPORT_SYMBOL_GPL(i2c_mux_alloc);
0270
0271 static const struct i2c_lock_operations i2c_mux_lock_ops = {
0272 .lock_bus = i2c_mux_lock_bus,
0273 .trylock_bus = i2c_mux_trylock_bus,
0274 .unlock_bus = i2c_mux_unlock_bus,
0275 };
0276
0277 static const struct i2c_lock_operations i2c_parent_lock_ops = {
0278 .lock_bus = i2c_parent_lock_bus,
0279 .trylock_bus = i2c_parent_trylock_bus,
0280 .unlock_bus = i2c_parent_unlock_bus,
0281 };
0282
0283 int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
0284 u32 force_nr, u32 chan_id,
0285 unsigned int class)
0286 {
0287 struct i2c_adapter *parent = muxc->parent;
0288 struct i2c_mux_priv *priv;
0289 char symlink_name[20];
0290 int ret;
0291
0292 if (muxc->num_adapters >= muxc->max_adapters) {
0293 dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
0294 return -EINVAL;
0295 }
0296
0297 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0298 if (!priv)
0299 return -ENOMEM;
0300
0301
0302 priv->muxc = muxc;
0303 priv->chan_id = chan_id;
0304
0305
0306
0307
0308 if (parent->algo->master_xfer) {
0309 if (muxc->mux_locked)
0310 priv->algo.master_xfer = i2c_mux_master_xfer;
0311 else
0312 priv->algo.master_xfer = __i2c_mux_master_xfer;
0313 }
0314 if (parent->algo->master_xfer_atomic)
0315 priv->algo.master_xfer_atomic = priv->algo.master_xfer;
0316
0317 if (parent->algo->smbus_xfer) {
0318 if (muxc->mux_locked)
0319 priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
0320 else
0321 priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
0322 }
0323 if (parent->algo->smbus_xfer_atomic)
0324 priv->algo.smbus_xfer_atomic = priv->algo.smbus_xfer;
0325
0326 priv->algo.functionality = i2c_mux_functionality;
0327
0328
0329 snprintf(priv->adap.name, sizeof(priv->adap.name),
0330 "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
0331 priv->adap.owner = THIS_MODULE;
0332 priv->adap.algo = &priv->algo;
0333 priv->adap.algo_data = priv;
0334 priv->adap.dev.parent = &parent->dev;
0335 priv->adap.retries = parent->retries;
0336 priv->adap.timeout = parent->timeout;
0337 priv->adap.quirks = parent->quirks;
0338 if (muxc->mux_locked)
0339 priv->adap.lock_ops = &i2c_mux_lock_ops;
0340 else
0341 priv->adap.lock_ops = &i2c_parent_lock_ops;
0342
0343
0344 if (i2c_mux_parent_classes(parent) & class)
0345 dev_err(&parent->dev,
0346 "Segment %d behind mux can't share classes with ancestors\n",
0347 chan_id);
0348 else
0349 priv->adap.class = class;
0350
0351
0352
0353
0354
0355 if (muxc->dev->of_node) {
0356 struct device_node *dev_node = muxc->dev->of_node;
0357 struct device_node *mux_node, *child = NULL;
0358 u32 reg;
0359
0360 if (muxc->arbitrator)
0361 mux_node = of_get_child_by_name(dev_node, "i2c-arb");
0362 else if (muxc->gate)
0363 mux_node = of_get_child_by_name(dev_node, "i2c-gate");
0364 else
0365 mux_node = of_get_child_by_name(dev_node, "i2c-mux");
0366
0367 if (mux_node) {
0368
0369 if (!of_property_read_u32(mux_node, "reg", ®)) {
0370 of_node_put(mux_node);
0371 mux_node = NULL;
0372 }
0373 }
0374
0375 if (!mux_node)
0376 mux_node = of_node_get(dev_node);
0377 else if (muxc->arbitrator || muxc->gate)
0378 child = of_node_get(mux_node);
0379
0380 if (!child) {
0381 for_each_child_of_node(mux_node, child) {
0382 ret = of_property_read_u32(child, "reg", ®);
0383 if (ret)
0384 continue;
0385 if (chan_id == reg)
0386 break;
0387 }
0388 }
0389
0390 priv->adap.dev.of_node = child;
0391 of_node_put(mux_node);
0392 }
0393
0394
0395
0396
0397 if (has_acpi_companion(muxc->dev))
0398 acpi_preset_companion(&priv->adap.dev,
0399 ACPI_COMPANION(muxc->dev),
0400 chan_id);
0401
0402 if (force_nr) {
0403 priv->adap.nr = force_nr;
0404 ret = i2c_add_numbered_adapter(&priv->adap);
0405 if (ret < 0) {
0406 dev_err(&parent->dev,
0407 "failed to add mux-adapter %u as bus %u (error=%d)\n",
0408 chan_id, force_nr, ret);
0409 goto err_free_priv;
0410 }
0411 } else {
0412 ret = i2c_add_adapter(&priv->adap);
0413 if (ret < 0) {
0414 dev_err(&parent->dev,
0415 "failed to add mux-adapter %u (error=%d)\n",
0416 chan_id, ret);
0417 goto err_free_priv;
0418 }
0419 }
0420
0421 WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
0422 "mux_device"),
0423 "can't create symlink to mux device\n");
0424
0425 snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
0426 WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
0427 symlink_name),
0428 "can't create symlink to channel %u\n", chan_id);
0429 dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
0430 i2c_adapter_id(&priv->adap));
0431
0432 muxc->adapter[muxc->num_adapters++] = &priv->adap;
0433 return 0;
0434
0435 err_free_priv:
0436 kfree(priv);
0437 return ret;
0438 }
0439 EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
0440
0441 void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
0442 {
0443 char symlink_name[20];
0444
0445 while (muxc->num_adapters) {
0446 struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
0447 struct i2c_mux_priv *priv = adap->algo_data;
0448 struct device_node *np = adap->dev.of_node;
0449
0450 muxc->adapter[muxc->num_adapters] = NULL;
0451
0452 snprintf(symlink_name, sizeof(symlink_name),
0453 "channel-%u", priv->chan_id);
0454 sysfs_remove_link(&muxc->dev->kobj, symlink_name);
0455
0456 sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
0457 i2c_del_adapter(adap);
0458 of_node_put(np);
0459 kfree(priv);
0460 }
0461 }
0462 EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
0463
0464 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
0465 MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
0466 MODULE_LICENSE("GPL v2");