1 // SPDX-License-Identifier: GPL-2.0+
5 * ADP5585 I/O Expander Controller
12 #include <dt-bindings/gpio/gpio.h>
15 #define ADP5585_ID 0x00
16 #define ADP5585_INT_STATUS 0x01
17 #define ADP5585_STATUS 0x02
18 #define ADP5585_FIFO_1 0x03
19 #define ADP5585_FIFO_2 0x04
20 #define ADP5585_FIFO_3 0x05
21 #define ADP5585_FIFO_4 0x06
22 #define ADP5585_FIFO_5 0x07
23 #define ADP5585_FIFO_6 0x08
24 #define ADP5585_FIFO_7 0x09
25 #define ADP5585_FIFO_8 0x0A
26 #define ADP5585_FIFO_9 0x0B
27 #define ADP5585_FIFO_10 0x0C
28 #define ADP5585_FIFO_11 0x0D
29 #define ADP5585_FIFO_12 0x0E
30 #define ADP5585_FIFO_13 0x0F
31 #define ADP5585_FIFO_14 0x10
32 #define ADP5585_FIFO_15 0x11
33 #define ADP5585_FIFO_16 0x12
34 #define ADP5585_GPI_INT_STAT_A 0x13
35 #define ADP5585_GPI_INT_STAT_B 0x14
36 #define ADP5585_GPI_STATUS_A 0x15
37 #define ADP5585_GPI_STATUS_B 0x16
38 #define ADP5585_RPULL_CONFIG_A 0x17
39 #define ADP5585_RPULL_CONFIG_B 0x18
40 #define ADP5585_RPULL_CONFIG_C 0x19
41 #define ADP5585_RPULL_CONFIG_D 0x1A
42 #define ADP5585_GPI_INT_LEVEL_A 0x1B
43 #define ADP5585_GPI_INT_LEVEL_B 0x1C
44 #define ADP5585_GPI_EVENT_EN_A 0x1D
45 #define ADP5585_GPI_EVENT_EN_B 0x1E
46 #define ADP5585_GPI_INTERRUPT_EN_A 0x1F
47 #define ADP5585_GPI_INTERRUPT_EN_B 0x20
48 #define ADP5585_DEBOUNCE_DIS_A 0x21
49 #define ADP5585_DEBOUNCE_DIS_B 0x22
50 #define ADP5585_GPO_DATA_OUT_A 0x23
51 #define ADP5585_GPO_DATA_OUT_B 0x24
52 #define ADP5585_GPO_OUT_MODE_A 0x25
53 #define ADP5585_GPO_OUT_MODE_B 0x26
54 #define ADP5585_GPIO_DIRECTION_A 0x27
55 #define ADP5585_GPIO_DIRECTION_B 0x28
56 #define ADP5585_RESET1_EVENT_A 0x29
57 #define ADP5585_RESET1_EVENT_B 0x2A
58 #define ADP5585_RESET1_EVENT_C 0x2B
59 #define ADP5585_RESET2_EVENT_A 0x2C
60 #define ADP5585_RESET2_EVENT_B 0x2D
61 #define ADP5585_RESET_CFG 0x2E
62 #define ADP5585_PWM_OFFT_LOW 0x2F
63 #define ADP5585_PWM_OFFT_HIGH 0x30
64 #define ADP5585_PWM_ONT_LOW 0x31
65 #define ADP5585_PWM_ONT_HIGH 0x32
66 #define ADP5585_PWM_CFG 0x33
67 #define ADP5585_LOGIC_CFG 0x34
68 #define ADP5585_LOGIC_FF_CFG 0x35
69 #define ADP5585_LOGIC_INT_EVENT_EN 0x36
70 #define ADP5585_POLL_PTIME_CFG 0x37
71 #define ADP5585_PIN_CONFIG_A 0x38
72 #define ADP5585_PIN_CONFIG_B 0x39
73 #define ADP5585_PIN_CONFIG_D 0x3A
74 #define ADP5585_GENERAL_CFG 0x3B
75 #define ADP5585_INT_EN 0x3C
77 #define ADP5585_MAXGPIO 10
78 #define ADP5585_BANK(offs) ((offs) > 4)
79 #define ADP5585_BIT(offs) ((offs) > 4 ? \
80 1u << ((offs) - 5) : 1u << (offs))
89 static int adp5585_direction_input(struct udevice *dev, unsigned int offset)
93 struct adp5585_plat *plat = dev_get_plat(dev);
95 bank = ADP5585_BANK(offset);
97 plat->dir[bank] &= ~ADP5585_BIT(offset);
98 ret = dm_i2c_write(dev, ADP5585_GPIO_DIRECTION_A + bank, &plat->dir[bank], 1);
103 static int adp5585_direction_output(struct udevice *dev, unsigned int offset,
107 unsigned int bank, bit;
108 struct adp5585_plat *plat = dev_get_plat(dev);
110 bank = ADP5585_BANK(offset);
111 bit = ADP5585_BIT(offset);
113 plat->dir[bank] |= bit;
116 plat->dat_out[bank] |= bit;
118 plat->dat_out[bank] &= ~bit;
120 ret = dm_i2c_write(dev, ADP5585_GPO_DATA_OUT_A + bank, &plat->dat_out[bank], 1);
121 ret |= dm_i2c_write(dev, ADP5585_GPIO_DIRECTION_A + bank, &plat->dir[bank], 1);
126 static int adp5585_get_value(struct udevice *dev, unsigned int offset)
128 struct adp5585_plat *plat = dev_get_plat(dev);
129 unsigned int bank = ADP5585_BANK(offset);
130 unsigned int bit = ADP5585_BIT(offset);
133 if (plat->dir[bank] & bit)
134 val = plat->dat_out[bank];
136 dm_i2c_read(dev, ADP5585_GPI_STATUS_A + bank, &val, 1);
138 return !!(val & bit);
141 static int adp5585_set_value(struct udevice *dev, unsigned int offset, int value)
144 unsigned int bank, bit;
145 struct adp5585_plat *plat = dev_get_plat(dev);
147 bank = ADP5585_BANK(offset);
148 bit = ADP5585_BIT(offset);
151 plat->dat_out[bank] |= bit;
153 plat->dat_out[bank] &= ~bit;
155 ret = dm_i2c_write(dev, ADP5585_GPO_DATA_OUT_A + bank, &plat->dat_out[bank], 1);
160 static int adp5585_get_function(struct udevice *dev, unsigned int offset)
162 unsigned int bank, bit, dir;
163 struct adp5585_plat *plat = dev_get_plat(dev);
165 bank = ADP5585_BANK(offset);
166 bit = ADP5585_BIT(offset);
167 dir = plat->dir[bank] & bit;
175 static int adp5585_xlate(struct udevice *dev, struct gpio_desc *desc,
176 struct ofnode_phandle_args *args)
178 desc->offset = args->args[0];
179 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
184 static const struct dm_gpio_ops adp5585_ops = {
185 .direction_input = adp5585_direction_input,
186 .direction_output = adp5585_direction_output,
187 .get_value = adp5585_get_value,
188 .set_value = adp5585_set_value,
189 .get_function = adp5585_get_function,
190 .xlate = adp5585_xlate,
193 static int adp5585_probe(struct udevice *dev)
195 struct adp5585_plat *plat = dev_get_plat(dev);
196 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
202 plat->addr = dev_read_addr(dev);
203 if (plat->addr == FDT_ADDR_T_NONE)
206 ret = dm_i2c_read(dev, ADP5585_ID, &plat->id, 1);
210 uc_priv->gpio_count = ADP5585_MAXGPIO;
211 uc_priv->bank_name = "adp5585-gpio";
213 for (int i = 0; i < 2; i++) {
214 ret = dm_i2c_read(dev, ADP5585_GPO_DATA_OUT_A + i, &plat->dat_out[i], 1);
218 ret = dm_i2c_read(dev, ADP5585_GPIO_DIRECTION_A + i, &plat->dir[i], 1);
226 static const struct udevice_id adp5585_ids[] = {
227 { .compatible = "adp5585" },
231 U_BOOT_DRIVER(adp5585) = {
234 .of_match = adp5585_ids,
235 .probe = adp5585_probe,
237 .plat_auto = sizeof(struct adp5585_plat),