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
14 #include <asm-generic/gpio.h>
16 DECLARE_GLOBAL_DATA_PTR;
28 u8 enable; /* Enable mask in ctl register (used for muxes only) */
37 u32 addr; /* I2C mux address */
38 u32 width; /* I2C mux width - number of busses */
39 struct gpio_desc gpio_mux_reset;
42 static const struct chip_desc chips[] = {
44 .muxtype = pca954x_isswi,
49 .muxtype = pca954x_ismux,
53 .muxtype = pca954x_isswi,
58 .muxtype = pca954x_ismux,
62 .muxtype = pca954x_isswi,
66 .muxtype = pca954x_isswi,
71 static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
74 struct pca954x_priv *priv = dev_get_priv(mux);
77 return dm_i2c_write(mux, priv->addr, &byte, 1);
80 static int pca954x_select(struct udevice *mux, struct udevice *bus,
83 struct pca954x_priv *priv = dev_get_priv(mux);
84 const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
87 if (chip->muxtype == pca954x_ismux)
88 byte = channel | chip->enable;
92 return dm_i2c_write(mux, priv->addr, &byte, 1);
95 static const struct i2c_mux_ops pca954x_ops = {
96 .select = pca954x_select,
97 .deselect = pca954x_deselect,
100 static const struct udevice_id pca954x_ids[] = {
101 { .compatible = "nxp,pca9543", .data = PCA9543 },
102 { .compatible = "nxp,pca9544", .data = PCA9544 },
103 { .compatible = "nxp,pca9546", .data = PCA9546 },
104 { .compatible = "nxp,pca9547", .data = PCA9547 },
105 { .compatible = "nxp,pca9548", .data = PCA9548 },
106 { .compatible = "nxp,pca9646", .data = PCA9646 },
110 static int pca954x_ofdata_to_platdata(struct udevice *dev)
112 struct pca954x_priv *priv = dev_get_priv(dev);
113 const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
115 priv->addr = dev_read_u32_default(dev, "reg", 0);
117 debug("MUX not found\n");
120 priv->width = chip->width;
123 debug("No I2C MUX width specified\n");
127 debug("Device %s at 0x%x with width %d\n",
128 dev->name, priv->addr, priv->width);
133 static int pca954x_probe(struct udevice *dev)
135 if (CONFIG_IS_ENABLED(DM_GPIO)) {
136 struct pca954x_priv *priv = dev_get_priv(dev);
139 err = gpio_request_by_name(dev, "reset-gpios", 0,
140 &priv->gpio_mux_reset, GPIOD_IS_OUT);
142 /* it's optional so only bail if we get a real error */
143 if (err && (err != -ENOENT))
146 /* dm will take care of polarity */
147 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
148 dm_gpio_set_value(&priv->gpio_mux_reset, 0);
154 static int pca954x_remove(struct udevice *dev)
156 if (CONFIG_IS_ENABLED(DM_GPIO)) {
157 struct pca954x_priv *priv = dev_get_priv(dev);
159 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
160 dm_gpio_free(dev, &priv->gpio_mux_reset);
166 U_BOOT_DRIVER(pca954x) = {
168 .id = UCLASS_I2C_MUX,
169 .of_match = pca954x_ids,
170 .probe = pca954x_probe,
171 .remove = pca954x_remove,
173 .ofdata_to_platdata = pca954x_ofdata_to_platdata,
174 .priv_auto_alloc_size = sizeof(struct pca954x_priv),