i2c-mux.c 4.5 KB

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