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