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