i2c-mux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #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. static void i2c_mux_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
  110. {
  111. struct i2c_mux_priv *priv = adapter->algo_data;
  112. struct i2c_adapter *parent = priv->muxc->parent;
  113. rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
  114. if (!(flags & I2C_LOCK_ROOT_ADAPTER))
  115. return;
  116. i2c_lock_bus(parent, flags);
  117. }
  118. static int i2c_mux_trylock_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. if (!rt_mutex_trylock(&parent->mux_lock))
  123. return 0; /* mux_lock not locked, failure */
  124. if (!(flags & I2C_LOCK_ROOT_ADAPTER))
  125. return 1; /* we only want mux_lock, success */
  126. if (i2c_trylock_bus(parent, flags))
  127. return 1; /* parent locked too, success */
  128. rt_mutex_unlock(&parent->mux_lock);
  129. return 0; /* parent not locked, failure */
  130. }
  131. static void i2c_mux_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
  132. {
  133. struct i2c_mux_priv *priv = adapter->algo_data;
  134. struct i2c_adapter *parent = priv->muxc->parent;
  135. if (flags & I2C_LOCK_ROOT_ADAPTER)
  136. i2c_unlock_bus(parent, flags);
  137. rt_mutex_unlock(&parent->mux_lock);
  138. }
  139. static void i2c_parent_lock_bus(struct i2c_adapter *adapter,
  140. unsigned int flags)
  141. {
  142. struct i2c_mux_priv *priv = adapter->algo_data;
  143. struct i2c_adapter *parent = priv->muxc->parent;
  144. rt_mutex_lock_nested(&parent->mux_lock, i2c_adapter_depth(adapter));
  145. i2c_lock_bus(parent, flags);
  146. }
  147. static int i2c_parent_trylock_bus(struct i2c_adapter *adapter,
  148. unsigned int flags)
  149. {
  150. struct i2c_mux_priv *priv = adapter->algo_data;
  151. struct i2c_adapter *parent = priv->muxc->parent;
  152. if (!rt_mutex_trylock(&parent->mux_lock))
  153. return 0; /* mux_lock not locked, failure */
  154. if (i2c_trylock_bus(parent, flags))
  155. return 1; /* parent locked too, success */
  156. rt_mutex_unlock(&parent->mux_lock);
  157. return 0; /* parent not locked, failure */
  158. }
  159. static void i2c_parent_unlock_bus(struct i2c_adapter *adapter,
  160. unsigned int flags)
  161. {
  162. struct i2c_mux_priv *priv = adapter->algo_data;
  163. struct i2c_adapter *parent = priv->muxc->parent;
  164. i2c_unlock_bus(parent, flags);
  165. rt_mutex_unlock(&parent->mux_lock);
  166. }
  167. struct i2c_adapter *i2c_root_adapter(struct device *dev)
  168. {
  169. struct device *i2c;
  170. struct i2c_adapter *i2c_root;
  171. /*
  172. * Walk up the device tree to find an i2c adapter, indicating
  173. * that this is an i2c client device. Check all ancestors to
  174. * handle mfd devices etc.
  175. */
  176. for (i2c = dev; i2c; i2c = i2c->parent) {
  177. if (i2c->type == &i2c_adapter_type)
  178. break;
  179. }
  180. if (!i2c)
  181. return NULL;
  182. /* Continue up the tree to find the root i2c adapter */
  183. i2c_root = to_i2c_adapter(i2c);
  184. while (i2c_parent_is_i2c_adapter(i2c_root))
  185. i2c_root = i2c_parent_is_i2c_adapter(i2c_root);
  186. return i2c_root;
  187. }
  188. EXPORT_SYMBOL_GPL(i2c_root_adapter);
  189. struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
  190. struct device *dev, int max_adapters,
  191. int sizeof_priv, u32 flags,
  192. int (*select)(struct i2c_mux_core *, u32),
  193. int (*deselect)(struct i2c_mux_core *, u32))
  194. {
  195. struct i2c_mux_core *muxc;
  196. size_t mux_size;
  197. mux_size = struct_size(muxc, adapter, max_adapters);
  198. muxc = devm_kzalloc(dev, size_add(mux_size, sizeof_priv), GFP_KERNEL);
  199. if (!muxc)
  200. return NULL;
  201. if (sizeof_priv)
  202. muxc->priv = &muxc->adapter[max_adapters];
  203. muxc->parent = parent;
  204. muxc->dev = dev;
  205. if (flags & I2C_MUX_LOCKED)
  206. muxc->mux_locked = true;
  207. if (flags & I2C_MUX_ARBITRATOR)
  208. muxc->arbitrator = true;
  209. if (flags & I2C_MUX_GATE)
  210. muxc->gate = true;
  211. muxc->select = select;
  212. muxc->deselect = deselect;
  213. muxc->max_adapters = max_adapters;
  214. return muxc;
  215. }
  216. EXPORT_SYMBOL_GPL(i2c_mux_alloc);
  217. static const struct i2c_lock_operations i2c_mux_lock_ops = {
  218. .lock_bus = i2c_mux_lock_bus,
  219. .trylock_bus = i2c_mux_trylock_bus,
  220. .unlock_bus = i2c_mux_unlock_bus,
  221. };
  222. static const struct i2c_lock_operations i2c_parent_lock_ops = {
  223. .lock_bus = i2c_parent_lock_bus,
  224. .trylock_bus = i2c_parent_trylock_bus,
  225. .unlock_bus = i2c_parent_unlock_bus,
  226. };
  227. int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
  228. u32 force_nr, u32 chan_id)
  229. {
  230. struct i2c_adapter *parent = muxc->parent;
  231. struct i2c_mux_priv *priv;
  232. char symlink_name[20];
  233. int ret;
  234. if (muxc->num_adapters >= muxc->max_adapters) {
  235. dev_err(muxc->dev, "No room for more i2c-mux adapters\n");
  236. return -EINVAL;
  237. }
  238. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  239. if (!priv)
  240. return -ENOMEM;
  241. /* Set up private adapter data */
  242. priv->muxc = muxc;
  243. priv->chan_id = chan_id;
  244. /* Need to do algo dynamically because we don't know ahead
  245. * of time what sort of physical adapter we'll be dealing with.
  246. */
  247. if (parent->algo->master_xfer) {
  248. if (muxc->mux_locked)
  249. priv->algo.master_xfer = i2c_mux_master_xfer;
  250. else
  251. priv->algo.master_xfer = __i2c_mux_master_xfer;
  252. }
  253. if (parent->algo->master_xfer_atomic)
  254. priv->algo.master_xfer_atomic = priv->algo.master_xfer;
  255. if (parent->algo->smbus_xfer) {
  256. if (muxc->mux_locked)
  257. priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
  258. else
  259. priv->algo.smbus_xfer = __i2c_mux_smbus_xfer;
  260. }
  261. if (parent->algo->smbus_xfer_atomic)
  262. priv->algo.smbus_xfer_atomic = priv->algo.smbus_xfer;
  263. priv->algo.functionality = i2c_mux_functionality;
  264. /* Now fill out new adapter structure */
  265. snprintf(priv->adap.name, sizeof(priv->adap.name),
  266. "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
  267. priv->adap.owner = THIS_MODULE;
  268. priv->adap.algo = &priv->algo;
  269. priv->adap.algo_data = priv;
  270. priv->adap.dev.parent = &parent->dev;
  271. priv->adap.retries = parent->retries;
  272. priv->adap.timeout = parent->timeout;
  273. priv->adap.quirks = parent->quirks;
  274. if (muxc->mux_locked)
  275. priv->adap.lock_ops = &i2c_mux_lock_ops;
  276. else
  277. priv->adap.lock_ops = &i2c_parent_lock_ops;
  278. /*
  279. * Try to populate the mux adapter's of_node, expands to
  280. * nothing if !CONFIG_OF.
  281. */
  282. if (muxc->dev->of_node) {
  283. struct device_node *dev_node = muxc->dev->of_node;
  284. struct device_node *mux_node, *child = NULL;
  285. u32 reg;
  286. if (muxc->arbitrator)
  287. mux_node = of_get_child_by_name(dev_node, "i2c-arb");
  288. else if (muxc->gate)
  289. mux_node = of_get_child_by_name(dev_node, "i2c-gate");
  290. else
  291. mux_node = of_get_child_by_name(dev_node, "i2c-mux");
  292. if (mux_node) {
  293. /* A "reg" property indicates an old-style DT entry */
  294. if (!of_property_read_u32(mux_node, "reg", &reg)) {
  295. of_node_put(mux_node);
  296. mux_node = NULL;
  297. }
  298. }
  299. if (!mux_node)
  300. mux_node = of_node_get(dev_node);
  301. else if (muxc->arbitrator || muxc->gate)
  302. child = of_node_get(mux_node);
  303. if (!child) {
  304. for_each_child_of_node(mux_node, child) {
  305. ret = of_property_read_u32(child, "reg", &reg);
  306. if (ret)
  307. continue;
  308. if (chan_id == reg)
  309. break;
  310. }
  311. }
  312. priv->adap.dev.of_node = child;
  313. of_node_put(mux_node);
  314. }
  315. /*
  316. * Associate the mux channel with an ACPI node.
  317. */
  318. if (has_acpi_companion(muxc->dev))
  319. acpi_preset_companion(&priv->adap.dev,
  320. ACPI_COMPANION(muxc->dev),
  321. chan_id);
  322. if (force_nr) {
  323. priv->adap.nr = force_nr;
  324. ret = i2c_add_numbered_adapter(&priv->adap);
  325. if (ret < 0) {
  326. dev_err(&parent->dev,
  327. "failed to add mux-adapter %u as bus %u (error=%d)\n",
  328. chan_id, force_nr, ret);
  329. goto err_free_priv;
  330. }
  331. } else {
  332. ret = i2c_add_adapter(&priv->adap);
  333. if (ret < 0) {
  334. dev_err(&parent->dev,
  335. "failed to add mux-adapter %u (error=%d)\n",
  336. chan_id, ret);
  337. goto err_free_priv;
  338. }
  339. }
  340. WARN(sysfs_create_link(&priv->adap.dev.kobj, &muxc->dev->kobj,
  341. "mux_device"),
  342. "can't create symlink to mux device\n");
  343. snprintf(symlink_name, sizeof(symlink_name), "channel-%u", chan_id);
  344. WARN(sysfs_create_link(&muxc->dev->kobj, &priv->adap.dev.kobj,
  345. symlink_name),
  346. "can't create symlink to channel %u\n", chan_id);
  347. dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
  348. i2c_adapter_id(&priv->adap));
  349. muxc->adapter[muxc->num_adapters++] = &priv->adap;
  350. return 0;
  351. err_free_priv:
  352. kfree(priv);
  353. return ret;
  354. }
  355. EXPORT_SYMBOL_GPL(i2c_mux_add_adapter);
  356. void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
  357. {
  358. char symlink_name[20];
  359. while (muxc->num_adapters) {
  360. struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
  361. struct i2c_mux_priv *priv = adap->algo_data;
  362. struct device_node *np = adap->dev.of_node;
  363. muxc->adapter[muxc->num_adapters] = NULL;
  364. snprintf(symlink_name, sizeof(symlink_name),
  365. "channel-%u", priv->chan_id);
  366. sysfs_remove_link(&muxc->dev->kobj, symlink_name);
  367. sysfs_remove_link(&priv->adap.dev.kobj, "mux_device");
  368. i2c_del_adapter(adap);
  369. of_node_put(np);
  370. kfree(priv);
  371. }
  372. }
  373. EXPORT_SYMBOL_GPL(i2c_mux_del_adapters);
  374. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  375. MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
  376. MODULE_LICENSE("GPL v2");