2 * General Purpose I2C multiplexer
4 * Copyright (C) 2017 Axentia Technologies AB
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.
13 #include <linux/i2c.h>
14 #include <linux/i2c-mux.h>
15 #include <linux/module.h>
16 #include <linux/mux/consumer.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
21 struct mux_control *control;
26 static int i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
28 struct mux *mux = i2c_mux_priv(muxc);
31 ret = mux_control_select(mux->control, chan);
32 mux->do_not_deselect = ret < 0;
37 static int i2c_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
39 struct mux *mux = i2c_mux_priv(muxc);
41 if (mux->do_not_deselect)
44 return mux_control_deselect(mux->control);
47 static struct i2c_adapter *mux_parent_adapter(struct device *dev)
49 struct device_node *np = dev->of_node;
50 struct device_node *parent_np;
51 struct i2c_adapter *parent;
53 parent_np = of_parse_phandle(np, "i2c-parent", 0);
55 dev_err(dev, "Cannot parse i2c-parent\n");
56 return ERR_PTR(-ENODEV);
58 parent = of_find_i2c_adapter_by_node(parent_np);
59 of_node_put(parent_np);
61 return ERR_PTR(-EPROBE_DEFER);
66 static const struct of_device_id i2c_mux_of_match[] = {
67 { .compatible = "i2c-mux", },
70 MODULE_DEVICE_TABLE(of, i2c_mux_of_match);
72 static int i2c_mux_probe(struct platform_device *pdev)
74 struct device *dev = &pdev->dev;
75 struct device_node *np = dev->of_node;
76 struct device_node *child;
77 struct i2c_mux_core *muxc;
79 struct i2c_adapter *parent;
86 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
90 mux->control = devm_mux_control_get(dev, NULL);
91 if (IS_ERR(mux->control)) {
92 if (PTR_ERR(mux->control) != -EPROBE_DEFER)
93 dev_err(dev, "failed to get control-mux\n");
94 return PTR_ERR(mux->control);
97 parent = mux_parent_adapter(dev);
99 if (PTR_ERR(parent) != -EPROBE_DEFER)
100 dev_err(dev, "failed to get i2c-parent adapter\n");
101 return PTR_ERR(parent);
104 children = of_get_child_count(np);
106 muxc = i2c_mux_alloc(parent, dev, children, 0, 0,
107 i2c_mux_select, i2c_mux_deselect);
114 platform_set_drvdata(pdev, muxc);
116 muxc->mux_locked = of_property_read_bool(np, "mux-locked");
118 for_each_child_of_node(np, child) {
121 ret = of_property_read_u32(child, "reg", &chan);
123 dev_err(dev, "no reg property for node '%s'\n",
128 if (chan >= mux_control_states(mux->control)) {
129 dev_err(dev, "invalid reg %u\n", chan);
134 ret = i2c_mux_add_adapter(muxc, 0, chan, 0);
139 dev_info(dev, "%d-port mux on %s adapter\n", children, parent->name);
144 i2c_mux_del_adapters(muxc);
146 i2c_put_adapter(parent);
151 static int i2c_mux_remove(struct platform_device *pdev)
153 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
155 i2c_mux_del_adapters(muxc);
156 i2c_put_adapter(muxc->parent);
161 static struct platform_driver i2c_mux_driver = {
162 .probe = i2c_mux_probe,
163 .remove = i2c_mux_remove,
165 .name = "i2c-mux-gpmux",
166 .of_match_table = i2c_mux_of_match,
169 module_platform_driver(i2c_mux_driver);
171 MODULE_DESCRIPTION("General Purpose I2C multiplexer driver");
173 MODULE_LICENSE("GPL v2");