blob: 55858cf653f24a664a6ea368078e5f7f7a10c113 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek81aaacc2016-04-25 10:50:42 +02002/*
3 * Copyright (C) 2015 - 2016 Xilinx, Inc.
Moritz Fischer8238dd92017-09-12 06:46:59 -07004 * Copyright (C) 2017 National Instruments Corp
Michal Simek81aaacc2016-04-25 10:50:42 +02005 * Written by Michal Simek
Michal Simek81aaacc2016-04-25 10:50:42 +02006 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Moritz Fischer8238dd92017-09-12 06:46:59 -070015
16#include <asm-generic/gpio.h>
Michal Simek81aaacc2016-04-25 10:50:42 +020017
18DECLARE_GLOBAL_DATA_PTR;
19
Marek Behún267ae1c2017-06-09 19:28:43 +020020enum pca_type {
Luca Ceresolieed013982019-04-09 08:57:43 +020021 PCA9543,
Marek Behún267ae1c2017-06-09 19:28:43 +020022 PCA9544,
Chris Packham3e0259d2020-04-01 15:55:27 +130023 PCA9546,
Marek Behún267ae1c2017-06-09 19:28:43 +020024 PCA9547,
Peng Fane4ddfdb2018-07-17 20:38:32 +080025 PCA9548,
26 PCA9646
Marek Behún267ae1c2017-06-09 19:28:43 +020027};
28
29struct chip_desc {
Luca Ceresolieb893962019-04-09 08:57:42 +020030 u8 enable; /* Enable mask in ctl register (used for muxes only) */
Marek Behún267ae1c2017-06-09 19:28:43 +020031 enum muxtype {
32 pca954x_ismux = 0,
33 pca954x_isswi,
34 } muxtype;
Chris Packham05c93262017-09-29 10:53:36 +130035 u32 width;
Marek Behún267ae1c2017-06-09 19:28:43 +020036};
37
Michal Simek81aaacc2016-04-25 10:50:42 +020038struct pca954x_priv {
39 u32 addr; /* I2C mux address */
40 u32 width; /* I2C mux width - number of busses */
Moritz Fischer8238dd92017-09-12 06:46:59 -070041 struct gpio_desc gpio_mux_reset;
Michal Simek81aaacc2016-04-25 10:50:42 +020042};
43
Marek Behún267ae1c2017-06-09 19:28:43 +020044static const struct chip_desc chips[] = {
Luca Ceresolieed013982019-04-09 08:57:43 +020045 [PCA9543] = {
46 .muxtype = pca954x_isswi,
47 .width = 2,
48 },
Marek Behún267ae1c2017-06-09 19:28:43 +020049 [PCA9544] = {
50 .enable = 0x4,
51 .muxtype = pca954x_ismux,
Chris Packham05c93262017-09-29 10:53:36 +130052 .width = 4,
Marek Behún267ae1c2017-06-09 19:28:43 +020053 },
Chris Packham3e0259d2020-04-01 15:55:27 +130054 [PCA9546] = {
55 .muxtype = pca954x_isswi,
56 .width = 4,
57 },
Marek Behún267ae1c2017-06-09 19:28:43 +020058 [PCA9547] = {
59 .enable = 0x8,
60 .muxtype = pca954x_ismux,
Chris Packham05c93262017-09-29 10:53:36 +130061 .width = 8,
Marek Behún267ae1c2017-06-09 19:28:43 +020062 },
63 [PCA9548] = {
Marek Behún267ae1c2017-06-09 19:28:43 +020064 .muxtype = pca954x_isswi,
Chris Packham05c93262017-09-29 10:53:36 +130065 .width = 8,
Marek Behún267ae1c2017-06-09 19:28:43 +020066 },
Peng Fane4ddfdb2018-07-17 20:38:32 +080067 [PCA9646] = {
Peng Fane4ddfdb2018-07-17 20:38:32 +080068 .muxtype = pca954x_isswi,
69 .width = 4,
70 },
Marek Behún267ae1c2017-06-09 19:28:43 +020071};
72
Michal Simek81aaacc2016-04-25 10:50:42 +020073static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
74 uint channel)
75{
76 struct pca954x_priv *priv = dev_get_priv(mux);
77 uchar byte = 0;
78
79 return dm_i2c_write(mux, priv->addr, &byte, 1);
80}
81
82static int pca954x_select(struct udevice *mux, struct udevice *bus,
83 uint channel)
84{
85 struct pca954x_priv *priv = dev_get_priv(mux);
Marek Behún267ae1c2017-06-09 19:28:43 +020086 const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
87 uchar byte;
88
89 if (chip->muxtype == pca954x_ismux)
90 byte = channel | chip->enable;
91 else
92 byte = 1 << channel;
Michal Simek81aaacc2016-04-25 10:50:42 +020093
94 return dm_i2c_write(mux, priv->addr, &byte, 1);
95}
96
97static const struct i2c_mux_ops pca954x_ops = {
98 .select = pca954x_select,
99 .deselect = pca954x_deselect,
100};
101
102static const struct udevice_id pca954x_ids[] = {
Luca Ceresolieed013982019-04-09 08:57:43 +0200103 { .compatible = "nxp,pca9543", .data = PCA9543 },
Marek Behún267ae1c2017-06-09 19:28:43 +0200104 { .compatible = "nxp,pca9544", .data = PCA9544 },
Chris Packham3e0259d2020-04-01 15:55:27 +1300105 { .compatible = "nxp,pca9546", .data = PCA9546 },
Marek Behún267ae1c2017-06-09 19:28:43 +0200106 { .compatible = "nxp,pca9547", .data = PCA9547 },
107 { .compatible = "nxp,pca9548", .data = PCA9548 },
Peng Fane4ddfdb2018-07-17 20:38:32 +0800108 { .compatible = "nxp,pca9646", .data = PCA9646 },
Michal Simek81aaacc2016-04-25 10:50:42 +0200109 { }
110};
111
Simon Glassaad29ae2020-12-03 16:55:21 -0700112static int pca954x_of_to_plat(struct udevice *dev)
Michal Simek81aaacc2016-04-25 10:50:42 +0200113{
114 struct pca954x_priv *priv = dev_get_priv(dev);
Chris Packham05c93262017-09-29 10:53:36 +1300115 const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
Michal Simek81aaacc2016-04-25 10:50:42 +0200116
Michal Simekd5c97ac2019-01-09 11:58:24 +0100117 priv->addr = dev_read_u32_default(dev, "reg", 0);
Michal Simek81aaacc2016-04-25 10:50:42 +0200118 if (!priv->addr) {
119 debug("MUX not found\n");
120 return -ENODEV;
121 }
Chris Packham05c93262017-09-29 10:53:36 +1300122 priv->width = chip->width;
Michal Simek81aaacc2016-04-25 10:50:42 +0200123
124 if (!priv->width) {
125 debug("No I2C MUX width specified\n");
126 return -EINVAL;
127 }
128
129 debug("Device %s at 0x%x with width %d\n",
130 dev->name, priv->addr, priv->width);
131
132 return 0;
133}
134
Moritz Fischer8238dd92017-09-12 06:46:59 -0700135static int pca954x_probe(struct udevice *dev)
136{
Simon Glassfa4689a2019-12-06 21:41:35 -0700137 if (CONFIG_IS_ENABLED(DM_GPIO)) {
Moritz Fischer8238dd92017-09-12 06:46:59 -0700138 struct pca954x_priv *priv = dev_get_priv(dev);
139 int err;
140
141 err = gpio_request_by_name(dev, "reset-gpios", 0,
142 &priv->gpio_mux_reset, GPIOD_IS_OUT);
143
144 /* it's optional so only bail if we get a real error */
145 if (err && (err != -ENOENT))
146 return err;
147
148 /* dm will take care of polarity */
149 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
150 dm_gpio_set_value(&priv->gpio_mux_reset, 0);
151 }
152
153 return 0;
154}
155
156static int pca954x_remove(struct udevice *dev)
157{
Simon Glassfa4689a2019-12-06 21:41:35 -0700158 if (CONFIG_IS_ENABLED(DM_GPIO)) {
Moritz Fischer8238dd92017-09-12 06:46:59 -0700159 struct pca954x_priv *priv = dev_get_priv(dev);
160
161 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
162 dm_gpio_free(dev, &priv->gpio_mux_reset);
163 }
164
165 return 0;
166}
167
Michal Simek81aaacc2016-04-25 10:50:42 +0200168U_BOOT_DRIVER(pca954x) = {
169 .name = "pca954x",
170 .id = UCLASS_I2C_MUX,
171 .of_match = pca954x_ids,
Moritz Fischer8238dd92017-09-12 06:46:59 -0700172 .probe = pca954x_probe,
173 .remove = pca954x_remove,
Michal Simek81aaacc2016-04-25 10:50:42 +0200174 .ops = &pca954x_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700175 .of_to_plat = pca954x_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700176 .priv_auto = sizeof(struct pca954x_priv),
Michal Simek81aaacc2016-04-25 10:50:42 +0200177};