1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 - 2016 Xilinx, Inc.
4 * Copyright (C) 2017 National Instruments Corp
5 * Written by Michal Simek
13 #include <asm-generic/gpio.h>
15 DECLARE_GLOBAL_DATA_PTR;
34 u32 addr; /* I2C mux address */
35 u32 width; /* I2C mux width - number of busses */
36 struct gpio_desc gpio_mux_reset;
39 static const struct chip_desc chips[] = {
42 .muxtype = pca954x_ismux,
47 .muxtype = pca954x_ismux,
52 .muxtype = pca954x_isswi,
57 .muxtype = pca954x_isswi,
62 static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
65 struct pca954x_priv *priv = dev_get_priv(mux);
68 return dm_i2c_write(mux, priv->addr, &byte, 1);
71 static int pca954x_select(struct udevice *mux, struct udevice *bus,
74 struct pca954x_priv *priv = dev_get_priv(mux);
75 const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
78 if (chip->muxtype == pca954x_ismux)
79 byte = channel | chip->enable;
83 return dm_i2c_write(mux, priv->addr, &byte, 1);
86 static const struct i2c_mux_ops pca954x_ops = {
87 .select = pca954x_select,
88 .deselect = pca954x_deselect,
91 static const struct udevice_id pca954x_ids[] = {
92 { .compatible = "nxp,pca9544", .data = PCA9544 },
93 { .compatible = "nxp,pca9547", .data = PCA9547 },
94 { .compatible = "nxp,pca9548", .data = PCA9548 },
95 { .compatible = "nxp,pca9646", .data = PCA9646 },
99 static int pca954x_ofdata_to_platdata(struct udevice *dev)
101 struct pca954x_priv *priv = dev_get_priv(dev);
102 const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
104 priv->addr = dev_read_u32_default(dev, "reg", 0);
106 debug("MUX not found\n");
109 priv->width = chip->width;
112 debug("No I2C MUX width specified\n");
116 debug("Device %s at 0x%x with width %d\n",
117 dev->name, priv->addr, priv->width);
122 static int pca954x_probe(struct udevice *dev)
124 if (IS_ENABLED(CONFIG_DM_GPIO)) {
125 struct pca954x_priv *priv = dev_get_priv(dev);
128 err = gpio_request_by_name(dev, "reset-gpios", 0,
129 &priv->gpio_mux_reset, GPIOD_IS_OUT);
131 /* it's optional so only bail if we get a real error */
132 if (err && (err != -ENOENT))
135 /* dm will take care of polarity */
136 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
137 dm_gpio_set_value(&priv->gpio_mux_reset, 0);
143 static int pca954x_remove(struct udevice *dev)
145 if (IS_ENABLED(CONFIG_DM_GPIO)) {
146 struct pca954x_priv *priv = dev_get_priv(dev);
148 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
149 dm_gpio_free(dev, &priv->gpio_mux_reset);
155 U_BOOT_DRIVER(pca954x) = {
157 .id = UCLASS_I2C_MUX,
158 .of_match = pca954x_ids,
159 .probe = pca954x_probe,
160 .remove = pca954x_remove,
162 .ofdata_to_platdata = pca954x_ofdata_to_platdata,
163 .priv_auto_alloc_size = sizeof(struct pca954x_priv),