i2c-mux.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /* Return all parent classes, merged */
  77. static unsigned int i2c_mux_parent_classes(struct i2c_adapter *parent)
  78. {
  79. unsigned int class = 0;
  80. do {
  81. class |= parent->class;
  82. parent = i2c_parent_is_i2c_adapter(parent);
  83. } while (parent);
  84. return class;
  85. }
  86. struct i2c_adapter *i2c_add_mux_adapter(struct i2c_adapter *parent,
  87. struct device *mux_dev,
  88. void *mux_priv, u32 force_nr, u32 chan_id,
  89. unsigned int class,
  90. int (*select) (struct i2c_adapter *,
  91. void *, u32),
  92. int (*deselect) (struct i2c_adapter *,
  93. void *, u32))
  94. {
  95. struct i2c_mux_priv *priv;
  96. int ret;
  97. priv = kzalloc(sizeof(struct i2c_mux_priv), GFP_KERNEL);
  98. if (!priv)
  99. return NULL;
  100. /* Set up private adapter data */
  101. priv->parent = parent;
  102. priv->mux_priv = mux_priv;
  103. priv->chan_id = chan_id;
  104. priv->select = select;
  105. priv->deselect = deselect;
  106. /* Need to do algo dynamically because we don't know ahead
  107. * of time what sort of physical adapter we'll be dealing with.
  108. */
  109. if (parent->algo->master_xfer)
  110. priv->algo.master_xfer = i2c_mux_master_xfer;
  111. if (parent->algo->smbus_xfer)
  112. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  113. priv->algo.functionality = i2c_mux_functionality;
  114. /* Now fill out new adapter structure */
  115. snprintf(priv->adap.name, sizeof(priv->adap.name),
  116. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  117. priv->adap.owner = THIS_MODULE;
  118. priv->adap.algo = &priv->algo;
  119. priv->adap.algo_data = priv;
  120. priv->adap.dev.parent = &parent->dev;
  121. /* Sanity check on class */
  122. if (i2c_mux_parent_classes(parent) & class)
  123. dev_err(&parent->dev,
  124. "Segment %d behind mux can't share classes with ancestors\n",
  125. chan_id);
  126. else
  127. priv->adap.class = class;
  128. /*
  129. * Try to populate the mux adapter's of_node, expands to
  130. * nothing if !CONFIG_OF.
  131. */
  132. if (mux_dev->of_node) {
  133. struct device_node *child;
  134. u32 reg;
  135. for_each_child_of_node(mux_dev->of_node, child) {
  136. ret = of_property_read_u32(child, "reg", &reg);
  137. if (ret)
  138. continue;
  139. if (chan_id == reg) {
  140. priv->adap.dev.of_node = child;
  141. break;
  142. }
  143. }
  144. }
  145. if (force_nr) {
  146. priv->adap.nr = force_nr;
  147. ret = i2c_add_numbered_adapter(&priv->adap);
  148. } else {
  149. ret = i2c_add_adapter(&priv->adap);
  150. }
  151. if (ret < 0) {
  152. dev_err(&parent->dev,
  153. "failed to add mux-adapter (error=%d)\n",
  154. ret);
  155. kfree(priv);
  156. return NULL;
  157. }
  158. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  159. i2c_adapter_id(&priv->adap));
  160. of_i2c_register_devices(&priv->adap);
  161. return &priv->adap;
  162. }
  163. EXPORT_SYMBOL_GPL(i2c_add_mux_adapter);
  164. int i2c_del_mux_adapter(struct i2c_adapter *adap)
  165. {
  166. struct i2c_mux_priv *priv = adap->algo_data;
  167. int ret;
  168. ret = i2c_del_adapter(adap);
  169. if (ret < 0)
  170. return ret;
  171. kfree(priv);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL_GPL(i2c_del_mux_adapter);
  175. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  176. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  177. MODULE_LICENSE("GPL v2");