i2c-mux-pinctrl.c 7.0 KB

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