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_i2c.h>
20 #include <linux/of_gpio.h>
23 struct i2c_adapter
*parent
;
24 struct i2c_adapter
**adap
; /* child busses */
25 struct i2c_mux_gpio_platform_data data
;
29 static void i2c_mux_gpio_set(const struct gpiomux
*mux
, unsigned val
)
33 for (i
= 0; i
< mux
->data
.n_gpios
; i
++)
34 gpio_set_value(mux
->gpio_base
+ mux
->data
.gpios
[i
],
38 static int i2c_mux_gpio_select(struct i2c_adapter
*adap
, void *data
, u32 chan
)
40 struct gpiomux
*mux
= data
;
42 i2c_mux_gpio_set(mux
, mux
->data
.values
[chan
]);
47 static int i2c_mux_gpio_deselect(struct i2c_adapter
*adap
, void *data
, u32 chan
)
49 struct gpiomux
*mux
= data
;
51 i2c_mux_gpio_set(mux
, mux
->data
.idle
);
56 static int match_gpio_chip_by_label(struct gpio_chip
*chip
,
59 return !strcmp(chip
->label
, data
);
63 static int i2c_mux_gpio_probe_dt(struct gpiomux
*mux
,
64 struct platform_device
*pdev
)
66 struct device_node
*np
= pdev
->dev
.of_node
;
67 struct device_node
*adapter_np
, *child
;
68 struct i2c_adapter
*adapter
;
69 unsigned *values
, *gpios
;
75 adapter_np
= of_parse_phandle(np
, "i2c-parent", 0);
77 dev_err(&pdev
->dev
, "Cannot parse i2c-parent\n");
80 adapter
= of_find_i2c_adapter_by_node(adapter_np
);
82 dev_err(&pdev
->dev
, "Cannot find parent bus\n");
85 mux
->data
.parent
= i2c_adapter_id(adapter
);
86 put_device(&adapter
->dev
);
88 mux
->data
.n_values
= of_get_child_count(np
);
90 values
= devm_kzalloc(&pdev
->dev
,
91 sizeof(*mux
->data
.values
) * mux
->data
.n_values
,
94 dev_err(&pdev
->dev
, "Cannot allocate values array");
98 for_each_child_of_node(np
, child
) {
99 of_property_read_u32(child
, "reg", values
+ i
);
102 mux
->data
.values
= values
;
104 if (of_property_read_u32(np
, "idle-state", &mux
->data
.idle
))
105 mux
->data
.idle
= I2C_MUX_GPIO_NO_IDLE
;
107 mux
->data
.n_gpios
= of_gpio_named_count(np
, "mux-gpios");
108 if (mux
->data
.n_gpios
< 0) {
109 dev_err(&pdev
->dev
, "Missing mux-gpios property in the DT.\n");
113 gpios
= devm_kzalloc(&pdev
->dev
,
114 sizeof(*mux
->data
.gpios
) * mux
->data
.n_gpios
, GFP_KERNEL
);
116 dev_err(&pdev
->dev
, "Cannot allocate gpios array");
120 for (i
= 0; i
< mux
->data
.n_gpios
; i
++)
121 gpios
[i
] = of_get_named_gpio(np
, "mux-gpios", i
);
123 mux
->data
.gpios
= gpios
;
128 static int i2c_mux_gpio_probe_dt(struct gpiomux
*mux
,
129 struct platform_device
*pdev
)
135 static int i2c_mux_gpio_probe(struct platform_device
*pdev
)
138 struct i2c_adapter
*parent
;
139 int (*deselect
) (struct i2c_adapter
*, void *, u32
);
140 unsigned initial_state
, gpio_base
;
143 mux
= devm_kzalloc(&pdev
->dev
, sizeof(*mux
), GFP_KERNEL
);
145 dev_err(&pdev
->dev
, "Cannot allocate gpiomux structure");
149 platform_set_drvdata(pdev
, mux
);
151 if (!pdev
->dev
.platform_data
) {
152 ret
= i2c_mux_gpio_probe_dt(mux
, pdev
);
156 memcpy(&mux
->data
, pdev
->dev
.platform_data
, sizeof(mux
->data
));
159 * If a GPIO chip name is provided, the GPIO pin numbers provided are
160 * relative to its base GPIO number. Otherwise they are absolute.
162 if (mux
->data
.gpio_chip
) {
163 struct gpio_chip
*gpio
;
165 gpio
= gpiochip_find(mux
->data
.gpio_chip
,
166 match_gpio_chip_by_label
);
168 return -EPROBE_DEFER
;
170 gpio_base
= gpio
->base
;
175 parent
= i2c_get_adapter(mux
->data
.parent
);
177 dev_err(&pdev
->dev
, "Parent adapter (%d) not found\n",
182 mux
->parent
= parent
;
183 mux
->gpio_base
= gpio_base
;
185 mux
->adap
= devm_kzalloc(&pdev
->dev
,
186 sizeof(*mux
->adap
) * mux
->data
.n_values
,
189 dev_err(&pdev
->dev
, "Cannot allocate i2c_adapter structure");
194 if (mux
->data
.idle
!= I2C_MUX_GPIO_NO_IDLE
) {
195 initial_state
= mux
->data
.idle
;
196 deselect
= i2c_mux_gpio_deselect
;
198 initial_state
= mux
->data
.values
[0];
202 for (i
= 0; i
< mux
->data
.n_gpios
; i
++) {
203 ret
= gpio_request(gpio_base
+ mux
->data
.gpios
[i
], "i2c-mux-gpio");
205 dev_err(&pdev
->dev
, "Failed to request GPIO %d\n",
207 goto err_request_gpio
;
210 ret
= gpio_direction_output(gpio_base
+ mux
->data
.gpios
[i
],
211 initial_state
& (1 << i
));
214 "Failed to set direction of GPIO %d to output\n",
216 i
++; /* gpio_request above succeeded, so must free */
217 goto err_request_gpio
;
221 for (i
= 0; i
< mux
->data
.n_values
; i
++) {
222 u32 nr
= mux
->data
.base_nr
? (mux
->data
.base_nr
+ i
) : 0;
223 unsigned int class = mux
->data
.classes
? mux
->data
.classes
[i
] : 0;
225 mux
->adap
[i
] = i2c_add_mux_adapter(parent
, &pdev
->dev
, mux
, nr
,
227 i2c_mux_gpio_select
, deselect
);
230 dev_err(&pdev
->dev
, "Failed to add adapter %d\n", i
);
231 goto add_adapter_failed
;
235 dev_info(&pdev
->dev
, "%d port mux on %s adapter\n",
236 mux
->data
.n_values
, parent
->name
);
242 i2c_del_mux_adapter(mux
->adap
[i
- 1]);
243 i
= mux
->data
.n_gpios
;
246 gpio_free(gpio_base
+ mux
->data
.gpios
[i
- 1]);
248 i2c_put_adapter(parent
);
253 static int i2c_mux_gpio_remove(struct platform_device
*pdev
)
255 struct gpiomux
*mux
= platform_get_drvdata(pdev
);
258 for (i
= 0; i
< mux
->data
.n_values
; i
++)
259 i2c_del_mux_adapter(mux
->adap
[i
]);
261 for (i
= 0; i
< mux
->data
.n_gpios
; i
++)
262 gpio_free(mux
->gpio_base
+ mux
->data
.gpios
[i
]);
264 i2c_put_adapter(mux
->parent
);
269 static const struct of_device_id i2c_mux_gpio_of_match
[] = {
270 { .compatible
= "i2c-mux-gpio", },
273 MODULE_DEVICE_TABLE(of
, i2c_mux_gpio_of_match
);
275 static struct platform_driver i2c_mux_gpio_driver
= {
276 .probe
= i2c_mux_gpio_probe
,
277 .remove
= i2c_mux_gpio_remove
,
279 .owner
= THIS_MODULE
,
280 .name
= "i2c-mux-gpio",
281 .of_match_table
= of_match_ptr(i2c_mux_gpio_of_match
),
285 module_platform_driver(i2c_mux_gpio_driver
);
287 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
288 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
289 MODULE_LICENSE("GPL");
290 MODULE_ALIAS("platform:i2c-mux-gpio");