i2c-demux-pinctrl.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Pinctrl based I2C DeMultiplexer
  3. *
  4. * Copyright (C) 2015-16 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
  5. * Copyright (C) 2015-16 by Renesas Electronics Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; version 2 of the License.
  10. *
  11. * See the bindings doc for DTS setup and the sysfs doc for usage information.
  12. * (look for filenames containing 'i2c-demux-pinctrl' in Documentation/)
  13. */
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/sysfs.h>
  22. struct i2c_demux_pinctrl_chan {
  23. struct device_node *parent_np;
  24. struct i2c_adapter *parent_adap;
  25. struct of_changeset chgset;
  26. };
  27. struct i2c_demux_pinctrl_priv {
  28. int cur_chan;
  29. int num_chan;
  30. struct device *dev;
  31. const char *bus_name;
  32. struct i2c_adapter cur_adap;
  33. struct i2c_algorithm algo;
  34. struct i2c_demux_pinctrl_chan chan[];
  35. };
  36. static int i2c_demux_master_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  37. {
  38. struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
  39. struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
  40. return __i2c_transfer(parent, msgs, num);
  41. }
  42. static u32 i2c_demux_functionality(struct i2c_adapter *adap)
  43. {
  44. struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
  45. struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
  46. return parent->algo->functionality(parent);
  47. }
  48. static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
  49. {
  50. struct i2c_adapter *adap;
  51. struct pinctrl *p;
  52. int ret;
  53. ret = of_changeset_apply(&priv->chan[new_chan].chgset);
  54. if (ret)
  55. goto err;
  56. adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
  57. if (!adap) {
  58. ret = -ENODEV;
  59. goto err_with_revert;
  60. }
  61. /*
  62. * Check if there are pinctrl states at all. Note: we cant' use
  63. * devm_pinctrl_get_select() because we need to distinguish between
  64. * the -ENODEV from devm_pinctrl_get() and pinctrl_lookup_state().
  65. */
  66. p = devm_pinctrl_get(adap->dev.parent);
  67. if (IS_ERR(p)) {
  68. ret = PTR_ERR(p);
  69. /* continue if just no pinctrl states (e.g. i2c-gpio), otherwise exit */
  70. if (ret != -ENODEV)
  71. goto err_with_put;
  72. } else {
  73. /* there are states. check and use them */
  74. struct pinctrl_state *s = pinctrl_lookup_state(p, priv->bus_name);
  75. if (IS_ERR(s)) {
  76. ret = PTR_ERR(s);
  77. goto err_with_put;
  78. }
  79. ret = pinctrl_select_state(p, s);
  80. if (ret < 0)
  81. goto err_with_put;
  82. }
  83. priv->chan[new_chan].parent_adap = adap;
  84. priv->cur_chan = new_chan;
  85. /* Now fill out current adapter structure. cur_chan must be up to date */
  86. priv->algo.master_xfer = i2c_demux_master_xfer;
  87. priv->algo.functionality = i2c_demux_functionality;
  88. snprintf(priv->cur_adap.name, sizeof(priv->cur_adap.name),
  89. "i2c-demux (master i2c-%d)", i2c_adapter_id(adap));
  90. priv->cur_adap.owner = THIS_MODULE;
  91. priv->cur_adap.algo = &priv->algo;
  92. priv->cur_adap.algo_data = priv;
  93. priv->cur_adap.dev.parent = priv->dev;
  94. priv->cur_adap.class = adap->class;
  95. priv->cur_adap.retries = adap->retries;
  96. priv->cur_adap.timeout = adap->timeout;
  97. priv->cur_adap.quirks = adap->quirks;
  98. priv->cur_adap.dev.of_node = priv->dev->of_node;
  99. ret = i2c_add_adapter(&priv->cur_adap);
  100. if (ret < 0)
  101. goto err_with_put;
  102. return 0;
  103. err_with_put:
  104. i2c_put_adapter(adap);
  105. err_with_revert:
  106. of_changeset_revert(&priv->chan[new_chan].chgset);
  107. err:
  108. dev_err(priv->dev, "failed to setup demux-adapter %d (%d)\n", new_chan, ret);
  109. priv->cur_chan = -EINVAL;
  110. return ret;
  111. }
  112. static int i2c_demux_deactivate_master(struct i2c_demux_pinctrl_priv *priv)
  113. {
  114. int ret, cur = priv->cur_chan;
  115. if (cur < 0)
  116. return 0;
  117. i2c_del_adapter(&priv->cur_adap);
  118. i2c_put_adapter(priv->chan[cur].parent_adap);
  119. ret = of_changeset_revert(&priv->chan[cur].chgset);
  120. priv->chan[cur].parent_adap = NULL;
  121. priv->cur_chan = -EINVAL;
  122. return ret;
  123. }
  124. static int i2c_demux_change_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
  125. {
  126. int ret;
  127. if (new_chan == priv->cur_chan)
  128. return 0;
  129. ret = i2c_demux_deactivate_master(priv);
  130. if (ret)
  131. return ret;
  132. return i2c_demux_activate_master(priv, new_chan);
  133. }
  134. static ssize_t available_masters_show(struct device *dev,
  135. struct device_attribute *attr,
  136. char *buf)
  137. {
  138. struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
  139. int count = 0, i;
  140. for (i = 0; i < priv->num_chan && count < PAGE_SIZE; i++)
  141. count += scnprintf(buf + count, PAGE_SIZE - count, "%d:%s%c",
  142. i, priv->chan[i].parent_np->full_name,
  143. i == priv->num_chan - 1 ? '\n' : ' ');
  144. return count;
  145. }
  146. static DEVICE_ATTR_RO(available_masters);
  147. static ssize_t current_master_show(struct device *dev,
  148. struct device_attribute *attr,
  149. char *buf)
  150. {
  151. struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
  152. return sprintf(buf, "%d\n", priv->cur_chan);
  153. }
  154. static ssize_t current_master_store(struct device *dev,
  155. struct device_attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
  159. unsigned int val;
  160. int ret;
  161. ret = kstrtouint(buf, 0, &val);
  162. if (ret < 0)
  163. return ret;
  164. if (val >= priv->num_chan)
  165. return -EINVAL;
  166. ret = i2c_demux_change_master(priv, val);
  167. return ret < 0 ? ret : count;
  168. }
  169. static DEVICE_ATTR_RW(current_master);
  170. static int i2c_demux_pinctrl_probe(struct platform_device *pdev)
  171. {
  172. struct device_node *np = pdev->dev.of_node;
  173. struct i2c_demux_pinctrl_priv *priv;
  174. struct property *props;
  175. int num_chan, i, j, err;
  176. num_chan = of_count_phandle_with_args(np, "i2c-parent", NULL);
  177. if (num_chan < 2) {
  178. dev_err(&pdev->dev, "Need at least two I2C masters to switch\n");
  179. return -EINVAL;
  180. }
  181. priv = devm_kzalloc(&pdev->dev, sizeof(*priv)
  182. + num_chan * sizeof(struct i2c_demux_pinctrl_chan), GFP_KERNEL);
  183. props = devm_kcalloc(&pdev->dev, num_chan, sizeof(*props), GFP_KERNEL);
  184. if (!priv || !props)
  185. return -ENOMEM;
  186. err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name);
  187. if (err)
  188. return err;
  189. for (i = 0; i < num_chan; i++) {
  190. struct device_node *adap_np;
  191. adap_np = of_parse_phandle(np, "i2c-parent", i);
  192. if (!adap_np) {
  193. dev_err(&pdev->dev, "can't get phandle for parent %d\n", i);
  194. err = -ENOENT;
  195. goto err_rollback;
  196. }
  197. priv->chan[i].parent_np = adap_np;
  198. props[i].name = devm_kstrdup(&pdev->dev, "status", GFP_KERNEL);
  199. props[i].value = devm_kstrdup(&pdev->dev, "ok", GFP_KERNEL);
  200. props[i].length = 3;
  201. of_changeset_init(&priv->chan[i].chgset);
  202. of_changeset_update_property(&priv->chan[i].chgset, adap_np, &props[i]);
  203. }
  204. priv->num_chan = num_chan;
  205. priv->dev = &pdev->dev;
  206. platform_set_drvdata(pdev, priv);
  207. /* switch to first parent as active master */
  208. i2c_demux_activate_master(priv, 0);
  209. err = device_create_file(&pdev->dev, &dev_attr_available_masters);
  210. if (err)
  211. goto err_rollback;
  212. err = device_create_file(&pdev->dev, &dev_attr_current_master);
  213. if (err)
  214. goto err_rollback_available;
  215. return 0;
  216. err_rollback_available:
  217. device_remove_file(&pdev->dev, &dev_attr_available_masters);
  218. err_rollback:
  219. for (j = 0; j < i; j++) {
  220. of_node_put(priv->chan[j].parent_np);
  221. of_changeset_destroy(&priv->chan[j].chgset);
  222. }
  223. return err;
  224. }
  225. static int i2c_demux_pinctrl_remove(struct platform_device *pdev)
  226. {
  227. struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev);
  228. int i;
  229. device_remove_file(&pdev->dev, &dev_attr_current_master);
  230. device_remove_file(&pdev->dev, &dev_attr_available_masters);
  231. i2c_demux_deactivate_master(priv);
  232. for (i = 0; i < priv->num_chan; i++) {
  233. of_node_put(priv->chan[i].parent_np);
  234. of_changeset_destroy(&priv->chan[i].chgset);
  235. }
  236. return 0;
  237. }
  238. static const struct of_device_id i2c_demux_pinctrl_of_match[] = {
  239. { .compatible = "i2c-demux-pinctrl", },
  240. {},
  241. };
  242. MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match);
  243. static struct platform_driver i2c_demux_pinctrl_driver = {
  244. .driver = {
  245. .name = "i2c-demux-pinctrl",
  246. .of_match_table = i2c_demux_pinctrl_of_match,
  247. },
  248. .probe = i2c_demux_pinctrl_probe,
  249. .remove = i2c_demux_pinctrl_remove,
  250. };
  251. module_platform_driver(i2c_demux_pinctrl_driver);
  252. MODULE_DESCRIPTION("pinctrl-based I2C demux driver");
  253. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  254. MODULE_LICENSE("GPL v2");
  255. MODULE_ALIAS("platform:i2c-demux-pinctrl");