i2c-demux-pinctrl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Pinctrl based I2C DeMultiplexer
  4. *
  5. * Copyright (C) 2015-16 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
  6. * Copyright (C) 2015-16 by Renesas Electronics Corporation
  7. *
  8. * See the bindings doc for DTS setup and the sysfs doc for usage information.
  9. * (look for filenames containing 'i2c-demux-pinctrl' in Documentation/)
  10. */
  11. #include <linux/i2c.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/pinctrl/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/slab.h>
  19. #include <linux/sysfs.h>
  20. struct i2c_demux_pinctrl_chan {
  21. struct device_node *parent_np;
  22. struct i2c_adapter *parent_adap;
  23. struct of_changeset chgset;
  24. };
  25. struct i2c_demux_pinctrl_priv {
  26. int cur_chan;
  27. int num_chan;
  28. struct device *dev;
  29. const char *bus_name;
  30. struct i2c_adapter cur_adap;
  31. struct i2c_algorithm algo;
  32. struct i2c_demux_pinctrl_chan chan[] __counted_by(num_chan);
  33. };
  34. static int i2c_demux_master_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  35. {
  36. struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
  37. struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
  38. return __i2c_transfer(parent, msgs, num);
  39. }
  40. static u32 i2c_demux_functionality(struct i2c_adapter *adap)
  41. {
  42. struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
  43. struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
  44. return parent->algo->functionality(parent);
  45. }
  46. static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
  47. {
  48. struct i2c_adapter *adap;
  49. struct pinctrl *p;
  50. int ret;
  51. ret = of_changeset_apply(&priv->chan[new_chan].chgset);
  52. if (ret)
  53. goto err;
  54. adap = of_get_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
  55. if (!adap) {
  56. ret = -ENODEV;
  57. goto err_with_revert;
  58. }
  59. /*
  60. * Check if there are pinctrl states at all. Note: we cant' use
  61. * devm_pinctrl_get_select() because we need to distinguish between
  62. * the -ENODEV from devm_pinctrl_get() and pinctrl_lookup_state().
  63. */
  64. p = devm_pinctrl_get(adap->dev.parent);
  65. if (IS_ERR(p)) {
  66. ret = PTR_ERR(p);
  67. /* continue if just no pinctrl states (e.g. i2c-gpio), otherwise exit */
  68. if (ret != -ENODEV)
  69. goto err_with_put;
  70. } else {
  71. /* there are states. check and use them */
  72. struct pinctrl_state *s = pinctrl_lookup_state(p, priv->bus_name);
  73. if (IS_ERR(s)) {
  74. ret = PTR_ERR(s);
  75. goto err_with_put;
  76. }
  77. ret = pinctrl_select_state(p, s);
  78. if (ret < 0)
  79. goto err_with_put;
  80. }
  81. priv->chan[new_chan].parent_adap = adap;
  82. priv->cur_chan = new_chan;
  83. /* Now fill out current adapter structure. cur_chan must be up to date */
  84. priv->algo.master_xfer = i2c_demux_master_xfer;
  85. if (adap->algo->master_xfer_atomic)
  86. priv->algo.master_xfer_atomic = 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 = &adap->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 += sysfs_emit_at(buf, count, "%d:%pOF%c",
  142. i, priv->chan[i].parent_np,
  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, struct_size(priv, chan, num_chan),
  182. GFP_KERNEL);
  183. props = devm_kcalloc(&pdev->dev, num_chan, sizeof(*props), GFP_KERNEL);
  184. if (!priv || !props)
  185. return -ENOMEM;
  186. priv->num_chan = num_chan;
  187. err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name);
  188. if (err)
  189. return err;
  190. for (i = 0; i < num_chan; i++) {
  191. struct device_node *adap_np;
  192. adap_np = of_parse_phandle(np, "i2c-parent", i);
  193. if (!adap_np) {
  194. dev_err(&pdev->dev, "can't get phandle for parent %d\n", i);
  195. err = -ENOENT;
  196. goto err_rollback;
  197. }
  198. priv->chan[i].parent_np = adap_np;
  199. props[i].name = devm_kstrdup(&pdev->dev, "status", GFP_KERNEL);
  200. props[i].value = devm_kstrdup(&pdev->dev, "ok", GFP_KERNEL);
  201. if (!props[i].name || !props[i].value) {
  202. err = -ENOMEM;
  203. goto err_rollback;
  204. }
  205. props[i].length = 3;
  206. of_changeset_init(&priv->chan[i].chgset);
  207. of_changeset_update_property(&priv->chan[i].chgset, adap_np, &props[i]);
  208. }
  209. priv->dev = &pdev->dev;
  210. platform_set_drvdata(pdev, priv);
  211. pm_runtime_no_callbacks(&pdev->dev);
  212. /* switch to first parent as active master */
  213. err = i2c_demux_activate_master(priv, 0);
  214. if (err)
  215. goto err_rollback;
  216. err = device_create_file(&pdev->dev, &dev_attr_available_masters);
  217. if (err)
  218. goto err_rollback_activation;
  219. err = device_create_file(&pdev->dev, &dev_attr_current_master);
  220. if (err)
  221. goto err_rollback_available;
  222. return 0;
  223. err_rollback_available:
  224. device_remove_file(&pdev->dev, &dev_attr_available_masters);
  225. err_rollback_activation:
  226. i2c_demux_deactivate_master(priv);
  227. err_rollback:
  228. for (j = 0; j < i; j++) {
  229. of_node_put(priv->chan[j].parent_np);
  230. of_changeset_destroy(&priv->chan[j].chgset);
  231. }
  232. return err;
  233. }
  234. static void i2c_demux_pinctrl_remove(struct platform_device *pdev)
  235. {
  236. struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev);
  237. int i;
  238. device_remove_file(&pdev->dev, &dev_attr_current_master);
  239. device_remove_file(&pdev->dev, &dev_attr_available_masters);
  240. i2c_demux_deactivate_master(priv);
  241. for (i = 0; i < priv->num_chan; i++) {
  242. of_node_put(priv->chan[i].parent_np);
  243. of_changeset_destroy(&priv->chan[i].chgset);
  244. }
  245. }
  246. static const struct of_device_id i2c_demux_pinctrl_of_match[] = {
  247. { .compatible = "i2c-demux-pinctrl", },
  248. {},
  249. };
  250. MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match);
  251. static struct platform_driver i2c_demux_pinctrl_driver = {
  252. .driver = {
  253. .name = "i2c-demux-pinctrl",
  254. .of_match_table = i2c_demux_pinctrl_of_match,
  255. },
  256. .probe = i2c_demux_pinctrl_probe,
  257. .remove_new = i2c_demux_pinctrl_remove,
  258. };
  259. module_platform_driver(i2c_demux_pinctrl_driver);
  260. MODULE_DESCRIPTION("pinctrl-based I2C demux driver");
  261. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  262. MODULE_LICENSE("GPL v2");
  263. MODULE_ALIAS("platform:i2c-demux-pinctrl");