blob: be90a7b24a8e82ef7ad41549886568eaf7633acb [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 Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Moritz Fischer8238dd92017-09-12 06:46:59 -070013
14#include <asm-generic/gpio.h>
Michal Simek81aaacc2016-04-25 10:50:42 +020015
16DECLARE_GLOBAL_DATA_PTR;
17
Marek Behún267ae1c2017-06-09 19:28:43 +020018enum pca_type {
Luca Ceresolieed013982019-04-09 08:57:43 +020019 PCA9543,
Marek Behún267ae1c2017-06-09 19:28:43 +020020 PCA9544,
21 PCA9547,
Peng Fane4ddfdb2018-07-17 20:38:32 +080022 PCA9548,
23 PCA9646
Marek Behún267ae1c2017-06-09 19:28:43 +020024};
25
26struct chip_desc {
Luca Ceresolieb893962019-04-09 08:57:42 +020027 u8 enable; /* Enable mask in ctl register (used for muxes only) */
Marek Behún267ae1c2017-06-09 19:28:43 +020028 enum muxtype {
29 pca954x_ismux = 0,
30 pca954x_isswi,
31 } muxtype;
Chris Packham05c93262017-09-29 10:53:36 +130032 u32 width;
Marek Behún267ae1c2017-06-09 19:28:43 +020033};
34
Michal Simek81aaacc2016-04-25 10:50:42 +020035struct pca954x_priv {
36 u32 addr; /* I2C mux address */
37 u32 width; /* I2C mux width - number of busses */
Moritz Fischer8238dd92017-09-12 06:46:59 -070038 struct gpio_desc gpio_mux_reset;
Michal Simek81aaacc2016-04-25 10:50:42 +020039};
40
Marek Behún267ae1c2017-06-09 19:28:43 +020041static const struct chip_desc chips[] = {
Luca Ceresolieed013982019-04-09 08:57:43 +020042 [PCA9543] = {
43 .muxtype = pca954x_isswi,
44 .width = 2,
45 },
Marek Behún267ae1c2017-06-09 19:28:43 +020046 [PCA9544] = {
47 .enable = 0x4,
48 .muxtype = pca954x_ismux,
Chris Packham05c93262017-09-29 10:53:36 +130049 .width = 4,
Marek Behún267ae1c2017-06-09 19:28:43 +020050 },
51 [PCA9547] = {
52 .enable = 0x8,
53 .muxtype = pca954x_ismux,
Chris Packham05c93262017-09-29 10:53:36 +130054 .width = 8,
Marek Behún267ae1c2017-06-09 19:28:43 +020055 },
56 [PCA9548] = {
Marek Behún267ae1c2017-06-09 19:28:43 +020057 .muxtype = pca954x_isswi,
Chris Packham05c93262017-09-29 10:53:36 +130058 .width = 8,
Marek Behún267ae1c2017-06-09 19:28:43 +020059 },
Peng Fane4ddfdb2018-07-17 20:38:32 +080060 [PCA9646] = {
Peng Fane4ddfdb2018-07-17 20:38:32 +080061 .muxtype = pca954x_isswi,
62 .width = 4,
63 },
Marek Behún267ae1c2017-06-09 19:28:43 +020064};
65
Michal Simek81aaacc2016-04-25 10:50:42 +020066static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
67 uint channel)
68{
69 struct pca954x_priv *priv = dev_get_priv(mux);
70 uchar byte = 0;
71
72 return dm_i2c_write(mux, priv->addr, &byte, 1);
73}
74
75static int pca954x_select(struct udevice *mux, struct udevice *bus,
76 uint channel)
77{
78 struct pca954x_priv *priv = dev_get_priv(mux);
Marek Behún267ae1c2017-06-09 19:28:43 +020079 const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
80 uchar byte;
81
82 if (chip->muxtype == pca954x_ismux)
83 byte = channel | chip->enable;
84 else
85 byte = 1 << channel;
Michal Simek81aaacc2016-04-25 10:50:42 +020086
87 return dm_i2c_write(mux, priv->addr, &byte, 1);
88}
89
90static const struct i2c_mux_ops pca954x_ops = {
91 .select = pca954x_select,
92 .deselect = pca954x_deselect,
93};
94
95static const struct udevice_id pca954x_ids[] = {
Luca Ceresolieed013982019-04-09 08:57:43 +020096 { .compatible = "nxp,pca9543", .data = PCA9543 },
Marek Behún267ae1c2017-06-09 19:28:43 +020097 { .compatible = "nxp,pca9544", .data = PCA9544 },
98 { .compatible = "nxp,pca9547", .data = PCA9547 },
99 { .compatible = "nxp,pca9548", .data = PCA9548 },
Peng Fane4ddfdb2018-07-17 20:38:32 +0800100 { .compatible = "nxp,pca9646", .data = PCA9646 },
Michal Simek81aaacc2016-04-25 10:50:42 +0200101 { }
102};
103
104static int pca954x_ofdata_to_platdata(struct udevice *dev)
105{
106 struct pca954x_priv *priv = dev_get_priv(dev);
Chris Packham05c93262017-09-29 10:53:36 +1300107 const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
Michal Simek81aaacc2016-04-25 10:50:42 +0200108
Michal Simekd5c97ac2019-01-09 11:58:24 +0100109 priv->addr = dev_read_u32_default(dev, "reg", 0);
Michal Simek81aaacc2016-04-25 10:50:42 +0200110 if (!priv->addr) {
111 debug("MUX not found\n");
112 return -ENODEV;
113 }
Chris Packham05c93262017-09-29 10:53:36 +1300114 priv->width = chip->width;
Michal Simek81aaacc2016-04-25 10:50:42 +0200115
116 if (!priv->width) {
117 debug("No I2C MUX width specified\n");
118 return -EINVAL;
119 }
120
121 debug("Device %s at 0x%x with width %d\n",
122 dev->name, priv->addr, priv->width);
123
124 return 0;
125}
126
Moritz Fischer8238dd92017-09-12 06:46:59 -0700127static int pca954x_probe(struct udevice *dev)
128{
Simon Glassfa4689a2019-12-06 21:41:35 -0700129 if (CONFIG_IS_ENABLED(DM_GPIO)) {
Moritz Fischer8238dd92017-09-12 06:46:59 -0700130 struct pca954x_priv *priv = dev_get_priv(dev);
131 int err;
132
133 err = gpio_request_by_name(dev, "reset-gpios", 0,
134 &priv->gpio_mux_reset, GPIOD_IS_OUT);
135
136 /* it's optional so only bail if we get a real error */
137 if (err && (err != -ENOENT))
138 return err;
139
140 /* dm will take care of polarity */
141 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
142 dm_gpio_set_value(&priv->gpio_mux_reset, 0);
143 }
144
145 return 0;
146}
147
148static int pca954x_remove(struct udevice *dev)
149{
Simon Glassfa4689a2019-12-06 21:41:35 -0700150 if (CONFIG_IS_ENABLED(DM_GPIO)) {
Moritz Fischer8238dd92017-09-12 06:46:59 -0700151 struct pca954x_priv *priv = dev_get_priv(dev);
152
153 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
154 dm_gpio_free(dev, &priv->gpio_mux_reset);
155 }
156
157 return 0;
158}
159
Michal Simek81aaacc2016-04-25 10:50:42 +0200160U_BOOT_DRIVER(pca954x) = {
161 .name = "pca954x",
162 .id = UCLASS_I2C_MUX,
163 .of_match = pca954x_ids,
Moritz Fischer8238dd92017-09-12 06:46:59 -0700164 .probe = pca954x_probe,
165 .remove = pca954x_remove,
Michal Simek81aaacc2016-04-25 10:50:42 +0200166 .ops = &pca954x_ops,
167 .ofdata_to_platdata = pca954x_ofdata_to_platdata,
168 .priv_auto_alloc_size = sizeof(struct pca954x_priv),
169};