blob: 7c23bb51bd33c692328da117b640a4a4c3365361 [file] [log] [blame]
Peter Korsgaard92ed1a72011-01-10 22:11:23 +01001/*
2 * I2C multiplexer using GPIO API
3 *
4 * Peter Korsgaard <peter.korsgaard@barco.com>
5 *
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.
9 */
10
11#include <linux/i2c.h>
12#include <linux/i2c-mux.h>
Jean Delvaree7065e22012-04-28 15:32:06 +020013#include <linux/i2c-mux-gpio.h>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010014#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
20struct gpiomux {
21 struct i2c_adapter *parent;
22 struct i2c_adapter **adap; /* child busses */
Jean Delvaree7065e22012-04-28 15:32:06 +020023 struct i2c_mux_gpio_platform_data data;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010024};
25
Jean Delvaree7065e22012-04-28 15:32:06 +020026static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010027{
28 int i;
29
30 for (i = 0; i < mux->data.n_gpios; i++)
31 gpio_set_value(mux->data.gpios[i], val & (1 << i));
32}
33
Jean Delvaree7065e22012-04-28 15:32:06 +020034static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010035{
36 struct gpiomux *mux = data;
37
Jean Delvaree7065e22012-04-28 15:32:06 +020038 i2c_mux_gpio_set(mux, mux->data.values[chan]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010039
40 return 0;
41}
42
Jean Delvaree7065e22012-04-28 15:32:06 +020043static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010044{
45 struct gpiomux *mux = data;
46
Jean Delvaree7065e22012-04-28 15:32:06 +020047 i2c_mux_gpio_set(mux, mux->data.idle);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010048
49 return 0;
50}
51
Jean Delvaree7065e22012-04-28 15:32:06 +020052static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010053{
54 struct gpiomux *mux;
Jean Delvaree7065e22012-04-28 15:32:06 +020055 struct i2c_mux_gpio_platform_data *pdata;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010056 struct i2c_adapter *parent;
57 int (*deselect) (struct i2c_adapter *, void *, u32);
58 unsigned initial_state;
59 int i, ret;
60
61 pdata = pdev->dev.platform_data;
62 if (!pdata) {
63 dev_err(&pdev->dev, "Missing platform data\n");
64 return -ENODEV;
65 }
66
67 parent = i2c_get_adapter(pdata->parent);
68 if (!parent) {
69 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
70 pdata->parent);
71 return -ENODEV;
72 }
73
Maxime Ripard2614a852012-10-05 22:23:53 +020074 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010075 if (!mux) {
76 ret = -ENOMEM;
77 goto alloc_failed;
78 }
79
80 mux->parent = parent;
81 mux->data = *pdata;
Maxime Ripard2614a852012-10-05 22:23:53 +020082 mux->adap = devm_kzalloc(&pdev->dev,
83 sizeof(*mux->adap) * pdata->n_values,
84 GFP_KERNEL);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010085 if (!mux->adap) {
86 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +020087 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010088 }
89
Jean Delvaree7065e22012-04-28 15:32:06 +020090 if (pdata->idle != I2C_MUX_GPIO_NO_IDLE) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010091 initial_state = pdata->idle;
Jean Delvaree7065e22012-04-28 15:32:06 +020092 deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010093 } else {
94 initial_state = pdata->values[0];
95 deselect = NULL;
96 }
97
98 for (i = 0; i < pdata->n_gpios; i++) {
Jean Delvaree7065e22012-04-28 15:32:06 +020099 ret = gpio_request(pdata->gpios[i], "i2c-mux-gpio");
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100100 if (ret)
101 goto err_request_gpio;
102 gpio_direction_output(pdata->gpios[i],
103 initial_state & (1 << i));
104 }
105
106 for (i = 0; i < pdata->n_values; i++) {
107 u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0;
Jean Delvareeee543e2012-10-05 22:23:51 +0200108 unsigned int class = pdata->classes ? pdata->classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100109
Jean Delvareeee543e2012-10-05 22:23:51 +0200110 mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
111 i, class,
Jean Delvaree7065e22012-04-28 15:32:06 +0200112 i2c_mux_gpio_select, deselect);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100113 if (!mux->adap[i]) {
114 ret = -ENODEV;
115 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
116 goto add_adapter_failed;
117 }
118 }
119
120 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
121 pdata->n_values, parent->name);
122
123 platform_set_drvdata(pdev, mux);
124
125 return 0;
126
127add_adapter_failed:
128 for (; i > 0; i--)
129 i2c_del_mux_adapter(mux->adap[i - 1]);
130 i = pdata->n_gpios;
131err_request_gpio:
132 for (; i > 0; i--)
133 gpio_free(pdata->gpios[i - 1]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100134alloc_failed:
135 i2c_put_adapter(parent);
136
137 return ret;
138}
139
Jean Delvaree7065e22012-04-28 15:32:06 +0200140static int __devexit i2c_mux_gpio_remove(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100141{
142 struct gpiomux *mux = platform_get_drvdata(pdev);
143 int i;
144
145 for (i = 0; i < mux->data.n_values; i++)
146 i2c_del_mux_adapter(mux->adap[i]);
147
148 for (i = 0; i < mux->data.n_gpios; i++)
149 gpio_free(mux->data.gpios[i]);
150
151 platform_set_drvdata(pdev, NULL);
152 i2c_put_adapter(mux->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100153
154 return 0;
155}
156
Jean Delvaree7065e22012-04-28 15:32:06 +0200157static struct platform_driver i2c_mux_gpio_driver = {
158 .probe = i2c_mux_gpio_probe,
159 .remove = __devexit_p(i2c_mux_gpio_remove),
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100160 .driver = {
161 .owner = THIS_MODULE,
Jean Delvaree7065e22012-04-28 15:32:06 +0200162 .name = "i2c-mux-gpio",
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100163 },
164};
165
Jean Delvaree7065e22012-04-28 15:32:06 +0200166module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100167
168MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
169MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
170MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200171MODULE_ALIAS("platform:i2c-mux-gpio");