2 * I2C multiplexer using GPIO API
4 * Peter Korsgaard <peter.korsgaard@barco.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/i2c.h>
12 #include <linux/i2c-mux.h>
13 #include <linux/i2c-mux-gpio.h>
14 #include <linux/platform_device.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
22 struct i2c_adapter *parent;
23 struct i2c_adapter **adap; /* child busses */
24 struct i2c_mux_gpio_platform_data data;
28 static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
32 for (i = 0; i < mux->data.n_gpios; i++)
33 gpio_set_value(mux->gpio_base + mux->data.gpios[i],
37 static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
39 struct gpiomux *mux = data;
41 i2c_mux_gpio_set(mux, mux->data.values[chan]);
46 static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
48 struct gpiomux *mux = data;
50 i2c_mux_gpio_set(mux, mux->data.idle);
55 static int match_gpio_chip_by_label(struct gpio_chip *chip,
58 return !strcmp(chip->label, data);
62 static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
63 struct platform_device *pdev)
65 struct device_node *np = pdev->dev.of_node;
66 struct device_node *adapter_np, *child;
67 struct i2c_adapter *adapter;
68 unsigned *values, *gpios;
74 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
76 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
79 adapter = of_find_i2c_adapter_by_node(adapter_np);
81 dev_err(&pdev->dev, "Cannot find parent bus\n");
84 mux->data.parent = i2c_adapter_id(adapter);
85 put_device(&adapter->dev);
87 mux->data.n_values = of_get_child_count(np);
89 values = devm_kzalloc(&pdev->dev,
90 sizeof(*mux->data.values) * mux->data.n_values,
93 dev_err(&pdev->dev, "Cannot allocate values array");
97 for_each_child_of_node(np, child) {
98 of_property_read_u32(child, "reg", values + i);
101 mux->data.values = values;
103 if (of_property_read_u32(np, "idle-state", &mux->data.idle))
104 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
106 mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios");
107 if (mux->data.n_gpios < 0) {
108 dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n");
112 gpios = devm_kzalloc(&pdev->dev,
113 sizeof(*mux->data.gpios) * mux->data.n_gpios, GFP_KERNEL);
115 dev_err(&pdev->dev, "Cannot allocate gpios array");
119 for (i = 0; i < mux->data.n_gpios; i++) {
120 ret = of_get_named_gpio(np, "mux-gpios", i);
126 mux->data.gpios = gpios;
131 static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
132 struct platform_device *pdev)
138 static int i2c_mux_gpio_probe(struct platform_device *pdev)
141 struct i2c_adapter *parent;
142 int (*deselect) (struct i2c_adapter *, void *, u32);
143 unsigned initial_state, gpio_base;
146 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
148 dev_err(&pdev->dev, "Cannot allocate gpiomux structure");
152 platform_set_drvdata(pdev, mux);
154 if (!dev_get_platdata(&pdev->dev)) {
155 ret = i2c_mux_gpio_probe_dt(mux, pdev);
159 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
164 * If a GPIO chip name is provided, the GPIO pin numbers provided are
165 * relative to its base GPIO number. Otherwise they are absolute.
167 if (mux->data.gpio_chip) {
168 struct gpio_chip *gpio;
170 gpio = gpiochip_find(mux->data.gpio_chip,
171 match_gpio_chip_by_label);
173 return -EPROBE_DEFER;
175 gpio_base = gpio->base;
180 parent = i2c_get_adapter(mux->data.parent);
182 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
184 return -EPROBE_DEFER;
187 mux->parent = parent;
188 mux->gpio_base = gpio_base;
190 mux->adap = devm_kzalloc(&pdev->dev,
191 sizeof(*mux->adap) * mux->data.n_values,
194 dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure");
199 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
200 initial_state = mux->data.idle;
201 deselect = i2c_mux_gpio_deselect;
203 initial_state = mux->data.values[0];
207 for (i = 0; i < mux->data.n_gpios; i++) {
208 ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio");
210 dev_err(&pdev->dev, "Failed to request GPIO %d\n",
212 goto err_request_gpio;
215 ret = gpio_direction_output(gpio_base + mux->data.gpios[i],
216 initial_state & (1 << i));
219 "Failed to set direction of GPIO %d to output\n",
221 i++; /* gpio_request above succeeded, so must free */
222 goto err_request_gpio;
226 for (i = 0; i < mux->data.n_values; i++) {
227 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
228 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
230 mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
232 i2c_mux_gpio_select, deselect);
235 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
236 goto add_adapter_failed;
240 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
241 mux->data.n_values, parent->name);
247 i2c_del_mux_adapter(mux->adap[i - 1]);
248 i = mux->data.n_gpios;
251 gpio_free(gpio_base + mux->data.gpios[i - 1]);
253 i2c_put_adapter(parent);
258 static int i2c_mux_gpio_remove(struct platform_device *pdev)
260 struct gpiomux *mux = platform_get_drvdata(pdev);
263 for (i = 0; i < mux->data.n_values; i++)
264 i2c_del_mux_adapter(mux->adap[i]);
266 for (i = 0; i < mux->data.n_gpios; i++)
267 gpio_free(mux->gpio_base + mux->data.gpios[i]);
269 i2c_put_adapter(mux->parent);
274 static const struct of_device_id i2c_mux_gpio_of_match[] = {
275 { .compatible = "i2c-mux-gpio", },
278 MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
280 static struct platform_driver i2c_mux_gpio_driver = {
281 .probe = i2c_mux_gpio_probe,
282 .remove = i2c_mux_gpio_remove,
284 .owner = THIS_MODULE,
285 .name = "i2c-mux-gpio",
286 .of_match_table = i2c_mux_gpio_of_match,
290 module_platform_driver(i2c_mux_gpio_driver);
292 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
293 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
294 MODULE_LICENSE("GPL");
295 MODULE_ALIAS("platform:i2c-mux-gpio");