i2c-mux-gpmux.c 3.7 KB

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