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/gpio-i2cmux.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>
21 struct i2c_adapter *parent;
22 struct i2c_adapter **adap; /* child busses */
23 struct gpio_i2cmux_platform_data data;
26 static void gpiomux_set(const struct gpiomux *mux, unsigned val)
30 for (i = 0; i < mux->data.n_gpios; i++)
31 gpio_set_value(mux->data.gpios[i], val & (1 << i));
34 static int gpiomux_select(struct i2c_adapter *adap, void *data, u32 chan)
36 struct gpiomux *mux = data;
38 gpiomux_set(mux, mux->data.values[chan]);
43 static int gpiomux_deselect(struct i2c_adapter *adap, void *data, u32 chan)
45 struct gpiomux *mux = data;
47 gpiomux_set(mux, mux->data.idle);
52 static int __devinit gpiomux_probe(struct platform_device *pdev)
55 struct gpio_i2cmux_platform_data *pdata;
56 struct i2c_adapter *parent;
57 int (*deselect) (struct i2c_adapter *, void *, u32);
58 unsigned initial_state;
61 pdata = pdev->dev.platform_data;
63 dev_err(&pdev->dev, "Missing platform data\n");
67 parent = i2c_get_adapter(pdata->parent);
69 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
74 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
82 mux->adap = kzalloc(sizeof(struct i2c_adapter *) * pdata->n_values,
89 if (pdata->idle != GPIO_I2CMUX_NO_IDLE) {
90 initial_state = pdata->idle;
91 deselect = gpiomux_deselect;
93 initial_state = pdata->values[0];
97 for (i = 0; i < pdata->n_gpios; i++) {
98 ret = gpio_request(pdata->gpios[i], "gpio-i2cmux");
100 goto err_request_gpio;
101 gpio_direction_output(pdata->gpios[i],
102 initial_state & (1 << i));
105 for (i = 0; i < pdata->n_values; i++) {
106 u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0;
108 mux->adap[i] = i2c_add_mux_adapter(parent, mux, nr, i,
109 gpiomux_select, deselect);
112 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
113 goto add_adapter_failed;
117 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
118 pdata->n_values, parent->name);
120 platform_set_drvdata(pdev, mux);
126 i2c_del_mux_adapter(mux->adap[i - 1]);
130 gpio_free(pdata->gpios[i - 1]);
135 i2c_put_adapter(parent);
140 static int __devexit gpiomux_remove(struct platform_device *pdev)
142 struct gpiomux *mux = platform_get_drvdata(pdev);
145 for (i = 0; i < mux->data.n_values; i++)
146 i2c_del_mux_adapter(mux->adap[i]);
148 for (i = 0; i < mux->data.n_gpios; i++)
149 gpio_free(mux->data.gpios[i]);
151 platform_set_drvdata(pdev, NULL);
152 i2c_put_adapter(mux->parent);
159 static struct platform_driver gpiomux_driver = {
160 .probe = gpiomux_probe,
161 .remove = __devexit_p(gpiomux_remove),
163 .owner = THIS_MODULE,
164 .name = "gpio-i2cmux",
168 static int __init gpiomux_init(void)
170 return platform_driver_register(&gpiomux_driver);
173 static void __exit gpiomux_exit(void)
175 platform_driver_unregister(&gpiomux_driver);
178 module_init(gpiomux_init);
179 module_exit(gpiomux_exit);
181 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
182 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
183 MODULE_LICENSE("GPL");
184 MODULE_ALIAS("platform:gpio-i2cmux");