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/math.h>
20 #include <linux/module.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include <linux/spinlock.h>
23 #include <linux/string_helpers.h>
24 #include <linux/types.h>
26 #include <linux/gpio/driver.h>
28 #include "gpio-tangier.h"
30 #define GCCR 0x000 /* Controller configuration */
31 #define GPLR 0x004 /* Pin level r/o */
32 #define GPDR 0x01c /* Pin direction */
33 #define GPSR 0x034 /* Pin set w/o */
34 #define GPCR 0x04c /* Pin clear w/o */
35 #define GRER 0x064 /* Rising edge detect */
36 #define GFER 0x07c /* Falling edge detect */
37 #define GFBR 0x094 /* Glitch filter bypass */
38 #define GIMR 0x0ac /* Interrupt mask */
39 #define GISR 0x0c4 /* Interrupt source */
40 #define GITR 0x300 /* Input type */
41 #define GLPR 0x318 /* Level input polarity */
44 * struct tng_gpio_context - Context to be saved during suspend-resume
46 * @gpdr: Pin direction
47 * @grer: Rising edge detect enable
48 * @gfer: Falling edge detect enable
49 * @gimr: Interrupt mask
52 struct tng_gpio_context {
61 static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset,
64 struct tng_gpio *priv = gpiochip_get_data(chip);
65 u8 reg_offset = offset / 32;
67 return priv->reg_base + reg + reg_offset * 4;
70 static void __iomem *gpio_reg_and_bit(struct gpio_chip *chip, unsigned int offset,
71 unsigned int reg, u8 *bit)
73 struct tng_gpio *priv = gpiochip_get_data(chip);
74 u8 reg_offset = offset / 32;
75 u8 shift = offset % 32;
78 return priv->reg_base + reg + reg_offset * 4;
81 static int tng_gpio_get(struct gpio_chip *chip, unsigned int offset)
86 gplr = gpio_reg_and_bit(chip, offset, GPLR, &shift);
88 return !!(readl(gplr) & BIT(shift));
91 static void tng_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
93 struct tng_gpio *priv = gpiochip_get_data(chip);
98 reg = gpio_reg_and_bit(chip, offset, value ? GPSR : GPCR, &shift);
100 raw_spin_lock_irqsave(&priv->lock, flags);
102 writel(BIT(shift), reg);
104 raw_spin_unlock_irqrestore(&priv->lock, flags);
107 static int tng_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
109 struct tng_gpio *priv = gpiochip_get_data(chip);
115 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
117 raw_spin_lock_irqsave(&priv->lock, flags);
120 value &= ~BIT(shift);
123 raw_spin_unlock_irqrestore(&priv->lock, flags);
128 static int tng_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
131 struct tng_gpio *priv = gpiochip_get_data(chip);
136 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
137 tng_gpio_set(chip, offset, value);
139 raw_spin_lock_irqsave(&priv->lock, flags);
145 raw_spin_unlock_irqrestore(&priv->lock, flags);
150 static int tng_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
155 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
157 if (readl(gpdr) & BIT(shift))
158 return GPIO_LINE_DIRECTION_OUT;
160 return GPIO_LINE_DIRECTION_IN;
163 static int tng_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
164 unsigned int debounce)
166 struct tng_gpio *priv = gpiochip_get_data(chip);
172 gfbr = gpio_reg_and_bit(chip, offset, GFBR, &shift);
174 raw_spin_lock_irqsave(&priv->lock, flags);
178 value &= ~BIT(shift);
183 raw_spin_unlock_irqrestore(&priv->lock, flags);
188 static int tng_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
189 unsigned long config)
193 switch (pinconf_to_config_param(config)) {
194 case PIN_CONFIG_BIAS_DISABLE:
195 case PIN_CONFIG_BIAS_PULL_UP:
196 case PIN_CONFIG_BIAS_PULL_DOWN:
197 return gpiochip_generic_config(chip, offset, config);
198 case PIN_CONFIG_INPUT_DEBOUNCE:
199 debounce = pinconf_to_config_argument(config);
200 return tng_gpio_set_debounce(chip, offset, debounce);
206 static void tng_irq_ack(struct irq_data *d)
208 struct tng_gpio *priv = irq_data_get_irq_chip_data(d);
209 irq_hw_number_t gpio = irqd_to_hwirq(d);
214 gisr = gpio_reg_and_bit(&priv->chip, gpio, GISR, &shift);
216 raw_spin_lock_irqsave(&priv->lock, flags);
217 writel(BIT(shift), gisr);
218 raw_spin_unlock_irqrestore(&priv->lock, flags);
221 static void tng_irq_unmask_mask(struct tng_gpio *priv, u32 gpio, bool unmask)
228 gimr = gpio_reg_and_bit(&priv->chip, gpio, GIMR, &shift);
230 raw_spin_lock_irqsave(&priv->lock, flags);
236 value &= ~BIT(shift);
239 raw_spin_unlock_irqrestore(&priv->lock, flags);
242 static void tng_irq_mask(struct irq_data *d)
244 struct tng_gpio *priv = irq_data_get_irq_chip_data(d);
245 irq_hw_number_t gpio = irqd_to_hwirq(d);
247 tng_irq_unmask_mask(priv, gpio, false);
248 gpiochip_disable_irq(&priv->chip, gpio);
251 static void tng_irq_unmask(struct irq_data *d)
253 struct tng_gpio *priv = irq_data_get_irq_chip_data(d);
254 irq_hw_number_t gpio = irqd_to_hwirq(d);
256 gpiochip_enable_irq(&priv->chip, gpio);
257 tng_irq_unmask_mask(priv, gpio, true);
260 static int tng_irq_set_type(struct irq_data *d, unsigned int type)
262 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
263 struct tng_gpio *priv = gpiochip_get_data(gc);
264 irq_hw_number_t gpio = irqd_to_hwirq(d);
265 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
266 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
267 void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR);
268 void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR);
269 u8 shift = gpio % 32;
273 raw_spin_lock_irqsave(&priv->lock, flags);
276 if (type & IRQ_TYPE_EDGE_RISING)
279 value &= ~BIT(shift);
283 if (type & IRQ_TYPE_EDGE_FALLING)
286 value &= ~BIT(shift);
290 * To prevent glitches from triggering an unintended level interrupt,
291 * configure GLPR register first and then configure GITR.
294 if (type & IRQ_TYPE_LEVEL_LOW)
297 value &= ~BIT(shift);
300 if (type & IRQ_TYPE_LEVEL_MASK) {
305 irq_set_handler_locked(d, handle_level_irq);
306 } else if (type & IRQ_TYPE_EDGE_BOTH) {
308 value &= ~BIT(shift);
311 irq_set_handler_locked(d, handle_edge_irq);
314 raw_spin_unlock_irqrestore(&priv->lock, flags);
319 static int tng_irq_set_wake(struct irq_data *d, unsigned int on)
321 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
322 struct tng_gpio *priv = gpiochip_get_data(gc);
323 irq_hw_number_t gpio = irqd_to_hwirq(d);
324 void __iomem *gwmr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwmr);
325 void __iomem *gwsr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwsr);
326 u8 shift = gpio % 32;
330 raw_spin_lock_irqsave(&priv->lock, flags);
332 /* Clear the existing wake status */
333 writel(BIT(shift), gwsr);
339 value &= ~BIT(shift);
342 raw_spin_unlock_irqrestore(&priv->lock, flags);
344 dev_dbg(priv->dev, "%s wake for gpio %lu\n", str_enable_disable(on), gpio);
348 static const struct irq_chip tng_irqchip = {
349 .name = "gpio-tangier",
350 .irq_ack = tng_irq_ack,
351 .irq_mask = tng_irq_mask,
352 .irq_unmask = tng_irq_unmask,
353 .irq_set_type = tng_irq_set_type,
354 .irq_set_wake = tng_irq_set_wake,
355 .flags = IRQCHIP_IMMUTABLE,
356 GPIOCHIP_IRQ_RESOURCE_HELPERS,
359 static void tng_irq_handler(struct irq_desc *desc)
361 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
362 struct tng_gpio *priv = gpiochip_get_data(gc);
363 struct irq_chip *irqchip = irq_desc_get_chip(desc);
364 unsigned long base, gpio;
366 chained_irq_enter(irqchip, desc);
368 /* Check GPIO controller to check which pin triggered the interrupt */
369 for (base = 0; base < priv->chip.ngpio; base += 32) {
370 void __iomem *gisr = gpio_reg(&priv->chip, base, GISR);
371 void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR);
372 unsigned long pending, enabled;
374 pending = readl(gisr);
375 enabled = readl(gimr);
377 /* Only interrupts that are enabled */
380 for_each_set_bit(gpio, &pending, 32)
381 generic_handle_domain_irq(gc->irq.domain, base + gpio);
384 chained_irq_exit(irqchip, desc);
387 static int tng_irq_init_hw(struct gpio_chip *chip)
389 struct tng_gpio *priv = gpiochip_get_data(chip);
393 for (base = 0; base < priv->chip.ngpio; base += 32) {
394 /* Clear the rising-edge detect register */
395 reg = gpio_reg(&priv->chip, base, GRER);
398 /* Clear the falling-edge detect register */
399 reg = gpio_reg(&priv->chip, base, GFER);
406 static int tng_gpio_add_pin_ranges(struct gpio_chip *chip)
408 struct tng_gpio *priv = gpiochip_get_data(chip);
409 const struct tng_gpio_pinrange *range;
413 for (i = 0; i < priv->pin_info.nranges; i++) {
414 range = &priv->pin_info.pin_ranges[i];
415 ret = gpiochip_add_pin_range(&priv->chip,
421 dev_err(priv->dev, "failed to add GPIO pin range\n");
429 int devm_tng_gpio_probe(struct device *dev, struct tng_gpio *gpio)
431 const struct tng_gpio_info *info = &gpio->info;
432 size_t nctx = DIV_ROUND_UP(info->ngpio, 32);
433 struct gpio_irq_chip *girq;
436 gpio->ctx = devm_kcalloc(dev, nctx, sizeof(*gpio->ctx), GFP_KERNEL);
440 gpio->chip.label = dev_name(dev);
441 gpio->chip.parent = dev;
442 gpio->chip.request = gpiochip_generic_request;
443 gpio->chip.free = gpiochip_generic_free;
444 gpio->chip.direction_input = tng_gpio_direction_input;
445 gpio->chip.direction_output = tng_gpio_direction_output;
446 gpio->chip.get = tng_gpio_get;
447 gpio->chip.set = tng_gpio_set;
448 gpio->chip.get_direction = tng_gpio_get_direction;
449 gpio->chip.set_config = tng_gpio_set_config;
450 gpio->chip.base = info->base;
451 gpio->chip.ngpio = info->ngpio;
452 gpio->chip.can_sleep = false;
453 gpio->chip.add_pin_ranges = tng_gpio_add_pin_ranges;
455 raw_spin_lock_init(&gpio->lock);
457 girq = &gpio->chip.irq;
458 gpio_irq_chip_set_chip(girq, &tng_irqchip);
459 girq->init_hw = tng_irq_init_hw;
460 girq->parent_handler = tng_irq_handler;
461 girq->num_parents = 1;
462 girq->parents = devm_kcalloc(dev, girq->num_parents,
463 sizeof(*girq->parents), GFP_KERNEL);
467 girq->parents[0] = gpio->irq;
468 girq->first = info->first;
469 girq->default_type = IRQ_TYPE_NONE;
470 girq->handler = handle_bad_irq;
472 ret = devm_gpiochip_add_data(dev, &gpio->chip, gpio);
474 return dev_err_probe(dev, ret, "gpiochip_add error\n");
478 EXPORT_SYMBOL_NS_GPL(devm_tng_gpio_probe, GPIO_TANGIER);
480 int tng_gpio_suspend(struct device *dev)
482 struct tng_gpio *priv = dev_get_drvdata(dev);
483 struct tng_gpio_context *ctx = priv->ctx;
487 raw_spin_lock_irqsave(&priv->lock, flags);
489 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) {
490 /* GPLR is RO, values read will be restored using GPSR */
491 ctx->level = readl(gpio_reg(&priv->chip, base, GPLR));
493 ctx->gpdr = readl(gpio_reg(&priv->chip, base, GPDR));
494 ctx->grer = readl(gpio_reg(&priv->chip, base, GRER));
495 ctx->gfer = readl(gpio_reg(&priv->chip, base, GFER));
496 ctx->gimr = readl(gpio_reg(&priv->chip, base, GIMR));
498 ctx->gwmr = readl(gpio_reg(&priv->chip, base, priv->wake_regs.gwmr));
501 raw_spin_unlock_irqrestore(&priv->lock, flags);
505 EXPORT_SYMBOL_NS_GPL(tng_gpio_suspend, GPIO_TANGIER);
507 int tng_gpio_resume(struct device *dev)
509 struct tng_gpio *priv = dev_get_drvdata(dev);
510 struct tng_gpio_context *ctx = priv->ctx;
514 raw_spin_lock_irqsave(&priv->lock, flags);
516 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) {
517 /* GPLR is RO, values read will be restored using GPSR */
518 writel(ctx->level, gpio_reg(&priv->chip, base, GPSR));
520 writel(ctx->gpdr, gpio_reg(&priv->chip, base, GPDR));
521 writel(ctx->grer, gpio_reg(&priv->chip, base, GRER));
522 writel(ctx->gfer, gpio_reg(&priv->chip, base, GFER));
523 writel(ctx->gimr, gpio_reg(&priv->chip, base, GIMR));
525 writel(ctx->gwmr, gpio_reg(&priv->chip, base, priv->wake_regs.gwmr));
528 raw_spin_unlock_irqrestore(&priv->lock, flags);
532 EXPORT_SYMBOL_NS_GPL(tng_gpio_resume, GPIO_TANGIER);
534 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
535 MODULE_AUTHOR("Pandith N <pandith.n@intel.com>");
536 MODULE_AUTHOR("Raag Jadav <raag.jadav@intel.com>");
537 MODULE_DESCRIPTION("Intel Tangier GPIO driver");
538 MODULE_LICENSE("GPL");