i2c-mux-pinctrl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * I2C multiplexer using pinctrl API
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/i2c.h>
  19. #include <linux/i2c-mux.h>
  20. #include <linux/module.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include "../../pinctrl/core.h"
  26. struct i2c_mux_pinctrl {
  27. struct pinctrl *pinctrl;
  28. struct pinctrl_state **states;
  29. };
  30. static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan)
  31. {
  32. struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc);
  33. return pinctrl_select_state(mux->pinctrl, mux->states[chan]);
  34. }
  35. static int i2c_mux_pinctrl_deselect(struct i2c_mux_core *muxc, u32 chan)
  36. {
  37. return i2c_mux_pinctrl_select(muxc, muxc->num_adapters);
  38. }
  39. static struct i2c_adapter *i2c_mux_pinctrl_root_adapter(
  40. struct pinctrl_state *state)
  41. {
  42. struct i2c_adapter *root = NULL;
  43. struct pinctrl_setting *setting;
  44. struct i2c_adapter *pin_root;
  45. list_for_each_entry(setting, &state->settings, node) {
  46. pin_root = i2c_root_adapter(setting->pctldev->dev);
  47. if (!pin_root)
  48. return NULL;
  49. if (!root)
  50. root = pin_root;
  51. else if (root != pin_root)
  52. return NULL;
  53. }
  54. return root;
  55. }
  56. static struct i2c_adapter *i2c_mux_pinctrl_parent_adapter(struct device *dev)
  57. {
  58. struct device_node *np = dev->of_node;
  59. struct device_node *parent_np;
  60. struct i2c_adapter *parent;
  61. parent_np = of_parse_phandle(np, "i2c-parent", 0);
  62. if (!parent_np) {
  63. dev_err(dev, "Cannot parse i2c-parent\n");
  64. return ERR_PTR(-ENODEV);
  65. }
  66. parent = of_find_i2c_adapter_by_node(parent_np);
  67. of_node_put(parent_np);
  68. if (!parent)
  69. return ERR_PTR(-EPROBE_DEFER);
  70. return parent;
  71. }
  72. static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
  73. {
  74. struct device *dev = &pdev->dev;
  75. struct device_node *np = dev->of_node;
  76. struct i2c_mux_core *muxc;
  77. struct i2c_mux_pinctrl *mux;
  78. struct i2c_adapter *parent;
  79. struct i2c_adapter *root;
  80. int num_names, i, ret;
  81. const char *name;
  82. num_names = of_property_count_strings(np, "pinctrl-names");
  83. if (num_names < 0) {
  84. dev_err(dev, "Cannot parse pinctrl-names: %d\n",
  85. num_names);
  86. return num_names;
  87. }
  88. parent = i2c_mux_pinctrl_parent_adapter(dev);
  89. if (IS_ERR(parent))
  90. return PTR_ERR(parent);
  91. muxc = i2c_mux_alloc(parent, dev, num_names,
  92. sizeof(*mux) + num_names * sizeof(*mux->states),
  93. 0, i2c_mux_pinctrl_select, NULL);
  94. if (!muxc) {
  95. ret = -ENOMEM;
  96. goto err_put_parent;
  97. }
  98. mux = i2c_mux_priv(muxc);
  99. mux->states = (struct pinctrl_state **)(mux + 1);
  100. platform_set_drvdata(pdev, muxc);
  101. mux->pinctrl = devm_pinctrl_get(dev);
  102. if (IS_ERR(mux->pinctrl)) {
  103. ret = PTR_ERR(mux->pinctrl);
  104. dev_err(dev, "Cannot get pinctrl: %d\n", ret);
  105. goto err_put_parent;
  106. }
  107. for (i = 0; i < num_names; i++) {
  108. ret = of_property_read_string_index(np, "pinctrl-names", i,
  109. &name);
  110. if (ret < 0) {
  111. dev_err(dev, "Cannot parse pinctrl-names: %d\n", ret);
  112. goto err_put_parent;
  113. }
  114. mux->states[i] = pinctrl_lookup_state(mux->pinctrl, name);
  115. if (IS_ERR(mux->states[i])) {
  116. ret = PTR_ERR(mux->states[i]);
  117. dev_err(dev, "Cannot look up pinctrl state %s: %d\n",
  118. name, ret);
  119. goto err_put_parent;
  120. }
  121. if (strcmp(name, "idle"))
  122. continue;
  123. if (i != num_names - 1) {
  124. dev_err(dev, "idle state must be last\n");
  125. ret = -EINVAL;
  126. goto err_put_parent;
  127. }
  128. muxc->deselect = i2c_mux_pinctrl_deselect;
  129. }
  130. root = i2c_root_adapter(&muxc->parent->dev);
  131. muxc->mux_locked = true;
  132. for (i = 0; i < num_names; i++) {
  133. if (root != i2c_mux_pinctrl_root_adapter(mux->states[i])) {
  134. muxc->mux_locked = false;
  135. break;
  136. }
  137. }
  138. if (muxc->mux_locked)
  139. dev_info(dev, "mux-locked i2c mux\n");
  140. /* Do not add any adapter for the idle state (if it's there at all). */
  141. for (i = 0; i < num_names - !!muxc->deselect; i++) {
  142. ret = i2c_mux_add_adapter(muxc, 0, i, 0);
  143. if (ret)
  144. goto err_del_adapter;
  145. }
  146. return 0;
  147. err_del_adapter:
  148. i2c_mux_del_adapters(muxc);
  149. err_put_parent:
  150. i2c_put_adapter(parent);
  151. return ret;
  152. }
  153. static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
  154. {
  155. struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
  156. i2c_mux_del_adapters(muxc);
  157. i2c_put_adapter(muxc->parent);
  158. return 0;
  159. }
  160. static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
  161. { .compatible = "i2c-mux-pinctrl", },
  162. {},
  163. };
  164. MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
  165. static struct platform_driver i2c_mux_pinctrl_driver = {
  166. .driver = {
  167. .name = "i2c-mux-pinctrl",
  168. .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
  169. },
  170. .probe = i2c_mux_pinctrl_probe,
  171. .remove = i2c_mux_pinctrl_remove,
  172. };
  173. module_platform_driver(i2c_mux_pinctrl_driver);
  174. MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
  175. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  176. MODULE_LICENSE("GPL v2");
  177. MODULE_ALIAS("platform:i2c-mux-pinctrl");