Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (C) 2021, Collabora Ltd. |
| 4 | * Copyright (C) 2021, General Electric Company |
| 5 | * Author(s): Sebastian Reichel <sebastian.reichel@collabora.com> |
| 6 | */ |
| 7 | |
| 8 | #define LOG_CATEGORY UCLASS_GPIO |
| 9 | |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <dm.h> |
| 12 | #include <i2c.h> |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 13 | #include <spi.h> |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 14 | #include <asm/gpio.h> |
| 15 | #include <dm/device_compat.h> |
| 16 | #include <dt-bindings/gpio/gpio.h> |
| 17 | |
| 18 | enum mcp230xx_type { |
| 19 | UNKNOWN = 0, |
| 20 | MCP23008, |
| 21 | MCP23017, |
| 22 | MCP23018, |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 23 | MCP23S08, |
| 24 | MCP23S17, |
| 25 | MCP23S18, |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 28 | struct mcp230xx_info { |
| 29 | uint dev_addr; |
| 30 | }; |
| 31 | |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 32 | #define MCP230XX_IODIR 0x00 |
| 33 | #define MCP230XX_GPPU 0x06 |
| 34 | #define MCP230XX_GPIO 0x09 |
| 35 | #define MCP230XX_OLAT 0x0a |
| 36 | |
| 37 | #define BANKSIZE 8 |
| 38 | |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 39 | #define MCP230XX_ADDR 0x20 |
| 40 | |
| 41 | static int mcp230xx_read_spi(struct udevice *dev, uint reg_addr) |
| 42 | { |
| 43 | struct mcp230xx_info *info = dev_get_plat(dev); |
| 44 | uint dev_addr, value = 0; |
| 45 | int ret; |
| 46 | |
| 47 | /* set R/W bit for reading */ |
| 48 | dev_addr = (info->dev_addr << 1) | 1; |
| 49 | |
| 50 | ret = dm_spi_claim_bus(dev); |
| 51 | if (ret) |
| 52 | return ret; |
| 53 | |
| 54 | ret = dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_BEGIN); |
| 55 | if (ret < 0) |
| 56 | goto fail; |
| 57 | udelay(1); |
| 58 | |
| 59 | ret = dm_spi_xfer(dev, 8, &dev_addr, NULL, 0); |
| 60 | if (ret < 0) |
| 61 | goto fail; |
| 62 | |
| 63 | ret = dm_spi_xfer(dev, 8, ®_addr, NULL, 0); |
| 64 | if (ret < 0) |
| 65 | goto fail; |
| 66 | |
| 67 | ret = dm_spi_xfer(dev, 8, NULL, &value, 0); |
| 68 | |
| 69 | fail: |
| 70 | dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END); |
| 71 | dm_spi_release_bus(dev); |
| 72 | if (ret < 0) |
| 73 | return ret; |
| 74 | return value; |
| 75 | } |
| 76 | |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 77 | static int mcp230xx_read(struct udevice *dev, uint reg, uint offset) |
| 78 | { |
| 79 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 80 | int bank = offset / BANKSIZE; |
| 81 | int mask = 1 << (offset % BANKSIZE); |
| 82 | int shift = (uc_priv->gpio_count / BANKSIZE) - 1; |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 83 | int reg_addr = (reg << shift) | bank; |
| 84 | int ret = 0; |
| 85 | |
| 86 | switch (dev_get_driver_data(dev)) { |
| 87 | case MCP23008: |
| 88 | case MCP23017: |
| 89 | case MCP23018: |
| 90 | ret = dm_i2c_reg_read(dev, reg_addr); |
| 91 | break; |
| 92 | case MCP23S08: |
| 93 | case MCP23S17: |
| 94 | case MCP23S18: |
| 95 | ret = mcp230xx_read_spi(dev, reg_addr); |
| 96 | break; |
| 97 | default: |
| 98 | return -ENODEV; |
| 99 | } |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 100 | |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | |
| 104 | return !!(ret & mask); |
| 105 | } |
| 106 | |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 107 | static int mcp230xx_clrset_spi(struct udevice *dev, uint reg_addr, uint clr, uint set) |
| 108 | { |
| 109 | struct mcp230xx_info *info = dev_get_plat(dev); |
| 110 | int dev_addr, value; |
| 111 | int ret; |
| 112 | |
| 113 | /* R/W bit = 0 for writing */ |
| 114 | dev_addr = (info->dev_addr << 1); |
| 115 | |
| 116 | ret = mcp230xx_read_spi(dev, reg_addr); |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | |
| 120 | value = ret; |
| 121 | value &= ~clr; |
| 122 | value |= set; |
| 123 | |
| 124 | ret = dm_spi_claim_bus(dev); |
| 125 | if (ret) |
| 126 | return ret; |
| 127 | |
| 128 | ret = dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_BEGIN); |
| 129 | if (ret < 0) |
| 130 | goto fail; |
| 131 | udelay(1); |
| 132 | |
| 133 | ret = dm_spi_xfer(dev, 8, &dev_addr, NULL, 0); |
| 134 | if (ret < 0) |
| 135 | goto fail; |
| 136 | |
| 137 | ret = dm_spi_xfer(dev, 8, ®_addr, NULL, 0); |
| 138 | if (ret < 0) |
| 139 | goto fail; |
| 140 | |
| 141 | ret = dm_spi_xfer(dev, 8, &value, NULL, 0); |
| 142 | |
| 143 | fail: |
| 144 | dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END); |
| 145 | dm_spi_release_bus(dev); |
| 146 | return ret; |
| 147 | } |
| 148 | |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 149 | static int mcp230xx_write(struct udevice *dev, uint reg, uint offset, bool val) |
| 150 | { |
| 151 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 152 | int bank = offset / BANKSIZE; |
| 153 | int mask = 1 << (offset % BANKSIZE); |
| 154 | int shift = (uc_priv->gpio_count / BANKSIZE) - 1; |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 155 | int reg_addr = (reg << shift) | bank; |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 156 | |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 157 | switch (dev_get_driver_data(dev)) { |
| 158 | case MCP23008: |
| 159 | case MCP23017: |
| 160 | case MCP23018: |
| 161 | return dm_i2c_reg_clrset(dev, reg_addr, mask, val ? mask : 0); |
| 162 | case MCP23S08: |
| 163 | case MCP23S17: |
| 164 | case MCP23S18: |
| 165 | return mcp230xx_clrset_spi(dev, reg_addr, mask, val ? mask : 0); |
| 166 | default: |
| 167 | return -ENODEV; |
| 168 | } |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static int mcp230xx_get_value(struct udevice *dev, uint offset) |
| 172 | { |
| 173 | int ret; |
| 174 | |
| 175 | ret = mcp230xx_read(dev, MCP230XX_GPIO, offset); |
| 176 | if (ret < 0) { |
| 177 | dev_err(dev, "%s error: %d\n", __func__, ret); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int mcp230xx_set_value(struct udevice *dev, uint offset, int val) |
| 185 | { |
| 186 | int ret; |
| 187 | |
| 188 | ret = mcp230xx_write(dev, MCP230XX_GPIO, offset, val); |
| 189 | if (ret < 0) { |
| 190 | dev_err(dev, "%s error: %d\n", __func__, ret); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static int mcp230xx_get_flags(struct udevice *dev, unsigned int offset, |
| 198 | ulong *flags) |
| 199 | { |
| 200 | int direction, pullup; |
| 201 | |
| 202 | pullup = mcp230xx_read(dev, MCP230XX_GPPU, offset); |
| 203 | if (pullup < 0) { |
| 204 | dev_err(dev, "%s error: %d\n", __func__, pullup); |
| 205 | return pullup; |
| 206 | } |
| 207 | |
| 208 | direction = mcp230xx_read(dev, MCP230XX_IODIR, offset); |
| 209 | if (direction < 0) { |
| 210 | dev_err(dev, "%s error: %d\n", __func__, direction); |
| 211 | return direction; |
| 212 | } |
| 213 | |
| 214 | *flags = direction ? GPIOD_IS_IN : GPIOD_IS_OUT; |
| 215 | |
| 216 | if (pullup) |
| 217 | *flags |= GPIOD_PULL_UP; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int mcp230xx_set_flags(struct udevice *dev, uint offset, ulong flags) |
| 223 | { |
| 224 | bool input = !(flags & (GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE)); |
| 225 | bool pullup = flags & GPIOD_PULL_UP; |
| 226 | ulong supported_mask; |
| 227 | int ret; |
| 228 | |
| 229 | /* Note: active-low is ignored (handled by core) */ |
| 230 | supported_mask = GPIOD_ACTIVE_LOW | GPIOD_MASK_DIR | GPIOD_PULL_UP; |
| 231 | if (flags & ~supported_mask) { |
| 232 | dev_err(dev, "%s unsupported flag(s): %lx\n", __func__, flags); |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | ret = mcp230xx_write(dev, MCP230XX_OLAT, offset, !!(flags & GPIOD_IS_OUT_ACTIVE)); |
| 237 | if (ret) { |
| 238 | dev_err(dev, "%s failed to setup output latch: %d\n", __func__, ret); |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | ret = mcp230xx_write(dev, MCP230XX_GPPU, offset, pullup); |
| 243 | if (ret) { |
| 244 | dev_err(dev, "%s failed to setup pull-up: %d\n", __func__, ret); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | ret = mcp230xx_write(dev, MCP230XX_IODIR, offset, input); |
| 249 | if (ret) { |
| 250 | dev_err(dev, "%s failed to setup direction: %d\n", __func__, ret); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int mcp230xx_direction_input(struct udevice *dev, uint offset) |
| 258 | { |
| 259 | return mcp230xx_set_flags(dev, offset, GPIOD_IS_IN); |
| 260 | } |
| 261 | |
| 262 | static int mcp230xx_direction_output(struct udevice *dev, uint offset, int val) |
| 263 | { |
| 264 | int ret = mcp230xx_set_value(dev, offset, val); |
| 265 | if (ret < 0) { |
| 266 | dev_err(dev, "%s error: %d\n", __func__, ret); |
| 267 | return ret; |
| 268 | } |
| 269 | return mcp230xx_set_flags(dev, offset, GPIOD_IS_OUT); |
| 270 | } |
| 271 | |
| 272 | static int mcp230xx_get_function(struct udevice *dev, uint offset) |
| 273 | { |
| 274 | int ret; |
| 275 | |
| 276 | ret = mcp230xx_read(dev, MCP230XX_IODIR, offset); |
| 277 | if (ret < 0) { |
| 278 | dev_err(dev, "%s error: %d\n", __func__, ret); |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | return ret ? GPIOF_INPUT : GPIOF_OUTPUT; |
| 283 | } |
| 284 | |
| 285 | static const struct dm_gpio_ops mcp230xx_ops = { |
| 286 | .direction_input = mcp230xx_direction_input, |
| 287 | .direction_output = mcp230xx_direction_output, |
| 288 | .get_value = mcp230xx_get_value, |
| 289 | .set_value = mcp230xx_set_value, |
| 290 | .get_function = mcp230xx_get_function, |
| 291 | .set_flags = mcp230xx_set_flags, |
| 292 | .get_flags = mcp230xx_get_flags, |
| 293 | }; |
| 294 | |
| 295 | static int mcp230xx_probe(struct udevice *dev) |
| 296 | { |
| 297 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 298 | struct mcp230xx_info *info = dev_get_plat(dev); |
| 299 | char name[32], label[32], *str; |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 300 | int addr, gpio_count, size; |
| 301 | const u8 *tmp; |
| 302 | |
| 303 | switch (dev_get_driver_data(dev)) { |
| 304 | case MCP23008: |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 305 | case MCP23S08: |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 306 | gpio_count = 8; |
| 307 | break; |
| 308 | case MCP23017: |
| 309 | case MCP23018: |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 310 | case MCP23S17: |
| 311 | case MCP23S18: |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 312 | gpio_count = 16; |
| 313 | break; |
| 314 | default: |
| 315 | return -ENODEV; |
| 316 | } |
| 317 | |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 318 | switch (dev_get_driver_data(dev)) { |
| 319 | case MCP23S08: |
| 320 | case MCP23S17: |
| 321 | case MCP23S18: |
| 322 | info->dev_addr = dev_read_u32_default(dev, "addr", MCP230XX_ADDR); |
| 323 | break; |
| 324 | default: |
| 325 | info->dev_addr = 0; |
| 326 | break; |
| 327 | } |
| 328 | |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 329 | addr = dev_read_addr(dev); |
| 330 | tmp = dev_read_prop(dev, "label", &size); |
| 331 | if (tmp) { |
| 332 | memcpy(label, tmp, sizeof(label) - 1); |
| 333 | label[sizeof(label) - 1] = '\0'; |
| 334 | snprintf(name, sizeof(name), "%s@%x_", label, addr); |
| 335 | } else { |
| 336 | snprintf(name, sizeof(name), "gpio@%x_", addr); |
| 337 | } |
| 338 | |
| 339 | str = strdup(name); |
| 340 | if (!str) |
| 341 | return -ENOMEM; |
| 342 | |
| 343 | uc_priv->bank_name = str; |
| 344 | uc_priv->gpio_count = gpio_count; |
| 345 | |
| 346 | dev_dbg(dev, "%s is ready\n", str); |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static const struct udevice_id mcp230xx_ids[] = { |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 352 | /* i2c interface */ |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 353 | { .compatible = "microchip,mcp23008", .data = MCP23008, }, |
| 354 | { .compatible = "microchip,mcp23017", .data = MCP23017, }, |
| 355 | { .compatible = "microchip,mcp23018", .data = MCP23018, }, |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 356 | /* spi interface */ |
| 357 | { .compatible = "microchip,mcp23s08", .data = MCP23S08, }, |
| 358 | { .compatible = "microchip,mcp23s17", .data = MCP23S17, }, |
| 359 | { .compatible = "microchip,mcp23s18", .data = MCP23S18, }, |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 360 | { } |
| 361 | }; |
| 362 | |
| 363 | U_BOOT_DRIVER(mcp230xx) = { |
| 364 | .name = "mcp230xx", |
| 365 | .id = UCLASS_GPIO, |
| 366 | .ops = &mcp230xx_ops, |
| 367 | .probe = mcp230xx_probe, |
Piotr Wojtaszczyk | 9050d04 | 2024-03-13 12:54:56 +0100 | [diff] [blame] | 368 | .plat_auto = sizeof(struct mcp230xx_info), |
Sebastian Reichel | d761e82 | 2021-07-15 17:40:00 +0200 | [diff] [blame] | 369 | .of_match = mcp230xx_ids, |
| 370 | }; |