1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021, Collabora Ltd.
4 * Copyright (C) 2021, General Electric Company
5 * Author(s): Sebastian Reichel <sebastian.reichel@collabora.com>
8 #define LOG_CATEGORY UCLASS_GPIO
15 #include <dm/device_compat.h>
16 #include <dt-bindings/gpio/gpio.h>
25 #define MCP230XX_IODIR 0x00
26 #define MCP230XX_GPPU 0x06
27 #define MCP230XX_GPIO 0x09
28 #define MCP230XX_OLAT 0x0a
32 static int mcp230xx_read(struct udevice *dev, uint reg, uint offset)
34 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
35 int bank = offset / BANKSIZE;
36 int mask = 1 << (offset % BANKSIZE);
37 int shift = (uc_priv->gpio_count / BANKSIZE) - 1;
40 ret = dm_i2c_reg_read(dev, (reg << shift) | bank);
44 return !!(ret & mask);
47 static int mcp230xx_write(struct udevice *dev, uint reg, uint offset, bool val)
49 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
50 int bank = offset / BANKSIZE;
51 int mask = 1 << (offset % BANKSIZE);
52 int shift = (uc_priv->gpio_count / BANKSIZE) - 1;
54 return dm_i2c_reg_clrset(dev, (reg << shift) | bank, mask, val ? mask : 0);
57 static int mcp230xx_get_value(struct udevice *dev, uint offset)
61 ret = mcp230xx_read(dev, MCP230XX_GPIO, offset);
63 dev_err(dev, "%s error: %d\n", __func__, ret);
70 static int mcp230xx_set_value(struct udevice *dev, uint offset, int val)
74 ret = mcp230xx_write(dev, MCP230XX_GPIO, offset, val);
76 dev_err(dev, "%s error: %d\n", __func__, ret);
83 static int mcp230xx_get_flags(struct udevice *dev, unsigned int offset,
86 int direction, pullup;
88 pullup = mcp230xx_read(dev, MCP230XX_GPPU, offset);
90 dev_err(dev, "%s error: %d\n", __func__, pullup);
94 direction = mcp230xx_read(dev, MCP230XX_IODIR, offset);
96 dev_err(dev, "%s error: %d\n", __func__, direction);
100 *flags = direction ? GPIOD_IS_IN : GPIOD_IS_OUT;
103 *flags |= GPIOD_PULL_UP;
108 static int mcp230xx_set_flags(struct udevice *dev, uint offset, ulong flags)
110 bool input = !(flags & (GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE));
111 bool pullup = flags & GPIOD_PULL_UP;
112 ulong supported_mask;
115 /* Note: active-low is ignored (handled by core) */
116 supported_mask = GPIOD_ACTIVE_LOW | GPIOD_MASK_DIR | GPIOD_PULL_UP;
117 if (flags & ~supported_mask) {
118 dev_err(dev, "%s unsupported flag(s): %lx\n", __func__, flags);
122 ret = mcp230xx_write(dev, MCP230XX_OLAT, offset, !!(flags & GPIOD_IS_OUT_ACTIVE));
124 dev_err(dev, "%s failed to setup output latch: %d\n", __func__, ret);
128 ret = mcp230xx_write(dev, MCP230XX_GPPU, offset, pullup);
130 dev_err(dev, "%s failed to setup pull-up: %d\n", __func__, ret);
134 ret = mcp230xx_write(dev, MCP230XX_IODIR, offset, input);
136 dev_err(dev, "%s failed to setup direction: %d\n", __func__, ret);
143 static int mcp230xx_direction_input(struct udevice *dev, uint offset)
145 return mcp230xx_set_flags(dev, offset, GPIOD_IS_IN);
148 static int mcp230xx_direction_output(struct udevice *dev, uint offset, int val)
150 int ret = mcp230xx_set_value(dev, offset, val);
152 dev_err(dev, "%s error: %d\n", __func__, ret);
155 return mcp230xx_set_flags(dev, offset, GPIOD_IS_OUT);
158 static int mcp230xx_get_function(struct udevice *dev, uint offset)
162 ret = mcp230xx_read(dev, MCP230XX_IODIR, offset);
164 dev_err(dev, "%s error: %d\n", __func__, ret);
168 return ret ? GPIOF_INPUT : GPIOF_OUTPUT;
171 static const struct dm_gpio_ops mcp230xx_ops = {
172 .direction_input = mcp230xx_direction_input,
173 .direction_output = mcp230xx_direction_output,
174 .get_value = mcp230xx_get_value,
175 .set_value = mcp230xx_set_value,
176 .get_function = mcp230xx_get_function,
177 .set_flags = mcp230xx_set_flags,
178 .get_flags = mcp230xx_get_flags,
181 static int mcp230xx_probe(struct udevice *dev)
183 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
184 char name[32], label[8], *str;
185 int addr, gpio_count, size;
188 switch (dev_get_driver_data(dev)) {
200 addr = dev_read_addr(dev);
201 tmp = dev_read_prop(dev, "label", &size);
203 memcpy(label, tmp, sizeof(label) - 1);
204 label[sizeof(label) - 1] = '\0';
205 snprintf(name, sizeof(name), "%s@%x_", label, addr);
207 snprintf(name, sizeof(name), "gpio@%x_", addr);
214 uc_priv->bank_name = str;
215 uc_priv->gpio_count = gpio_count;
217 dev_dbg(dev, "%s is ready\n", str);
222 static const struct udevice_id mcp230xx_ids[] = {
223 { .compatible = "microchip,mcp23008", .data = MCP23008, },
224 { .compatible = "microchip,mcp23017", .data = MCP23017, },
225 { .compatible = "microchip,mcp23018", .data = MCP23018, },
229 U_BOOT_DRIVER(mcp230xx) = {
232 .ops = &mcp230xx_ops,
233 .probe = mcp230xx_probe,
234 .of_match = mcp230xx_ids,