1 // SPDX-License-Identifier: GPL-2.0+
3 * GPIO driver for the sl28cpld
5 * Copyright (c) 2021 Michael Walle <michael@walle.cc>
14 #define SL28CPLD_GPIO_DIR 0x00
15 #define SL28CPLD_GPIO_OUT 0x01
16 #define SL28CPLD_GPIO_IN 0x02
18 /* input-only flavor */
19 #define SL28CPLD_GPI_IN 0x00
21 /* output-only flavor */
22 #define SL28CPLD_GPO_OUT 0x00
30 static int sl28cpld_gpio_get_value(struct udevice *dev, unsigned int gpio)
32 ulong type = dev_get_driver_data(dev);
37 reg = SL28CPLD_GPIO_IN;
40 reg = SL28CPLD_GPI_IN;
43 /* we are output only, thus just return the output value */
44 reg = SL28CPLD_GPO_OUT;
50 val = sl28cpld_read(dev, reg);
52 return val < 0 ? val : !!(val & BIT(gpio));
55 static int sl28cpld_gpio_set_value(struct udevice *dev, unsigned int gpio,
58 ulong type = dev_get_driver_data(dev);
63 reg = SL28CPLD_GPIO_OUT;
66 reg = SL28CPLD_GPO_OUT;
74 return sl28cpld_update(dev, reg, 0, BIT(gpio));
76 return sl28cpld_update(dev, reg, BIT(gpio), 0);
79 static int sl28cpld_gpio_direction_input(struct udevice *dev, unsigned int gpio)
81 ulong type = dev_get_driver_data(dev);
87 return sl28cpld_update(dev, SL28CPLD_GPIO_DIR, BIT(gpio), 0);
94 static int sl28cpld_gpio_direction_output(struct udevice *dev,
95 unsigned int gpio, int value)
97 ulong type = dev_get_driver_data(dev);
100 /* set_value() will report an error if we are input-only */
101 ret = sl28cpld_gpio_set_value(dev, gpio, value);
105 if (type == SL28CPLD_GPIO)
106 return sl28cpld_update(dev, SL28CPLD_GPIO_DIR, 0, BIT(gpio));
111 static int sl28cpld_gpio_get_function(struct udevice *dev, unsigned int gpio)
113 ulong type = dev_get_driver_data(dev);
118 val = sl28cpld_read(dev, SL28CPLD_GPIO_DIR);
134 static const struct dm_gpio_ops sl28cpld_gpio_ops = {
135 .direction_input = sl28cpld_gpio_direction_input,
136 .direction_output = sl28cpld_gpio_direction_output,
137 .get_value = sl28cpld_gpio_get_value,
138 .set_value = sl28cpld_gpio_set_value,
139 .get_function = sl28cpld_gpio_get_function,
142 static int sl28cpld_gpio_probe(struct udevice *dev)
144 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
146 uc_priv->gpio_count = 8;
147 uc_priv->bank_name = dev_read_name(dev);
152 static const struct udevice_id sl28cpld_gpio_ids[] = {
153 { .compatible = "kontron,sl28cpld-gpio", .data = SL28CPLD_GPIO},
154 { .compatible = "kontron,sl28cpld-gpo", .data = SL28CPLD_GPO},
155 { .compatible = "kontron,sl28cpld-gpi", .data = SL28CPLD_GPI},
159 U_BOOT_DRIVER(sl28cpld_gpio) = {
160 .name = "sl28cpld_gpio",
162 .of_match = sl28cpld_gpio_ids,
163 .probe = sl28cpld_gpio_probe,
164 .ops = &sl28cpld_gpio_ops,