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
15 #include <asm-generic/gpio.h>
17 DECLARE_GLOBAL_DATA_PTR;
29 u8 enable; /* Enable mask in ctl register (used for muxes only) */
38 u32 addr; /* I2C mux address */
39 u32 width; /* I2C mux width - number of busses */
40 struct gpio_desc gpio_mux_reset;
43 static const struct chip_desc chips[] = {
45 .muxtype = pca954x_isswi,
50 .muxtype = pca954x_ismux,
54 .muxtype = pca954x_isswi,
59 .muxtype = pca954x_ismux,
63 .muxtype = pca954x_isswi,
67 .muxtype = pca954x_isswi,
72 static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
75 struct pca954x_priv *priv = dev_get_priv(mux);
78 return dm_i2c_write(mux, priv->addr, &byte, 1);
81 static int pca954x_select(struct udevice *mux, struct udevice *bus,
84 struct pca954x_priv *priv = dev_get_priv(mux);
85 const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
88 if (chip->muxtype == pca954x_ismux)
89 byte = channel | chip->enable;
93 return dm_i2c_write(mux, priv->addr, &byte, 1);
96 static const struct i2c_mux_ops pca954x_ops = {
97 .select = pca954x_select,
98 .deselect = pca954x_deselect,
101 static const struct udevice_id pca954x_ids[] = {
102 { .compatible = "nxp,pca9543", .data = PCA9543 },
103 { .compatible = "nxp,pca9544", .data = PCA9544 },
104 { .compatible = "nxp,pca9546", .data = PCA9546 },
105 { .compatible = "nxp,pca9547", .data = PCA9547 },
106 { .compatible = "nxp,pca9548", .data = PCA9548 },
107 { .compatible = "nxp,pca9646", .data = PCA9646 },
111 static int pca954x_ofdata_to_platdata(struct udevice *dev)
113 struct pca954x_priv *priv = dev_get_priv(dev);
114 const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
116 priv->addr = dev_read_u32_default(dev, "reg", 0);
118 debug("MUX not found\n");
121 priv->width = chip->width;
124 debug("No I2C MUX width specified\n");
128 debug("Device %s at 0x%x with width %d\n",
129 dev->name, priv->addr, priv->width);
134 static int pca954x_probe(struct udevice *dev)
136 if (CONFIG_IS_ENABLED(DM_GPIO)) {
137 struct pca954x_priv *priv = dev_get_priv(dev);
140 err = gpio_request_by_name(dev, "reset-gpios", 0,
141 &priv->gpio_mux_reset, GPIOD_IS_OUT);
143 /* it's optional so only bail if we get a real error */
144 if (err && (err != -ENOENT))
147 /* dm will take care of polarity */
148 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
149 dm_gpio_set_value(&priv->gpio_mux_reset, 0);
155 static int pca954x_remove(struct udevice *dev)
157 if (CONFIG_IS_ENABLED(DM_GPIO)) {
158 struct pca954x_priv *priv = dev_get_priv(dev);
160 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
161 dm_gpio_free(dev, &priv->gpio_mux_reset);
167 U_BOOT_DRIVER(pca954x) = {
169 .id = UCLASS_I2C_MUX,
170 .of_match = pca954x_ids,
171 .probe = pca954x_probe,
172 .remove = pca954x_remove,
174 .ofdata_to_platdata = pca954x_ofdata_to_platdata,
175 .priv_auto_alloc_size = sizeof(struct pca954x_priv),