i2c-mux-gpmux.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * General Purpose I2C multiplexer
  4. *
  5. * Copyright (C) 2017 Axentia Technologies AB
  6. *
  7. * Author: Peter Rosin <peda@axentia.se>
  8. */
  9. #include <linux/i2c.h>
  10. #include <linux/i2c-mux.h>
  11. #include <linux/module.h>
  12. #include <linux/mux/consumer.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. struct mux {
  16. struct mux_control *control;
  17. bool do_not_deselect;
  18. };
  19. static int i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
  20. {
  21. struct mux *mux = i2c_mux_priv(muxc);
  22. int ret;
  23. ret = mux_control_select(mux->control, chan);
  24. mux->do_not_deselect = ret < 0;
  25. return ret;
  26. }
  27. static int i2c_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
  28. {
  29. struct mux *mux = i2c_mux_priv(muxc);
  30. if (mux->do_not_deselect)
  31. return 0;
  32. return mux_control_deselect(mux->control);
  33. }
  34. static struct i2c_adapter *mux_parent_adapter(struct device *dev)
  35. {
  36. struct device_node *np = dev->of_node;
  37. struct device_node *parent_np;
  38. struct i2c_adapter *parent;
  39. parent_np = of_parse_phandle(np, "i2c-parent", 0);
  40. if (!parent_np) {
  41. dev_err(dev, "Cannot parse i2c-parent\n");
  42. return ERR_PTR(-ENODEV);
  43. }
  44. parent = of_get_i2c_adapter_by_node(parent_np);
  45. of_node_put(parent_np);
  46. if (!parent)
  47. return ERR_PTR(-EPROBE_DEFER);
  48. return parent;
  49. }
  50. static const struct of_device_id i2c_mux_of_match[] = {
  51. { .compatible = "i2c-mux", },
  52. {},
  53. };
  54. MODULE_DEVICE_TABLE(of, i2c_mux_of_match);
  55. static int i2c_mux_probe(struct platform_device *pdev)
  56. {
  57. struct device *dev = &pdev->dev;
  58. struct device_node *np = dev->of_node;
  59. struct device_node *child;
  60. struct i2c_mux_core *muxc;
  61. struct mux *mux;
  62. struct i2c_adapter *parent;
  63. int children;
  64. int ret;
  65. if (!np)
  66. return -ENODEV;
  67. mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
  68. if (!mux)
  69. return -ENOMEM;
  70. mux->control = devm_mux_control_get(dev, NULL);
  71. if (IS_ERR(mux->control))
  72. return dev_err_probe(dev, PTR_ERR(mux->control),
  73. "failed to get control-mux\n");
  74. parent = mux_parent_adapter(dev);
  75. if (IS_ERR(parent))
  76. return dev_err_probe(dev, PTR_ERR(parent),
  77. "failed to get i2c-parent adapter\n");
  78. children = of_get_child_count(np);
  79. muxc = i2c_mux_alloc(parent, dev, children, 0, 0,
  80. i2c_mux_select, i2c_mux_deselect);
  81. if (!muxc) {
  82. ret = -ENOMEM;
  83. goto err_parent;
  84. }
  85. muxc->priv = mux;
  86. platform_set_drvdata(pdev, muxc);
  87. muxc->mux_locked = of_property_read_bool(np, "mux-locked");
  88. for_each_child_of_node(np, child) {
  89. u32 chan;
  90. ret = of_property_read_u32(child, "reg", &chan);
  91. if (ret < 0) {
  92. dev_err(dev, "no reg property for node '%pOFn'\n",
  93. child);
  94. goto err_children;
  95. }
  96. if (chan >= mux_control_states(mux->control)) {
  97. dev_err(dev, "invalid reg %u\n", chan);
  98. ret = -EINVAL;
  99. goto err_children;
  100. }
  101. ret = i2c_mux_add_adapter(muxc, 0, chan);
  102. if (ret)
  103. goto err_children;
  104. }
  105. dev_info(dev, "%d-port mux on %s adapter\n", children, parent->name);
  106. return 0;
  107. err_children:
  108. of_node_put(child);
  109. i2c_mux_del_adapters(muxc);
  110. err_parent:
  111. i2c_put_adapter(parent);
  112. return ret;
  113. }
  114. static void i2c_mux_remove(struct platform_device *pdev)
  115. {
  116. struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
  117. i2c_mux_del_adapters(muxc);
  118. i2c_put_adapter(muxc->parent);
  119. }
  120. static struct platform_driver i2c_mux_driver = {
  121. .probe = i2c_mux_probe,
  122. .remove_new = i2c_mux_remove,
  123. .driver = {
  124. .name = "i2c-mux-gpmux",
  125. .of_match_table = i2c_mux_of_match,
  126. },
  127. };
  128. module_platform_driver(i2c_mux_driver);
  129. MODULE_DESCRIPTION("General Purpose I2C multiplexer driver");
  130. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  131. MODULE_LICENSE("GPL v2");