i2c-mux.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. struct device *mux_dev;
  33. void *mux_priv;
  34. u32 chan_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 = __i2c_transfer(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. char symlink_name[20];
  97. int ret;
  98. priv = kzalloc(sizeof(struct i2c_mux_priv), GFP_KERNEL);
  99. if (!priv)
  100. return NULL;
  101. /* Set up private adapter data */
  102. priv->parent = parent;
  103. priv->mux_dev = mux_dev;
  104. priv->mux_priv = mux_priv;
  105. priv->chan_id = chan_id;
  106. priv->select = select;
  107. priv->deselect = deselect;
  108. /* Need to do algo dynamically because we don't know ahead
  109. * of time what sort of physical adapter we'll be dealing with.
  110. */
  111. if (parent->algo->master_xfer)
  112. priv->algo.master_xfer = i2c_mux_master_xfer;
  113. if (parent->algo->smbus_xfer)
  114. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  115. priv->algo.functionality = i2c_mux_functionality;
  116. /* Now fill out new adapter structure */
  117. snprintf(priv->adap.name, sizeof(priv->adap.name),
  118. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  119. priv->adap.owner = THIS_MODULE;
  120. priv->adap.algo = &priv->algo;
  121. priv->adap.algo_data = priv;
  122. priv->adap.dev.parent = &parent->dev;
  123. priv->adap.retries = parent->retries;
  124. priv->adap.timeout = parent->timeout;
  125. priv->adap.quirks = parent->quirks;
  126. /* Sanity check on class */
  127. if (i2c_mux_parent_classes(parent) & class)
  128. dev_err(&parent->dev,
  129. "Segment %d behind mux can't share classes with ancestors\n",
  130. chan_id);
  131. else
  132. priv->adap.class = class;
  133. /*
  134. * Try to populate the mux adapter's of_node, expands to
  135. * nothing if !CONFIG_OF.
  136. */
  137. if (mux_dev->of_node) {
  138. struct device_node *child;
  139. u32 reg;
  140. for_each_child_of_node(mux_dev->of_node, child) {
  141. ret = of_property_read_u32(child, "reg", &reg);
  142. if (ret)
  143. continue;
  144. if (chan_id == reg) {
  145. priv->adap.dev.of_node = child;
  146. break;
  147. }
  148. }
  149. }
  150. if (force_nr) {
  151. priv->adap.nr = force_nr;
  152. ret = i2c_add_numbered_adapter(&priv->adap);
  153. } else {
  154. ret = i2c_add_adapter(&priv->adap);
  155. }
  156. if (ret < 0) {
  157. dev_err(&parent->dev,
  158. "failed to add mux-adapter (error=%d)\n",
  159. ret);
  160. kfree(priv);
  161. return NULL;
  162. }
  163. WARN(sysfs_create_link(&priv->adap.dev.kobj, &mux_dev->kobj, "mux_device"),
  164. "can't create symlink to mux device\n");
  165. snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
  166. WARN(sysfs_create_link(&mux_dev->kobj, &priv->adap.dev.kobj, symlink_name),
  167. "can't create symlink for channel %u\n", chan_id);
  168. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  169. i2c_adapter_id(&priv->adap));
  170. return &priv->adap;
  171. }
  172. EXPORT_SYMBOL_GPL(i2c_add_mux_adapter);
  173. void i2c_del_mux_adapter(struct i2c_adapter *adap)
  174. {
  175. struct i2c_mux_priv *priv = adap->algo_data;
  176. char symlink_name[20];
  177. snprintf(symlink_name, sizeof(symlink_name), "channel-%u", priv->chan_id);
  178. sysfs_remove_link(&priv->mux_dev->kobj, symlink_name);
  179. sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
  180. i2c_del_adapter(adap);
  181. kfree(priv);
  182. }
  183. EXPORT_SYMBOL_GPL(i2c_del_mux_adapter);
  184. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  185. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  186. MODULE_LICENSE("GPL v2");