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. if (!adapter) {
  102. dev_err(mux->dev, "Cannot find parent bus\n");
  103. return -EPROBE_DEFER;
  104. }
  105. mux->pdata->parent_bus_num = i2c_adapter_id(adapter);
  106. put_device(&adapter->dev);
  107. return 0;
  108. }
  109. #else
  110. static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
  111. struct platform_device *pdev)
  112. {
  113. return 0;
  114. }
  115. #endif
  116. static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
  117. {
  118. struct i2c_mux_pinctrl *mux;
  119. int (*deselect)(struct i2c_adapter *, void *, u32);
  120. int i, ret;
  121. mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
  122. if (!mux) {
  123. dev_err(&pdev->dev, "Cannot allocate i2c_mux_pinctrl\n");
  124. ret = -ENOMEM;
  125. goto err;
  126. }
  127. platform_set_drvdata(pdev, mux);
  128. mux->dev = &pdev->dev;
  129. mux->pdata = dev_get_platdata(&pdev->dev);
  130. if (!mux->pdata) {
  131. ret = i2c_mux_pinctrl_parse_dt(mux, pdev);
  132. if (ret < 0)
  133. goto err;
  134. }
  135. if (!mux->pdata) {
  136. dev_err(&pdev->dev, "Missing platform data\n");
  137. ret = -ENODEV;
  138. goto err;
  139. }
  140. mux->states = devm_kzalloc(&pdev->dev,
  141. sizeof(*mux->states) * mux->pdata->bus_count,
  142. GFP_KERNEL);
  143. if (!mux->states) {
  144. dev_err(&pdev->dev, "Cannot allocate states\n");
  145. ret = -ENOMEM;
  146. goto err;
  147. }
  148. mux->busses = devm_kzalloc(&pdev->dev,
  149. sizeof(*mux->busses) * mux->pdata->bus_count,
  150. GFP_KERNEL);
  151. if (!mux->busses) {
  152. dev_err(&pdev->dev, "Cannot allocate busses\n");
  153. ret = -ENOMEM;
  154. goto err;
  155. }
  156. mux->pinctrl = devm_pinctrl_get(&pdev->dev);
  157. if (IS_ERR(mux->pinctrl)) {
  158. ret = PTR_ERR(mux->pinctrl);
  159. dev_err(&pdev->dev, "Cannot get pinctrl: %d\n", ret);
  160. goto err;
  161. }
  162. for (i = 0; i < mux->pdata->bus_count; i++) {
  163. mux->states[i] = pinctrl_lookup_state(mux->pinctrl,
  164. mux->pdata->pinctrl_states[i]);
  165. if (IS_ERR(mux->states[i])) {
  166. ret = PTR_ERR(mux->states[i]);
  167. dev_err(&pdev->dev,
  168. "Cannot look up pinctrl state %s: %d\n",
  169. mux->pdata->pinctrl_states[i], ret);
  170. goto err;
  171. }
  172. }
  173. if (mux->pdata->pinctrl_state_idle) {
  174. mux->state_idle = pinctrl_lookup_state(mux->pinctrl,
  175. mux->pdata->pinctrl_state_idle);
  176. if (IS_ERR(mux->state_idle)) {
  177. ret = PTR_ERR(mux->state_idle);
  178. dev_err(&pdev->dev,
  179. "Cannot look up pinctrl state %s: %d\n",
  180. mux->pdata->pinctrl_state_idle, ret);
  181. goto err;
  182. }
  183. deselect = i2c_mux_pinctrl_deselect;
  184. } else {
  185. deselect = NULL;
  186. }
  187. mux->parent = i2c_get_adapter(mux->pdata->parent_bus_num);
  188. if (!mux->parent) {
  189. dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
  190. mux->pdata->parent_bus_num);
  191. ret = -EPROBE_DEFER;
  192. goto err;
  193. }
  194. for (i = 0; i < mux->pdata->bus_count; i++) {
  195. u32 bus = mux->pdata->base_bus_num ?
  196. (mux->pdata->base_bus_num + i) : 0;
  197. mux->busses[i] = i2c_add_mux_adapter(mux->parent, &pdev->dev,
  198. mux, bus, i, 0,
  199. i2c_mux_pinctrl_select,
  200. deselect);
  201. if (!mux->busses[i]) {
  202. ret = -ENODEV;
  203. dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
  204. goto err_del_adapter;
  205. }
  206. }
  207. return 0;
  208. err_del_adapter:
  209. for (; i > 0; i--)
  210. i2c_del_mux_adapter(mux->busses[i - 1]);
  211. i2c_put_adapter(mux->parent);
  212. err:
  213. return ret;
  214. }
  215. static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
  216. {
  217. struct i2c_mux_pinctrl *mux = platform_get_drvdata(pdev);
  218. int i;
  219. for (i = 0; i < mux->pdata->bus_count; i++)
  220. i2c_del_mux_adapter(mux->busses[i]);
  221. i2c_put_adapter(mux->parent);
  222. return 0;
  223. }
  224. #ifdef CONFIG_OF
  225. static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
  226. { .compatible = "i2c-mux-pinctrl", },
  227. {},
  228. };
  229. MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
  230. #endif
  231. static struct platform_driver i2c_mux_pinctrl_driver = {
  232. .driver = {
  233. .name = "i2c-mux-pinctrl",
  234. .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
  235. },
  236. .probe = i2c_mux_pinctrl_probe,
  237. .remove = i2c_mux_pinctrl_remove,
  238. };
  239. module_platform_driver(i2c_mux_pinctrl_driver);
  240. MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
  241. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  242. MODULE_LICENSE("GPL v2");
  243. MODULE_ALIAS("platform:i2c-mux-pinctrl");