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