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