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/cleanup.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/export.h>
17 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/math.h>
21 #include <linux/module.h>
22 #include <linux/pinctrl/pinconf-generic.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 */
46 * struct tng_gpio_context - Context to be saved during suspend-resume
48 * @gpdr: Pin direction
49 * @grer: Rising edge detect enable
50 * @gfer: Falling edge detect enable
51 * @gimr: Interrupt mask
54 struct tng_gpio_context
{
63 static void __iomem
*gpio_reg(struct gpio_chip
*chip
, unsigned int offset
,
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;
80 return priv
->reg_base
+ reg
+ reg_offset
* 4;
83 static int tng_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
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
);
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
);
113 gpdr
= gpio_reg_and_bit(chip
, offset
, GPDR
, &shift
);
115 guard(raw_spinlock_irqsave
)(&priv
->lock
);
118 value
&= ~BIT(shift
);
124 static int tng_gpio_direction_output(struct gpio_chip
*chip
, unsigned int offset
,
127 struct tng_gpio
*priv
= gpiochip_get_data(chip
);
131 gpdr
= gpio_reg_and_bit(chip
, offset
, GPDR
, &shift
);
132 tng_gpio_set(chip
, offset
, value
);
134 guard(raw_spinlock_irqsave
)(&priv
->lock
);
143 static int tng_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
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
);
164 gfbr
= gpio_reg_and_bit(chip
, offset
, GFBR
, &shift
);
166 guard(raw_spinlock_irqsave
)(&priv
->lock
);
170 value
&= ~BIT(shift
);
178 static int tng_gpio_set_config(struct gpio_chip
*chip
, unsigned int offset
,
179 unsigned long config
)
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
);
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
);
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
)
217 gimr
= gpio_reg_and_bit(&priv
->chip
, gpio
, GIMR
, &shift
);
219 guard(raw_spinlock_irqsave
)(&priv
->lock
);
225 value
&= ~BIT(shift
);
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;
261 guard(raw_spinlock_irqsave
)(&priv
->lock
);
264 if (type
& IRQ_TYPE_EDGE_RISING
)
267 value
&= ~BIT(shift
);
271 if (type
& IRQ_TYPE_EDGE_FALLING
)
274 value
&= ~BIT(shift
);
278 * To prevent glitches from triggering an unintended level interrupt,
279 * configure GLPR register first and then configure GITR.
282 if (type
& IRQ_TYPE_LEVEL_LOW
)
285 value
&= ~BIT(shift
);
288 if (type
& IRQ_TYPE_LEVEL_MASK
) {
293 irq_set_handler_locked(d
, handle_level_irq
);
294 } else if (type
& IRQ_TYPE_EDGE_BOTH
) {
296 value
&= ~BIT(shift
);
299 irq_set_handler_locked(d
, handle_edge_irq
);
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;
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
);
326 value
&= ~BIT(shift
);
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 */
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
);
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
);
382 /* Clear the falling-edge detect register */
383 reg
= gpio_reg(&priv
->chip
, base
, GFER
);
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
;
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
,
405 dev_err(priv
->dev
, "failed to add GPIO pin range\n");
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
;
420 gpio
->ctx
= devm_kcalloc(dev
, nctx
, sizeof(*gpio
->ctx
), GFP_KERNEL
);
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
);
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
);
458 return dev_err_probe(dev
, ret
, "gpiochip_add error\n");
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
;
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
));
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
;
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
));
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");