Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: merrifield: Adapt to Intel Tangier GPIO driver

Make use of Intel Tangier GPIO as a library driver for Merrifield.

Signed-off-by: Pandith N <pandith.n@intel.com>
Co-developed-by: Raag Jadav <raag.jadav@intel.com>
Signed-off-by: Raag Jadav <raag.jadav@intel.com>
Co-developed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

authored by

Pandith N and committed by
Andy Shevchenko
34840be5 d2c19e89

+34 -415
+3 -1
drivers/gpio/Kconfig
··· 624 624 select GPIOLIB_IRQCHIP 625 625 help 626 626 GPIO support for Intel Tangier and compatible platforms. 627 + Currently supported: 628 + - Merrifield 627 629 628 630 If built as a module its name will be gpio-tangier. 629 631 ··· 1518 1516 config GPIO_MERRIFIELD 1519 1517 tristate "Intel Merrifield GPIO support" 1520 1518 depends on X86_INTEL_MID 1521 - select GPIOLIB_IRQCHIP 1519 + select GPIO_TANGIER 1522 1520 help 1523 1521 Say Y here to support Intel Merrifield GPIO. 1524 1522
+26 -414
drivers/gpio/gpio-merrifield.c
··· 2 2 /* 3 3 * Intel Merrifield SoC GPIO driver 4 4 * 5 - * Copyright (c) 2016 Intel Corporation. 5 + * Copyright (c) 2016, 2023 Intel Corporation. 6 6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com> 7 7 */ 8 8 9 9 #include <linux/acpi.h> 10 10 #include <linux/bitops.h> 11 - #include <linux/gpio/driver.h> 12 - #include <linux/interrupt.h> 11 + #include <linux/device.h> 12 + #include <linux/err.h> 13 13 #include <linux/io.h> 14 14 #include <linux/module.h> 15 15 #include <linux/pci.h> 16 - #include <linux/pinctrl/consumer.h> 17 - #include <linux/string_helpers.h> 16 + #include <linux/types.h> 18 17 19 - #define GCCR 0x000 /* controller configuration */ 20 - #define GPLR 0x004 /* pin level r/o */ 21 - #define GPDR 0x01c /* pin direction */ 22 - #define GPSR 0x034 /* pin set w/o */ 23 - #define GPCR 0x04c /* pin clear w/o */ 24 - #define GRER 0x064 /* rising edge detect */ 25 - #define GFER 0x07c /* falling edge detect */ 26 - #define GFBR 0x094 /* glitch filter bypass */ 27 - #define GIMR 0x0ac /* interrupt mask */ 28 - #define GISR 0x0c4 /* interrupt source */ 29 - #define GITR 0x300 /* input type */ 30 - #define GLPR 0x318 /* level input polarity */ 31 - #define GWMR 0x400 /* wake mask */ 32 - #define GWSR 0x418 /* wake source */ 33 - #define GSIR 0xc00 /* secure input */ 18 + #include "gpio-tangier.h" 34 19 35 20 /* Intel Merrifield has 192 GPIO pins */ 36 21 #define MRFLD_NGPIO 192 37 22 38 - struct mrfld_gpio_pinrange { 39 - unsigned int gpio_base; 40 - unsigned int pin_base; 41 - unsigned int npins; 42 - }; 43 - 44 - #define GPIO_PINRANGE(gstart, gend, pstart) \ 45 - { \ 46 - .gpio_base = (gstart), \ 47 - .pin_base = (pstart), \ 48 - .npins = (gend) - (gstart) + 1, \ 49 - } 50 - 51 - struct mrfld_gpio { 52 - struct gpio_chip chip; 53 - void __iomem *reg_base; 54 - raw_spinlock_t lock; 55 - struct device *dev; 56 - }; 57 - 58 - static const struct mrfld_gpio_pinrange mrfld_gpio_ranges[] = { 23 + static const struct tng_gpio_pinrange mrfld_gpio_ranges[] = { 59 24 GPIO_PINRANGE(0, 11, 146), 60 25 GPIO_PINRANGE(12, 13, 144), 61 26 GPIO_PINRANGE(14, 15, 35), ··· 49 84 GPIO_PINRANGE(190, 191, 178), 50 85 }; 51 86 52 - static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset, 53 - unsigned int reg_type_offset) 54 - { 55 - struct mrfld_gpio *priv = gpiochip_get_data(chip); 56 - u8 reg = offset / 32; 57 - 58 - return priv->reg_base + reg_type_offset + reg * 4; 59 - } 60 - 61 - static int mrfld_gpio_get(struct gpio_chip *chip, unsigned int offset) 62 - { 63 - void __iomem *gplr = gpio_reg(chip, offset, GPLR); 64 - 65 - return !!(readl(gplr) & BIT(offset % 32)); 66 - } 67 - 68 - static void mrfld_gpio_set(struct gpio_chip *chip, unsigned int offset, 69 - int value) 70 - { 71 - struct mrfld_gpio *priv = gpiochip_get_data(chip); 72 - void __iomem *gpsr, *gpcr; 73 - unsigned long flags; 74 - 75 - raw_spin_lock_irqsave(&priv->lock, flags); 76 - 77 - if (value) { 78 - gpsr = gpio_reg(chip, offset, GPSR); 79 - writel(BIT(offset % 32), gpsr); 80 - } else { 81 - gpcr = gpio_reg(chip, offset, GPCR); 82 - writel(BIT(offset % 32), gpcr); 83 - } 84 - 85 - raw_spin_unlock_irqrestore(&priv->lock, flags); 86 - } 87 - 88 - static int mrfld_gpio_direction_input(struct gpio_chip *chip, 89 - unsigned int offset) 90 - { 91 - struct mrfld_gpio *priv = gpiochip_get_data(chip); 92 - void __iomem *gpdr = gpio_reg(chip, offset, GPDR); 93 - unsigned long flags; 94 - u32 value; 95 - 96 - raw_spin_lock_irqsave(&priv->lock, flags); 97 - 98 - value = readl(gpdr); 99 - value &= ~BIT(offset % 32); 100 - writel(value, gpdr); 101 - 102 - raw_spin_unlock_irqrestore(&priv->lock, flags); 103 - 104 - return 0; 105 - } 106 - 107 - static int mrfld_gpio_direction_output(struct gpio_chip *chip, 108 - unsigned int offset, int value) 109 - { 110 - struct mrfld_gpio *priv = gpiochip_get_data(chip); 111 - void __iomem *gpdr = gpio_reg(chip, offset, GPDR); 112 - unsigned long flags; 113 - 114 - mrfld_gpio_set(chip, offset, value); 115 - 116 - raw_spin_lock_irqsave(&priv->lock, flags); 117 - 118 - value = readl(gpdr); 119 - value |= BIT(offset % 32); 120 - writel(value, gpdr); 121 - 122 - raw_spin_unlock_irqrestore(&priv->lock, flags); 123 - 124 - return 0; 125 - } 126 - 127 - static int mrfld_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) 128 - { 129 - void __iomem *gpdr = gpio_reg(chip, offset, GPDR); 130 - 131 - if (readl(gpdr) & BIT(offset % 32)) 132 - return GPIO_LINE_DIRECTION_OUT; 133 - 134 - return GPIO_LINE_DIRECTION_IN; 135 - } 136 - 137 - static int mrfld_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset, 138 - unsigned int debounce) 139 - { 140 - struct mrfld_gpio *priv = gpiochip_get_data(chip); 141 - void __iomem *gfbr = gpio_reg(chip, offset, GFBR); 142 - unsigned long flags; 143 - u32 value; 144 - 145 - raw_spin_lock_irqsave(&priv->lock, flags); 146 - 147 - if (debounce) 148 - value = readl(gfbr) & ~BIT(offset % 32); 149 - else 150 - value = readl(gfbr) | BIT(offset % 32); 151 - writel(value, gfbr); 152 - 153 - raw_spin_unlock_irqrestore(&priv->lock, flags); 154 - 155 - return 0; 156 - } 157 - 158 - static int mrfld_gpio_set_config(struct gpio_chip *chip, unsigned int offset, 159 - unsigned long config) 160 - { 161 - u32 debounce; 162 - 163 - if ((pinconf_to_config_param(config) == PIN_CONFIG_BIAS_DISABLE) || 164 - (pinconf_to_config_param(config) == PIN_CONFIG_BIAS_PULL_UP) || 165 - (pinconf_to_config_param(config) == PIN_CONFIG_BIAS_PULL_DOWN)) 166 - return gpiochip_generic_config(chip, offset, config); 167 - 168 - if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE) 169 - return -ENOTSUPP; 170 - 171 - debounce = pinconf_to_config_argument(config); 172 - return mrfld_gpio_set_debounce(chip, offset, debounce); 173 - } 174 - 175 - static void mrfld_irq_ack(struct irq_data *d) 176 - { 177 - struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d); 178 - u32 gpio = irqd_to_hwirq(d); 179 - void __iomem *gisr = gpio_reg(&priv->chip, gpio, GISR); 180 - unsigned long flags; 181 - 182 - raw_spin_lock_irqsave(&priv->lock, flags); 183 - 184 - writel(BIT(gpio % 32), gisr); 185 - 186 - raw_spin_unlock_irqrestore(&priv->lock, flags); 187 - } 188 - 189 - static void mrfld_irq_unmask_mask(struct mrfld_gpio *priv, u32 gpio, bool unmask) 190 - { 191 - void __iomem *gimr = gpio_reg(&priv->chip, gpio, GIMR); 192 - unsigned long flags; 193 - u32 value; 194 - 195 - raw_spin_lock_irqsave(&priv->lock, flags); 196 - 197 - if (unmask) 198 - value = readl(gimr) | BIT(gpio % 32); 199 - else 200 - value = readl(gimr) & ~BIT(gpio % 32); 201 - writel(value, gimr); 202 - 203 - raw_spin_unlock_irqrestore(&priv->lock, flags); 204 - } 205 - 206 - static void mrfld_irq_mask(struct irq_data *d) 207 - { 208 - struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d); 209 - u32 gpio = irqd_to_hwirq(d); 210 - 211 - mrfld_irq_unmask_mask(priv, gpio, false); 212 - gpiochip_disable_irq(&priv->chip, gpio); 213 - } 214 - 215 - static void mrfld_irq_unmask(struct irq_data *d) 216 - { 217 - struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d); 218 - u32 gpio = irqd_to_hwirq(d); 219 - 220 - gpiochip_enable_irq(&priv->chip, gpio); 221 - mrfld_irq_unmask_mask(priv, gpio, true); 222 - } 223 - 224 - static int mrfld_irq_set_type(struct irq_data *d, unsigned int type) 225 - { 226 - struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 227 - struct mrfld_gpio *priv = gpiochip_get_data(gc); 228 - u32 gpio = irqd_to_hwirq(d); 229 - void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER); 230 - void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER); 231 - void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR); 232 - void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR); 233 - unsigned long flags; 234 - u32 value; 235 - 236 - raw_spin_lock_irqsave(&priv->lock, flags); 237 - 238 - if (type & IRQ_TYPE_EDGE_RISING) 239 - value = readl(grer) | BIT(gpio % 32); 240 - else 241 - value = readl(grer) & ~BIT(gpio % 32); 242 - writel(value, grer); 243 - 244 - if (type & IRQ_TYPE_EDGE_FALLING) 245 - value = readl(gfer) | BIT(gpio % 32); 246 - else 247 - value = readl(gfer) & ~BIT(gpio % 32); 248 - writel(value, gfer); 249 - 250 - /* 251 - * To prevent glitches from triggering an unintended level interrupt, 252 - * configure GLPR register first and then configure GITR. 253 - */ 254 - if (type & IRQ_TYPE_LEVEL_LOW) 255 - value = readl(glpr) | BIT(gpio % 32); 256 - else 257 - value = readl(glpr) & ~BIT(gpio % 32); 258 - writel(value, glpr); 259 - 260 - if (type & IRQ_TYPE_LEVEL_MASK) { 261 - value = readl(gitr) | BIT(gpio % 32); 262 - writel(value, gitr); 263 - 264 - irq_set_handler_locked(d, handle_level_irq); 265 - } else if (type & IRQ_TYPE_EDGE_BOTH) { 266 - value = readl(gitr) & ~BIT(gpio % 32); 267 - writel(value, gitr); 268 - 269 - irq_set_handler_locked(d, handle_edge_irq); 270 - } 271 - 272 - raw_spin_unlock_irqrestore(&priv->lock, flags); 273 - 274 - return 0; 275 - } 276 - 277 - static int mrfld_irq_set_wake(struct irq_data *d, unsigned int on) 278 - { 279 - struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 280 - struct mrfld_gpio *priv = gpiochip_get_data(gc); 281 - u32 gpio = irqd_to_hwirq(d); 282 - void __iomem *gwmr = gpio_reg(&priv->chip, gpio, GWMR); 283 - void __iomem *gwsr = gpio_reg(&priv->chip, gpio, GWSR); 284 - unsigned long flags; 285 - u32 value; 286 - 287 - raw_spin_lock_irqsave(&priv->lock, flags); 288 - 289 - /* Clear the existing wake status */ 290 - writel(BIT(gpio % 32), gwsr); 291 - 292 - if (on) 293 - value = readl(gwmr) | BIT(gpio % 32); 294 - else 295 - value = readl(gwmr) & ~BIT(gpio % 32); 296 - writel(value, gwmr); 297 - 298 - raw_spin_unlock_irqrestore(&priv->lock, flags); 299 - 300 - dev_dbg(priv->dev, "%s wake for gpio %u\n", str_enable_disable(on), gpio); 301 - return 0; 302 - } 303 - 304 - static const struct irq_chip mrfld_irqchip = { 305 - .name = "gpio-merrifield", 306 - .irq_ack = mrfld_irq_ack, 307 - .irq_mask = mrfld_irq_mask, 308 - .irq_unmask = mrfld_irq_unmask, 309 - .irq_set_type = mrfld_irq_set_type, 310 - .irq_set_wake = mrfld_irq_set_wake, 311 - .flags = IRQCHIP_IMMUTABLE, 312 - GPIOCHIP_IRQ_RESOURCE_HELPERS, 313 - }; 314 - 315 - static void mrfld_irq_handler(struct irq_desc *desc) 316 - { 317 - struct gpio_chip *gc = irq_desc_get_handler_data(desc); 318 - struct mrfld_gpio *priv = gpiochip_get_data(gc); 319 - struct irq_chip *irqchip = irq_desc_get_chip(desc); 320 - unsigned long base, gpio; 321 - 322 - chained_irq_enter(irqchip, desc); 323 - 324 - /* Check GPIO controller to check which pin triggered the interrupt */ 325 - for (base = 0; base < priv->chip.ngpio; base += 32) { 326 - void __iomem *gisr = gpio_reg(&priv->chip, base, GISR); 327 - void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR); 328 - unsigned long pending, enabled; 329 - 330 - pending = readl(gisr); 331 - enabled = readl(gimr); 332 - 333 - /* Only interrupts that are enabled */ 334 - pending &= enabled; 335 - 336 - for_each_set_bit(gpio, &pending, 32) 337 - generic_handle_domain_irq(gc->irq.domain, base + gpio); 338 - } 339 - 340 - chained_irq_exit(irqchip, desc); 341 - } 342 - 343 - static int mrfld_irq_init_hw(struct gpio_chip *chip) 344 - { 345 - struct mrfld_gpio *priv = gpiochip_get_data(chip); 346 - void __iomem *reg; 347 - unsigned int base; 348 - 349 - for (base = 0; base < priv->chip.ngpio; base += 32) { 350 - /* Clear the rising-edge detect register */ 351 - reg = gpio_reg(&priv->chip, base, GRER); 352 - writel(0, reg); 353 - /* Clear the falling-edge detect register */ 354 - reg = gpio_reg(&priv->chip, base, GFER); 355 - writel(0, reg); 356 - } 357 - 358 - return 0; 359 - } 360 - 361 - static const char *mrfld_gpio_get_pinctrl_dev_name(struct mrfld_gpio *priv) 87 + static const char *mrfld_gpio_get_pinctrl_dev_name(struct tng_gpio *priv) 362 88 { 363 89 struct acpi_device *adev; 364 90 const char *name; ··· 65 409 return name; 66 410 } 67 411 68 - static int mrfld_gpio_add_pin_ranges(struct gpio_chip *chip) 69 - { 70 - struct mrfld_gpio *priv = gpiochip_get_data(chip); 71 - const struct mrfld_gpio_pinrange *range; 72 - const char *pinctrl_dev_name; 73 - unsigned int i; 74 - int retval; 75 - 76 - pinctrl_dev_name = mrfld_gpio_get_pinctrl_dev_name(priv); 77 - if (!pinctrl_dev_name) 78 - return -ENOMEM; 79 - 80 - for (i = 0; i < ARRAY_SIZE(mrfld_gpio_ranges); i++) { 81 - range = &mrfld_gpio_ranges[i]; 82 - retval = gpiochip_add_pin_range(&priv->chip, pinctrl_dev_name, 83 - range->gpio_base, 84 - range->pin_base, 85 - range->npins); 86 - if (retval) { 87 - dev_err(priv->dev, "failed to add GPIO pin range\n"); 88 - return retval; 89 - } 90 - } 91 - 92 - return 0; 93 - } 94 - 95 412 static int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id) 96 413 { 97 - struct gpio_irq_chip *girq; 98 - struct mrfld_gpio *priv; 414 + struct device *dev = &pdev->dev; 415 + struct tng_gpio *priv; 99 416 u32 gpio_base, irq_base; 100 417 void __iomem *base; 101 418 int retval; ··· 98 469 priv->dev = &pdev->dev; 99 470 priv->reg_base = pcim_iomap_table(pdev)[0]; 100 471 101 - priv->chip.label = dev_name(&pdev->dev); 102 - priv->chip.parent = &pdev->dev; 103 - priv->chip.request = gpiochip_generic_request; 104 - priv->chip.free = gpiochip_generic_free; 105 - priv->chip.direction_input = mrfld_gpio_direction_input; 106 - priv->chip.direction_output = mrfld_gpio_direction_output; 107 - priv->chip.get = mrfld_gpio_get; 108 - priv->chip.set = mrfld_gpio_set; 109 - priv->chip.get_direction = mrfld_gpio_get_direction; 110 - priv->chip.set_config = mrfld_gpio_set_config; 111 - priv->chip.base = gpio_base; 112 - priv->chip.ngpio = MRFLD_NGPIO; 113 - priv->chip.can_sleep = false; 114 - priv->chip.add_pin_ranges = mrfld_gpio_add_pin_ranges; 472 + priv->pin_info.pin_ranges = mrfld_gpio_ranges; 473 + priv->pin_info.nranges = ARRAY_SIZE(mrfld_gpio_ranges); 474 + priv->pin_info.name = mrfld_gpio_get_pinctrl_dev_name(priv); 475 + if (!priv->pin_info.name) 476 + return -ENOMEM; 115 477 116 - raw_spin_lock_init(&priv->lock); 478 + priv->info.base = gpio_base; 479 + priv->info.ngpio = MRFLD_NGPIO; 480 + priv->info.first = irq_base; 117 481 118 482 retval = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); 119 483 if (retval < 0) 120 484 return retval; 121 485 122 - girq = &priv->chip.irq; 123 - gpio_irq_chip_set_chip(girq, &mrfld_irqchip); 124 - girq->init_hw = mrfld_irq_init_hw; 125 - girq->parent_handler = mrfld_irq_handler; 126 - girq->num_parents = 1; 127 - girq->parents = devm_kcalloc(&pdev->dev, girq->num_parents, 128 - sizeof(*girq->parents), GFP_KERNEL); 129 - if (!girq->parents) 130 - return -ENOMEM; 131 - girq->parents[0] = pci_irq_vector(pdev, 0); 132 - girq->first = irq_base; 133 - girq->default_type = IRQ_TYPE_NONE; 134 - girq->handler = handle_bad_irq; 486 + priv->irq = pci_irq_vector(pdev, 0); 135 487 136 - retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv); 137 - if (retval) { 138 - dev_err(&pdev->dev, "gpiochip_add error %d\n", retval); 139 - return retval; 140 - } 488 + priv->wake_regs.gwmr = GWMR_MRFLD; 489 + priv->wake_regs.gwsr = GWSR_MRFLD; 490 + priv->wake_regs.gsir = GSIR_MRFLD; 491 + 492 + retval = devm_tng_gpio_probe(dev, priv); 493 + if (retval) 494 + return dev_err_probe(dev, retval, "tng_gpio_probe error\n"); 141 495 142 496 pci_set_drvdata(pdev, priv); 143 497 return 0; ··· 137 525 .id_table = mrfld_gpio_ids, 138 526 .probe = mrfld_gpio_probe, 139 527 }; 140 - 141 528 module_pci_driver(mrfld_gpio_driver); 142 529 143 530 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>"); 144 531 MODULE_DESCRIPTION("Intel Merrifield SoC GPIO driver"); 145 532 MODULE_LICENSE("GPL v2"); 533 + MODULE_IMPORT_NS(GPIO_TANGIER);
+5
drivers/gpio/gpio-tangier.h
··· 20 20 21 21 struct tng_gpio_context; 22 22 23 + /* Merrifield specific wake registers */ 24 + #define GWMR_MRFLD 0x400 /* Wake mask */ 25 + #define GWSR_MRFLD 0x418 /* Wake source */ 26 + #define GSIR_MRFLD 0xc00 /* Secure input */ 27 + 23 28 /** 24 29 * struct tng_wake_regs - Platform specific wake registers 25 30 * @gwmr: Wake mask