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/platform_data/i2c-mux-gpio.h>
14 #include <linux/platform_device.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/gpio.h>
18 #include "../../gpio/gpiolib.h"
19 #include <linux/of_gpio.h>
22 struct i2c_mux_gpio_platform_data data
;
24 struct gpio_desc
**gpios
;
28 static void i2c_mux_gpio_set(const struct gpiomux
*mux
, unsigned val
)
32 for (i
= 0; i
< mux
->data
.n_gpios
; i
++)
33 mux
->values
[i
] = (val
>> i
) & 1;
35 gpiod_set_array_value_cansleep(mux
->data
.n_gpios
,
36 mux
->gpios
, mux
->values
);
39 static int i2c_mux_gpio_select(struct i2c_mux_core
*muxc
, u32 chan
)
41 struct gpiomux
*mux
= i2c_mux_priv(muxc
);
43 i2c_mux_gpio_set(mux
, chan
);
48 static int i2c_mux_gpio_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
50 struct gpiomux
*mux
= i2c_mux_priv(muxc
);
52 i2c_mux_gpio_set(mux
, mux
->data
.idle
);
57 static int match_gpio_chip_by_label(struct gpio_chip
*chip
,
60 return !strcmp(chip
->label
, data
);
64 static int i2c_mux_gpio_probe_dt(struct gpiomux
*mux
,
65 struct platform_device
*pdev
)
67 struct device_node
*np
= pdev
->dev
.of_node
;
68 struct device_node
*adapter_np
, *child
;
69 struct i2c_adapter
*adapter
;
70 unsigned *values
, *gpios
;
76 adapter_np
= of_parse_phandle(np
, "i2c-parent", 0);
78 dev_err(&pdev
->dev
, "Cannot parse i2c-parent\n");
81 adapter
= of_find_i2c_adapter_by_node(adapter_np
);
82 of_node_put(adapter_np
);
86 mux
->data
.parent
= i2c_adapter_id(adapter
);
87 put_device(&adapter
->dev
);
89 mux
->data
.n_values
= of_get_child_count(np
);
91 values
= devm_kcalloc(&pdev
->dev
,
92 mux
->data
.n_values
, sizeof(*mux
->data
.values
),
95 dev_err(&pdev
->dev
, "Cannot allocate values array");
99 for_each_child_of_node(np
, child
) {
100 of_property_read_u32(child
, "reg", values
+ i
);
103 mux
->data
.values
= values
;
105 if (of_property_read_u32(np
, "idle-state", &mux
->data
.idle
))
106 mux
->data
.idle
= I2C_MUX_GPIO_NO_IDLE
;
108 mux
->data
.n_gpios
= of_gpio_named_count(np
, "mux-gpios");
109 if (mux
->data
.n_gpios
< 0) {
110 dev_err(&pdev
->dev
, "Missing mux-gpios property in the DT.\n");
114 gpios
= devm_kcalloc(&pdev
->dev
,
115 mux
->data
.n_gpios
, sizeof(*mux
->data
.gpios
),
118 dev_err(&pdev
->dev
, "Cannot allocate gpios array");
122 for (i
= 0; i
< mux
->data
.n_gpios
; i
++) {
123 ret
= of_get_named_gpio(np
, "mux-gpios", i
);
129 mux
->data
.gpios
= gpios
;
134 static int i2c_mux_gpio_probe_dt(struct gpiomux
*mux
,
135 struct platform_device
*pdev
)
141 static int i2c_mux_gpio_probe(struct platform_device
*pdev
)
143 struct i2c_mux_core
*muxc
;
145 struct i2c_adapter
*parent
;
146 struct i2c_adapter
*root
;
147 unsigned initial_state
, gpio_base
;
150 mux
= devm_kzalloc(&pdev
->dev
, sizeof(*mux
), GFP_KERNEL
);
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 return -EPROBE_DEFER
;
184 muxc
= i2c_mux_alloc(parent
, &pdev
->dev
, mux
->data
.n_values
,
185 mux
->data
.n_gpios
* sizeof(*mux
->gpios
) +
186 mux
->data
.n_gpios
* sizeof(*mux
->values
), 0,
187 i2c_mux_gpio_select
, NULL
);
192 mux
->gpios
= muxc
->priv
;
193 mux
->values
= (int *)(mux
->gpios
+ mux
->data
.n_gpios
);
196 platform_set_drvdata(pdev
, muxc
);
198 root
= i2c_root_adapter(&parent
->dev
);
200 muxc
->mux_locked
= true;
201 mux
->gpio_base
= gpio_base
;
203 if (mux
->data
.idle
!= I2C_MUX_GPIO_NO_IDLE
) {
204 initial_state
= mux
->data
.idle
;
205 muxc
->deselect
= i2c_mux_gpio_deselect
;
207 initial_state
= mux
->data
.values
[0];
210 for (i
= 0; i
< mux
->data
.n_gpios
; i
++) {
211 struct device
*gpio_dev
;
212 struct gpio_desc
*gpio_desc
;
214 ret
= gpio_request(gpio_base
+ mux
->data
.gpios
[i
], "i2c-mux-gpio");
216 dev_err(&pdev
->dev
, "Failed to request GPIO %d\n",
218 goto err_request_gpio
;
221 ret
= gpio_direction_output(gpio_base
+ mux
->data
.gpios
[i
],
222 initial_state
& (1 << i
));
225 "Failed to set direction of GPIO %d to output\n",
227 i
++; /* gpio_request above succeeded, so must free */
228 goto err_request_gpio
;
231 gpio_desc
= gpio_to_desc(gpio_base
+ mux
->data
.gpios
[i
]);
232 mux
->gpios
[i
] = gpio_desc
;
234 if (!muxc
->mux_locked
)
237 gpio_dev
= &gpio_desc
->gdev
->dev
;
238 muxc
->mux_locked
= i2c_root_adapter(gpio_dev
) == root
;
241 if (muxc
->mux_locked
)
242 dev_info(&pdev
->dev
, "mux-locked i2c mux\n");
244 for (i
= 0; i
< mux
->data
.n_values
; i
++) {
245 u32 nr
= mux
->data
.base_nr
? (mux
->data
.base_nr
+ i
) : 0;
246 unsigned int class = mux
->data
.classes
? mux
->data
.classes
[i
] : 0;
248 ret
= i2c_mux_add_adapter(muxc
, nr
, mux
->data
.values
[i
], class);
250 goto add_adapter_failed
;
253 dev_info(&pdev
->dev
, "%d port mux on %s adapter\n",
254 mux
->data
.n_values
, parent
->name
);
259 i2c_mux_del_adapters(muxc
);
260 i
= mux
->data
.n_gpios
;
263 gpio_free(gpio_base
+ mux
->data
.gpios
[i
- 1]);
265 i2c_put_adapter(parent
);
270 static int i2c_mux_gpio_remove(struct platform_device
*pdev
)
272 struct i2c_mux_core
*muxc
= platform_get_drvdata(pdev
);
273 struct gpiomux
*mux
= i2c_mux_priv(muxc
);
276 i2c_mux_del_adapters(muxc
);
278 for (i
= 0; i
< mux
->data
.n_gpios
; i
++)
279 gpio_free(mux
->gpio_base
+ mux
->data
.gpios
[i
]);
281 i2c_put_adapter(muxc
->parent
);
286 static const struct of_device_id i2c_mux_gpio_of_match
[] = {
287 { .compatible
= "i2c-mux-gpio", },
290 MODULE_DEVICE_TABLE(of
, i2c_mux_gpio_of_match
);
292 static struct platform_driver i2c_mux_gpio_driver
= {
293 .probe
= i2c_mux_gpio_probe
,
294 .remove
= i2c_mux_gpio_remove
,
296 .name
= "i2c-mux-gpio",
297 .of_match_table
= i2c_mux_gpio_of_match
,
301 module_platform_driver(i2c_mux_gpio_driver
);
303 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
304 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
305 MODULE_LICENSE("GPL");
306 MODULE_ALIAS("platform:i2c-mux-gpio");