i2c-mux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 (i2c_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 (i2c_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. if (flags & I2C_MUX_ARBITRATOR)
  217. muxc->arbitrator = true;
  218. if (flags & I2C_MUX_GATE)
  219. muxc->gate = true;
  220. muxc->select = select;
  221. muxc->deselect = deselect;
  222. muxc->max_adapters = max_adapters;
  223. return muxc;
  224. }
  225. EXPORT_SYMBOL_GPL(i2c_mux_alloc);
  226. static const struct i2c_lock_operations i2c_mux_lock_ops = {
  227. .lock_bus = i2c_mux_lock_bus,
  228. .trylock_bus = i2c_mux_trylock_bus,
  229. .unlock_bus = i2c_mux_unlock_bus,
  230. };
  231. static const struct i2c_lock_operations i2c_parent_lock_ops = {
  232. .lock_bus = i2c_parent_lock_bus,
  233. .trylock_bus = i2c_parent_trylock_bus,
  234. .unlock_bus = i2c_parent_unlock_bus,
  235. };
  236. int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
  237. u32 force_nr, u32 chan_id,
  238. unsigned int class)
  239. {
  240. struct i2c_adapter *parent = muxc->parent;
  241. struct i2c_mux_priv *priv;
  242. char symlink_name[20];
  243. int ret;
  244. if (muxc->num_adapters >= muxc->max_adapters) {
  245. dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
  246. return -EINVAL;
  247. }
  248. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  249. if (!priv)
  250. return -ENOMEM;
  251. /* Set up private adapter data */
  252. priv->muxc = muxc;
  253. priv->chan_id = chan_id;
  254. /* Need to do algo dynamically because we don't know ahead
  255. * of time what sort of physical adapter we'll be dealing with.
  256. */
  257. if (parent->algo->master_xfer) {
  258. if (muxc->mux_locked)
  259. priv->algo.master_xfer = i2c_mux_master_xfer;
  260. else
  261. priv->algo.master_xfer = __i2c_mux_master_xfer;
  262. }
  263. if (parent->algo->smbus_xfer) {
  264. if (muxc->mux_locked)
  265. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  266. else
  267. priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
  268. }
  269. priv->algo.functionality = i2c_mux_functionality;
  270. /* Now fill out new adapter structure */
  271. snprintf(priv->adap.name, sizeof(priv->adap.name),
  272. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  273. priv->adap.owner = THIS_MODULE;
  274. priv->adap.algo = &priv->algo;
  275. priv->adap.algo_data = priv;
  276. priv->adap.dev.parent = &parent->dev;
  277. priv->adap.retries = parent->retries;
  278. priv->adap.timeout = parent->timeout;
  279. priv->adap.quirks = parent->quirks;
  280. if (muxc->mux_locked)
  281. priv->adap.lock_ops = &i2c_mux_lock_ops;
  282. else
  283. priv->adap.lock_ops = &i2c_parent_lock_ops;
  284. /* Sanity check on class */
  285. if (i2c_mux_parent_classes(parent) & class)
  286. dev_err(&parent->dev,
  287. "Segment %d behind mux can't share classes with ancestors\n",
  288. chan_id);
  289. else
  290. priv->adap.class = class;
  291. /*
  292. * Try to populate the mux adapter's of_node, expands to
  293. * nothing if !CONFIG_OF.
  294. */
  295. if (muxc->dev->of_node) {
  296. struct device_node *dev_node = muxc->dev->of_node;
  297. struct device_node *mux_node, *child = NULL;
  298. u32 reg;
  299. if (muxc->arbitrator)
  300. mux_node = of_get_child_by_name(dev_node, "i2c-arb");
  301. else if (muxc->gate)
  302. mux_node = of_get_child_by_name(dev_node, "i2c-gate");
  303. else
  304. mux_node = of_get_child_by_name(dev_node, "i2c-mux");
  305. if (mux_node) {
  306. /* A "reg" property indicates an old-style DT entry */
  307. if (!of_property_read_u32(mux_node, "reg", &reg)) {
  308. of_node_put(mux_node);
  309. mux_node = NULL;
  310. }
  311. }
  312. if (!mux_node)
  313. mux_node = of_node_get(dev_node);
  314. else if (muxc->arbitrator || muxc->gate)
  315. child = of_node_get(mux_node);
  316. if (!child) {
  317. for_each_child_of_node(mux_node, child) {
  318. ret = of_property_read_u32(child, "reg", &reg);
  319. if (ret)
  320. continue;
  321. if (chan_id == reg)
  322. break;
  323. }
  324. }
  325. priv->adap.dev.of_node = child;
  326. of_node_put(mux_node);
  327. }
  328. /*
  329. * Associate the mux channel with an ACPI node.
  330. */
  331. if (has_acpi_companion(muxc->dev))
  332. acpi_preset_companion(&priv->adap.dev,
  333. ACPI_COMPANION(muxc->dev),
  334. chan_id);
  335. if (force_nr) {
  336. priv->adap.nr = force_nr;
  337. ret = i2c_add_numbered_adapter(&priv->adap);
  338. } else {
  339. ret = i2c_add_adapter(&priv->adap);
  340. }
  341. if (ret < 0) {
  342. dev_err(&parent->dev,
  343. "failed to add mux-adapter (error=%d)\n",
  344. ret);
  345. kfree(priv);
  346. return ret;
  347. }
  348. WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
  349. "mux_device"),
  350. "can't create symlink to mux device\n");
  351. snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
  352. WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
  353. symlink_name),
  354. "can't create symlink for channel %u\n", chan_id);
  355. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  356. i2c_adapter_id(&priv->adap));
  357. muxc->adapter[muxc->num_adapters++] = &priv->adap;
  358. return 0;
  359. }
  360. EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
  361. void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
  362. {
  363. char symlink_name[20];
  364. while (muxc->num_adapters) {
  365. struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
  366. struct i2c_mux_priv *priv = adap->algo_data;
  367. struct device_node *np = adap->dev.of_node;
  368. muxc->adapter[muxc->num_adapters] = NULL;
  369. snprintf(symlink_name, sizeof(symlink_name),
  370. "channel-%u", priv->chan_id);
  371. sysfs_remove_link(&muxc->dev->kobj, symlink_name);
  372. sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
  373. i2c_del_adapter(adap);
  374. of_node_put(np);
  375. kfree(priv);
  376. }
  377. }
  378. EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
  379. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  380. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  381. MODULE_LICENSE("GPL v2");