2 * I2C multiplexer using GPIO API
6 * Peng Fan <peng.fan@nxp.com>
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm-generic/gpio.h>
15 #include <dm/pinctrl.h>
18 #include <linux/errno.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 * struct i2c_mux_gpio_priv - private data for i2c mux gpio
25 * @values: the reg value of each child node
26 * @n_values: num of regs
27 * @gpios: the mux-gpios array
28 * @n_gpios: num of gpios in mux-gpios
29 * @idle: the value of idle-state
31 struct i2c_mux_gpio_priv {
34 struct gpio_desc *gpios;
40 static int i2c_mux_gpio_select(struct udevice *dev, struct udevice *bus,
43 struct i2c_mux_gpio_priv *priv = dev_get_priv(dev);
46 for (i = 0; i < priv->n_gpios; i++) {
47 ret = dm_gpio_set_value(&priv->gpios[i], (channel >> i) & 1);
55 static int i2c_mux_gpio_deselect(struct udevice *dev, struct udevice *bus,
58 struct i2c_mux_gpio_priv *priv = dev_get_priv(dev);
61 for (i = 0; i < priv->n_gpios; i++) {
62 ret = dm_gpio_set_value(&priv->gpios[i], (priv->idle >> i) & 1);
70 static int i2c_mux_gpio_probe(struct udevice *dev)
72 const void *fdt = gd->fdt_blob;
73 int node = dev_of_offset(dev);
74 struct i2c_mux_gpio_priv *mux = dev_get_priv(dev);
75 struct gpio_desc *gpios;
77 int i = 0, subnode, ret;
79 mux->n_values = fdtdec_get_child_count(fdt, node);
80 values = devm_kzalloc(dev, sizeof(*mux->values) * mux->n_values,
83 dev_err(dev, "Cannot alloc values array");
87 fdt_for_each_subnode(subnode, fdt, node) {
88 *(values + i) = fdtdec_get_uint(fdt, subnode, "reg", -1);
94 mux->idle = fdtdec_get_uint(fdt, node, "idle-state", -1);
96 mux->n_gpios = gpio_get_list_count(dev, "mux-gpios");
97 if (mux->n_gpios < 0) {
98 dev_err(dev, "Missing mux-gpios property\n");
102 gpios = devm_kzalloc(dev, sizeof(struct gpio_desc) * mux->n_gpios,
105 dev_err(dev, "Cannot allocate gpios array\n");
109 ret = gpio_request_list_by_name(dev, "mux-gpios", gpios, mux->n_gpios,
110 GPIOD_IS_OUT_ACTIVE);
112 dev_err(dev, "Failed to request mux-gpios\n");
121 static const struct i2c_mux_ops i2c_mux_gpio_ops = {
122 .select = i2c_mux_gpio_select,
123 .deselect = i2c_mux_gpio_deselect,
126 static const struct udevice_id i2c_mux_gpio_ids[] = {
127 { .compatible = "i2c-mux-gpio", },
131 U_BOOT_DRIVER(i2c_mux_gpio) = {
132 .name = "i2c_mux_gpio",
133 .id = UCLASS_I2C_MUX,
134 .of_match = i2c_mux_gpio_ids,
135 .ops = &i2c_mux_gpio_ops,
136 .probe = i2c_mux_gpio_probe,
137 .priv_auto_alloc_size = sizeof(struct i2c_mux_gpio_priv),