1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Tangier GPIO driver
5 * Copyright (c) 2016, 2021, 2023 Intel Corporation.
7 * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8 * Pandith N <pandith.n@intel.com>
9 * Raag Jadav <raag.jadav@intel.com>
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/export.h>
16 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/module.h>
20 #include <linux/pinctrl/pinconf-generic.h>
21 #include <linux/spinlock.h>
22 #include <linux/string_helpers.h>
23 #include <linux/types.h>
25 #include <linux/gpio/driver.h>
27 #include "gpio-tangier.h"
29 #define GCCR 0x000 /* Controller configuration */
30 #define GPLR 0x004 /* Pin level r/o */
31 #define GPDR 0x01c /* Pin direction */
32 #define GPSR 0x034 /* Pin set w/o */
33 #define GPCR 0x04c /* Pin clear w/o */
34 #define GRER 0x064 /* Rising edge detect */
35 #define GFER 0x07c /* Falling edge detect */
36 #define GFBR 0x094 /* Glitch filter bypass */
37 #define GIMR 0x0ac /* Interrupt mask */
38 #define GISR 0x0c4 /* Interrupt source */
39 #define GITR 0x300 /* Input type */
40 #define GLPR 0x318 /* Level input polarity */
43 * struct tng_gpio_context - Context to be saved during suspend-resume
45 * @gpdr: Pin direction
46 * @grer: Rising edge detect enable
47 * @gfer: Falling edge detect enable
48 * @gimr: Interrupt mask
51 struct tng_gpio_context {
60 static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset,
63 struct tng_gpio *priv = gpiochip_get_data(chip);
64 u8 reg_offset = offset / 32;
66 return priv->reg_base + reg + reg_offset * 4;
69 static void __iomem *gpio_reg_and_bit(struct gpio_chip *chip, unsigned int offset,
70 unsigned int reg, u8 *bit)
72 struct tng_gpio *priv = gpiochip_get_data(chip);
73 u8 reg_offset = offset / 32;
74 u8 shift = offset % 32;
77 return priv->reg_base + reg + reg_offset * 4;
80 static int tng_gpio_get(struct gpio_chip *chip, unsigned int offset)
85 gplr = gpio_reg_and_bit(chip, offset, GPLR, &shift);
87 return !!(readl(gplr) & BIT(shift));
90 static void tng_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
92 struct tng_gpio *priv = gpiochip_get_data(chip);
97 reg = gpio_reg_and_bit(chip, offset, value ? GPSR : GPCR, &shift);
99 raw_spin_lock_irqsave(&priv->lock, flags);
101 writel(BIT(shift), reg);
103 raw_spin_unlock_irqrestore(&priv->lock, flags);
106 static int tng_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
108 struct tng_gpio *priv = gpiochip_get_data(chip);
114 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
116 raw_spin_lock_irqsave(&priv->lock, flags);
119 value &= ~BIT(shift);
122 raw_spin_unlock_irqrestore(&priv->lock, flags);
127 static int tng_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
130 struct tng_gpio *priv = gpiochip_get_data(chip);
135 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
136 tng_gpio_set(chip, offset, value);
138 raw_spin_lock_irqsave(&priv->lock, flags);
144 raw_spin_unlock_irqrestore(&priv->lock, flags);
149 static int tng_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
154 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
156 if (readl(gpdr) & BIT(shift))
157 return GPIO_LINE_DIRECTION_OUT;
159 return GPIO_LINE_DIRECTION_IN;
162 static int tng_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
163 unsigned int debounce)
165 struct tng_gpio *priv = gpiochip_get_data(chip);
171 gfbr = gpio_reg_and_bit(chip, offset, GFBR, &shift);
173 raw_spin_lock_irqsave(&priv->lock, flags);
177 value &= ~BIT(shift);
182 raw_spin_unlock_irqrestore(&priv->lock, flags);
187 static int tng_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
188 unsigned long config)
192 switch (pinconf_to_config_param(config)) {
193 case PIN_CONFIG_BIAS_DISABLE:
194 case PIN_CONFIG_BIAS_PULL_UP:
195 case PIN_CONFIG_BIAS_PULL_DOWN:
196 return gpiochip_generic_config(chip, offset, config);
197 case PIN_CONFIG_INPUT_DEBOUNCE:
198 debounce = pinconf_to_config_argument(config);
199 return tng_gpio_set_debounce(chip, offset, debounce);
205 static void tng_irq_ack(struct irq_data *d)
207 struct tng_gpio *priv = irq_data_get_irq_chip_data(d);
208 irq_hw_number_t gpio = irqd_to_hwirq(d);
213 gisr = gpio_reg_and_bit(&priv->chip, gpio, GISR, &shift);
215 raw_spin_lock_irqsave(&priv->lock, flags);
216 writel(BIT(shift), gisr);
217 raw_spin_unlock_irqrestore(&priv->lock, flags);
220 static void tng_irq_unmask_mask(struct tng_gpio *priv, u32 gpio, bool unmask)
227 gimr = gpio_reg_and_bit(&priv->chip, gpio, GIMR, &shift);
229 raw_spin_lock_irqsave(&priv->lock, flags);
235 value &= ~BIT(shift);
238 raw_spin_unlock_irqrestore(&priv->lock, flags);
241 static void tng_irq_mask(struct irq_data *d)
243 struct tng_gpio *priv = irq_data_get_irq_chip_data(d);
244 irq_hw_number_t gpio = irqd_to_hwirq(d);
246 tng_irq_unmask_mask(priv, gpio, false);
247 gpiochip_disable_irq(&priv->chip, gpio);
250 static void tng_irq_unmask(struct irq_data *d)
252 struct tng_gpio *priv = irq_data_get_irq_chip_data(d);
253 irq_hw_number_t gpio = irqd_to_hwirq(d);
255 gpiochip_enable_irq(&priv->chip, gpio);
256 tng_irq_unmask_mask(priv, gpio, true);
259 static int tng_irq_set_type(struct irq_data *d, unsigned int type)
261 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
262 struct tng_gpio *priv = gpiochip_get_data(gc);
263 irq_hw_number_t gpio = irqd_to_hwirq(d);
264 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
265 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
266 void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR);
267 void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR);
268 u8 shift = gpio % 32;
272 raw_spin_lock_irqsave(&priv->lock, flags);
275 if (type & IRQ_TYPE_EDGE_RISING)
278 value &= ~BIT(shift);
282 if (type & IRQ_TYPE_EDGE_FALLING)
285 value &= ~BIT(shift);
289 * To prevent glitches from triggering an unintended level interrupt,
290 * configure GLPR register first and then configure GITR.
293 if (type & IRQ_TYPE_LEVEL_LOW)
296 value &= ~BIT(shift);
299 if (type & IRQ_TYPE_LEVEL_MASK) {
304 irq_set_handler_locked(d, handle_level_irq);
305 } else if (type & IRQ_TYPE_EDGE_BOTH) {
307 value &= ~BIT(shift);
310 irq_set_handler_locked(d, handle_edge_irq);
313 raw_spin_unlock_irqrestore(&priv->lock, flags);
318 static int tng_irq_set_wake(struct irq_data *d, unsigned int on)
320 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
321 struct tng_gpio *priv = gpiochip_get_data(gc);
322 irq_hw_number_t gpio = irqd_to_hwirq(d);
323 void __iomem *gwmr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwmr);
324 void __iomem *gwsr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwsr);
325 u8 shift = gpio % 32;
329 raw_spin_lock_irqsave(&priv->lock, flags);
331 /* Clear the existing wake status */
332 writel(BIT(shift), gwsr);
338 value &= ~BIT(shift);
341 raw_spin_unlock_irqrestore(&priv->lock, flags);
343 dev_dbg(priv->dev, "%s wake for gpio %lu\n", str_enable_disable(on), gpio);
347 static const struct irq_chip tng_irqchip = {
348 .name = "gpio-tangier",
349 .irq_ack = tng_irq_ack,
350 .irq_mask = tng_irq_mask,
351 .irq_unmask = tng_irq_unmask,
352 .irq_set_type = tng_irq_set_type,
353 .irq_set_wake = tng_irq_set_wake,
354 .flags = IRQCHIP_IMMUTABLE,
355 GPIOCHIP_IRQ_RESOURCE_HELPERS,
358 static void tng_irq_handler(struct irq_desc *desc)
360 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
361 struct tng_gpio *priv = gpiochip_get_data(gc);
362 struct irq_chip *irqchip = irq_desc_get_chip(desc);
363 unsigned long base, gpio;
365 chained_irq_enter(irqchip, desc);
367 /* Check GPIO controller to check which pin triggered the interrupt */
368 for (base = 0; base < priv->chip.ngpio; base += 32) {
369 void __iomem *gisr = gpio_reg(&priv->chip, base, GISR);
370 void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR);
371 unsigned long pending, enabled;
373 pending = readl(gisr);
374 enabled = readl(gimr);
376 /* Only interrupts that are enabled */
379 for_each_set_bit(gpio, &pending, 32)
380 generic_handle_domain_irq(gc->irq.domain, base + gpio);
383 chained_irq_exit(irqchip, desc);
386 static int tng_irq_init_hw(struct gpio_chip *chip)
388 struct tng_gpio *priv = gpiochip_get_data(chip);
392 for (base = 0; base < priv->chip.ngpio; base += 32) {
393 /* Clear the rising-edge detect register */
394 reg = gpio_reg(&priv->chip, base, GRER);
397 /* Clear the falling-edge detect register */
398 reg = gpio_reg(&priv->chip, base, GFER);
405 static int tng_gpio_add_pin_ranges(struct gpio_chip *chip)
407 struct tng_gpio *priv = gpiochip_get_data(chip);
408 const struct tng_gpio_pinrange *range;
412 for (i = 0; i < priv->pin_info.nranges; i++) {
413 range = &priv->pin_info.pin_ranges[i];
414 ret = gpiochip_add_pin_range(&priv->chip,
420 dev_err(priv->dev, "failed to add GPIO pin range\n");
428 int devm_tng_gpio_probe(struct device *dev, struct tng_gpio *gpio)
430 const struct tng_gpio_info *info = &gpio->info;
431 struct gpio_irq_chip *girq;
434 gpio->ctx = devm_kcalloc(dev, DIV_ROUND_UP(info->ngpio, 32), sizeof(*gpio->ctx), GFP_KERNEL);
438 gpio->chip.label = dev_name(dev);
439 gpio->chip.parent = dev;
440 gpio->chip.request = gpiochip_generic_request;
441 gpio->chip.free = gpiochip_generic_free;
442 gpio->chip.direction_input = tng_gpio_direction_input;
443 gpio->chip.direction_output = tng_gpio_direction_output;
444 gpio->chip.get = tng_gpio_get;
445 gpio->chip.set = tng_gpio_set;
446 gpio->chip.get_direction = tng_gpio_get_direction;
447 gpio->chip.set_config = tng_gpio_set_config;
448 gpio->chip.base = info->base;
449 gpio->chip.ngpio = info->ngpio;
450 gpio->chip.can_sleep = false;
451 gpio->chip.add_pin_ranges = tng_gpio_add_pin_ranges;
453 raw_spin_lock_init(&gpio->lock);
455 girq = &gpio->chip.irq;
456 gpio_irq_chip_set_chip(girq, &tng_irqchip);
457 girq->init_hw = tng_irq_init_hw;
458 girq->parent_handler = tng_irq_handler;
459 girq->num_parents = 1;
460 girq->parents = devm_kcalloc(dev, girq->num_parents,
461 sizeof(*girq->parents), GFP_KERNEL);
465 girq->parents[0] = gpio->irq;
466 girq->first = info->first;
467 girq->default_type = IRQ_TYPE_NONE;
468 girq->handler = handle_bad_irq;
470 ret = devm_gpiochip_add_data(dev, &gpio->chip, gpio);
472 return dev_err_probe(dev, ret, "gpiochip_add error\n");
476 EXPORT_SYMBOL_NS_GPL(devm_tng_gpio_probe, GPIO_TANGIER);
478 int tng_gpio_suspend(struct device *dev)
480 struct tng_gpio *priv = dev_get_drvdata(dev);
481 struct tng_gpio_context *ctx = priv->ctx;
485 raw_spin_lock_irqsave(&priv->lock, flags);
487 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) {
488 /* GPLR is RO, values read will be restored using GPSR */
489 ctx->level = readl(gpio_reg(&priv->chip, base, GPLR));
491 ctx->gpdr = readl(gpio_reg(&priv->chip, base, GPDR));
492 ctx->grer = readl(gpio_reg(&priv->chip, base, GRER));
493 ctx->gfer = readl(gpio_reg(&priv->chip, base, GFER));
494 ctx->gimr = readl(gpio_reg(&priv->chip, base, GIMR));
496 ctx->gwmr = readl(gpio_reg(&priv->chip, base, priv->wake_regs.gwmr));
499 raw_spin_unlock_irqrestore(&priv->lock, flags);
503 EXPORT_SYMBOL_NS_GPL(tng_gpio_suspend, GPIO_TANGIER);
505 int tng_gpio_resume(struct device *dev)
507 struct tng_gpio *priv = dev_get_drvdata(dev);
508 struct tng_gpio_context *ctx = priv->ctx;
512 raw_spin_lock_irqsave(&priv->lock, flags);
514 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) {
515 /* GPLR is RO, values read will be restored using GPSR */
516 writel(ctx->level, gpio_reg(&priv->chip, base, GPSR));
518 writel(ctx->gpdr, gpio_reg(&priv->chip, base, GPDR));
519 writel(ctx->grer, gpio_reg(&priv->chip, base, GRER));
520 writel(ctx->gfer, gpio_reg(&priv->chip, base, GFER));
521 writel(ctx->gimr, gpio_reg(&priv->chip, base, GIMR));
523 writel(ctx->gwmr, gpio_reg(&priv->chip, base, priv->wake_regs.gwmr));
526 raw_spin_unlock_irqrestore(&priv->lock, flags);
530 EXPORT_SYMBOL_NS_GPL(tng_gpio_resume, GPIO_TANGIER);
532 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
533 MODULE_AUTHOR("Pandith N <pandith.n@intel.com>");
534 MODULE_AUTHOR("Raag Jadav <raag.jadav@intel.com>");
535 MODULE_DESCRIPTION("Intel Tangier GPIO driver");
536 MODULE_LICENSE("GPL");