i2c-mux.c 5.4 KB

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