1 // SPDX-License-Identifier: GPL-2.0+
3 * I2C multiplexer using GPIO API
7 * Peng Fan <peng.fan@nxp.com>
11 #include <asm-generic/gpio.h>
14 #include <dm/device_compat.h>
15 #include <dm/devres.h>
16 #include <dm/pinctrl.h>
19 #include <linux/errno.h>
20 #include <linux/libfdt.h>
22 DECLARE_GLOBAL_DATA_PTR;
25 * struct i2c_mux_gpio_priv - private data for i2c mux gpio
27 * @values: the reg value of each child node
28 * @n_values: num of regs
29 * @gpios: the mux-gpios array
30 * @n_gpios: num of gpios in mux-gpios
31 * @idle: the value of idle-state
33 struct i2c_mux_gpio_priv {
36 struct gpio_desc *gpios;
42 static int i2c_mux_gpio_select(struct udevice *dev, struct udevice *bus,
45 struct i2c_mux_gpio_priv *priv = dev_get_priv(dev);
48 for (i = 0; i < priv->n_gpios; i++) {
49 ret = dm_gpio_set_value(&priv->gpios[i], (channel >> i) & 1);
57 static int i2c_mux_gpio_deselect(struct udevice *dev, struct udevice *bus,
60 struct i2c_mux_gpio_priv *priv = dev_get_priv(dev);
63 for (i = 0; i < priv->n_gpios; i++) {
64 ret = dm_gpio_set_value(&priv->gpios[i], (priv->idle >> i) & 1);
72 static int i2c_mux_gpio_probe(struct udevice *dev)
74 const void *fdt = gd->fdt_blob;
75 int node = dev_of_offset(dev);
76 struct i2c_mux_gpio_priv *mux = dev_get_priv(dev);
77 struct gpio_desc *gpios;
79 int i = 0, subnode, ret;
81 mux->n_values = fdtdec_get_child_count(fdt, node);
82 values = devm_kzalloc(dev, sizeof(*mux->values) * mux->n_values,
85 dev_err(dev, "Cannot alloc values array");
89 fdt_for_each_subnode(subnode, fdt, node) {
90 *(values + i) = fdtdec_get_uint(fdt, subnode, "reg", -1);
96 mux->idle = fdtdec_get_uint(fdt, node, "idle-state", -1);
98 mux->n_gpios = gpio_get_list_count(dev, "mux-gpios");
99 if (mux->n_gpios < 0) {
100 dev_err(dev, "Missing mux-gpios property\n");
104 gpios = devm_kzalloc(dev, sizeof(struct gpio_desc) * mux->n_gpios,
107 dev_err(dev, "Cannot allocate gpios array\n");
111 ret = gpio_request_list_by_name(dev, "mux-gpios", gpios, mux->n_gpios,
112 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
114 dev_err(dev, "Failed to request mux-gpios\n");
123 static const struct i2c_mux_ops i2c_mux_gpio_ops = {
124 .select = i2c_mux_gpio_select,
125 .deselect = i2c_mux_gpio_deselect,
128 static const struct udevice_id i2c_mux_gpio_ids[] = {
129 { .compatible = "i2c-mux-gpio", },
133 U_BOOT_DRIVER(i2c_mux_gpio) = {
134 .name = "i2c_mux_gpio",
135 .id = UCLASS_I2C_MUX,
136 .of_match = i2c_mux_gpio_ids,
137 .ops = &i2c_mux_gpio_ops,
138 .probe = i2c_mux_gpio_probe,
139 .priv_auto = sizeof(struct i2c_mux_gpio_priv),