1 // SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause
2 /* Copyright (C) 2022 NVIDIA CORPORATION & AFFILIATES */
4 #include <linux/bitfield.h>
5 #include <linux/bitops.h>
6 #include <linux/device.h>
8 #include <linux/gpio/driver.h>
9 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/spinlock.h>
14 #include <linux/types.h>
17 * There are 2 YU GPIO blocks:
18 * gpio[0]: HOST_GPIO0->HOST_GPIO31
19 * gpio[1]: HOST_GPIO32->HOST_GPIO55
21 #define MLXBF3_GPIO_MAX_PINS_PER_BLOCK 32
22 #define MLXBF3_GPIO_MAX_PINS_BLOCK0 32
23 #define MLXBF3_GPIO_MAX_PINS_BLOCK1 24
26 * fw_gpio[x] block registers and their offset
28 #define MLXBF_GPIO_FW_OUTPUT_ENABLE_SET 0x00
29 #define MLXBF_GPIO_FW_DATA_OUT_SET 0x04
31 #define MLXBF_GPIO_FW_OUTPUT_ENABLE_CLEAR 0x00
32 #define MLXBF_GPIO_FW_DATA_OUT_CLEAR 0x04
34 #define MLXBF_GPIO_CAUSE_RISE_EN 0x00
35 #define MLXBF_GPIO_CAUSE_FALL_EN 0x04
36 #define MLXBF_GPIO_READ_DATA_IN 0x08
38 #define MLXBF_GPIO_CAUSE_OR_CAUSE_EVTEN0 0x00
39 #define MLXBF_GPIO_CAUSE_OR_EVTEN0 0x14
40 #define MLXBF_GPIO_CAUSE_OR_CLRCAUSE 0x18
42 #define MLXBF_GPIO_CLR_ALL_INTS GENMASK(31, 0)
44 struct mlxbf3_gpio_context
{
47 /* YU GPIO block address */
48 void __iomem
*gpio_set_io
;
49 void __iomem
*gpio_clr_io
;
50 void __iomem
*gpio_io
;
52 /* YU GPIO cause block address */
53 void __iomem
*gpio_cause_io
;
56 static void mlxbf3_gpio_irq_enable(struct irq_data
*irqd
)
58 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(irqd
);
59 struct mlxbf3_gpio_context
*gs
= gpiochip_get_data(gc
);
60 irq_hw_number_t offset
= irqd_to_hwirq(irqd
);
64 gpiochip_enable_irq(gc
, offset
);
66 raw_spin_lock_irqsave(&gs
->gc
.bgpio_lock
, flags
);
67 writel(BIT(offset
), gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_CLRCAUSE
);
69 val
= readl(gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_EVTEN0
);
71 writel(val
, gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_EVTEN0
);
72 raw_spin_unlock_irqrestore(&gs
->gc
.bgpio_lock
, flags
);
75 static void mlxbf3_gpio_irq_disable(struct irq_data
*irqd
)
77 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(irqd
);
78 struct mlxbf3_gpio_context
*gs
= gpiochip_get_data(gc
);
79 irq_hw_number_t offset
= irqd_to_hwirq(irqd
);
83 raw_spin_lock_irqsave(&gs
->gc
.bgpio_lock
, flags
);
84 val
= readl(gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_EVTEN0
);
86 writel(val
, gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_EVTEN0
);
88 writel(BIT(offset
), gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_CLRCAUSE
);
89 raw_spin_unlock_irqrestore(&gs
->gc
.bgpio_lock
, flags
);
91 gpiochip_disable_irq(gc
, offset
);
94 static irqreturn_t
mlxbf3_gpio_irq_handler(int irq
, void *ptr
)
96 struct mlxbf3_gpio_context
*gs
= ptr
;
97 struct gpio_chip
*gc
= &gs
->gc
;
98 unsigned long pending
;
101 pending
= readl(gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_CAUSE_EVTEN0
);
102 writel(pending
, gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_CLRCAUSE
);
104 for_each_set_bit(level
, &pending
, gc
->ngpio
)
105 generic_handle_domain_irq(gc
->irq
.domain
, level
);
107 return IRQ_RETVAL(pending
);
111 mlxbf3_gpio_irq_set_type(struct irq_data
*irqd
, unsigned int type
)
113 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(irqd
);
114 struct mlxbf3_gpio_context
*gs
= gpiochip_get_data(gc
);
115 irq_hw_number_t offset
= irqd_to_hwirq(irqd
);
119 raw_spin_lock_irqsave(&gs
->gc
.bgpio_lock
, flags
);
121 switch (type
& IRQ_TYPE_SENSE_MASK
) {
122 case IRQ_TYPE_EDGE_BOTH
:
123 val
= readl(gs
->gpio_io
+ MLXBF_GPIO_CAUSE_FALL_EN
);
125 writel(val
, gs
->gpio_io
+ MLXBF_GPIO_CAUSE_FALL_EN
);
126 val
= readl(gs
->gpio_io
+ MLXBF_GPIO_CAUSE_RISE_EN
);
128 writel(val
, gs
->gpio_io
+ MLXBF_GPIO_CAUSE_RISE_EN
);
130 case IRQ_TYPE_EDGE_RISING
:
131 val
= readl(gs
->gpio_io
+ MLXBF_GPIO_CAUSE_RISE_EN
);
133 writel(val
, gs
->gpio_io
+ MLXBF_GPIO_CAUSE_RISE_EN
);
135 case IRQ_TYPE_EDGE_FALLING
:
136 val
= readl(gs
->gpio_io
+ MLXBF_GPIO_CAUSE_FALL_EN
);
138 writel(val
, gs
->gpio_io
+ MLXBF_GPIO_CAUSE_FALL_EN
);
141 raw_spin_unlock_irqrestore(&gs
->gc
.bgpio_lock
, flags
);
145 raw_spin_unlock_irqrestore(&gs
->gc
.bgpio_lock
, flags
);
147 irq_set_handler_locked(irqd
, handle_edge_irq
);
152 /* This function needs to be defined for handle_edge_irq() */
153 static void mlxbf3_gpio_irq_ack(struct irq_data
*data
)
157 static const struct irq_chip gpio_mlxbf3_irqchip
= {
159 .irq_ack
= mlxbf3_gpio_irq_ack
,
160 .irq_set_type
= mlxbf3_gpio_irq_set_type
,
161 .irq_enable
= mlxbf3_gpio_irq_enable
,
162 .irq_disable
= mlxbf3_gpio_irq_disable
,
163 .flags
= IRQCHIP_IMMUTABLE
,
164 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
167 static int mlxbf3_gpio_add_pin_ranges(struct gpio_chip
*chip
)
171 switch(chip
->ngpio
) {
172 case MLXBF3_GPIO_MAX_PINS_BLOCK0
:
175 case MLXBF3_GPIO_MAX_PINS_BLOCK1
:
182 return gpiochip_add_pin_range(chip
, "MLNXBF34:00",
183 chip
->base
, id
* MLXBF3_GPIO_MAX_PINS_PER_BLOCK
,
187 static int mlxbf3_gpio_probe(struct platform_device
*pdev
)
189 struct device
*dev
= &pdev
->dev
;
190 struct mlxbf3_gpio_context
*gs
;
191 struct gpio_irq_chip
*girq
;
192 struct gpio_chip
*gc
;
195 gs
= devm_kzalloc(dev
, sizeof(*gs
), GFP_KERNEL
);
199 gs
->gpio_io
= devm_platform_ioremap_resource(pdev
, 0);
200 if (IS_ERR(gs
->gpio_io
))
201 return PTR_ERR(gs
->gpio_io
);
203 gs
->gpio_cause_io
= devm_platform_ioremap_resource(pdev
, 1);
204 if (IS_ERR(gs
->gpio_cause_io
))
205 return PTR_ERR(gs
->gpio_cause_io
);
207 gs
->gpio_set_io
= devm_platform_ioremap_resource(pdev
, 2);
208 if (IS_ERR(gs
->gpio_set_io
))
209 return PTR_ERR(gs
->gpio_set_io
);
211 gs
->gpio_clr_io
= devm_platform_ioremap_resource(pdev
, 3);
212 if (IS_ERR(gs
->gpio_clr_io
))
213 return PTR_ERR(gs
->gpio_clr_io
);
216 ret
= bgpio_init(gc
, dev
, 4,
217 gs
->gpio_io
+ MLXBF_GPIO_READ_DATA_IN
,
218 gs
->gpio_set_io
+ MLXBF_GPIO_FW_DATA_OUT_SET
,
219 gs
->gpio_clr_io
+ MLXBF_GPIO_FW_DATA_OUT_CLEAR
,
220 gs
->gpio_set_io
+ MLXBF_GPIO_FW_OUTPUT_ENABLE_SET
,
221 gs
->gpio_clr_io
+ MLXBF_GPIO_FW_OUTPUT_ENABLE_CLEAR
, 0);
223 return dev_err_probe(dev
, ret
, "%s: bgpio_init() failed", __func__
);
225 gc
->request
= gpiochip_generic_request
;
226 gc
->free
= gpiochip_generic_free
;
227 gc
->owner
= THIS_MODULE
;
228 gc
->add_pin_ranges
= mlxbf3_gpio_add_pin_ranges
;
230 irq
= platform_get_irq(pdev
, 0);
233 gpio_irq_chip_set_chip(girq
, &gpio_mlxbf3_irqchip
);
234 girq
->default_type
= IRQ_TYPE_NONE
;
235 /* This will let us handle the parent IRQ in the driver */
236 girq
->num_parents
= 0;
237 girq
->parents
= NULL
;
238 girq
->parent_handler
= NULL
;
239 girq
->handler
= handle_bad_irq
;
242 * Directly request the irq here instead of passing
243 * a flow-handler because the irq is shared.
245 ret
= devm_request_irq(dev
, irq
, mlxbf3_gpio_irq_handler
,
246 IRQF_SHARED
, dev_name(dev
), gs
);
248 return dev_err_probe(dev
, ret
, "failed to request IRQ");
251 platform_set_drvdata(pdev
, gs
);
253 ret
= devm_gpiochip_add_data(dev
, &gs
->gc
, gs
);
255 dev_err_probe(dev
, ret
, "Failed adding memory mapped gpiochip\n");
260 static void mlxbf3_gpio_shutdown(struct platform_device
*pdev
)
262 struct mlxbf3_gpio_context
*gs
= platform_get_drvdata(pdev
);
264 /* Disable and clear all interrupts */
265 writel(0, gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_EVTEN0
);
266 writel(MLXBF_GPIO_CLR_ALL_INTS
, gs
->gpio_cause_io
+ MLXBF_GPIO_CAUSE_OR_CLRCAUSE
);
269 static const struct acpi_device_id mlxbf3_gpio_acpi_match
[] = {
273 MODULE_DEVICE_TABLE(acpi
, mlxbf3_gpio_acpi_match
);
275 static struct platform_driver mlxbf3_gpio_driver
= {
277 .name
= "mlxbf3_gpio",
278 .acpi_match_table
= mlxbf3_gpio_acpi_match
,
280 .probe
= mlxbf3_gpio_probe
,
281 .shutdown
= mlxbf3_gpio_shutdown
,
283 module_platform_driver(mlxbf3_gpio_driver
);
285 MODULE_SOFTDEP("pre: pinctrl-mlxbf3");
286 MODULE_DESCRIPTION("NVIDIA BlueField-3 GPIO Driver");
287 MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
288 MODULE_LICENSE("Dual BSD/GPL");