Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * I2C multiplexer using GPIO API
4 *
5 * Peter Korsgaard <peter.korsgaard@barco.com>
6 */
7
8#include <linux/i2c.h>
9#include <linux/i2c-mux.h>
10#include <linux/overflow.h>
11#include <linux/platform_data/i2c-mux-gpio.h>
12#include <linux/platform_device.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/bits.h>
16#include <linux/gpio/consumer.h>
17/* FIXME: stop poking around inside gpiolib */
18#include "../../gpio/gpiolib.h"
19
20struct gpiomux {
21 struct i2c_mux_gpio_platform_data data;
22 int ngpios;
23 struct gpio_desc **gpios;
24};
25
26static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
27{
28 DECLARE_BITMAP(values, BITS_PER_TYPE(val));
29
30 values[0] = val;
31
32 gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values);
33}
34
35static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
36{
37 struct gpiomux *mux = i2c_mux_priv(muxc);
38
39 i2c_mux_gpio_set(mux, chan);
40
41 return 0;
42}
43
44static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
45{
46 struct gpiomux *mux = i2c_mux_priv(muxc);
47
48 i2c_mux_gpio_set(mux, mux->data.idle);
49
50 return 0;
51}
52
53static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
54 struct platform_device *pdev)
55{
56 struct device *dev = &pdev->dev;
57 struct fwnode_handle *fwnode = dev_fwnode(dev);
58 struct device_node *np = dev->of_node;
59 struct device_node *adapter_np;
60 struct i2c_adapter *adapter = NULL;
61 struct fwnode_handle *child;
62 unsigned *values;
63 int rc, i = 0;
64
65 if (is_of_node(fwnode)) {
66 if (!np)
67 return -ENODEV;
68
69 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
70 if (!adapter_np) {
71 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
72 return -ENODEV;
73 }
74 adapter = of_find_i2c_adapter_by_node(adapter_np);
75 of_node_put(adapter_np);
76
77 } else if (is_acpi_node(fwnode)) {
78 /*
79 * In ACPI land the mux should be a direct child of the i2c
80 * bus it muxes.
81 */
82 acpi_handle dev_handle = ACPI_HANDLE(dev->parent);
83
84 adapter = i2c_acpi_find_adapter_by_handle(dev_handle);
85 }
86
87 if (!adapter)
88 return -EPROBE_DEFER;
89
90 mux->data.parent = i2c_adapter_id(adapter);
91 put_device(&adapter->dev);
92
93 mux->data.n_values = device_get_child_node_count(dev);
94 values = devm_kcalloc(dev,
95 mux->data.n_values, sizeof(*mux->data.values),
96 GFP_KERNEL);
97 if (!values) {
98 dev_err(dev, "Cannot allocate values array");
99 return -ENOMEM;
100 }
101
102 device_for_each_child_node(dev, child) {
103 if (is_of_node(child)) {
104 fwnode_property_read_u32(child, "reg", values + i);
105
106 } else if (is_acpi_node(child)) {
107 rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i);
108 if (rc)
109 return dev_err_probe(dev, rc, "Cannot get address\n");
110 }
111
112 i++;
113 }
114 mux->data.values = values;
115
116 if (device_property_read_u32(dev, "idle-state", &mux->data.idle))
117 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
118
119 return 0;
120}
121
122static int i2c_mux_gpio_probe(struct platform_device *pdev)
123{
124 struct i2c_mux_core *muxc;
125 struct gpiomux *mux;
126 struct i2c_adapter *parent;
127 struct i2c_adapter *root;
128 unsigned initial_state;
129 int i, ngpios, ret;
130
131 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
132 if (!mux)
133 return -ENOMEM;
134
135 if (!dev_get_platdata(&pdev->dev)) {
136 ret = i2c_mux_gpio_probe_fw(mux, pdev);
137 if (ret < 0)
138 return ret;
139 } else {
140 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
141 sizeof(mux->data));
142 }
143
144 ngpios = gpiod_count(&pdev->dev, "mux");
145 if (ngpios <= 0) {
146 dev_err(&pdev->dev, "no valid gpios provided\n");
147 return ngpios ?: -EINVAL;
148 }
149 mux->ngpios = ngpios;
150
151 parent = i2c_get_adapter(mux->data.parent);
152 if (!parent)
153 return -EPROBE_DEFER;
154
155 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
156 array_size(ngpios, sizeof(*mux->gpios)), 0,
157 i2c_mux_gpio_select, NULL);
158 if (!muxc) {
159 ret = -ENOMEM;
160 goto alloc_failed;
161 }
162 mux->gpios = muxc->priv;
163 muxc->priv = mux;
164
165 platform_set_drvdata(pdev, muxc);
166
167 root = i2c_root_adapter(&parent->dev);
168
169 muxc->mux_locked = true;
170
171 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
172 initial_state = mux->data.idle;
173 muxc->deselect = i2c_mux_gpio_deselect;
174 } else {
175 initial_state = mux->data.values[0];
176 }
177
178 for (i = 0; i < ngpios; i++) {
179 struct device *gpio_dev;
180 struct gpio_desc *gpiod;
181 enum gpiod_flags flag;
182
183 if (initial_state & BIT(i))
184 flag = GPIOD_OUT_HIGH;
185 else
186 flag = GPIOD_OUT_LOW;
187 gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
188 if (IS_ERR(gpiod)) {
189 ret = PTR_ERR(gpiod);
190 goto alloc_failed;
191 }
192
193 mux->gpios[i] = gpiod;
194
195 if (!muxc->mux_locked)
196 continue;
197
198 /* FIXME: find a proper way to access the GPIO device */
199 gpio_dev = &gpiod->gdev->dev;
200 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
201 }
202
203 if (muxc->mux_locked)
204 dev_info(&pdev->dev, "mux-locked i2c mux\n");
205
206 for (i = 0; i < mux->data.n_values; i++) {
207 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
208 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
209
210 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
211 if (ret)
212 goto add_adapter_failed;
213 }
214
215 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
216 mux->data.n_values, parent->name);
217
218 return 0;
219
220add_adapter_failed:
221 i2c_mux_del_adapters(muxc);
222alloc_failed:
223 i2c_put_adapter(parent);
224
225 return ret;
226}
227
228static int i2c_mux_gpio_remove(struct platform_device *pdev)
229{
230 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
231
232 i2c_mux_del_adapters(muxc);
233 i2c_put_adapter(muxc->parent);
234
235 return 0;
236}
237
238static const struct of_device_id i2c_mux_gpio_of_match[] = {
239 { .compatible = "i2c-mux-gpio", },
240 {},
241};
242MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
243
244static struct platform_driver i2c_mux_gpio_driver = {
245 .probe = i2c_mux_gpio_probe,
246 .remove = i2c_mux_gpio_remove,
247 .driver = {
248 .name = "i2c-mux-gpio",
249 .of_match_table = i2c_mux_gpio_of_match,
250 },
251};
252
253module_platform_driver(i2c_mux_gpio_driver);
254
255MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
256MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
257MODULE_LICENSE("GPL");
258MODULE_ALIAS("platform:i2c-mux-gpio");
1/*
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>
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
20struct gpiomux {
21 struct i2c_adapter *parent;
22 struct i2c_adapter **adap; /* child busses */
23 struct i2c_mux_gpio_platform_data data;
24};
25
26static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
27{
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
34static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
35{
36 struct gpiomux *mux = data;
37
38 i2c_mux_gpio_set(mux, mux->data.values[chan]);
39
40 return 0;
41}
42
43static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
44{
45 struct gpiomux *mux = data;
46
47 i2c_mux_gpio_set(mux, mux->data.idle);
48
49 return 0;
50}
51
52static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
53{
54 struct gpiomux *mux;
55 struct i2c_mux_gpio_platform_data *pdata;
56 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
74 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
75 if (!mux) {
76 ret = -ENOMEM;
77 goto alloc_failed;
78 }
79
80 mux->parent = parent;
81 mux->data = *pdata;
82 mux->adap = kzalloc(sizeof(struct i2c_adapter *) * pdata->n_values,
83 GFP_KERNEL);
84 if (!mux->adap) {
85 ret = -ENOMEM;
86 goto alloc_failed2;
87 }
88
89 if (pdata->idle != I2C_MUX_GPIO_NO_IDLE) {
90 initial_state = pdata->idle;
91 deselect = i2c_mux_gpio_deselect;
92 } else {
93 initial_state = pdata->values[0];
94 deselect = NULL;
95 }
96
97 for (i = 0; i < pdata->n_gpios; i++) {
98 ret = gpio_request(pdata->gpios[i], "i2c-mux-gpio");
99 if (ret)
100 goto err_request_gpio;
101 gpio_direction_output(pdata->gpios[i],
102 initial_state & (1 << i));
103 }
104
105 for (i = 0; i < pdata->n_values; i++) {
106 u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0;
107
108 mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr, i,
109 i2c_mux_gpio_select, deselect);
110 if (!mux->adap[i]) {
111 ret = -ENODEV;
112 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
113 goto add_adapter_failed;
114 }
115 }
116
117 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
118 pdata->n_values, parent->name);
119
120 platform_set_drvdata(pdev, mux);
121
122 return 0;
123
124add_adapter_failed:
125 for (; i > 0; i--)
126 i2c_del_mux_adapter(mux->adap[i - 1]);
127 i = pdata->n_gpios;
128err_request_gpio:
129 for (; i > 0; i--)
130 gpio_free(pdata->gpios[i - 1]);
131 kfree(mux->adap);
132alloc_failed2:
133 kfree(mux);
134alloc_failed:
135 i2c_put_adapter(parent);
136
137 return ret;
138}
139
140static int __devexit i2c_mux_gpio_remove(struct platform_device *pdev)
141{
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);
153 kfree(mux->adap);
154 kfree(mux);
155
156 return 0;
157}
158
159static struct platform_driver i2c_mux_gpio_driver = {
160 .probe = i2c_mux_gpio_probe,
161 .remove = __devexit_p(i2c_mux_gpio_remove),
162 .driver = {
163 .owner = THIS_MODULE,
164 .name = "i2c-mux-gpio",
165 },
166};
167
168module_platform_driver(i2c_mux_gpio_driver);
169
170MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
171MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
172MODULE_LICENSE("GPL");
173MODULE_ALIAS("platform:i2c-mux-gpio");