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;
26 u8 enable; /* Enable mask in ctl register (used for muxes only) */
35 u32 addr; /* I2C mux address */
36 u32 width; /* I2C mux width - number of busses */
37 struct gpio_desc gpio_mux_reset;
40 static const struct chip_desc chips[] = {
42 .muxtype = pca954x_isswi,
47 .muxtype = pca954x_ismux,
52 .muxtype = pca954x_ismux,
56 .muxtype = pca954x_isswi,
60 .muxtype = pca954x_isswi,
65 static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
68 struct pca954x_priv *priv = dev_get_priv(mux);
71 return dm_i2c_write(mux, priv->addr, &byte, 1);
74 static int pca954x_select(struct udevice *mux, struct udevice *bus,
77 struct pca954x_priv *priv = dev_get_priv(mux);
78 const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
81 if (chip->muxtype == pca954x_ismux)
82 byte = channel | chip->enable;
86 return dm_i2c_write(mux, priv->addr, &byte, 1);
89 static const struct i2c_mux_ops pca954x_ops = {
90 .select = pca954x_select,
91 .deselect = pca954x_deselect,
94 static const struct udevice_id pca954x_ids[] = {
95 { .compatible = "nxp,pca9543", .data = PCA9543 },
96 { .compatible = "nxp,pca9544", .data = PCA9544 },
97 { .compatible = "nxp,pca9547", .data = PCA9547 },
98 { .compatible = "nxp,pca9548", .data = PCA9548 },
99 { .compatible = "nxp,pca9646", .data = PCA9646 },
103 static int pca954x_ofdata_to_platdata(struct udevice *dev)
105 struct pca954x_priv *priv = dev_get_priv(dev);
106 const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
108 priv->addr = dev_read_u32_default(dev, "reg", 0);
110 debug("MUX not found\n");
113 priv->width = chip->width;
116 debug("No I2C MUX width specified\n");
120 debug("Device %s at 0x%x with width %d\n",
121 dev->name, priv->addr, priv->width);
126 static int pca954x_probe(struct udevice *dev)
128 if (IS_ENABLED(CONFIG_DM_GPIO)) {
129 struct pca954x_priv *priv = dev_get_priv(dev);
132 err = gpio_request_by_name(dev, "reset-gpios", 0,
133 &priv->gpio_mux_reset, GPIOD_IS_OUT);
135 /* it's optional so only bail if we get a real error */
136 if (err && (err != -ENOENT))
139 /* dm will take care of polarity */
140 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
141 dm_gpio_set_value(&priv->gpio_mux_reset, 0);
147 static int pca954x_remove(struct udevice *dev)
149 if (IS_ENABLED(CONFIG_DM_GPIO)) {
150 struct pca954x_priv *priv = dev_get_priv(dev);
152 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
153 dm_gpio_free(dev, &priv->gpio_mux_reset);
159 U_BOOT_DRIVER(pca954x) = {
161 .id = UCLASS_I2C_MUX,
162 .of_match = pca954x_ids,
163 .probe = pca954x_probe,
164 .remove = pca954x_remove,
166 .ofdata_to_platdata = pca954x_ofdata_to_platdata,
167 .priv_auto_alloc_size = sizeof(struct pca954x_priv),