1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019 Google LLC
6 #define LOG_CATEGORY UCLASS_GPIO
17 #include <acpi/acpi_device.h>
20 #include <asm/intel_pinctrl.h>
21 #include <asm/intel_pinctrl_defs.h>
24 #include <asm/arch/gpio.h>
26 #include <dm/device-internal.h>
27 #include <dt-bindings/gpio/x86-gpio.h>
29 static int intel_gpio_get_value(struct udevice *dev, uint offset)
31 struct udevice *pinctrl = dev_get_parent(dev);
35 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
36 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
38 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
39 if (rx_tx == PAD_CFG0_TX_DISABLE)
40 return reg & PAD_CFG0_RX_STATE ? 1 : 0;
41 else if (rx_tx == PAD_CFG0_RX_DISABLE)
42 return reg & PAD_CFG0_TX_STATE ? 1 : 0;
48 static int intel_gpio_set_value(struct udevice *dev, unsigned int offset,
51 struct udevice *pinctrl = dev_get_parent(dev);
54 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
56 pcr_clrsetbits32(pinctrl, config_offset, PAD_CFG0_TX_STATE,
57 value ? PAD_CFG0_TX_STATE : 0);
62 static int intel_gpio_get_function(struct udevice *dev, uint offset)
64 struct udevice *pinctrl = dev_get_parent(dev);
68 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
69 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
71 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
72 if (rx_tx == PAD_CFG0_TX_DISABLE)
74 else if (rx_tx == PAD_CFG0_RX_DISABLE)
81 static int intel_gpio_xlate(struct udevice *orig_dev, struct gpio_desc *desc,
82 struct ofnode_phandle_args *args)
84 struct udevice *pinctrl, *dev;
88 * GPIO numbers are global in the device tree so it doesn't matter
89 * which @orig_dev is used
92 ret = intel_pinctrl_get_pad(gpio, &pinctrl, &desc->offset);
94 return log_msg_ret("bad", ret);
95 device_find_first_child(pinctrl, &dev);
97 return log_msg_ret("no child", -ENOENT);
98 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
102 * Handle the case where the wrong GPIO device was provided, since this
103 * will not have been probed by the GPIO uclass before calling here
104 * (see gpio_request_tail()).
106 if (orig_dev != dev) {
107 ret = device_probe(dev);
109 return log_msg_ret("probe", ret);
115 static int intel_gpio_set_flags(struct udevice *dev, unsigned int offset,
118 struct udevice *pinctrl = dev_get_parent(dev);
119 u32 bic0 = 0, bic1 = 0;
123 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
125 if (flags & GPIOD_IS_OUT) {
126 bic0 |= PAD_CFG0_MODE_MASK | PAD_CFG0_RX_STATE |
128 or0 |= PAD_CFG0_MODE_GPIO | PAD_CFG0_RX_DISABLE;
129 } else if (flags & GPIOD_IS_IN) {
130 bic0 |= PAD_CFG0_MODE_MASK | PAD_CFG0_TX_STATE |
132 or0 |= PAD_CFG0_MODE_GPIO | PAD_CFG0_TX_DISABLE;
134 if (flags & GPIOD_PULL_UP) {
135 bic1 |= PAD_CFG1_PULL_MASK;
136 or1 |= PAD_CFG1_PULL_UP_20K;
137 } else if (flags & GPIOD_PULL_DOWN) {
138 bic1 |= PAD_CFG1_PULL_MASK;
139 or1 |= PAD_CFG1_PULL_DN_20K;
142 pcr_clrsetbits32(pinctrl, PAD_CFG0_OFFSET(config_offset), bic0, or0);
143 pcr_clrsetbits32(pinctrl, PAD_CFG1_OFFSET(config_offset), bic1, or1);
144 log_debug("%s: flags=%lx, offset=%x, config_offset=%x, %x/%x %x/%x\n",
145 dev->name, flags, offset, config_offset, bic0, or0, bic1, or1);
150 #if CONFIG_IS_ENABLED(ACPIGEN)
151 static int intel_gpio_get_acpi(const struct gpio_desc *desc,
152 struct acpi_gpio *gpio)
154 struct udevice *pinctrl;
157 if (!dm_gpio_is_valid(desc))
159 pinctrl = dev_get_parent(desc->dev);
161 memset(gpio, '\0', sizeof(*gpio));
163 gpio->type = ACPI_GPIO_TYPE_IO;
164 gpio->pull = ACPI_GPIO_PULL_DEFAULT;
165 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_OUTPUT;
166 gpio->polarity = ACPI_GPIO_ACTIVE_HIGH;
168 gpio->pins[0] = intel_pinctrl_get_acpi_pin(pinctrl, desc->offset);
169 gpio->pin0_addr = intel_pinctrl_get_config_reg_addr(pinctrl,
171 ret = acpi_get_path(pinctrl, gpio->resource, sizeof(gpio->resource));
173 return log_msg_ret("resource", ret);
179 static int intel_gpio_probe(struct udevice *dev)
184 static int intel_gpio_of_to_plat(struct udevice *dev)
186 struct gpio_dev_priv *upriv = dev_get_uclass_priv(dev);
187 struct intel_pinctrl_priv *pinctrl_priv = dev_get_priv(dev->parent);
188 const struct pad_community *comm = pinctrl_priv->comm;
190 upriv->gpio_count = comm->last_pad - comm->first_pad + 1;
191 upriv->bank_name = dev->name;
196 static const struct dm_gpio_ops gpio_intel_ops = {
197 .get_value = intel_gpio_get_value,
198 .set_value = intel_gpio_set_value,
199 .get_function = intel_gpio_get_function,
200 .xlate = intel_gpio_xlate,
201 .set_flags = intel_gpio_set_flags,
202 #if CONFIG_IS_ENABLED(ACPIGEN)
203 .get_acpi = intel_gpio_get_acpi,
207 #if CONFIG_IS_ENABLED(OF_REAL)
208 static const struct udevice_id intel_intel_gpio_ids[] = {
209 { .compatible = "intel,gpio" },
214 U_BOOT_DRIVER(intel_gpio) = {
215 .name = "intel_gpio",
217 .of_match = of_match_ptr(intel_intel_gpio_ids),
218 .ops = &gpio_intel_ops,
219 .of_to_plat = intel_gpio_of_to_plat,
220 .probe = intel_gpio_probe,