i2c-mux.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_mux_core *muxc;
  33. u32 chan_id;
  34. };
  35. static int i2c_mux_master_xfer(struct i2c_adapter *adap,
  36. struct i2c_msg msgs[], int num)
  37. {
  38. struct i2c_mux_priv *priv = adap->algo_data;
  39. struct i2c_mux_core *muxc = priv->muxc;
  40. struct i2c_adapter *parent = muxc->parent;
  41. int ret;
  42. /* Switch to the right mux port and perform the transfer. */
  43. ret = muxc->select(muxc, priv->chan_id);
  44. if (ret >= 0)
  45. ret = __i2c_transfer(parent, msgs, num);
  46. if (muxc->deselect)
  47. muxc->deselect(muxc, 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_mux_core *muxc = priv->muxc;
  57. struct i2c_adapter *parent = muxc->parent;
  58. int ret;
  59. /* Select the right mux port and perform the transfer. */
  60. ret = muxc->select(muxc, priv->chan_id);
  61. if (ret >= 0)
  62. ret = parent->algo->smbus_xfer(parent, addr, flags,
  63. read_write, command, size, data);
  64. if (muxc->deselect)
  65. muxc->deselect(muxc, 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->muxc->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_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
  86. struct device *dev, int max_adapters,
  87. int sizeof_priv, u32 flags,
  88. int (*select)(struct i2c_mux_core *, u32),
  89. int (*deselect)(struct i2c_mux_core *, u32))
  90. {
  91. struct i2c_mux_core *muxc;
  92. muxc = devm_kzalloc(dev, sizeof(*muxc)
  93. + max_adapters * sizeof(muxc->adapter[0])
  94. + sizeof_priv, GFP_KERNEL);
  95. if (!muxc)
  96. return NULL;
  97. if (sizeof_priv)
  98. muxc->priv = &muxc->adapter[max_adapters];
  99. muxc->parent = parent;
  100. muxc->dev = dev;
  101. muxc->select = select;
  102. muxc->deselect = deselect;
  103. muxc->max_adapters = max_adapters;
  104. return muxc;
  105. }
  106. EXPORT_SYMBOL_GPL(i2c_mux_alloc);
  107. int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
  108. u32 force_nr, u32 chan_id,
  109. unsigned int class)
  110. {
  111. struct i2c_adapter *parent = muxc->parent;
  112. struct i2c_mux_priv *priv;
  113. char symlink_name[20];
  114. int ret;
  115. if (muxc->num_adapters >= muxc->max_adapters) {
  116. dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
  117. return -EINVAL;
  118. }
  119. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  120. if (!priv)
  121. return -ENOMEM;
  122. /* Set up private adapter data */
  123. priv->muxc = muxc;
  124. priv->chan_id = chan_id;
  125. /* Need to do algo dynamically because we don't know ahead
  126. * of time what sort of physical adapter we'll be dealing with.
  127. */
  128. if (parent->algo->master_xfer)
  129. priv->algo.master_xfer = i2c_mux_master_xfer;
  130. if (parent->algo->smbus_xfer)
  131. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  132. priv->algo.functionality = i2c_mux_functionality;
  133. /* Now fill out new adapter structure */
  134. snprintf(priv->adap.name, sizeof(priv->adap.name),
  135. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  136. priv->adap.owner = THIS_MODULE;
  137. priv->adap.algo = &priv->algo;
  138. priv->adap.algo_data = priv;
  139. priv->adap.dev.parent = &parent->dev;
  140. priv->adap.retries = parent->retries;
  141. priv->adap.timeout = parent->timeout;
  142. priv->adap.quirks = parent->quirks;
  143. /* Sanity check on class */
  144. if (i2c_mux_parent_classes(parent) & class)
  145. dev_err(&parent->dev,
  146. "Segment %d behind mux can't share classes with ancestors\n",
  147. chan_id);
  148. else
  149. priv->adap.class = class;
  150. /*
  151. * Try to populate the mux adapter's of_node, expands to
  152. * nothing if !CONFIG_OF.
  153. */
  154. if (muxc->dev->of_node) {
  155. struct device_node *child;
  156. u32 reg;
  157. for_each_child_of_node(muxc->dev->of_node, child) {
  158. ret = of_property_read_u32(child, "reg", &reg);
  159. if (ret)
  160. continue;
  161. if (chan_id == reg) {
  162. priv->adap.dev.of_node = child;
  163. break;
  164. }
  165. }
  166. }
  167. /*
  168. * Associate the mux channel with an ACPI node.
  169. */
  170. if (has_acpi_companion(muxc->dev))
  171. acpi_preset_companion(&priv->adap.dev,
  172. ACPI_COMPANION(muxc->dev),
  173. chan_id);
  174. if (force_nr) {
  175. priv->adap.nr = force_nr;
  176. ret = i2c_add_numbered_adapter(&priv->adap);
  177. } else {
  178. ret = i2c_add_adapter(&priv->adap);
  179. }
  180. if (ret < 0) {
  181. dev_err(&parent->dev,
  182. "failed to add mux-adapter (error=%d)\n",
  183. ret);
  184. kfree(priv);
  185. return ret;
  186. }
  187. WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
  188. "mux_device"),
  189. "can't create symlink to mux device\n");
  190. snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
  191. WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
  192. symlink_name),
  193. "can't create symlink for channel %u\n", chan_id);
  194. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  195. i2c_adapter_id(&priv->adap));
  196. muxc->adapter[muxc->num_adapters++] = &priv->adap;
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
  200. void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
  201. {
  202. char symlink_name[20];
  203. while (muxc->num_adapters) {
  204. struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
  205. struct i2c_mux_priv *priv = adap->algo_data;
  206. muxc->adapter[muxc->num_adapters] = NULL;
  207. snprintf(symlink_name, sizeof(symlink_name),
  208. "channel-%u", priv->chan_id);
  209. sysfs_remove_link(&muxc->dev->kobj, symlink_name);
  210. sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
  211. i2c_del_adapter(adap);
  212. kfree(priv);
  213. }
  214. }
  215. EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
  216. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  217. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  218. MODULE_LICENSE("GPL v2");