2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-04-25 08:50:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 - 2016 Xilinx, Inc.
|
2017-09-12 13:46:59 +00:00
|
|
|
* Copyright (C) 2017 National Instruments Corp
|
2016-04-25 08:50:42 +00:00
|
|
|
* Written by Michal Simek
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <i2c.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2017-09-12 13:46:59 +00:00
|
|
|
|
|
|
|
#include <asm-generic/gpio.h>
|
2016-04-25 08:50:42 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2017-06-09 17:28:43 +00:00
|
|
|
enum pca_type {
|
2019-04-09 06:57:43 +00:00
|
|
|
PCA9543,
|
2017-06-09 17:28:43 +00:00
|
|
|
PCA9544,
|
2020-04-01 02:55:27 +00:00
|
|
|
PCA9546,
|
2017-06-09 17:28:43 +00:00
|
|
|
PCA9547,
|
2018-07-17 12:38:32 +00:00
|
|
|
PCA9548,
|
|
|
|
PCA9646
|
2017-06-09 17:28:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct chip_desc {
|
2019-04-09 06:57:42 +00:00
|
|
|
u8 enable; /* Enable mask in ctl register (used for muxes only) */
|
2017-06-09 17:28:43 +00:00
|
|
|
enum muxtype {
|
|
|
|
pca954x_ismux = 0,
|
|
|
|
pca954x_isswi,
|
|
|
|
} muxtype;
|
2017-09-28 21:53:36 +00:00
|
|
|
u32 width;
|
2017-06-09 17:28:43 +00:00
|
|
|
};
|
|
|
|
|
2016-04-25 08:50:42 +00:00
|
|
|
struct pca954x_priv {
|
|
|
|
u32 addr; /* I2C mux address */
|
|
|
|
u32 width; /* I2C mux width - number of busses */
|
2017-09-12 13:46:59 +00:00
|
|
|
struct gpio_desc gpio_mux_reset;
|
2016-04-25 08:50:42 +00:00
|
|
|
};
|
|
|
|
|
2017-06-09 17:28:43 +00:00
|
|
|
static const struct chip_desc chips[] = {
|
2019-04-09 06:57:43 +00:00
|
|
|
[PCA9543] = {
|
|
|
|
.muxtype = pca954x_isswi,
|
|
|
|
.width = 2,
|
|
|
|
},
|
2017-06-09 17:28:43 +00:00
|
|
|
[PCA9544] = {
|
|
|
|
.enable = 0x4,
|
|
|
|
.muxtype = pca954x_ismux,
|
2017-09-28 21:53:36 +00:00
|
|
|
.width = 4,
|
2017-06-09 17:28:43 +00:00
|
|
|
},
|
2020-04-01 02:55:27 +00:00
|
|
|
[PCA9546] = {
|
|
|
|
.muxtype = pca954x_isswi,
|
|
|
|
.width = 4,
|
|
|
|
},
|
2017-06-09 17:28:43 +00:00
|
|
|
[PCA9547] = {
|
|
|
|
.enable = 0x8,
|
|
|
|
.muxtype = pca954x_ismux,
|
2017-09-28 21:53:36 +00:00
|
|
|
.width = 8,
|
2017-06-09 17:28:43 +00:00
|
|
|
},
|
|
|
|
[PCA9548] = {
|
|
|
|
.muxtype = pca954x_isswi,
|
2017-09-28 21:53:36 +00:00
|
|
|
.width = 8,
|
2017-06-09 17:28:43 +00:00
|
|
|
},
|
2018-07-17 12:38:32 +00:00
|
|
|
[PCA9646] = {
|
|
|
|
.muxtype = pca954x_isswi,
|
|
|
|
.width = 4,
|
|
|
|
},
|
2017-06-09 17:28:43 +00:00
|
|
|
};
|
|
|
|
|
2016-04-25 08:50:42 +00:00
|
|
|
static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
|
|
|
|
uint channel)
|
|
|
|
{
|
|
|
|
struct pca954x_priv *priv = dev_get_priv(mux);
|
|
|
|
uchar byte = 0;
|
|
|
|
|
|
|
|
return dm_i2c_write(mux, priv->addr, &byte, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pca954x_select(struct udevice *mux, struct udevice *bus,
|
|
|
|
uint channel)
|
|
|
|
{
|
|
|
|
struct pca954x_priv *priv = dev_get_priv(mux);
|
2017-06-09 17:28:43 +00:00
|
|
|
const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
|
|
|
|
uchar byte;
|
|
|
|
|
|
|
|
if (chip->muxtype == pca954x_ismux)
|
|
|
|
byte = channel | chip->enable;
|
|
|
|
else
|
|
|
|
byte = 1 << channel;
|
2016-04-25 08:50:42 +00:00
|
|
|
|
|
|
|
return dm_i2c_write(mux, priv->addr, &byte, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_mux_ops pca954x_ops = {
|
|
|
|
.select = pca954x_select,
|
|
|
|
.deselect = pca954x_deselect,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id pca954x_ids[] = {
|
2019-04-09 06:57:43 +00:00
|
|
|
{ .compatible = "nxp,pca9543", .data = PCA9543 },
|
2017-06-09 17:28:43 +00:00
|
|
|
{ .compatible = "nxp,pca9544", .data = PCA9544 },
|
2020-04-01 02:55:27 +00:00
|
|
|
{ .compatible = "nxp,pca9546", .data = PCA9546 },
|
2017-06-09 17:28:43 +00:00
|
|
|
{ .compatible = "nxp,pca9547", .data = PCA9547 },
|
|
|
|
{ .compatible = "nxp,pca9548", .data = PCA9548 },
|
2018-07-17 12:38:32 +00:00
|
|
|
{ .compatible = "nxp,pca9646", .data = PCA9646 },
|
2016-04-25 08:50:42 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int pca954x_of_to_plat(struct udevice *dev)
|
2016-04-25 08:50:42 +00:00
|
|
|
{
|
|
|
|
struct pca954x_priv *priv = dev_get_priv(dev);
|
2017-09-28 21:53:36 +00:00
|
|
|
const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
|
2016-04-25 08:50:42 +00:00
|
|
|
|
2019-01-09 10:58:24 +00:00
|
|
|
priv->addr = dev_read_u32_default(dev, "reg", 0);
|
2016-04-25 08:50:42 +00:00
|
|
|
if (!priv->addr) {
|
|
|
|
debug("MUX not found\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2017-09-28 21:53:36 +00:00
|
|
|
priv->width = chip->width;
|
2016-04-25 08:50:42 +00:00
|
|
|
|
|
|
|
if (!priv->width) {
|
|
|
|
debug("No I2C MUX width specified\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Device %s at 0x%x with width %d\n",
|
|
|
|
dev->name, priv->addr, priv->width);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-12 13:46:59 +00:00
|
|
|
static int pca954x_probe(struct udevice *dev)
|
|
|
|
{
|
2019-12-07 04:41:35 +00:00
|
|
|
if (CONFIG_IS_ENABLED(DM_GPIO)) {
|
2017-09-12 13:46:59 +00:00
|
|
|
struct pca954x_priv *priv = dev_get_priv(dev);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = gpio_request_by_name(dev, "reset-gpios", 0,
|
|
|
|
&priv->gpio_mux_reset, GPIOD_IS_OUT);
|
|
|
|
|
|
|
|
/* it's optional so only bail if we get a real error */
|
|
|
|
if (err && (err != -ENOENT))
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* dm will take care of polarity */
|
|
|
|
if (dm_gpio_is_valid(&priv->gpio_mux_reset))
|
|
|
|
dm_gpio_set_value(&priv->gpio_mux_reset, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pca954x_remove(struct udevice *dev)
|
|
|
|
{
|
2019-12-07 04:41:35 +00:00
|
|
|
if (CONFIG_IS_ENABLED(DM_GPIO)) {
|
2017-09-12 13:46:59 +00:00
|
|
|
struct pca954x_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (dm_gpio_is_valid(&priv->gpio_mux_reset))
|
|
|
|
dm_gpio_free(dev, &priv->gpio_mux_reset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-25 08:50:42 +00:00
|
|
|
U_BOOT_DRIVER(pca954x) = {
|
|
|
|
.name = "pca954x",
|
|
|
|
.id = UCLASS_I2C_MUX,
|
|
|
|
.of_match = pca954x_ids,
|
2017-09-12 13:46:59 +00:00
|
|
|
.probe = pca954x_probe,
|
|
|
|
.remove = pca954x_remove,
|
2016-04-25 08:50:42 +00:00
|
|
|
.ops = &pca954x_ops,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = pca954x_of_to_plat,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct pca954x_priv),
|
2016-04-25 08:50:42 +00:00
|
|
|
};
|