i2c-mux.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/i2c-mux.h>
  26. #include <linux/of.h>
  27. #include <linux/of_i2c.h>
  28. /* multiplexer per channel data */
  29. struct i2c_mux_priv {
  30. struct i2c_adapter adap;
  31. struct i2c_algorithm algo;
  32. struct i2c_adapter *parent;
  33. void *mux_priv; /* the mux chip/device */
  34. u32 chan_id; /* the channel id */
  35. int (*select)(struct i2c_adapter *, void *mux_priv, u32 chan_id);
  36. int (*deselect)(struct i2c_adapter *, void *mux_priv, u32 chan_id);
  37. };
  38. static int i2c_mux_master_xfer(struct i2c_adapter *adap,
  39. struct i2c_msg msgs[], int num)
  40. {
  41. struct i2c_mux_priv *priv = adap->algo_data;
  42. struct i2c_adapter *parent = priv->parent;
  43. int ret;
  44. /* Switch to the right mux port and perform the transfer. */
  45. ret = priv->select(parent, priv->mux_priv, priv->chan_id);
  46. if (ret >= 0)
  47. ret = parent->algo->master_xfer(parent, msgs, num);
  48. if (priv->deselect)
  49. priv->deselect(parent, priv->mux_priv, priv->chan_id);
  50. return ret;
  51. }
  52. static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
  53. u16 addr, unsigned short flags,
  54. char read_write, u8 command,
  55. int size, union i2c_smbus_data *data)
  56. {
  57. struct i2c_mux_priv *priv = adap->algo_data;
  58. struct i2c_adapter *parent = priv->parent;
  59. int ret;
  60. /* Select the right mux port and perform the transfer. */
  61. ret = priv->select(parent, priv->mux_priv, priv->chan_id);
  62. if (ret >= 0)
  63. ret = parent->algo->smbus_xfer(parent, addr, flags,
  64. read_write, command, size, data);
  65. if (priv->deselect)
  66. priv->deselect(parent, priv->mux_priv, priv->chan_id);
  67. return ret;
  68. }
  69. /* Return the parent's functionality */
  70. static u32 i2c_mux_functionality(struct i2c_adapter *adap)
  71. {
  72. struct i2c_mux_priv *priv = adap->algo_data;
  73. struct i2c_adapter *parent = priv->parent;
  74. return parent->algo->functionality(parent);
  75. }
  76. struct i2c_adapter *i2c_add_mux_adapter(struct i2c_adapter *parent,
  77. struct device *mux_dev,
  78. void *mux_priv, u32 force_nr, u32 chan_id,
  79. int (*select) (struct i2c_adapter *,
  80. void *, u32),
  81. int (*deselect) (struct i2c_adapter *,
  82. void *, u32))
  83. {
  84. struct i2c_mux_priv *priv;
  85. int ret;
  86. priv = kzalloc(sizeof(struct i2c_mux_priv), GFP_KERNEL);
  87. if (!priv)
  88. return NULL;
  89. /* Set up private adapter data */
  90. priv->parent = parent;
  91. priv->mux_priv = mux_priv;
  92. priv->chan_id = chan_id;
  93. priv->select = select;
  94. priv->deselect = deselect;
  95. /* Need to do algo dynamically because we don't know ahead
  96. * of time what sort of physical adapter we'll be dealing with.
  97. */
  98. if (parent->algo->master_xfer)
  99. priv->algo.master_xfer = i2c_mux_master_xfer;
  100. if (parent->algo->smbus_xfer)
  101. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  102. priv->algo.functionality = i2c_mux_functionality;
  103. /* Now fill out new adapter structure */
  104. snprintf(priv->adap.name, sizeof(priv->adap.name),
  105. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  106. priv->adap.owner = THIS_MODULE;
  107. priv->adap.algo = &priv->algo;
  108. priv->adap.algo_data = priv;
  109. priv->adap.dev.parent = &parent->dev;
  110. /*
  111. * Try to populate the mux adapter's of_node, expands to
  112. * nothing if !CONFIG_OF.
  113. */
  114. if (mux_dev->of_node) {
  115. struct device_node *child;
  116. u32 reg;
  117. for_each_child_of_node(mux_dev->of_node, child) {
  118. ret = of_property_read_u32(child, "reg", &reg);
  119. if (ret)
  120. continue;
  121. if (chan_id == reg) {
  122. priv->adap.dev.of_node = child;
  123. break;
  124. }
  125. }
  126. }
  127. if (force_nr) {
  128. priv->adap.nr = force_nr;
  129. ret = i2c_add_numbered_adapter(&priv->adap);
  130. } else {
  131. ret = i2c_add_adapter(&priv->adap);
  132. }
  133. if (ret < 0) {
  134. dev_err(&parent->dev,
  135. "failed to add mux-adapter (error=%d)\n",
  136. ret);
  137. kfree(priv);
  138. return NULL;
  139. }
  140. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  141. i2c_adapter_id(&priv->adap));
  142. of_i2c_register_devices(&priv->adap);
  143. return &priv->adap;
  144. }
  145. EXPORT_SYMBOL_GPL(i2c_add_mux_adapter);
  146. int i2c_del_mux_adapter(struct i2c_adapter *adap)
  147. {
  148. struct i2c_mux_priv *priv = adap->algo_data;
  149. int ret;
  150. ret = i2c_del_adapter(adap);
  151. if (ret < 0)
  152. return ret;
  153. kfree(priv);
  154. return 0;
  155. }
  156. EXPORT_SYMBOL_GPL(i2c_del_mux_adapter);
  157. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  158. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  159. MODULE_LICENSE("GPL v2");