1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
18 #include <asm/arch/gpio.h>
19 #include <dt-bindings/gpio/x86-gpio.h>
21 DECLARE_GLOBAL_DATA_PTR;
24 * struct broadwell_bank_priv - Private driver data
26 * @regs: Pointer to GPIO registers
27 * @bank: Bank number for this bank (0, 1 or 2)
28 * @offset: GPIO offset for this bank (0, 32 or 64)
30 struct broadwell_bank_priv {
31 struct pch_lp_gpio_regs *regs;
36 static int broadwell_gpio_request(struct udevice *dev, unsigned offset,
39 struct broadwell_bank_priv *priv = dev_get_priv(dev);
40 struct pch_lp_gpio_regs *regs = priv->regs;
44 * Make sure that the GPIO pin we want isn't already in use for some
45 * built-in hardware function. We have to check this for every
48 debug("%s: request bank %d offset %d: ", __func__, priv->bank, offset);
49 val = inl(®s->own[priv->bank]);
50 if (!(val & (1UL << offset))) {
51 debug("gpio is reserved for internal use\n");
59 static int broadwell_gpio_direction_input(struct udevice *dev, unsigned offset)
61 struct broadwell_bank_priv *priv = dev_get_priv(dev);
62 struct pch_lp_gpio_regs *regs = priv->regs;
64 setio_32(®s->config[priv->offset + offset], CONFA_DIR_INPUT);
69 static int broadwell_gpio_get_value(struct udevice *dev, unsigned offset)
71 struct broadwell_bank_priv *priv = dev_get_priv(dev);
72 struct pch_lp_gpio_regs *regs = priv->regs;
74 return inl(®s->config[priv->offset + offset]) & CONFA_LEVEL_HIGH ?
78 static int broadwell_gpio_set_value(struct udevice *dev, unsigned offset,
81 struct broadwell_bank_priv *priv = dev_get_priv(dev);
82 struct pch_lp_gpio_regs *regs = priv->regs;
84 debug("%s: dev=%s, offset=%d, value=%d\n", __func__, dev->name, offset,
86 clrsetio_32(®s->config[priv->offset + offset], CONFA_OUTPUT_HIGH,
87 value ? CONFA_OUTPUT_HIGH : 0);
92 static int broadwell_gpio_direction_output(struct udevice *dev, unsigned offset,
95 struct broadwell_bank_priv *priv = dev_get_priv(dev);
96 struct pch_lp_gpio_regs *regs = priv->regs;
98 broadwell_gpio_set_value(dev, offset, value);
99 clrio_32(®s->config[priv->offset + offset], CONFA_DIR_INPUT);
104 static int broadwell_gpio_get_function(struct udevice *dev, unsigned offset)
106 struct broadwell_bank_priv *priv = dev_get_priv(dev);
107 struct pch_lp_gpio_regs *regs = priv->regs;
108 u32 mask = 1UL << offset;
110 if (!(inl(®s->own[priv->bank]) & mask))
112 if (inl(®s->config[priv->offset + offset]) & CONFA_DIR_INPUT)
118 static int broadwell_gpio_probe(struct udevice *dev)
120 struct broadwell_bank_platdata *plat = dev_get_plat(dev);
121 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
122 struct broadwell_bank_priv *priv = dev_get_priv(dev);
123 struct udevice *pinctrl;
126 /* Set up pin control if available */
127 ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &pinctrl);
128 debug("%s, pinctrl=%p, ret=%d\n", __func__, pinctrl, ret);
130 uc_priv->gpio_count = GPIO_PER_BANK;
131 uc_priv->bank_name = plat->bank_name;
133 priv->regs = (struct pch_lp_gpio_regs *)(uintptr_t)plat->base_addr;
134 priv->bank = plat->bank;
135 priv->offset = priv->bank * 32;
136 debug("%s: probe done, regs %p, bank %d\n", __func__, priv->regs,
142 static int broadwell_gpio_ofdata_to_platdata(struct udevice *dev)
144 struct broadwell_bank_platdata *plat = dev_get_plat(dev);
149 ret = pch_get_gpio_base(dev->parent, &gpiobase);
153 bank = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
155 debug("%s: Invalid bank number %d\n", __func__, bank);
159 plat->base_addr = gpiobase;
160 plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
166 static const struct dm_gpio_ops gpio_broadwell_ops = {
167 .request = broadwell_gpio_request,
168 .direction_input = broadwell_gpio_direction_input,
169 .direction_output = broadwell_gpio_direction_output,
170 .get_value = broadwell_gpio_get_value,
171 .set_value = broadwell_gpio_set_value,
172 .get_function = broadwell_gpio_get_function,
175 static const struct udevice_id intel_broadwell_gpio_ids[] = {
176 { .compatible = "intel,broadwell-gpio" },
180 U_BOOT_DRIVER(gpio_broadwell) = {
181 .name = "gpio_broadwell",
183 .of_match = intel_broadwell_gpio_ids,
184 .ops = &gpio_broadwell_ops,
185 .ofdata_to_platdata = broadwell_gpio_ofdata_to_platdata,
186 .probe = broadwell_gpio_probe,
187 .priv_auto = sizeof(struct broadwell_bank_priv),
188 .plat_auto = sizeof(struct broadwell_bank_platdata),