i2c-mux.c 6.2 KB

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