module: Convert symbol namespace to string literal
[linux.git] / drivers / gpio / gpio-tangier.c
bloba415e6d361731e809d137a92b6c4658b447c26bd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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/cleanup.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/export.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/irq.h>
20 #include <linux/math.h>
21 #include <linux/module.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/pm.h>
24 #include <linux/spinlock.h>
25 #include <linux/string_helpers.h>
26 #include <linux/types.h>
28 #include <linux/gpio/driver.h>
30 #include "gpio-tangier.h"
32 #define GCCR 0x000 /* Controller configuration */
33 #define GPLR 0x004 /* Pin level r/o */
34 #define GPDR 0x01c /* Pin direction */
35 #define GPSR 0x034 /* Pin set w/o */
36 #define GPCR 0x04c /* Pin clear w/o */
37 #define GRER 0x064 /* Rising edge detect */
38 #define GFER 0x07c /* Falling edge detect */
39 #define GFBR 0x094 /* Glitch filter bypass */
40 #define GIMR 0x0ac /* Interrupt mask */
41 #define GISR 0x0c4 /* Interrupt source */
42 #define GITR 0x300 /* Input type */
43 #define GLPR 0x318 /* Level input polarity */
45 /**
46 * struct tng_gpio_context - Context to be saved during suspend-resume
47 * @level: Pin level
48 * @gpdr: Pin direction
49 * @grer: Rising edge detect enable
50 * @gfer: Falling edge detect enable
51 * @gimr: Interrupt mask
52 * @gwmr: Wake mask
54 struct tng_gpio_context {
55 u32 level;
56 u32 gpdr;
57 u32 grer;
58 u32 gfer;
59 u32 gimr;
60 u32 gwmr;
63 static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset,
64 unsigned int reg)
66 struct tng_gpio *priv = gpiochip_get_data(chip);
67 u8 reg_offset = offset / 32;
69 return priv->reg_base + reg + reg_offset * 4;
72 static void __iomem *gpio_reg_and_bit(struct gpio_chip *chip, unsigned int offset,
73 unsigned int reg, u8 *bit)
75 struct tng_gpio *priv = gpiochip_get_data(chip);
76 u8 reg_offset = offset / 32;
77 u8 shift = offset % 32;
79 *bit = shift;
80 return priv->reg_base + reg + reg_offset * 4;
83 static int tng_gpio_get(struct gpio_chip *chip, unsigned int offset)
85 void __iomem *gplr;
86 u8 shift;
88 gplr = gpio_reg_and_bit(chip, offset, GPLR, &shift);
90 return !!(readl(gplr) & BIT(shift));
93 static void tng_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
95 struct tng_gpio *priv = gpiochip_get_data(chip);
96 void __iomem *reg;
97 u8 shift;
99 reg = gpio_reg_and_bit(chip, offset, value ? GPSR : GPCR, &shift);
101 guard(raw_spinlock_irqsave)(&priv->lock);
103 writel(BIT(shift), reg);
106 static int tng_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
108 struct tng_gpio *priv = gpiochip_get_data(chip);
109 void __iomem *gpdr;
110 u32 value;
111 u8 shift;
113 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
115 guard(raw_spinlock_irqsave)(&priv->lock);
117 value = readl(gpdr);
118 value &= ~BIT(shift);
119 writel(value, gpdr);
121 return 0;
124 static int tng_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
125 int value)
127 struct tng_gpio *priv = gpiochip_get_data(chip);
128 void __iomem *gpdr;
129 u8 shift;
131 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
132 tng_gpio_set(chip, offset, value);
134 guard(raw_spinlock_irqsave)(&priv->lock);
136 value = readl(gpdr);
137 value |= BIT(shift);
138 writel(value, gpdr);
140 return 0;
143 static int tng_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
145 void __iomem *gpdr;
146 u8 shift;
148 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift);
150 if (readl(gpdr) & BIT(shift))
151 return GPIO_LINE_DIRECTION_OUT;
153 return GPIO_LINE_DIRECTION_IN;
156 static int tng_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
157 unsigned int debounce)
159 struct tng_gpio *priv = gpiochip_get_data(chip);
160 void __iomem *gfbr;
161 u32 value;
162 u8 shift;
164 gfbr = gpio_reg_and_bit(chip, offset, GFBR, &shift);
166 guard(raw_spinlock_irqsave)(&priv->lock);
168 value = readl(gfbr);
169 if (debounce)
170 value &= ~BIT(shift);
171 else
172 value |= BIT(shift);
173 writel(value, gfbr);
175 return 0;
178 static int tng_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
179 unsigned long config)
181 u32 debounce;
183 switch (pinconf_to_config_param(config)) {
184 case PIN_CONFIG_BIAS_DISABLE:
185 case PIN_CONFIG_BIAS_PULL_UP:
186 case PIN_CONFIG_BIAS_PULL_DOWN:
187 return gpiochip_generic_config(chip, offset, config);
188 case PIN_CONFIG_INPUT_DEBOUNCE:
189 debounce = pinconf_to_config_argument(config);
190 return tng_gpio_set_debounce(chip, offset, debounce);
191 default:
192 return -ENOTSUPP;
196 static void tng_irq_ack(struct irq_data *d)
198 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
199 struct tng_gpio *priv = gpiochip_get_data(gc);
200 irq_hw_number_t gpio = irqd_to_hwirq(d);
201 void __iomem *gisr;
202 u8 shift;
204 gisr = gpio_reg_and_bit(&priv->chip, gpio, GISR, &shift);
206 guard(raw_spinlock_irqsave)(&priv->lock);
208 writel(BIT(shift), gisr);
211 static void tng_irq_unmask_mask(struct tng_gpio *priv, u32 gpio, bool unmask)
213 void __iomem *gimr;
214 u32 value;
215 u8 shift;
217 gimr = gpio_reg_and_bit(&priv->chip, gpio, GIMR, &shift);
219 guard(raw_spinlock_irqsave)(&priv->lock);
221 value = readl(gimr);
222 if (unmask)
223 value |= BIT(shift);
224 else
225 value &= ~BIT(shift);
226 writel(value, gimr);
229 static void tng_irq_mask(struct irq_data *d)
231 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
232 struct tng_gpio *priv = gpiochip_get_data(gc);
233 irq_hw_number_t gpio = irqd_to_hwirq(d);
235 tng_irq_unmask_mask(priv, gpio, false);
236 gpiochip_disable_irq(&priv->chip, gpio);
239 static void tng_irq_unmask(struct irq_data *d)
241 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
242 struct tng_gpio *priv = gpiochip_get_data(gc);
243 irq_hw_number_t gpio = irqd_to_hwirq(d);
245 gpiochip_enable_irq(&priv->chip, gpio);
246 tng_irq_unmask_mask(priv, gpio, true);
249 static int tng_irq_set_type(struct irq_data *d, unsigned int type)
251 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
252 struct tng_gpio *priv = gpiochip_get_data(gc);
253 irq_hw_number_t gpio = irqd_to_hwirq(d);
254 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
255 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
256 void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR);
257 void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR);
258 u8 shift = gpio % 32;
259 u32 value;
261 guard(raw_spinlock_irqsave)(&priv->lock);
263 value = readl(grer);
264 if (type & IRQ_TYPE_EDGE_RISING)
265 value |= BIT(shift);
266 else
267 value &= ~BIT(shift);
268 writel(value, grer);
270 value = readl(gfer);
271 if (type & IRQ_TYPE_EDGE_FALLING)
272 value |= BIT(shift);
273 else
274 value &= ~BIT(shift);
275 writel(value, gfer);
278 * To prevent glitches from triggering an unintended level interrupt,
279 * configure GLPR register first and then configure GITR.
281 value = readl(glpr);
282 if (type & IRQ_TYPE_LEVEL_LOW)
283 value |= BIT(shift);
284 else
285 value &= ~BIT(shift);
286 writel(value, glpr);
288 if (type & IRQ_TYPE_LEVEL_MASK) {
289 value = readl(gitr);
290 value |= BIT(shift);
291 writel(value, gitr);
293 irq_set_handler_locked(d, handle_level_irq);
294 } else if (type & IRQ_TYPE_EDGE_BOTH) {
295 value = readl(gitr);
296 value &= ~BIT(shift);
297 writel(value, gitr);
299 irq_set_handler_locked(d, handle_edge_irq);
302 return 0;
305 static int tng_irq_set_wake(struct irq_data *d, unsigned int on)
307 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
308 struct tng_gpio *priv = gpiochip_get_data(gc);
309 irq_hw_number_t gpio = irqd_to_hwirq(d);
310 void __iomem *gwmr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwmr);
311 void __iomem *gwsr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwsr);
312 u8 shift = gpio % 32;
313 u32 value;
315 dev_dbg(priv->dev, "%s wake for gpio %lu\n", str_enable_disable(on), gpio);
317 guard(raw_spinlock_irqsave)(&priv->lock);
319 /* Clear the existing wake status */
320 writel(BIT(shift), gwsr);
322 value = readl(gwmr);
323 if (on)
324 value |= BIT(shift);
325 else
326 value &= ~BIT(shift);
327 writel(value, gwmr);
329 return 0;
332 static const struct irq_chip tng_irqchip = {
333 .name = "gpio-tangier",
334 .irq_ack = tng_irq_ack,
335 .irq_mask = tng_irq_mask,
336 .irq_unmask = tng_irq_unmask,
337 .irq_set_type = tng_irq_set_type,
338 .irq_set_wake = tng_irq_set_wake,
339 .flags = IRQCHIP_IMMUTABLE,
340 GPIOCHIP_IRQ_RESOURCE_HELPERS,
343 static void tng_irq_handler(struct irq_desc *desc)
345 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
346 struct tng_gpio *priv = gpiochip_get_data(gc);
347 struct irq_chip *irqchip = irq_desc_get_chip(desc);
348 unsigned long base, gpio;
350 chained_irq_enter(irqchip, desc);
352 /* Check GPIO controller to check which pin triggered the interrupt */
353 for (base = 0; base < priv->chip.ngpio; base += 32) {
354 void __iomem *gisr = gpio_reg(&priv->chip, base, GISR);
355 void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR);
356 unsigned long pending, enabled;
358 pending = readl(gisr);
359 enabled = readl(gimr);
361 /* Only interrupts that are enabled */
362 pending &= enabled;
364 for_each_set_bit(gpio, &pending, 32)
365 generic_handle_domain_irq(gc->irq.domain, base + gpio);
368 chained_irq_exit(irqchip, desc);
371 static int tng_irq_init_hw(struct gpio_chip *chip)
373 struct tng_gpio *priv = gpiochip_get_data(chip);
374 void __iomem *reg;
375 unsigned int base;
377 for (base = 0; base < priv->chip.ngpio; base += 32) {
378 /* Clear the rising-edge detect register */
379 reg = gpio_reg(&priv->chip, base, GRER);
380 writel(0, reg);
382 /* Clear the falling-edge detect register */
383 reg = gpio_reg(&priv->chip, base, GFER);
384 writel(0, reg);
387 return 0;
390 static int tng_gpio_add_pin_ranges(struct gpio_chip *chip)
392 struct tng_gpio *priv = gpiochip_get_data(chip);
393 const struct tng_gpio_pinrange *range;
394 unsigned int i;
395 int ret;
397 for (i = 0; i < priv->pin_info.nranges; i++) {
398 range = &priv->pin_info.pin_ranges[i];
399 ret = gpiochip_add_pin_range(&priv->chip,
400 priv->pin_info.name,
401 range->gpio_base,
402 range->pin_base,
403 range->npins);
404 if (ret) {
405 dev_err(priv->dev, "failed to add GPIO pin range\n");
406 return ret;
410 return 0;
413 int devm_tng_gpio_probe(struct device *dev, struct tng_gpio *gpio)
415 const struct tng_gpio_info *info = &gpio->info;
416 size_t nctx = DIV_ROUND_UP(info->ngpio, 32);
417 struct gpio_irq_chip *girq;
418 int ret;
420 gpio->ctx = devm_kcalloc(dev, nctx, sizeof(*gpio->ctx), GFP_KERNEL);
421 if (!gpio->ctx)
422 return -ENOMEM;
424 gpio->chip.label = dev_name(dev);
425 gpio->chip.parent = dev;
426 gpio->chip.request = gpiochip_generic_request;
427 gpio->chip.free = gpiochip_generic_free;
428 gpio->chip.direction_input = tng_gpio_direction_input;
429 gpio->chip.direction_output = tng_gpio_direction_output;
430 gpio->chip.get = tng_gpio_get;
431 gpio->chip.set = tng_gpio_set;
432 gpio->chip.get_direction = tng_gpio_get_direction;
433 gpio->chip.set_config = tng_gpio_set_config;
434 gpio->chip.base = info->base;
435 gpio->chip.ngpio = info->ngpio;
436 gpio->chip.can_sleep = false;
437 gpio->chip.add_pin_ranges = tng_gpio_add_pin_ranges;
439 raw_spin_lock_init(&gpio->lock);
441 girq = &gpio->chip.irq;
442 gpio_irq_chip_set_chip(girq, &tng_irqchip);
443 girq->init_hw = tng_irq_init_hw;
444 girq->parent_handler = tng_irq_handler;
445 girq->num_parents = 1;
446 girq->parents = devm_kcalloc(dev, girq->num_parents,
447 sizeof(*girq->parents), GFP_KERNEL);
448 if (!girq->parents)
449 return -ENOMEM;
451 girq->parents[0] = gpio->irq;
452 girq->first = info->first;
453 girq->default_type = IRQ_TYPE_NONE;
454 girq->handler = handle_bad_irq;
456 ret = devm_gpiochip_add_data(dev, &gpio->chip, gpio);
457 if (ret)
458 return dev_err_probe(dev, ret, "gpiochip_add error\n");
460 return 0;
462 EXPORT_SYMBOL_NS_GPL(devm_tng_gpio_probe, "GPIO_TANGIER");
464 static int tng_gpio_suspend(struct device *dev)
466 struct tng_gpio *priv = dev_get_drvdata(dev);
467 struct tng_gpio_context *ctx = priv->ctx;
468 unsigned int base;
470 guard(raw_spinlock_irqsave)(&priv->lock);
472 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) {
473 /* GPLR is RO, values read will be restored using GPSR */
474 ctx->level = readl(gpio_reg(&priv->chip, base, GPLR));
476 ctx->gpdr = readl(gpio_reg(&priv->chip, base, GPDR));
477 ctx->grer = readl(gpio_reg(&priv->chip, base, GRER));
478 ctx->gfer = readl(gpio_reg(&priv->chip, base, GFER));
479 ctx->gimr = readl(gpio_reg(&priv->chip, base, GIMR));
481 ctx->gwmr = readl(gpio_reg(&priv->chip, base, priv->wake_regs.gwmr));
484 return 0;
487 static int tng_gpio_resume(struct device *dev)
489 struct tng_gpio *priv = dev_get_drvdata(dev);
490 struct tng_gpio_context *ctx = priv->ctx;
491 unsigned int base;
493 guard(raw_spinlock_irqsave)(&priv->lock);
495 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) {
496 /* GPLR is RO, values read will be restored using GPSR */
497 writel(ctx->level, gpio_reg(&priv->chip, base, GPSR));
499 writel(ctx->gpdr, gpio_reg(&priv->chip, base, GPDR));
500 writel(ctx->grer, gpio_reg(&priv->chip, base, GRER));
501 writel(ctx->gfer, gpio_reg(&priv->chip, base, GFER));
502 writel(ctx->gimr, gpio_reg(&priv->chip, base, GIMR));
504 writel(ctx->gwmr, gpio_reg(&priv->chip, base, priv->wake_regs.gwmr));
507 return 0;
510 EXPORT_NS_GPL_SIMPLE_DEV_PM_OPS(tng_gpio_pm_ops, tng_gpio_suspend, tng_gpio_resume, GPIO_TANGIER);
512 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
513 MODULE_AUTHOR("Pandith N <pandith.n@intel.com>");
514 MODULE_AUTHOR("Raag Jadav <raag.jadav@intel.com>");
515 MODULE_DESCRIPTION("Intel Tangier GPIO driver");
516 MODULE_LICENSE("GPL");