i2c-mux-pinctrl.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/i2c-mux-pinctrl.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/of.h>
  26. #include "../../pinctrl/core.h"
  27. struct i2c_mux_pinctrl {
  28. struct i2c_mux_pinctrl_platform_data *pdata;
  29. struct pinctrl *pinctrl;
  30. struct pinctrl_state **states;
  31. struct pinctrl_state *state_idle;
  32. };
  33. static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan)
  34. {
  35. struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc);
  36. return pinctrl_select_state(mux->pinctrl, mux->states[chan]);
  37. }
  38. static int i2c_mux_pinctrl_deselect(struct i2c_mux_core *muxc, u32 chan)
  39. {
  40. struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc);
  41. return pinctrl_select_state(mux->pinctrl, mux->state_idle);
  42. }
  43. #ifdef CONFIG_OF
  44. static int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
  45. struct platform_device *pdev)
  46. {
  47. struct device_node *np = pdev->dev.of_node;
  48. int num_names, i, ret;
  49. struct device_node *adapter_np;
  50. struct i2c_adapter *adapter;
  51. if (!np)
  52. return 0;
  53. mux->pdata = devm_kzalloc(&pdev->dev, sizeof(*mux->pdata), GFP_KERNEL);
  54. if (!mux->pdata)
  55. return -ENOMEM;
  56. num_names = of_property_count_strings(np, "pinctrl-names");
  57. if (num_names < 0) {
  58. dev_err(&pdev->dev, "Cannot parse pinctrl-names: %d\n",
  59. num_names);
  60. return num_names;
  61. }
  62. mux->pdata->pinctrl_states = devm_kzalloc(&pdev->dev,
  63. sizeof(*mux->pdata->pinctrl_states) * num_names,
  64. GFP_KERNEL);
  65. if (!mux->pdata->pinctrl_states)
  66. return -ENOMEM;
  67. for (i = 0; i < num_names; i++) {
  68. ret = of_property_read_string_index(np, "pinctrl-names", i,
  69. &mux->pdata->pinctrl_states[mux->pdata->bus_count]);
  70. if (ret < 0) {
  71. dev_err(&pdev->dev, "Cannot parse pinctrl-names: %d\n",
  72. ret);
  73. return ret;
  74. }
  75. if (!strcmp(mux->pdata->pinctrl_states[mux->pdata->bus_count],
  76. "idle")) {
  77. if (i != num_names - 1) {
  78. dev_err(&pdev->dev,
  79. "idle state must be last\n");
  80. return -EINVAL;
  81. }
  82. mux->pdata->pinctrl_state_idle = "idle";
  83. } else {
  84. mux->pdata->bus_count++;
  85. }
  86. }
  87. adapter_np = of_parse_phandle(np, "i2c-parent", 0);
  88. if (!adapter_np) {
  89. dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
  90. return -ENODEV;
  91. }
  92. adapter = of_find_i2c_adapter_by_node(adapter_np);
  93. of_node_put(adapter_np);
  94. if (!adapter) {
  95. dev_err(&pdev->dev, "Cannot find parent bus\n");
  96. return -EPROBE_DEFER;
  97. }
  98. mux->pdata->parent_bus_num = i2c_adapter_id(adapter);
  99. put_device(&adapter->dev);
  100. return 0;
  101. }
  102. #else
  103. static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
  104. struct platform_device *pdev)
  105. {
  106. return 0;
  107. }
  108. #endif
  109. static struct i2c_adapter *i2c_mux_pinctrl_root_adapter(
  110. struct pinctrl_state *state)
  111. {
  112. struct i2c_adapter *root = NULL;
  113. struct pinctrl_setting *setting;
  114. struct i2c_adapter *pin_root;
  115. list_for_each_entry(setting, &state->settings, node) {
  116. pin_root = i2c_root_adapter(setting->pctldev->dev);
  117. if (!pin_root)
  118. return NULL;
  119. if (!root)
  120. root = pin_root;
  121. else if (root != pin_root)
  122. return NULL;
  123. }
  124. return root;
  125. }
  126. static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
  127. {
  128. struct i2c_mux_core *muxc;
  129. struct i2c_mux_pinctrl *mux;
  130. struct i2c_adapter *root;
  131. int i, ret;
  132. mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
  133. if (!mux) {
  134. ret = -ENOMEM;
  135. goto err;
  136. }
  137. mux->pdata = dev_get_platdata(&pdev->dev);
  138. if (!mux->pdata) {
  139. ret = i2c_mux_pinctrl_parse_dt(mux, pdev);
  140. if (ret < 0)
  141. goto err;
  142. }
  143. if (!mux->pdata) {
  144. dev_err(&pdev->dev, "Missing platform data\n");
  145. ret = -ENODEV;
  146. goto err;
  147. }
  148. mux->states = devm_kzalloc(&pdev->dev,
  149. sizeof(*mux->states) * mux->pdata->bus_count,
  150. GFP_KERNEL);
  151. if (!mux->states) {
  152. dev_err(&pdev->dev, "Cannot allocate states\n");
  153. ret = -ENOMEM;
  154. goto err;
  155. }
  156. muxc = i2c_mux_alloc(NULL, &pdev->dev, mux->pdata->bus_count, 0, 0,
  157. i2c_mux_pinctrl_select, NULL);
  158. if (!muxc) {
  159. ret = -ENOMEM;
  160. goto err;
  161. }
  162. muxc->priv = mux;
  163. platform_set_drvdata(pdev, muxc);
  164. mux->pinctrl = devm_pinctrl_get(&pdev->dev);
  165. if (IS_ERR(mux->pinctrl)) {
  166. ret = PTR_ERR(mux->pinctrl);
  167. dev_err(&pdev->dev, "Cannot get pinctrl: %d\n", ret);
  168. goto err;
  169. }
  170. for (i = 0; i < mux->pdata->bus_count; i++) {
  171. mux->states[i] = pinctrl_lookup_state(mux->pinctrl,
  172. mux->pdata->pinctrl_states[i]);
  173. if (IS_ERR(mux->states[i])) {
  174. ret = PTR_ERR(mux->states[i]);
  175. dev_err(&pdev->dev,
  176. "Cannot look up pinctrl state %s: %d\n",
  177. mux->pdata->pinctrl_states[i], ret);
  178. goto err;
  179. }
  180. }
  181. if (mux->pdata->pinctrl_state_idle) {
  182. mux->state_idle = pinctrl_lookup_state(mux->pinctrl,
  183. mux->pdata->pinctrl_state_idle);
  184. if (IS_ERR(mux->state_idle)) {
  185. ret = PTR_ERR(mux->state_idle);
  186. dev_err(&pdev->dev,
  187. "Cannot look up pinctrl state %s: %d\n",
  188. mux->pdata->pinctrl_state_idle, ret);
  189. goto err;
  190. }
  191. muxc->deselect = i2c_mux_pinctrl_deselect;
  192. }
  193. muxc->parent = i2c_get_adapter(mux->pdata->parent_bus_num);
  194. if (!muxc->parent) {
  195. dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
  196. mux->pdata->parent_bus_num);
  197. ret = -EPROBE_DEFER;
  198. goto err;
  199. }
  200. root = i2c_root_adapter(&muxc->parent->dev);
  201. muxc->mux_locked = true;
  202. for (i = 0; i < mux->pdata->bus_count; i++) {
  203. if (root != i2c_mux_pinctrl_root_adapter(mux->states[i])) {
  204. muxc->mux_locked = false;
  205. break;
  206. }
  207. }
  208. if (muxc->mux_locked && mux->pdata->pinctrl_state_idle &&
  209. root != i2c_mux_pinctrl_root_adapter(mux->state_idle))
  210. muxc->mux_locked = false;
  211. if (muxc->mux_locked)
  212. dev_info(&pdev->dev, "mux-locked i2c mux\n");
  213. for (i = 0; i < mux->pdata->bus_count; i++) {
  214. u32 bus = mux->pdata->base_bus_num ?
  215. (mux->pdata->base_bus_num + i) : 0;
  216. ret = i2c_mux_add_adapter(muxc, bus, i, 0);
  217. if (ret) {
  218. dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
  219. goto err_del_adapter;
  220. }
  221. }
  222. return 0;
  223. err_del_adapter:
  224. i2c_mux_del_adapters(muxc);
  225. i2c_put_adapter(muxc->parent);
  226. err:
  227. return ret;
  228. }
  229. static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
  230. {
  231. struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
  232. i2c_mux_del_adapters(muxc);
  233. i2c_put_adapter(muxc->parent);
  234. return 0;
  235. }
  236. #ifdef CONFIG_OF
  237. static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
  238. { .compatible = "i2c-mux-pinctrl", },
  239. {},
  240. };
  241. MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
  242. #endif
  243. static struct platform_driver i2c_mux_pinctrl_driver = {
  244. .driver = {
  245. .name = "i2c-mux-pinctrl",
  246. .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
  247. },
  248. .probe = i2c_mux_pinctrl_probe,
  249. .remove = i2c_mux_pinctrl_remove,
  250. };
  251. module_platform_driver(i2c_mux_pinctrl_driver);
  252. MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
  253. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  254. MODULE_LICENSE("GPL v2");
  255. MODULE_ALIAS("platform:i2c-mux-pinctrl");