i2c-mux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_master_xfer(struct i2c_adapter *adap,
  51. struct i2c_msg msgs[], int num)
  52. {
  53. struct i2c_mux_priv *priv = adap->algo_data;
  54. struct i2c_mux_core *muxc = priv->muxc;
  55. struct i2c_adapter *parent = muxc->parent;
  56. int ret;
  57. /* Switch to the right mux port and perform the transfer. */
  58. ret = muxc->select(muxc, priv->chan_id);
  59. if (ret >= 0)
  60. ret = i2c_transfer(parent, msgs, num);
  61. if (muxc->deselect)
  62. muxc->deselect(muxc, priv->chan_id);
  63. return ret;
  64. }
  65. static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
  66. u16 addr, unsigned short flags,
  67. char read_write, u8 command,
  68. int size, union i2c_smbus_data *data)
  69. {
  70. struct i2c_mux_priv *priv = adap->algo_data;
  71. struct i2c_mux_core *muxc = priv->muxc;
  72. struct i2c_adapter *parent = muxc->parent;
  73. int ret;
  74. /* Select the right mux port and perform the transfer. */
  75. ret = muxc->select(muxc, priv->chan_id);
  76. if (ret >= 0)
  77. ret = parent->algo->smbus_xfer(parent, addr, flags,
  78. read_write, command, size, data);
  79. if (muxc->deselect)
  80. muxc->deselect(muxc, priv->chan_id);
  81. return ret;
  82. }
  83. static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
  84. u16 addr, unsigned short flags,
  85. char read_write, u8 command,
  86. int size, union i2c_smbus_data *data)
  87. {
  88. struct i2c_mux_priv *priv = adap->algo_data;
  89. struct i2c_mux_core *muxc = priv->muxc;
  90. struct i2c_adapter *parent = muxc->parent;
  91. int ret;
  92. /* Select the right mux port and perform the transfer. */
  93. ret = muxc->select(muxc, priv->chan_id);
  94. if (ret >= 0)
  95. ret = i2c_smbus_xfer(parent, addr, flags,
  96. read_write, command, size, data);
  97. if (muxc->deselect)
  98. muxc->deselect(muxc, priv->chan_id);
  99. return ret;
  100. }
  101. /* Return the parent's functionality */
  102. static u32 i2c_mux_functionality(struct i2c_adapter *adap)
  103. {
  104. struct i2c_mux_priv *priv = adap->algo_data;
  105. struct i2c_adapter *parent = priv->muxc->parent;
  106. return parent->algo->functionality(parent);
  107. }
  108. /* Return all parent classes, merged */
  109. static unsigned int i2c_mux_parent_classes(struct i2c_adapter *parent)
  110. {
  111. unsigned int class = 0;
  112. do {
  113. class |= parent->class;
  114. parent = i2c_parent_is_i2c_adapter(parent);
  115. } while (parent);
  116. return class;
  117. }
  118. static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
  119. {
  120. struct i2c_mux_priv *priv = adapter->algo_data;
  121. struct i2c_adapter *parent = priv->muxc->parent;
  122. rt_mutex_lock(&parent->mux_lock);
  123. if (!(flags & I2C_LOCK_ROOT_ADAPTER))
  124. return;
  125. i2c_lock_bus(parent, flags);
  126. }
  127. static int i2c_mux_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
  128. {
  129. struct i2c_mux_priv *priv = adapter->algo_data;
  130. struct i2c_adapter *parent = priv->muxc->parent;
  131. if (!rt_mutex_trylock(&parent->mux_lock))
  132. return 0; /* mux_lock not locked, failure */
  133. if (!(flags & I2C_LOCK_ROOT_ADAPTER))
  134. return 1; /* we only want mux_lock, success */
  135. if (parent->trylock_bus(parent, flags))
  136. return 1; /* parent locked too, success */
  137. rt_mutex_unlock(&parent->mux_lock);
  138. return 0; /* parent not locked, failure */
  139. }
  140. static void i2c_mux_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
  141. {
  142. struct i2c_mux_priv *priv = adapter->algo_data;
  143. struct i2c_adapter *parent = priv->muxc->parent;
  144. if (flags & I2C_LOCK_ROOT_ADAPTER)
  145. i2c_unlock_bus(parent, flags);
  146. rt_mutex_unlock(&parent->mux_lock);
  147. }
  148. static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
  149. unsigned int flags)
  150. {
  151. struct i2c_mux_priv *priv = adapter->algo_data;
  152. struct i2c_adapter *parent = priv->muxc->parent;
  153. rt_mutex_lock(&parent->mux_lock);
  154. i2c_lock_bus(parent, flags);
  155. }
  156. static int i2c_parent_trylock_bus(struct i2c_adapter *adapter,
  157. unsigned int flags)
  158. {
  159. struct i2c_mux_priv *priv = adapter->algo_data;
  160. struct i2c_adapter *parent = priv->muxc->parent;
  161. if (!rt_mutex_trylock(&parent->mux_lock))
  162. return 0; /* mux_lock not locked, failure */
  163. if (parent->trylock_bus(parent, flags))
  164. return 1; /* parent locked too, success */
  165. rt_mutex_unlock(&parent->mux_lock);
  166. return 0; /* parent not locked, failure */
  167. }
  168. static void i2c_parent_unlock_bus(struct i2c_adapter *adapter,
  169. unsigned int flags)
  170. {
  171. struct i2c_mux_priv *priv = adapter->algo_data;
  172. struct i2c_adapter *parent = priv->muxc->parent;
  173. i2c_unlock_bus(parent, flags);
  174. rt_mutex_unlock(&parent->mux_lock);
  175. }
  176. struct i2c_adapter *i2c_root_adapter(struct device *dev)
  177. {
  178. struct device *i2c;
  179. struct i2c_adapter *i2c_root;
  180. /*
  181. * Walk up the device tree to find an i2c adapter, indicating
  182. * that this is an i2c client device. Check all ancestors to
  183. * handle mfd devices etc.
  184. */
  185. for (i2c = dev; i2c; i2c = i2c->parent) {
  186. if (i2c->type == &i2c_adapter_type)
  187. break;
  188. }
  189. if (!i2c)
  190. return NULL;
  191. /* Continue up the tree to find the root i2c adapter */
  192. i2c_root = to_i2c_adapter(i2c);
  193. while (i2c_parent_is_i2c_adapter(i2c_root))
  194. i2c_root = i2c_parent_is_i2c_adapter(i2c_root);
  195. return i2c_root;
  196. }
  197. EXPORT_SYMBOL_GPL(i2c_root_adapter);
  198. struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
  199. struct device *dev, int max_adapters,
  200. int sizeof_priv, u32 flags,
  201. int (*select)(struct i2c_mux_core *, u32),
  202. int (*deselect)(struct i2c_mux_core *, u32))
  203. {
  204. struct i2c_mux_core *muxc;
  205. muxc = devm_kzalloc(dev, sizeof(*muxc)
  206. + max_adapters * sizeof(muxc->adapter[0])
  207. + sizeof_priv, GFP_KERNEL);
  208. if (!muxc)
  209. return NULL;
  210. if (sizeof_priv)
  211. muxc->priv = &muxc->adapter[max_adapters];
  212. muxc->parent = parent;
  213. muxc->dev = dev;
  214. if (flags & I2C_MUX_LOCKED)
  215. muxc->mux_locked = true;
  216. muxc->select = select;
  217. muxc->deselect = deselect;
  218. muxc->max_adapters = max_adapters;
  219. return muxc;
  220. }
  221. EXPORT_SYMBOL_GPL(i2c_mux_alloc);
  222. int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
  223. u32 force_nr, u32 chan_id,
  224. unsigned int class)
  225. {
  226. struct i2c_adapter *parent = muxc->parent;
  227. struct i2c_mux_priv *priv;
  228. char symlink_name[20];
  229. int ret;
  230. if (muxc->num_adapters >= muxc->max_adapters) {
  231. dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
  232. return -EINVAL;
  233. }
  234. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  235. if (!priv)
  236. return -ENOMEM;
  237. /* Set up private adapter data */
  238. priv->muxc = muxc;
  239. priv->chan_id = chan_id;
  240. /* Need to do algo dynamically because we don't know ahead
  241. * of time what sort of physical adapter we'll be dealing with.
  242. */
  243. if (parent->algo->master_xfer) {
  244. if (muxc->mux_locked)
  245. priv->algo.master_xfer = i2c_mux_master_xfer;
  246. else
  247. priv->algo.master_xfer = __i2c_mux_master_xfer;
  248. }
  249. if (parent->algo->smbus_xfer) {
  250. if (muxc->mux_locked)
  251. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  252. else
  253. priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
  254. }
  255. priv->algo.functionality = i2c_mux_functionality;
  256. /* Now fill out new adapter structure */
  257. snprintf(priv->adap.name, sizeof(priv->adap.name),
  258. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  259. priv->adap.owner = THIS_MODULE;
  260. priv->adap.algo = &priv->algo;
  261. priv->adap.algo_data = priv;
  262. priv->adap.dev.parent = &parent->dev;
  263. priv->adap.retries = parent->retries;
  264. priv->adap.timeout = parent->timeout;
  265. priv->adap.quirks = parent->quirks;
  266. if (muxc->mux_locked) {
  267. priv->adap.lock_bus = i2c_mux_lock_bus;
  268. priv->adap.trylock_bus = i2c_mux_trylock_bus;
  269. priv->adap.unlock_bus = i2c_mux_unlock_bus;
  270. } else {
  271. priv->adap.lock_bus = i2c_parent_lock_bus;
  272. priv->adap.trylock_bus = i2c_parent_trylock_bus;
  273. priv->adap.unlock_bus = i2c_parent_unlock_bus;
  274. }
  275. /* Sanity check on class */
  276. if (i2c_mux_parent_classes(parent) & class)
  277. dev_err(&parent->dev,
  278. "Segment %d behind mux can't share classes with ancestors\n",
  279. chan_id);
  280. else
  281. priv->adap.class = class;
  282. /*
  283. * Try to populate the mux adapter's of_node, expands to
  284. * nothing if !CONFIG_OF.
  285. */
  286. if (muxc->dev->of_node) {
  287. struct device_node *child;
  288. u32 reg;
  289. for_each_child_of_node(muxc->dev->of_node, child) {
  290. ret = of_property_read_u32(child, "reg", &reg);
  291. if (ret)
  292. continue;
  293. if (chan_id == reg) {
  294. priv->adap.dev.of_node = child;
  295. break;
  296. }
  297. }
  298. }
  299. /*
  300. * Associate the mux channel with an ACPI node.
  301. */
  302. if (has_acpi_companion(muxc->dev))
  303. acpi_preset_companion(&priv->adap.dev,
  304. ACPI_COMPANION(muxc->dev),
  305. chan_id);
  306. if (force_nr) {
  307. priv->adap.nr = force_nr;
  308. ret = i2c_add_numbered_adapter(&priv->adap);
  309. } else {
  310. ret = i2c_add_adapter(&priv->adap);
  311. }
  312. if (ret < 0) {
  313. dev_err(&parent->dev,
  314. "failed to add mux-adapter (error=%d)\n",
  315. ret);
  316. kfree(priv);
  317. return ret;
  318. }
  319. WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
  320. "mux_device"),
  321. "can't create symlink to mux device\n");
  322. snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
  323. WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
  324. symlink_name),
  325. "can't create symlink for channel %u\n", chan_id);
  326. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  327. i2c_adapter_id(&priv->adap));
  328. muxc->adapter[muxc->num_adapters++] = &priv->adap;
  329. return 0;
  330. }
  331. EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
  332. void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
  333. {
  334. char symlink_name[20];
  335. while (muxc->num_adapters) {
  336. struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
  337. struct i2c_mux_priv *priv = adap->algo_data;
  338. muxc->adapter[muxc->num_adapters] = NULL;
  339. snprintf(symlink_name, sizeof(symlink_name),
  340. "channel-%u", priv->chan_id);
  341. sysfs_remove_link(&muxc->dev->kobj, symlink_name);
  342. sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
  343. i2c_del_adapter(adap);
  344. kfree(priv);
  345. }
  346. }
  347. EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
  348. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  349. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  350. MODULE_LICENSE("GPL v2");