1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019 Google LLC
15 #include <acpi/acpi_device.h>
18 #include <asm/intel_pinctrl.h>
19 #include <asm/intel_pinctrl_defs.h>
22 #include <asm/arch/gpio.h>
24 #include <dt-bindings/gpio/x86-gpio.h>
26 static int intel_gpio_direction_input(struct udevice *dev, uint offset)
28 struct udevice *pinctrl = dev_get_parent(dev);
31 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
33 pcr_clrsetbits32(pinctrl, config_offset,
34 PAD_CFG0_MODE_MASK | PAD_CFG0_TX_STATE |
36 PAD_CFG0_MODE_GPIO | PAD_CFG0_TX_DISABLE);
41 static int intel_gpio_direction_output(struct udevice *dev, uint offset,
44 struct udevice *pinctrl = dev_get_parent(dev);
47 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
49 pcr_clrsetbits32(pinctrl, config_offset,
50 PAD_CFG0_MODE_MASK | PAD_CFG0_RX_STATE |
51 PAD_CFG0_TX_DISABLE | PAD_CFG0_TX_STATE,
52 PAD_CFG0_MODE_GPIO | PAD_CFG0_RX_DISABLE |
53 (value ? PAD_CFG0_TX_STATE : 0));
58 static int intel_gpio_get_value(struct udevice *dev, uint offset)
60 struct udevice *pinctrl = dev_get_parent(dev);
64 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
65 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
67 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
68 if (rx_tx == PAD_CFG0_TX_DISABLE)
69 return reg & PAD_CFG0_RX_STATE ? 1 : 0;
70 else if (rx_tx == PAD_CFG0_RX_DISABLE)
71 return reg & PAD_CFG0_TX_STATE ? 1 : 0;
77 static int intel_gpio_set_value(struct udevice *dev, unsigned int offset,
80 struct udevice *pinctrl = dev_get_parent(dev);
83 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
85 pcr_clrsetbits32(pinctrl, config_offset, PAD_CFG0_TX_STATE,
86 value ? PAD_CFG0_TX_STATE : 0);
91 static int intel_gpio_get_function(struct udevice *dev, uint offset)
93 struct udevice *pinctrl = dev_get_parent(dev);
97 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
98 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
100 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
101 if (rx_tx == PAD_CFG0_TX_DISABLE)
103 else if (rx_tx == PAD_CFG0_RX_DISABLE)
110 static int intel_gpio_xlate(struct udevice *orig_dev, struct gpio_desc *desc,
111 struct ofnode_phandle_args *args)
113 struct udevice *pinctrl, *dev;
117 * GPIO numbers are global in the device tree so it doesn't matter
120 gpio = args->args[0];
121 ret = intel_pinctrl_get_pad(gpio, &pinctrl, &desc->offset);
123 return log_msg_ret("bad", ret);
124 device_find_first_child(pinctrl, &dev);
126 return log_msg_ret("no child", -ENOENT);
127 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
133 #if CONFIG_IS_ENABLED(ACPIGEN)
134 static int intel_gpio_get_acpi(const struct gpio_desc *desc,
135 struct acpi_gpio *gpio)
137 struct udevice *pinctrl;
140 if (!dm_gpio_is_valid(desc))
142 pinctrl = dev_get_parent(desc->dev);
144 memset(gpio, '\0', sizeof(*gpio));
146 gpio->type = ACPI_GPIO_TYPE_IO;
147 gpio->pull = ACPI_GPIO_PULL_DEFAULT;
148 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_OUTPUT;
149 gpio->polarity = ACPI_GPIO_ACTIVE_HIGH;
151 gpio->pins[0] = intel_pinctrl_get_acpi_pin(pinctrl, desc->offset);
152 gpio->pin0_addr = intel_pinctrl_get_config_reg_addr(pinctrl,
154 ret = acpi_get_path(pinctrl, gpio->resource, sizeof(gpio->resource));
156 return log_msg_ret("resource", ret);
162 static int intel_gpio_probe(struct udevice *dev)
167 static int intel_gpio_ofdata_to_platdata(struct udevice *dev)
169 struct gpio_dev_priv *upriv = dev_get_uclass_priv(dev);
170 struct intel_pinctrl_priv *pinctrl_priv = dev_get_priv(dev->parent);
171 const struct pad_community *comm = pinctrl_priv->comm;
173 upriv->gpio_count = comm->last_pad - comm->first_pad + 1;
174 upriv->bank_name = dev->name;
179 static const struct dm_gpio_ops gpio_intel_ops = {
180 .direction_input = intel_gpio_direction_input,
181 .direction_output = intel_gpio_direction_output,
182 .get_value = intel_gpio_get_value,
183 .set_value = intel_gpio_set_value,
184 .get_function = intel_gpio_get_function,
185 .xlate = intel_gpio_xlate,
186 #if CONFIG_IS_ENABLED(ACPIGEN)
187 .get_acpi = intel_gpio_get_acpi,
191 static const struct udevice_id intel_intel_gpio_ids[] = {
192 { .compatible = "intel,gpio" },
196 U_BOOT_DRIVER(intel_gpio) = {
197 .name = "intel_gpio",
199 .of_match = intel_intel_gpio_ids,
200 .ops = &gpio_intel_ops,
201 .ofdata_to_platdata = intel_gpio_ofdata_to_platdata,
202 .probe = intel_gpio_probe,