i2c-mux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Multiplexed I2C bus driver.
  3. *
  4. * Copyright (c) 2008-2009 Rodolfo Giometti <[email protected]>
  5. * Copyright (c) 2008-2009 Eurotech S.p.A. <[email protected]>
  6. * Copyright (c) 2009-2010 NSN GmbH & Co KG <[email protected]>
  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 <[email protected]>
  14. * i2c-virtual.c from Ken Harrenstien, Copyright (c) 2004 Google, Inc.
  15. * i2c-virtual.c from Brian Kuschak <[email protected]>
  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. #include <linux/sysfs.h>
  29. /* multiplexer per channel data */
  30. struct i2c_mux_priv {
  31. struct i2c_adapter adap;
  32. struct i2c_algorithm algo;
  33. struct i2c_mux_core *muxc;
  34. u32 chan_id;
  35. };
  36. static int __i2c_mux_master_xfer(struct i2c_adapter *adap,
  37. struct i2c_msg msgs[], int num)
  38. {
  39. struct i2c_mux_priv *priv = adap->algo_data;
  40. struct i2c_mux_core *muxc = priv->muxc;
  41. struct i2c_adapter *parent = muxc->parent;
  42. int ret;
  43. /* Switch to the right mux port and perform the transfer. */
  44. ret = muxc->select(muxc, priv->chan_id);
  45. if (ret >= 0)
  46. ret = __i2c_transfer(parent, msgs, num);
  47. if (muxc->deselect)
  48. muxc->deselect(muxc, priv->chan_id);
  49. return ret;
  50. }
  51. static int i2c_mux_master_xfer(struct i2c_adapter *adap,
  52. struct i2c_msg msgs[], int num)
  53. {
  54. struct i2c_mux_priv *priv = adap->algo_data;
  55. struct i2c_mux_core *muxc = priv->muxc;
  56. struct i2c_adapter *parent = muxc->parent;
  57. int ret;
  58. /* Switch to the right mux port and perform the transfer. */
  59. ret = muxc->select(muxc, priv->chan_id);
  60. if (ret >= 0)
  61. ret = i2c_transfer(parent, msgs, num);
  62. if (muxc->deselect)
  63. muxc->deselect(muxc, priv->chan_id);
  64. return ret;
  65. }
  66. static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
  67. u16 addr, unsigned short flags,
  68. char read_write, u8 command,
  69. int size, union i2c_smbus_data *data)
  70. {
  71. struct i2c_mux_priv *priv = adap->algo_data;
  72. struct i2c_mux_core *muxc = priv->muxc;
  73. struct i2c_adapter *parent = muxc->parent;
  74. int ret;
  75. /* Select the right mux port and perform the transfer. */
  76. ret = muxc->select(muxc, priv->chan_id);
  77. if (ret >= 0)
  78. ret = __i2c_smbus_xfer(parent, addr, flags,
  79. read_write, command, size, data);
  80. if (muxc->deselect)
  81. muxc->deselect(muxc, priv->chan_id);
  82. return ret;
  83. }
  84. static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
  85. u16 addr, unsigned short flags,
  86. char read_write, u8 command,
  87. int size, union i2c_smbus_data *data)
  88. {
  89. struct i2c_mux_priv *priv = adap->algo_data;
  90. struct i2c_mux_core *muxc = priv->muxc;
  91. struct i2c_adapter *parent = muxc->parent;
  92. int ret;
  93. /* Select the right mux port and perform the transfer. */
  94. ret = muxc->select(muxc, priv->chan_id);
  95. if (ret >= 0)
  96. ret = i2c_smbus_xfer(parent, addr, flags,
  97. read_write, command, size, data);
  98. if (muxc->deselect)
  99. muxc->deselect(muxc, priv->chan_id);
  100. return ret;
  101. }
  102. /* Return the parent's functionality */
  103. static u32 i2c_mux_functionality(struct i2c_adapter *adap)
  104. {
  105. struct i2c_mux_priv *priv = adap->algo_data;
  106. struct i2c_adapter *parent = priv->muxc->parent;
  107. return parent->algo->functionality(parent);
  108. }
  109. /* Return all parent classes, merged */
  110. static unsigned int i2c_mux_parent_classes(struct i2c_adapter *parent)
  111. {
  112. unsigned int class = 0;
  113. do {
  114. class |= parent->class;
  115. parent = i2c_parent_is_i2c_adapter(parent);
  116. } while (parent);
  117. return class;
  118. }
  119. static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
  120. {
  121. struct i2c_mux_priv *priv = adapter->algo_data;
  122. struct i2c_adapter *parent = priv->muxc->parent;
  123. rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
  124. if (!(flags & I2C_LOCK_ROOT_ADAPTER))
  125. return;
  126. i2c_lock_bus(parent, flags);
  127. }
  128. static int i2c_mux_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
  129. {
  130. struct i2c_mux_priv *priv = adapter->algo_data;
  131. struct i2c_adapter *parent = priv->muxc->parent;
  132. if (!rt_mutex_trylock(&parent->mux_lock))
  133. return 0; /* mux_lock not locked, failure */
  134. if (!(flags & I2C_LOCK_ROOT_ADAPTER))
  135. return 1; /* we only want mux_lock, success */
  136. if (i2c_trylock_bus(parent, flags))
  137. return 1; /* parent locked too, success */
  138. rt_mutex_unlock(&parent->mux_lock);
  139. return 0; /* parent not locked, failure */
  140. }
  141. static void i2c_mux_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
  142. {
  143. struct i2c_mux_priv *priv = adapter->algo_data;
  144. struct i2c_adapter *parent = priv->muxc->parent;
  145. if (flags & I2C_LOCK_ROOT_ADAPTER)
  146. i2c_unlock_bus(parent, flags);
  147. rt_mutex_unlock(&parent->mux_lock);
  148. }
  149. static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
  150. unsigned int flags)
  151. {
  152. struct i2c_mux_priv *priv = adapter->algo_data;
  153. struct i2c_adapter *parent = priv->muxc->parent;
  154. rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
  155. i2c_lock_bus(parent, flags);
  156. }
  157. static int i2c_parent_trylock_bus(struct i2c_adapter *adapter,
  158. unsigned int flags)
  159. {
  160. struct i2c_mux_priv *priv = adapter->algo_data;
  161. struct i2c_adapter *parent = priv->muxc->parent;
  162. if (!rt_mutex_trylock(&parent->mux_lock))
  163. return 0; /* mux_lock not locked, failure */
  164. if (i2c_trylock_bus(parent, flags))
  165. return 1; /* parent locked too, success */
  166. rt_mutex_unlock(&parent->mux_lock);
  167. return 0; /* parent not locked, failure */
  168. }
  169. static void i2c_parent_unlock_bus(struct i2c_adapter *adapter,
  170. unsigned int flags)
  171. {
  172. struct i2c_mux_priv *priv = adapter->algo_data;
  173. struct i2c_adapter *parent = priv->muxc->parent;
  174. i2c_unlock_bus(parent, flags);
  175. rt_mutex_unlock(&parent->mux_lock);
  176. }
  177. struct i2c_adapter *i2c_root_adapter(struct device *dev)
  178. {
  179. struct device *i2c;
  180. struct i2c_adapter *i2c_root;
  181. /*
  182. * Walk up the device tree to find an i2c adapter, indicating
  183. * that this is an i2c client device. Check all ancestors to
  184. * handle mfd devices etc.
  185. */
  186. for (i2c = dev; i2c; i2c = i2c->parent) {
  187. if (i2c->type == &i2c_adapter_type)
  188. break;
  189. }
  190. if (!i2c)
  191. return NULL;
  192. /* Continue up the tree to find the root i2c adapter */
  193. i2c_root = to_i2c_adapter(i2c);
  194. while (i2c_parent_is_i2c_adapter(i2c_root))
  195. i2c_root = i2c_parent_is_i2c_adapter(i2c_root);
  196. return i2c_root;
  197. }
  198. EXPORT_SYMBOL_GPL(i2c_root_adapter);
  199. struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
  200. struct device *dev, int max_adapters,
  201. int sizeof_priv, u32 flags,
  202. int (*select)(struct i2c_mux_core *, u32),
  203. int (*deselect)(struct i2c_mux_core *, u32))
  204. {
  205. struct i2c_mux_core *muxc;
  206. size_t mux_size;
  207. mux_size = struct_size(muxc, adapter, max_adapters);
  208. muxc = devm_kzalloc(dev, size_add(mux_size, sizeof_priv), GFP_KERNEL);
  209. if (!muxc)
  210. return NULL;
  211. if (sizeof_priv)
  212. muxc->priv = &muxc->adapter[max_adapters];
  213. muxc->parent = parent;
  214. muxc->dev = dev;
  215. if (flags & I2C_MUX_LOCKED)
  216. muxc->mux_locked = true;
  217. if (flags & I2C_MUX_ARBITRATOR)
  218. muxc->arbitrator = true;
  219. if (flags & I2C_MUX_GATE)
  220. muxc->gate = true;
  221. muxc->select = select;
  222. muxc->deselect = deselect;
  223. muxc->max_adapters = max_adapters;
  224. return muxc;
  225. }
  226. EXPORT_SYMBOL_GPL(i2c_mux_alloc);
  227. static const struct i2c_lock_operations i2c_mux_lock_ops = {
  228. .lock_bus = i2c_mux_lock_bus,
  229. .trylock_bus = i2c_mux_trylock_bus,
  230. .unlock_bus = i2c_mux_unlock_bus,
  231. };
  232. static const struct i2c_lock_operations i2c_parent_lock_ops = {
  233. .lock_bus = i2c_parent_lock_bus,
  234. .trylock_bus = i2c_parent_trylock_bus,
  235. .unlock_bus = i2c_parent_unlock_bus,
  236. };
  237. int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
  238. u32 force_nr, u32 chan_id,
  239. unsigned int class)
  240. {
  241. struct i2c_adapter *parent = muxc->parent;
  242. struct i2c_mux_priv *priv;
  243. char symlink_name[20];
  244. int ret;
  245. if (muxc->num_adapters >= muxc->max_adapters) {
  246. dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
  247. return -EINVAL;
  248. }
  249. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  250. if (!priv)
  251. return -ENOMEM;
  252. /* Set up private adapter data */
  253. priv->muxc = muxc;
  254. priv->chan_id = chan_id;
  255. /* Need to do algo dynamically because we don't know ahead
  256. * of time what sort of physical adapter we'll be dealing with.
  257. */
  258. if (parent->algo->master_xfer) {
  259. if (muxc->mux_locked)
  260. priv->algo.master_xfer = i2c_mux_master_xfer;
  261. else
  262. priv->algo.master_xfer = __i2c_mux_master_xfer;
  263. }
  264. if (parent->algo->master_xfer_atomic)
  265. priv->algo.master_xfer_atomic = priv->algo.master_xfer;
  266. if (parent->algo->smbus_xfer) {
  267. if (muxc->mux_locked)
  268. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  269. else
  270. priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
  271. }
  272. if (parent->algo->smbus_xfer_atomic)
  273. priv->algo.smbus_xfer_atomic = priv->algo.smbus_xfer;
  274. priv->algo.functionality = i2c_mux_functionality;
  275. /* Now fill out new adapter structure */
  276. snprintf(priv->adap.name, sizeof(priv->adap.name),
  277. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  278. priv->adap.owner = THIS_MODULE;
  279. priv->adap.algo = &priv->algo;
  280. priv->adap.algo_data = priv;
  281. priv->adap.dev.parent = &parent->dev;
  282. priv->adap.retries = parent->retries;
  283. priv->adap.timeout = parent->timeout;
  284. priv->adap.quirks = parent->quirks;
  285. if (muxc->mux_locked)
  286. priv->adap.lock_ops = &i2c_mux_lock_ops;
  287. else
  288. priv->adap.lock_ops = &i2c_parent_lock_ops;
  289. /* Sanity check on class */
  290. if (i2c_mux_parent_classes(parent) & class & ~I2C_CLASS_DEPRECATED)
  291. dev_err(&parent->dev,
  292. "Segment %d behind mux can't share classes with ancestors\n",
  293. chan_id);
  294. else
  295. priv->adap.class = class;
  296. /*
  297. * Try to populate the mux adapter's of_node, expands to
  298. * nothing if !CONFIG_OF.
  299. */
  300. if (muxc->dev->of_node) {
  301. struct device_node *dev_node = muxc->dev->of_node;
  302. struct device_node *mux_node, *child = NULL;
  303. u32 reg;
  304. if (muxc->arbitrator)
  305. mux_node = of_get_child_by_name(dev_node, "i2c-arb");
  306. else if (muxc->gate)
  307. mux_node = of_get_child_by_name(dev_node, "i2c-gate");
  308. else
  309. mux_node = of_get_child_by_name(dev_node, "i2c-mux");
  310. if (mux_node) {
  311. /* A "reg" property indicates an old-style DT entry */
  312. if (!of_property_read_u32(mux_node, "reg", &reg)) {
  313. of_node_put(mux_node);
  314. mux_node = NULL;
  315. }
  316. }
  317. if (!mux_node)
  318. mux_node = of_node_get(dev_node);
  319. else if (muxc->arbitrator || muxc->gate)
  320. child = of_node_get(mux_node);
  321. if (!child) {
  322. for_each_child_of_node(mux_node, child) {
  323. ret = of_property_read_u32(child, "reg", &reg);
  324. if (ret)
  325. continue;
  326. if (chan_id == reg)
  327. break;
  328. }
  329. }
  330. priv->adap.dev.of_node = child;
  331. of_node_put(mux_node);
  332. }
  333. /*
  334. * Associate the mux channel with an ACPI node.
  335. */
  336. if (has_acpi_companion(muxc->dev))
  337. acpi_preset_companion(&priv->adap.dev,
  338. ACPI_COMPANION(muxc->dev),
  339. chan_id);
  340. if (force_nr) {
  341. priv->adap.nr = force_nr;
  342. ret = i2c_add_numbered_adapter(&priv->adap);
  343. if (ret < 0) {
  344. dev_err(&parent->dev,
  345. "failed to add mux-adapter %u as bus %u (error=%d)\n",
  346. chan_id, force_nr, ret);
  347. goto err_free_priv;
  348. }
  349. } else {
  350. ret = i2c_add_adapter(&priv->adap);
  351. if (ret < 0) {
  352. dev_err(&parent->dev,
  353. "failed to add mux-adapter %u (error=%d)\n",
  354. chan_id, ret);
  355. goto err_free_priv;
  356. }
  357. }
  358. WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
  359. "mux_device"),
  360. "can't create symlink to mux device\n");
  361. snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
  362. WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
  363. symlink_name),
  364. "can't create symlink to channel %u\n", chan_id);
  365. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  366. i2c_adapter_id(&priv->adap));
  367. muxc->adapter[muxc->num_adapters++] = &priv->adap;
  368. return 0;
  369. err_free_priv:
  370. kfree(priv);
  371. return ret;
  372. }
  373. EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
  374. void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
  375. {
  376. char symlink_name[20];
  377. while (muxc->num_adapters) {
  378. struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
  379. struct i2c_mux_priv *priv = adap->algo_data;
  380. struct device_node *np = adap->dev.of_node;
  381. muxc->adapter[muxc->num_adapters] = NULL;
  382. snprintf(symlink_name, sizeof(symlink_name),
  383. "channel-%u", priv->chan_id);
  384. sysfs_remove_link(&muxc->dev->kobj, symlink_name);
  385. sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
  386. i2c_del_adapter(adap);
  387. of_node_put(np);
  388. kfree(priv);
  389. }
  390. }
  391. EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
  392. MODULE_AUTHOR("Rodolfo Giometti <[email protected]>");
  393. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  394. MODULE_LICENSE("GPL v2");