0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/i2c.h>
0011 #include <linux/i2c-mux.h>
0012 #include <linux/module.h>
0013 #include <linux/mux/consumer.h>
0014 #include <linux/of_device.h>
0015 #include <linux/platform_device.h>
0016
0017 struct mux {
0018 struct mux_control *control;
0019
0020 bool do_not_deselect;
0021 };
0022
0023 static int i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
0024 {
0025 struct mux *mux = i2c_mux_priv(muxc);
0026 int ret;
0027
0028 ret = mux_control_select(mux->control, chan);
0029 mux->do_not_deselect = ret < 0;
0030
0031 return ret;
0032 }
0033
0034 static int i2c_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
0035 {
0036 struct mux *mux = i2c_mux_priv(muxc);
0037
0038 if (mux->do_not_deselect)
0039 return 0;
0040
0041 return mux_control_deselect(mux->control);
0042 }
0043
0044 static struct i2c_adapter *mux_parent_adapter(struct device *dev)
0045 {
0046 struct device_node *np = dev->of_node;
0047 struct device_node *parent_np;
0048 struct i2c_adapter *parent;
0049
0050 parent_np = of_parse_phandle(np, "i2c-parent", 0);
0051 if (!parent_np) {
0052 dev_err(dev, "Cannot parse i2c-parent\n");
0053 return ERR_PTR(-ENODEV);
0054 }
0055 parent = of_find_i2c_adapter_by_node(parent_np);
0056 of_node_put(parent_np);
0057 if (!parent)
0058 return ERR_PTR(-EPROBE_DEFER);
0059
0060 return parent;
0061 }
0062
0063 static const struct of_device_id i2c_mux_of_match[] = {
0064 { .compatible = "i2c-mux", },
0065 {},
0066 };
0067 MODULE_DEVICE_TABLE(of, i2c_mux_of_match);
0068
0069 static int i2c_mux_probe(struct platform_device *pdev)
0070 {
0071 struct device *dev = &pdev->dev;
0072 struct device_node *np = dev->of_node;
0073 struct device_node *child;
0074 struct i2c_mux_core *muxc;
0075 struct mux *mux;
0076 struct i2c_adapter *parent;
0077 int children;
0078 int ret;
0079
0080 if (!np)
0081 return -ENODEV;
0082
0083 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
0084 if (!mux)
0085 return -ENOMEM;
0086
0087 mux->control = devm_mux_control_get(dev, NULL);
0088 if (IS_ERR(mux->control))
0089 return dev_err_probe(dev, PTR_ERR(mux->control),
0090 "failed to get control-mux\n");
0091
0092 parent = mux_parent_adapter(dev);
0093 if (IS_ERR(parent))
0094 return dev_err_probe(dev, PTR_ERR(parent),
0095 "failed to get i2c-parent adapter\n");
0096
0097 children = of_get_child_count(np);
0098
0099 muxc = i2c_mux_alloc(parent, dev, children, 0, 0,
0100 i2c_mux_select, i2c_mux_deselect);
0101 if (!muxc) {
0102 ret = -ENOMEM;
0103 goto err_parent;
0104 }
0105 muxc->priv = mux;
0106
0107 platform_set_drvdata(pdev, muxc);
0108
0109 muxc->mux_locked = of_property_read_bool(np, "mux-locked");
0110
0111 for_each_child_of_node(np, child) {
0112 u32 chan;
0113
0114 ret = of_property_read_u32(child, "reg", &chan);
0115 if (ret < 0) {
0116 dev_err(dev, "no reg property for node '%pOFn'\n",
0117 child);
0118 goto err_children;
0119 }
0120
0121 if (chan >= mux_control_states(mux->control)) {
0122 dev_err(dev, "invalid reg %u\n", chan);
0123 ret = -EINVAL;
0124 goto err_children;
0125 }
0126
0127 ret = i2c_mux_add_adapter(muxc, 0, chan, 0);
0128 if (ret)
0129 goto err_children;
0130 }
0131
0132 dev_info(dev, "%d-port mux on %s adapter\n", children, parent->name);
0133
0134 return 0;
0135
0136 err_children:
0137 of_node_put(child);
0138 i2c_mux_del_adapters(muxc);
0139 err_parent:
0140 i2c_put_adapter(parent);
0141
0142 return ret;
0143 }
0144
0145 static int i2c_mux_remove(struct platform_device *pdev)
0146 {
0147 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
0148
0149 i2c_mux_del_adapters(muxc);
0150 i2c_put_adapter(muxc->parent);
0151
0152 return 0;
0153 }
0154
0155 static struct platform_driver i2c_mux_driver = {
0156 .probe = i2c_mux_probe,
0157 .remove = i2c_mux_remove,
0158 .driver = {
0159 .name = "i2c-mux-gpmux",
0160 .of_match_table = i2c_mux_of_match,
0161 },
0162 };
0163 module_platform_driver(i2c_mux_driver);
0164
0165 MODULE_DESCRIPTION("General Purpose I2C multiplexer driver");
0166 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
0167 MODULE_LICENSE("GPL v2");