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, |
| 26 | PCA9646 |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct chip_desc { |
Luca Ceresoli | eb89396 | 2019-04-09 08:57:42 +0200 | [diff] [blame] | 30 | u8 enable; /* Enable mask in ctl register (used for muxes only) */ |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 31 | enum muxtype { |
| 32 | pca954x_ismux = 0, |
| 33 | pca954x_isswi, |
| 34 | } muxtype; |
Chris Packham | 05c9326 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 35 | u32 width; |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 38 | struct pca954x_priv { |
| 39 | u32 addr; /* I2C mux address */ |
| 40 | u32 width; /* I2C mux width - number of busses */ |
Moritz Fischer | 8238dd9 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 41 | struct gpio_desc gpio_mux_reset; |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 44 | static const struct chip_desc chips[] = { |
Luca Ceresoli | eed01398 | 2019-04-09 08:57:43 +0200 | [diff] [blame] | 45 | [PCA9543] = { |
| 46 | .muxtype = pca954x_isswi, |
| 47 | .width = 2, |
| 48 | }, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 49 | [PCA9544] = { |
| 50 | .enable = 0x4, |
| 51 | .muxtype = pca954x_ismux, |
Chris Packham | 05c9326 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 52 | .width = 4, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 53 | }, |
Chris Packham | 3e0259d | 2020-04-01 15:55:27 +1300 | [diff] [blame] | 54 | [PCA9546] = { |
| 55 | .muxtype = pca954x_isswi, |
| 56 | .width = 4, |
| 57 | }, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 58 | [PCA9547] = { |
| 59 | .enable = 0x8, |
| 60 | .muxtype = pca954x_ismux, |
Chris Packham | 05c9326 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 61 | .width = 8, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 62 | }, |
| 63 | [PCA9548] = { |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 64 | .muxtype = pca954x_isswi, |
Chris Packham | 05c9326 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 65 | .width = 8, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 66 | }, |
Peng Fan | e4ddfdb | 2018-07-17 20:38:32 +0800 | [diff] [blame] | 67 | [PCA9646] = { |
Peng Fan | e4ddfdb | 2018-07-17 20:38:32 +0800 | [diff] [blame] | 68 | .muxtype = pca954x_isswi, |
| 69 | .width = 4, |
| 70 | }, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 73 | static 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 | |
| 82 | static int pca954x_select(struct udevice *mux, struct udevice *bus, |
| 83 | uint channel) |
| 84 | { |
| 85 | struct pca954x_priv *priv = dev_get_priv(mux); |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 86 | 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 Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 93 | |
| 94 | return dm_i2c_write(mux, priv->addr, &byte, 1); |
| 95 | } |
| 96 | |
| 97 | static const struct i2c_mux_ops pca954x_ops = { |
| 98 | .select = pca954x_select, |
| 99 | .deselect = pca954x_deselect, |
| 100 | }; |
| 101 | |
| 102 | static const struct udevice_id pca954x_ids[] = { |
Luca Ceresoli | eed01398 | 2019-04-09 08:57:43 +0200 | [diff] [blame] | 103 | { .compatible = "nxp,pca9543", .data = PCA9543 }, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 104 | { .compatible = "nxp,pca9544", .data = PCA9544 }, |
Chris Packham | 3e0259d | 2020-04-01 15:55:27 +1300 | [diff] [blame] | 105 | { .compatible = "nxp,pca9546", .data = PCA9546 }, |
Marek Behún | 267ae1c | 2017-06-09 19:28:43 +0200 | [diff] [blame] | 106 | { .compatible = "nxp,pca9547", .data = PCA9547 }, |
| 107 | { .compatible = "nxp,pca9548", .data = PCA9548 }, |
Peng Fan | e4ddfdb | 2018-07-17 20:38:32 +0800 | [diff] [blame] | 108 | { .compatible = "nxp,pca9646", .data = PCA9646 }, |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 109 | { } |
| 110 | }; |
| 111 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 112 | static int pca954x_of_to_plat(struct udevice *dev) |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 113 | { |
| 114 | struct pca954x_priv *priv = dev_get_priv(dev); |
Chris Packham | 05c9326 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 115 | const struct chip_desc *chip = &chips[dev_get_driver_data(dev)]; |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 116 | |
Michal Simek | d5c97ac | 2019-01-09 11:58:24 +0100 | [diff] [blame] | 117 | priv->addr = dev_read_u32_default(dev, "reg", 0); |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 118 | if (!priv->addr) { |
| 119 | debug("MUX not found\n"); |
| 120 | return -ENODEV; |
| 121 | } |
Chris Packham | 05c9326 | 2017-09-29 10:53:36 +1300 | [diff] [blame] | 122 | priv->width = chip->width; |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 123 | |
| 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 Fischer | 8238dd9 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 135 | static int pca954x_probe(struct udevice *dev) |
| 136 | { |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 137 | if (CONFIG_IS_ENABLED(DM_GPIO)) { |
Moritz Fischer | 8238dd9 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 138 | 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 | |
| 156 | static int pca954x_remove(struct udevice *dev) |
| 157 | { |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 158 | if (CONFIG_IS_ENABLED(DM_GPIO)) { |
Moritz Fischer | 8238dd9 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 159 | 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 Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 168 | U_BOOT_DRIVER(pca954x) = { |
| 169 | .name = "pca954x", |
| 170 | .id = UCLASS_I2C_MUX, |
| 171 | .of_match = pca954x_ids, |
Moritz Fischer | 8238dd9 | 2017-09-12 06:46:59 -0700 | [diff] [blame] | 172 | .probe = pca954x_probe, |
| 173 | .remove = pca954x_remove, |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 174 | .ops = &pca954x_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 175 | .of_to_plat = pca954x_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 176 | .priv_auto = sizeof(struct pca954x_priv), |
Michal Simek | 81aaacc | 2016-04-25 10:50:42 +0200 | [diff] [blame] | 177 | }; |