1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020-2021 NVIDIA CORPORATION & AFFILIATES
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
9 #include <linux/device.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/interrupt.h>
13 #include <linux/ioport.h>
14 #include <linux/kernel.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
19 #include <linux/resource.h>
20 #include <linux/seq_file.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
25 * There are 3 YU GPIO blocks:
26 * gpio[0]: HOST_GPIO0->HOST_GPIO31
27 * gpio[1]: HOST_GPIO32->HOST_GPIO63
28 * gpio[2]: HOST_GPIO64->HOST_GPIO69
30 #define MLXBF2_GPIO_MAX_PINS_PER_BLOCK 32
33 * arm_gpio_lock register:
34 * bit[31] lock status: active if set
36 * The lock is enabled only if 0xd42f is written to this field
38 #define YU_ARM_GPIO_LOCK_ADDR 0x2801088
39 #define YU_ARM_GPIO_LOCK_SIZE 0x8
40 #define YU_LOCK_ACTIVE_BIT(val) (val >> 31)
41 #define YU_ARM_GPIO_LOCK_ACQUIRE 0xd42f
42 #define YU_ARM_GPIO_LOCK_RELEASE 0x0
45 * gpio[x] block registers and their offset
47 #define YU_GPIO_DATAIN 0x04
48 #define YU_GPIO_MODE1 0x08
49 #define YU_GPIO_MODE0 0x0c
50 #define YU_GPIO_DATASET 0x14
51 #define YU_GPIO_DATACLEAR 0x18
52 #define YU_GPIO_CAUSE_RISE_EN 0x44
53 #define YU_GPIO_CAUSE_FALL_EN 0x48
54 #define YU_GPIO_MODE1_CLEAR 0x50
55 #define YU_GPIO_MODE0_SET 0x54
56 #define YU_GPIO_MODE0_CLEAR 0x58
57 #define YU_GPIO_CAUSE_OR_CAUSE_EVTEN0 0x80
58 #define YU_GPIO_CAUSE_OR_EVTEN0 0x94
59 #define YU_GPIO_CAUSE_OR_CLRCAUSE 0x98
61 struct mlxbf2_gpio_context_save_regs {
66 /* BlueField-2 gpio block context structure. */
67 struct mlxbf2_gpio_context {
70 /* YU GPIO blocks address */
71 void __iomem *gpio_io;
74 struct mlxbf2_gpio_context_save_regs *csave_regs;
77 /* BlueField-2 gpio shared structure. */
78 struct mlxbf2_gpio_param {
84 static struct resource yu_arm_gpio_lock_res =
85 DEFINE_RES_MEM_NAMED(YU_ARM_GPIO_LOCK_ADDR, YU_ARM_GPIO_LOCK_SIZE, "YU_ARM_GPIO_LOCK");
87 static DEFINE_MUTEX(yu_arm_gpio_lock_mutex);
89 static struct mlxbf2_gpio_param yu_arm_gpio_lock_param = {
90 .res = &yu_arm_gpio_lock_res,
91 .lock = &yu_arm_gpio_lock_mutex,
94 /* Request memory region and map yu_arm_gpio_lock resource */
95 static int mlxbf2_gpio_get_lock_res(struct platform_device *pdev)
97 struct device *dev = &pdev->dev;
102 mutex_lock(yu_arm_gpio_lock_param.lock);
104 /* Check if the memory map already exists */
105 if (yu_arm_gpio_lock_param.io)
108 res = yu_arm_gpio_lock_param.res;
109 size = resource_size(res);
111 if (!devm_request_mem_region(dev, res->start, size, res->name)) {
116 yu_arm_gpio_lock_param.io = devm_ioremap(dev, res->start, size);
117 if (!yu_arm_gpio_lock_param.io)
121 mutex_unlock(yu_arm_gpio_lock_param.lock);
127 * Acquire the YU arm_gpio_lock to be able to change the direction
128 * mode. If the lock_active bit is already set, return an error.
130 static int mlxbf2_gpio_lock_acquire(struct mlxbf2_gpio_context *gs)
132 u32 arm_gpio_lock_val;
134 mutex_lock(yu_arm_gpio_lock_param.lock);
135 raw_spin_lock(&gs->gc.bgpio_lock);
137 arm_gpio_lock_val = readl(yu_arm_gpio_lock_param.io);
140 * When lock active bit[31] is set, ModeX is write enabled
142 if (YU_LOCK_ACTIVE_BIT(arm_gpio_lock_val)) {
143 raw_spin_unlock(&gs->gc.bgpio_lock);
144 mutex_unlock(yu_arm_gpio_lock_param.lock);
148 writel(YU_ARM_GPIO_LOCK_ACQUIRE, yu_arm_gpio_lock_param.io);
154 * Release the YU arm_gpio_lock after changing the direction mode.
156 static void mlxbf2_gpio_lock_release(struct mlxbf2_gpio_context *gs)
157 __releases(&gs->gc.bgpio_lock)
158 __releases(yu_arm_gpio_lock_param.lock)
160 writel(YU_ARM_GPIO_LOCK_RELEASE, yu_arm_gpio_lock_param.io);
161 raw_spin_unlock(&gs->gc.bgpio_lock);
162 mutex_unlock(yu_arm_gpio_lock_param.lock);
166 * mode0 and mode1 are both locked by the gpio_lock field.
168 * Together, mode0 and mode1 define the gpio Mode dependeing also
171 * {mode1,mode0}:{Reg_DataOut=0,Reg_DataOut=1}->{DataOut=0,DataOut=1}
173 * {0,0}:Reg_DataOut{0,1}->{Z,Z} Input PAD
174 * {0,1}:Reg_DataOut{0,1}->{0,1} Full drive Output PAD
175 * {1,0}:Reg_DataOut{0,1}->{0,Z} 0-set PAD to low, 1-float
176 * {1,1}:Reg_DataOut{0,1}->{Z,1} 0-float, 1-set PAD to high
180 * Set input direction:
181 * {mode1,mode0} = {0,0}
183 static int mlxbf2_gpio_direction_input(struct gpio_chip *chip,
186 struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
190 * Although the arm_gpio_lock was set in the probe function, check again
191 * if it is still enabled to be able to write to the ModeX registers.
193 ret = mlxbf2_gpio_lock_acquire(gs);
197 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_CLEAR);
198 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
200 mlxbf2_gpio_lock_release(gs);
206 * Set output direction:
207 * {mode1,mode0} = {0,1}
209 static int mlxbf2_gpio_direction_output(struct gpio_chip *chip,
213 struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
217 * Although the arm_gpio_lock was set in the probe function,
218 * check again it is still enabled to be able to write to the
221 ret = mlxbf2_gpio_lock_acquire(gs);
225 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
226 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_SET);
228 mlxbf2_gpio_lock_release(gs);
233 static void mlxbf2_gpio_irq_enable(struct irq_data *irqd)
235 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
236 struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
237 int offset = irqd_to_hwirq(irqd);
241 gpiochip_enable_irq(gc, irqd_to_hwirq(irqd));
242 raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
243 val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
245 writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
247 val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
249 writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
250 raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
253 static void mlxbf2_gpio_irq_disable(struct irq_data *irqd)
255 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
256 struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
257 int offset = irqd_to_hwirq(irqd);
261 raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
262 val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
264 writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
265 raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
266 gpiochip_disable_irq(gc, irqd_to_hwirq(irqd));
269 static irqreturn_t mlxbf2_gpio_irq_handler(int irq, void *ptr)
271 struct mlxbf2_gpio_context *gs = ptr;
272 struct gpio_chip *gc = &gs->gc;
273 unsigned long pending;
276 pending = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_CAUSE_EVTEN0);
277 writel(pending, gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
279 for_each_set_bit(level, &pending, gc->ngpio)
280 generic_handle_domain_irq_safe(gc->irq.domain, level);
282 return IRQ_RETVAL(pending);
286 mlxbf2_gpio_irq_set_type(struct irq_data *irqd, unsigned int type)
288 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
289 struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
290 int offset = irqd_to_hwirq(irqd);
296 switch (type & IRQ_TYPE_SENSE_MASK) {
297 case IRQ_TYPE_EDGE_BOTH:
301 case IRQ_TYPE_EDGE_RISING:
304 case IRQ_TYPE_EDGE_FALLING:
311 raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
313 val = readl(gs->gpio_io + YU_GPIO_CAUSE_FALL_EN);
315 writel(val, gs->gpio_io + YU_GPIO_CAUSE_FALL_EN);
319 val = readl(gs->gpio_io + YU_GPIO_CAUSE_RISE_EN);
321 writel(val, gs->gpio_io + YU_GPIO_CAUSE_RISE_EN);
323 raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
328 static void mlxbf2_gpio_irq_print_chip(struct irq_data *irqd,
331 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
332 struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
334 seq_printf(p, dev_name(gs->dev));
337 static const struct irq_chip mlxbf2_gpio_irq_chip = {
338 .irq_set_type = mlxbf2_gpio_irq_set_type,
339 .irq_enable = mlxbf2_gpio_irq_enable,
340 .irq_disable = mlxbf2_gpio_irq_disable,
341 .irq_print_chip = mlxbf2_gpio_irq_print_chip,
342 .flags = IRQCHIP_IMMUTABLE,
343 GPIOCHIP_IRQ_RESOURCE_HELPERS,
346 /* BlueField-2 GPIO driver initialization routine. */
348 mlxbf2_gpio_probe(struct platform_device *pdev)
350 struct mlxbf2_gpio_context *gs;
351 struct device *dev = &pdev->dev;
352 struct gpio_irq_chip *girq;
353 struct gpio_chip *gc;
358 name = dev_name(dev);
360 gs = devm_kzalloc(dev, sizeof(*gs), GFP_KERNEL);
366 /* YU GPIO block address */
367 gs->gpio_io = devm_platform_ioremap_resource(pdev, 0);
368 if (IS_ERR(gs->gpio_io))
369 return PTR_ERR(gs->gpio_io);
371 ret = mlxbf2_gpio_get_lock_res(pdev);
373 dev_err(dev, "Failed to get yu_arm_gpio_lock resource\n");
377 if (device_property_read_u32(dev, "npins", &npins))
378 npins = MLXBF2_GPIO_MAX_PINS_PER_BLOCK;
382 ret = bgpio_init(gc, dev, 4,
383 gs->gpio_io + YU_GPIO_DATAIN,
384 gs->gpio_io + YU_GPIO_DATASET,
385 gs->gpio_io + YU_GPIO_DATACLEAR,
391 dev_err(dev, "bgpio_init failed\n");
395 gc->direction_input = mlxbf2_gpio_direction_input;
396 gc->direction_output = mlxbf2_gpio_direction_output;
398 gc->owner = THIS_MODULE;
400 irq = platform_get_irq(pdev, 0);
403 gpio_irq_chip_set_chip(girq, &mlxbf2_gpio_irq_chip);
404 girq->handler = handle_simple_irq;
405 girq->default_type = IRQ_TYPE_NONE;
406 /* This will let us handle the parent IRQ in the driver */
407 girq->num_parents = 0;
408 girq->parents = NULL;
409 girq->parent_handler = NULL;
412 * Directly request the irq here instead of passing
413 * a flow-handler because the irq is shared.
415 ret = devm_request_irq(dev, irq, mlxbf2_gpio_irq_handler,
416 IRQF_SHARED, name, gs);
418 dev_err(dev, "failed to request IRQ");
423 platform_set_drvdata(pdev, gs);
425 ret = devm_gpiochip_add_data(dev, &gs->gc, gs);
427 dev_err(dev, "Failed adding memory mapped gpiochip\n");
434 static int __maybe_unused mlxbf2_gpio_suspend(struct device *dev)
436 struct mlxbf2_gpio_context *gs = dev_get_drvdata(dev);
438 gs->csave_regs->gpio_mode0 = readl(gs->gpio_io +
440 gs->csave_regs->gpio_mode1 = readl(gs->gpio_io +
446 static int __maybe_unused mlxbf2_gpio_resume(struct device *dev)
448 struct mlxbf2_gpio_context *gs = dev_get_drvdata(dev);
450 writel(gs->csave_regs->gpio_mode0, gs->gpio_io +
452 writel(gs->csave_regs->gpio_mode1, gs->gpio_io +
457 static SIMPLE_DEV_PM_OPS(mlxbf2_pm_ops, mlxbf2_gpio_suspend, mlxbf2_gpio_resume);
459 static const struct acpi_device_id __maybe_unused mlxbf2_gpio_acpi_match[] = {
463 MODULE_DEVICE_TABLE(acpi, mlxbf2_gpio_acpi_match);
465 static struct platform_driver mlxbf2_gpio_driver = {
467 .name = "mlxbf2_gpio",
468 .acpi_match_table = mlxbf2_gpio_acpi_match,
469 .pm = &mlxbf2_pm_ops,
471 .probe = mlxbf2_gpio_probe,
474 module_platform_driver(mlxbf2_gpio_driver);
476 MODULE_DESCRIPTION("Mellanox BlueField-2 GPIO Driver");
477 MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
478 MODULE_LICENSE("GPL v2");