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/spinlock.h>
21 #include <linux/types.h>
24 * There are 3 YU GPIO blocks:
25 * gpio[0]: HOST_GPIO0->HOST_GPIO31
26 * gpio[1]: HOST_GPIO32->HOST_GPIO63
27 * gpio[2]: HOST_GPIO64->HOST_GPIO69
29 #define MLXBF2_GPIO_MAX_PINS_PER_BLOCK 32
32 * arm_gpio_lock register:
33 * bit[31] lock status: active if set
35 * The lock is enabled only if 0xd42f is written to this field
37 #define YU_ARM_GPIO_LOCK_ADDR 0x2801088
38 #define YU_ARM_GPIO_LOCK_SIZE 0x8
39 #define YU_LOCK_ACTIVE_BIT(val) (val >> 31)
40 #define YU_ARM_GPIO_LOCK_ACQUIRE 0xd42f
41 #define YU_ARM_GPIO_LOCK_RELEASE 0x0
44 * gpio[x] block registers and their offset
46 #define YU_GPIO_DATAIN 0x04
47 #define YU_GPIO_MODE1 0x08
48 #define YU_GPIO_MODE0 0x0c
49 #define YU_GPIO_DATASET 0x14
50 #define YU_GPIO_DATACLEAR 0x18
51 #define YU_GPIO_CAUSE_RISE_EN 0x44
52 #define YU_GPIO_CAUSE_FALL_EN 0x48
53 #define YU_GPIO_MODE1_CLEAR 0x50
54 #define YU_GPIO_MODE0_SET 0x54
55 #define YU_GPIO_MODE0_CLEAR 0x58
56 #define YU_GPIO_CAUSE_OR_CAUSE_EVTEN0 0x80
57 #define YU_GPIO_CAUSE_OR_EVTEN0 0x94
58 #define YU_GPIO_CAUSE_OR_CLRCAUSE 0x98
60 struct mlxbf2_gpio_context_save_regs {
65 /* BlueField-2 gpio block context structure. */
66 struct mlxbf2_gpio_context {
68 struct irq_chip irq_chip;
70 /* YU GPIO blocks address */
71 void __iomem *gpio_io;
73 struct mlxbf2_gpio_context_save_regs *csave_regs;
76 /* BlueField-2 gpio shared structure. */
77 struct mlxbf2_gpio_param {
83 static struct resource yu_arm_gpio_lock_res =
84 DEFINE_RES_MEM_NAMED(YU_ARM_GPIO_LOCK_ADDR, YU_ARM_GPIO_LOCK_SIZE, "YU_ARM_GPIO_LOCK");
86 static DEFINE_MUTEX(yu_arm_gpio_lock_mutex);
88 static struct mlxbf2_gpio_param yu_arm_gpio_lock_param = {
89 .res = &yu_arm_gpio_lock_res,
90 .lock = &yu_arm_gpio_lock_mutex,
93 /* Request memory region and map yu_arm_gpio_lock resource */
94 static int mlxbf2_gpio_get_lock_res(struct platform_device *pdev)
96 struct device *dev = &pdev->dev;
101 mutex_lock(yu_arm_gpio_lock_param.lock);
103 /* Check if the memory map already exists */
104 if (yu_arm_gpio_lock_param.io)
107 res = yu_arm_gpio_lock_param.res;
108 size = resource_size(res);
110 if (!devm_request_mem_region(dev, res->start, size, res->name)) {
115 yu_arm_gpio_lock_param.io = devm_ioremap(dev, res->start, size);
116 if (!yu_arm_gpio_lock_param.io)
120 mutex_unlock(yu_arm_gpio_lock_param.lock);
126 * Acquire the YU arm_gpio_lock to be able to change the direction
127 * mode. If the lock_active bit is already set, return an error.
129 static int mlxbf2_gpio_lock_acquire(struct mlxbf2_gpio_context *gs)
131 u32 arm_gpio_lock_val;
133 mutex_lock(yu_arm_gpio_lock_param.lock);
134 raw_spin_lock(&gs->gc.bgpio_lock);
136 arm_gpio_lock_val = readl(yu_arm_gpio_lock_param.io);
139 * When lock active bit[31] is set, ModeX is write enabled
141 if (YU_LOCK_ACTIVE_BIT(arm_gpio_lock_val)) {
142 raw_spin_unlock(&gs->gc.bgpio_lock);
143 mutex_unlock(yu_arm_gpio_lock_param.lock);
147 writel(YU_ARM_GPIO_LOCK_ACQUIRE, yu_arm_gpio_lock_param.io);
153 * Release the YU arm_gpio_lock after changing the direction mode.
155 static void mlxbf2_gpio_lock_release(struct mlxbf2_gpio_context *gs)
156 __releases(&gs->gc.bgpio_lock)
157 __releases(yu_arm_gpio_lock_param.lock)
159 writel(YU_ARM_GPIO_LOCK_RELEASE, yu_arm_gpio_lock_param.io);
160 raw_spin_unlock(&gs->gc.bgpio_lock);
161 mutex_unlock(yu_arm_gpio_lock_param.lock);
165 * mode0 and mode1 are both locked by the gpio_lock field.
167 * Together, mode0 and mode1 define the gpio Mode dependeing also
170 * {mode1,mode0}:{Reg_DataOut=0,Reg_DataOut=1}->{DataOut=0,DataOut=1}
172 * {0,0}:Reg_DataOut{0,1}->{Z,Z} Input PAD
173 * {0,1}:Reg_DataOut{0,1}->{0,1} Full drive Output PAD
174 * {1,0}:Reg_DataOut{0,1}->{0,Z} 0-set PAD to low, 1-float
175 * {1,1}:Reg_DataOut{0,1}->{Z,1} 0-float, 1-set PAD to high
179 * Set input direction:
180 * {mode1,mode0} = {0,0}
182 static int mlxbf2_gpio_direction_input(struct gpio_chip *chip,
185 struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
189 * Although the arm_gpio_lock was set in the probe function, check again
190 * if it is still enabled to be able to write to the ModeX registers.
192 ret = mlxbf2_gpio_lock_acquire(gs);
196 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_CLEAR);
197 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
199 mlxbf2_gpio_lock_release(gs);
205 * Set output direction:
206 * {mode1,mode0} = {0,1}
208 static int mlxbf2_gpio_direction_output(struct gpio_chip *chip,
212 struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
216 * Although the arm_gpio_lock was set in the probe function,
217 * check again it is still enabled to be able to write to the
220 ret = mlxbf2_gpio_lock_acquire(gs);
224 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
225 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_SET);
227 mlxbf2_gpio_lock_release(gs);
232 static void mlxbf2_gpio_irq_enable(struct irq_data *irqd)
234 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
235 struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
236 int offset = irqd_to_hwirq(irqd);
240 raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
241 val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
243 writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
245 val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
247 writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
248 raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
251 static void mlxbf2_gpio_irq_disable(struct irq_data *irqd)
253 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
254 struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
255 int offset = irqd_to_hwirq(irqd);
259 raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
260 val = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
262 writel(val, gs->gpio_io + YU_GPIO_CAUSE_OR_EVTEN0);
263 raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
266 static irqreturn_t mlxbf2_gpio_irq_handler(int irq, void *ptr)
268 struct mlxbf2_gpio_context *gs = ptr;
269 struct gpio_chip *gc = &gs->gc;
270 unsigned long pending;
273 pending = readl(gs->gpio_io + YU_GPIO_CAUSE_OR_CAUSE_EVTEN0);
274 writel(pending, gs->gpio_io + YU_GPIO_CAUSE_OR_CLRCAUSE);
276 for_each_set_bit(level, &pending, gc->ngpio)
277 generic_handle_domain_irq_safe(gc->irq.domain, level);
279 return IRQ_RETVAL(pending);
283 mlxbf2_gpio_irq_set_type(struct irq_data *irqd, unsigned int type)
285 struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
286 struct mlxbf2_gpio_context *gs = gpiochip_get_data(gc);
287 int offset = irqd_to_hwirq(irqd);
293 switch (type & IRQ_TYPE_SENSE_MASK) {
294 case IRQ_TYPE_EDGE_BOTH:
298 case IRQ_TYPE_EDGE_RISING:
301 case IRQ_TYPE_EDGE_FALLING:
308 raw_spin_lock_irqsave(&gs->gc.bgpio_lock, flags);
310 val = readl(gs->gpio_io + YU_GPIO_CAUSE_FALL_EN);
312 writel(val, gs->gpio_io + YU_GPIO_CAUSE_FALL_EN);
316 val = readl(gs->gpio_io + YU_GPIO_CAUSE_RISE_EN);
318 writel(val, gs->gpio_io + YU_GPIO_CAUSE_RISE_EN);
320 raw_spin_unlock_irqrestore(&gs->gc.bgpio_lock, flags);
325 /* BlueField-2 GPIO driver initialization routine. */
327 mlxbf2_gpio_probe(struct platform_device *pdev)
329 struct mlxbf2_gpio_context *gs;
330 struct device *dev = &pdev->dev;
331 struct gpio_irq_chip *girq;
332 struct gpio_chip *gc;
337 name = dev_name(dev);
339 gs = devm_kzalloc(dev, sizeof(*gs), GFP_KERNEL);
343 /* YU GPIO block address */
344 gs->gpio_io = devm_platform_ioremap_resource(pdev, 0);
345 if (IS_ERR(gs->gpio_io))
346 return PTR_ERR(gs->gpio_io);
348 ret = mlxbf2_gpio_get_lock_res(pdev);
350 dev_err(dev, "Failed to get yu_arm_gpio_lock resource\n");
354 if (device_property_read_u32(dev, "npins", &npins))
355 npins = MLXBF2_GPIO_MAX_PINS_PER_BLOCK;
359 ret = bgpio_init(gc, dev, 4,
360 gs->gpio_io + YU_GPIO_DATAIN,
361 gs->gpio_io + YU_GPIO_DATASET,
362 gs->gpio_io + YU_GPIO_DATACLEAR,
368 dev_err(dev, "bgpio_init failed\n");
372 gc->direction_input = mlxbf2_gpio_direction_input;
373 gc->direction_output = mlxbf2_gpio_direction_output;
375 gc->owner = THIS_MODULE;
377 irq = platform_get_irq(pdev, 0);
379 gs->irq_chip.name = name;
380 gs->irq_chip.irq_set_type = mlxbf2_gpio_irq_set_type;
381 gs->irq_chip.irq_enable = mlxbf2_gpio_irq_enable;
382 gs->irq_chip.irq_disable = mlxbf2_gpio_irq_disable;
385 girq->chip = &gs->irq_chip;
386 girq->handler = handle_simple_irq;
387 girq->default_type = IRQ_TYPE_NONE;
388 /* This will let us handle the parent IRQ in the driver */
389 girq->num_parents = 0;
390 girq->parents = NULL;
391 girq->parent_handler = NULL;
394 * Directly request the irq here instead of passing
395 * a flow-handler because the irq is shared.
397 ret = devm_request_irq(dev, irq, mlxbf2_gpio_irq_handler,
398 IRQF_SHARED, name, gs);
400 dev_err(dev, "failed to request IRQ");
405 platform_set_drvdata(pdev, gs);
407 ret = devm_gpiochip_add_data(dev, &gs->gc, gs);
409 dev_err(dev, "Failed adding memory mapped gpiochip\n");
416 static int __maybe_unused mlxbf2_gpio_suspend(struct device *dev)
418 struct mlxbf2_gpio_context *gs = dev_get_drvdata(dev);
420 gs->csave_regs->gpio_mode0 = readl(gs->gpio_io +
422 gs->csave_regs->gpio_mode1 = readl(gs->gpio_io +
428 static int __maybe_unused mlxbf2_gpio_resume(struct device *dev)
430 struct mlxbf2_gpio_context *gs = dev_get_drvdata(dev);
432 writel(gs->csave_regs->gpio_mode0, gs->gpio_io +
434 writel(gs->csave_regs->gpio_mode1, gs->gpio_io +
439 static SIMPLE_DEV_PM_OPS(mlxbf2_pm_ops, mlxbf2_gpio_suspend, mlxbf2_gpio_resume);
441 static const struct acpi_device_id __maybe_unused mlxbf2_gpio_acpi_match[] = {
445 MODULE_DEVICE_TABLE(acpi, mlxbf2_gpio_acpi_match);
447 static struct platform_driver mlxbf2_gpio_driver = {
449 .name = "mlxbf2_gpio",
450 .acpi_match_table = mlxbf2_gpio_acpi_match,
451 .pm = &mlxbf2_pm_ops,
453 .probe = mlxbf2_gpio_probe,
456 module_platform_driver(mlxbf2_gpio_driver);
458 MODULE_DESCRIPTION("Mellanox BlueField-2 GPIO Driver");
459 MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
460 MODULE_LICENSE("GPL v2");