Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 - 2016 Xilinx, Inc. |
Moritz Fischer | 36bdeb7 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 4 | * Copyright (C) 2017 National Instruments Corp |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 5 | * Written by Michal Simek |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <i2c.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Moritz Fischer | 36bdeb7 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 13 | |
| 14 | #include <asm-generic/gpio.h> |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 15 | |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 16 | enum pca_type { |
Maksim Kiselev | f7c9839 | 2024-09-19 19:06:56 +0300 | [diff] [blame] | 17 | MAX7356, |
| 18 | MAX7357, |
| 19 | MAX7358, |
| 20 | MAX7367, |
| 21 | MAX7368, |
| 22 | MAX7369, |
Luca Ceresoli | 9985b74 | 2019-04-09 08:57:43 +0200 | [diff] [blame] | 23 | PCA9543, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 24 | PCA9544, |
Chris Packham | 0b1d7b7 | 2020-04-01 15:55:27 +1300 | [diff] [blame] | 25 | PCA9546, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 26 | PCA9547, |
Peng Fan | 16f513e | 2018-07-17 20:38:32 +0800 | [diff] [blame] | 27 | PCA9548, |
Vladimir Oltean | 3285a57 | 2022-01-03 14:47:22 +0200 | [diff] [blame] | 28 | PCA9646, |
| 29 | PCA9847, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct chip_desc { |
Luca Ceresoli | 5995cdb | 2019-04-09 08:57:42 +0200 | [diff] [blame] | 33 | u8 enable; /* Enable mask in ctl register (used for muxes only) */ |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 34 | enum muxtype { |
| 35 | pca954x_ismux = 0, |
| 36 | pca954x_isswi, |
| 37 | } muxtype; |
Chris Packham | 5bc90a8 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 38 | u32 width; |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 41 | struct pca954x_priv { |
| 42 | u32 addr; /* I2C mux address */ |
| 43 | u32 width; /* I2C mux width - number of busses */ |
Moritz Fischer | 36bdeb7 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 44 | struct gpio_desc gpio_mux_reset; |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 47 | static const struct chip_desc chips[] = { |
Maksim Kiselev | f7c9839 | 2024-09-19 19:06:56 +0300 | [diff] [blame] | 48 | [MAX7356] = { |
| 49 | .muxtype = pca954x_isswi, |
| 50 | .width = 8, |
| 51 | }, |
| 52 | [MAX7357] = { |
| 53 | .muxtype = pca954x_isswi, |
| 54 | .width = 8, |
| 55 | }, |
| 56 | [MAX7358] = { |
| 57 | .muxtype = pca954x_isswi, |
| 58 | .width = 8, |
| 59 | }, |
| 60 | [MAX7367] = { |
| 61 | .muxtype = pca954x_isswi, |
| 62 | .width = 4, |
| 63 | }, |
| 64 | [MAX7368] = { |
| 65 | .muxtype = pca954x_isswi, |
| 66 | .width = 4, |
| 67 | }, |
| 68 | [MAX7369] = { |
| 69 | .enable = 0x4, |
| 70 | .muxtype = pca954x_ismux, |
| 71 | .width = 4, |
| 72 | }, |
Luca Ceresoli | 9985b74 | 2019-04-09 08:57:43 +0200 | [diff] [blame] | 73 | [PCA9543] = { |
| 74 | .muxtype = pca954x_isswi, |
| 75 | .width = 2, |
| 76 | }, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 77 | [PCA9544] = { |
| 78 | .enable = 0x4, |
| 79 | .muxtype = pca954x_ismux, |
Chris Packham | 5bc90a8 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 80 | .width = 4, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 81 | }, |
Chris Packham | 0b1d7b7 | 2020-04-01 15:55:27 +1300 | [diff] [blame] | 82 | [PCA9546] = { |
| 83 | .muxtype = pca954x_isswi, |
| 84 | .width = 4, |
| 85 | }, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 86 | [PCA9547] = { |
| 87 | .enable = 0x8, |
| 88 | .muxtype = pca954x_ismux, |
Chris Packham | 5bc90a8 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 89 | .width = 8, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 90 | }, |
| 91 | [PCA9548] = { |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 92 | .muxtype = pca954x_isswi, |
Chris Packham | 5bc90a8 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 93 | .width = 8, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 94 | }, |
Peng Fan | 16f513e | 2018-07-17 20:38:32 +0800 | [diff] [blame] | 95 | [PCA9646] = { |
Peng Fan | 16f513e | 2018-07-17 20:38:32 +0800 | [diff] [blame] | 96 | .muxtype = pca954x_isswi, |
| 97 | .width = 4, |
| 98 | }, |
Vladimir Oltean | 3285a57 | 2022-01-03 14:47:22 +0200 | [diff] [blame] | 99 | [PCA9847] = { |
| 100 | .enable = 0x8, |
| 101 | .muxtype = pca954x_ismux, |
| 102 | .width = 8, |
| 103 | }, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 106 | static int pca954x_deselect(struct udevice *mux, struct udevice *bus, |
| 107 | uint channel) |
| 108 | { |
| 109 | struct pca954x_priv *priv = dev_get_priv(mux); |
| 110 | uchar byte = 0; |
| 111 | |
| 112 | return dm_i2c_write(mux, priv->addr, &byte, 1); |
| 113 | } |
| 114 | |
| 115 | static int pca954x_select(struct udevice *mux, struct udevice *bus, |
| 116 | uint channel) |
| 117 | { |
| 118 | struct pca954x_priv *priv = dev_get_priv(mux); |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 119 | const struct chip_desc *chip = &chips[dev_get_driver_data(mux)]; |
| 120 | uchar byte; |
| 121 | |
| 122 | if (chip->muxtype == pca954x_ismux) |
| 123 | byte = channel | chip->enable; |
| 124 | else |
| 125 | byte = 1 << channel; |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 126 | |
| 127 | return dm_i2c_write(mux, priv->addr, &byte, 1); |
| 128 | } |
| 129 | |
| 130 | static const struct i2c_mux_ops pca954x_ops = { |
| 131 | .select = pca954x_select, |
| 132 | .deselect = pca954x_deselect, |
| 133 | }; |
| 134 | |
| 135 | static const struct udevice_id pca954x_ids[] = { |
Maksim Kiselev | f7c9839 | 2024-09-19 19:06:56 +0300 | [diff] [blame] | 136 | { .compatible = "maxim,max7356", .data = MAX7356 }, |
| 137 | { .compatible = "maxim,max7357", .data = MAX7357 }, |
| 138 | { .compatible = "maxim,max7358", .data = MAX7358 }, |
| 139 | { .compatible = "maxim,max7367", .data = MAX7367 }, |
| 140 | { .compatible = "maxim,max7368", .data = MAX7368 }, |
| 141 | { .compatible = "maxim,max7369", .data = MAX7369 }, |
Luca Ceresoli | 9985b74 | 2019-04-09 08:57:43 +0200 | [diff] [blame] | 142 | { .compatible = "nxp,pca9543", .data = PCA9543 }, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 143 | { .compatible = "nxp,pca9544", .data = PCA9544 }, |
Chris Packham | 0b1d7b7 | 2020-04-01 15:55:27 +1300 | [diff] [blame] | 144 | { .compatible = "nxp,pca9546", .data = PCA9546 }, |
Marek Behún | 8e6eda7 | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 145 | { .compatible = "nxp,pca9547", .data = PCA9547 }, |
| 146 | { .compatible = "nxp,pca9548", .data = PCA9548 }, |
Peng Fan | 16f513e | 2018-07-17 20:38:32 +0800 | [diff] [blame] | 147 | { .compatible = "nxp,pca9646", .data = PCA9646 }, |
Vladimir Oltean | 3285a57 | 2022-01-03 14:47:22 +0200 | [diff] [blame] | 148 | { .compatible = "nxp,pca9847", .data = PCA9847 }, |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 149 | { } |
| 150 | }; |
| 151 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 152 | static int pca954x_of_to_plat(struct udevice *dev) |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 153 | { |
| 154 | struct pca954x_priv *priv = dev_get_priv(dev); |
Chris Packham | 5bc90a8 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 155 | const struct chip_desc *chip = &chips[dev_get_driver_data(dev)]; |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 156 | |
Michal Simek | 58dc4a9 | 2019-01-09 11:58:24 +0100 | [diff] [blame] | 157 | priv->addr = dev_read_u32_default(dev, "reg", 0); |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 158 | if (!priv->addr) { |
| 159 | debug("MUX not found\n"); |
| 160 | return -ENODEV; |
| 161 | } |
Chris Packham | 5bc90a8 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 162 | priv->width = chip->width; |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 163 | |
| 164 | if (!priv->width) { |
| 165 | debug("No I2C MUX width specified\n"); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | debug("Device %s at 0x%x with width %d\n", |
| 170 | dev->name, priv->addr, priv->width); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Moritz Fischer | 36bdeb7 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 175 | static int pca954x_probe(struct udevice *dev) |
| 176 | { |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 177 | if (CONFIG_IS_ENABLED(DM_GPIO)) { |
Moritz Fischer | 36bdeb7 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 178 | struct pca954x_priv *priv = dev_get_priv(dev); |
| 179 | int err; |
| 180 | |
| 181 | err = gpio_request_by_name(dev, "reset-gpios", 0, |
| 182 | &priv->gpio_mux_reset, GPIOD_IS_OUT); |
| 183 | |
| 184 | /* it's optional so only bail if we get a real error */ |
| 185 | if (err && (err != -ENOENT)) |
| 186 | return err; |
| 187 | |
| 188 | /* dm will take care of polarity */ |
| 189 | if (dm_gpio_is_valid(&priv->gpio_mux_reset)) |
| 190 | dm_gpio_set_value(&priv->gpio_mux_reset, 0); |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int pca954x_remove(struct udevice *dev) |
| 197 | { |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 198 | if (CONFIG_IS_ENABLED(DM_GPIO)) { |
Moritz Fischer | 36bdeb7 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 199 | struct pca954x_priv *priv = dev_get_priv(dev); |
| 200 | |
| 201 | if (dm_gpio_is_valid(&priv->gpio_mux_reset)) |
| 202 | dm_gpio_free(dev, &priv->gpio_mux_reset); |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 208 | U_BOOT_DRIVER(pca954x) = { |
| 209 | .name = "pca954x", |
| 210 | .id = UCLASS_I2C_MUX, |
| 211 | .of_match = pca954x_ids, |
Moritz Fischer | 36bdeb7 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 212 | .probe = pca954x_probe, |
| 213 | .remove = pca954x_remove, |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 214 | .ops = &pca954x_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 215 | .of_to_plat = pca954x_of_to_plat, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 216 | .priv_auto = sizeof(struct pca954x_priv), |
Michal Simek | d79ac32 | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 217 | }; |