1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/acpi.h>
4 #include <linux/bitfield.h>
5 #include <linux/bitops.h>
6 #include <linux/device.h>
7 #include <linux/gpio/driver.h>
9 #include <linux/ioport.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
14 #include <linux/resource.h>
15 #include <linux/spinlock.h>
16 #include <linux/types.h>
17 #include <linux/version.h>
20 * There are 3 YU GPIO blocks:
21 * gpio[0]: HOST_GPIO0->HOST_GPIO31
22 * gpio[1]: HOST_GPIO32->HOST_GPIO63
23 * gpio[2]: HOST_GPIO64->HOST_GPIO69
25 #define MLXBF2_GPIO_MAX_PINS_PER_BLOCK 32
28 * arm_gpio_lock register:
29 * bit[31] lock status: active if set
31 * The lock is enabled only if 0xd42f is written to this field
33 #define YU_ARM_GPIO_LOCK_ADDR 0x2801088
34 #define YU_ARM_GPIO_LOCK_SIZE 0x8
35 #define YU_LOCK_ACTIVE_BIT(val) (val >> 31)
36 #define YU_ARM_GPIO_LOCK_ACQUIRE 0xd42f
37 #define YU_ARM_GPIO_LOCK_RELEASE 0x0
40 * gpio[x] block registers and their offset
42 #define YU_GPIO_DATAIN 0x04
43 #define YU_GPIO_MODE1 0x08
44 #define YU_GPIO_MODE0 0x0c
45 #define YU_GPIO_DATASET 0x14
46 #define YU_GPIO_DATACLEAR 0x18
47 #define YU_GPIO_MODE1_CLEAR 0x50
48 #define YU_GPIO_MODE0_SET 0x54
49 #define YU_GPIO_MODE0_CLEAR 0x58
52 struct mlxbf2_gpio_context_save_regs {
58 /* BlueField-2 gpio block context structure. */
59 struct mlxbf2_gpio_context {
62 /* YU GPIO blocks address */
63 void __iomem *gpio_io;
66 struct mlxbf2_gpio_context_save_regs *csave_regs;
70 /* BlueField-2 gpio shared structure. */
71 struct mlxbf2_gpio_param {
77 static struct resource yu_arm_gpio_lock_res = {
78 .start = YU_ARM_GPIO_LOCK_ADDR,
79 .end = YU_ARM_GPIO_LOCK_ADDR + YU_ARM_GPIO_LOCK_SIZE - 1,
80 .name = "YU_ARM_GPIO_LOCK",
83 static DEFINE_MUTEX(yu_arm_gpio_lock_mutex);
85 static struct mlxbf2_gpio_param yu_arm_gpio_lock_param = {
86 .res = &yu_arm_gpio_lock_res,
87 .lock = &yu_arm_gpio_lock_mutex,
90 /* Request memory region and map yu_arm_gpio_lock resource */
91 static int mlxbf2_gpio_get_lock_res(struct platform_device *pdev)
93 struct device *dev = &pdev->dev;
98 mutex_lock(yu_arm_gpio_lock_param.lock);
100 /* Check if the memory map already exists */
101 if (yu_arm_gpio_lock_param.io)
104 res = yu_arm_gpio_lock_param.res;
105 size = resource_size(res);
107 if (!devm_request_mem_region(dev, res->start, size, res->name)) {
112 yu_arm_gpio_lock_param.io = devm_ioremap(dev, res->start, size);
113 if (IS_ERR(yu_arm_gpio_lock_param.io))
114 ret = PTR_ERR(yu_arm_gpio_lock_param.io);
117 mutex_unlock(yu_arm_gpio_lock_param.lock);
123 * Acquire the YU arm_gpio_lock to be able to change the direction
124 * mode. If the lock_active bit is already set, return an error.
126 static int mlxbf2_gpio_lock_acquire(struct mlxbf2_gpio_context *gs)
128 u32 arm_gpio_lock_val;
130 spin_lock(&gs->gc.bgpio_lock);
131 mutex_lock(yu_arm_gpio_lock_param.lock);
133 arm_gpio_lock_val = readl(yu_arm_gpio_lock_param.io);
136 * When lock active bit[31] is set, ModeX is write enabled
138 if (YU_LOCK_ACTIVE_BIT(arm_gpio_lock_val)) {
139 mutex_unlock(yu_arm_gpio_lock_param.lock);
140 spin_unlock(&gs->gc.bgpio_lock);
144 writel(YU_ARM_GPIO_LOCK_ACQUIRE, yu_arm_gpio_lock_param.io);
150 * Release the YU arm_gpio_lock after changing the direction mode.
152 static void mlxbf2_gpio_lock_release(struct mlxbf2_gpio_context *gs)
154 writel(YU_ARM_GPIO_LOCK_RELEASE, yu_arm_gpio_lock_param.io);
155 mutex_unlock(yu_arm_gpio_lock_param.lock);
156 spin_unlock(&gs->gc.bgpio_lock);
160 * mode0 and mode1 are both locked by the gpio_lock field.
162 * Together, mode0 and mode1 define the gpio Mode dependeing also
165 * {mode1,mode0}:{Reg_DataOut=0,Reg_DataOut=1}->{DataOut=0,DataOut=1}
167 * {0,0}:Reg_DataOut{0,1}->{Z,Z} Input PAD
168 * {0,1}:Reg_DataOut{0,1}->{0,1} Full drive Output PAD
169 * {1,0}:Reg_DataOut{0,1}->{0,Z} 0-set PAD to low, 1-float
170 * {1,1}:Reg_DataOut{0,1}->{Z,1} 0-float, 1-set PAD to high
174 * Set input direction:
175 * {mode1,mode0} = {0,0}
177 static int mlxbf2_gpio_direction_input(struct gpio_chip *chip,
180 struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
184 * Although the arm_gpio_lock was set in the probe function, check again
185 * if it is still enabled to be able to write to the ModeX registers.
187 ret = mlxbf2_gpio_lock_acquire(gs);
191 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_CLEAR);
192 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
194 mlxbf2_gpio_lock_release(gs);
200 * Set output direction:
201 * {mode1,mode0} = {0,1}
203 static int mlxbf2_gpio_direction_output(struct gpio_chip *chip,
207 struct mlxbf2_gpio_context *gs = gpiochip_get_data(chip);
211 * Although the arm_gpio_lock was set in the probe function,
212 * check again it is still enabled to be able to write to the
215 ret = mlxbf2_gpio_lock_acquire(gs);
219 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE1_CLEAR);
220 writel(BIT(offset), gs->gpio_io + YU_GPIO_MODE0_SET);
222 mlxbf2_gpio_lock_release(gs);
227 /* BlueField-2 GPIO driver initialization routine. */
229 mlxbf2_gpio_probe(struct platform_device *pdev)
231 struct mlxbf2_gpio_context *gs;
232 struct device *dev = &pdev->dev;
233 struct gpio_chip *gc;
234 struct resource *res;
238 gs = devm_kzalloc(dev, sizeof(*gs), GFP_KERNEL);
242 /* YU GPIO block address */
243 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
247 gs->gpio_io = devm_ioremap(dev, res->start, resource_size(res));
251 ret = mlxbf2_gpio_get_lock_res(pdev);
253 dev_err(dev, "Failed to get yu_arm_gpio_lock resource\n");
257 if (device_property_read_u32(dev, "npins", &npins))
258 npins = MLXBF2_GPIO_MAX_PINS_PER_BLOCK;
262 ret = bgpio_init(gc, dev, 4,
263 gs->gpio_io + YU_GPIO_DATAIN,
264 gs->gpio_io + YU_GPIO_DATASET,
265 gs->gpio_io + YU_GPIO_DATACLEAR,
270 gc->direction_input = mlxbf2_gpio_direction_input;
271 gc->direction_output = mlxbf2_gpio_direction_output;
273 gc->owner = THIS_MODULE;
275 platform_set_drvdata(pdev, gs);
277 ret = devm_gpiochip_add_data(dev, &gs->gc, gs);
279 dev_err(dev, "Failed adding memory mapped gpiochip\n");
287 static int mlxbf2_gpio_suspend(struct platform_device *pdev,
290 struct mlxbf2_gpio_context *gs = platform_get_drvdata(pdev);
292 gs->csave_regs->gpio_mode0 = readl(gs->gpio_io +
294 gs->csave_regs->gpio_mode1 = readl(gs->gpio_io +
300 static int mlxbf2_gpio_resume(struct platform_device *pdev)
302 struct mlxbf2_gpio_context *gs = platform_get_drvdata(pdev);
304 writel(gs->csave_regs->gpio_mode0, gs->gpio_io +
306 writel(gs->csave_regs->gpio_mode1, gs->gpio_io +
313 static const struct acpi_device_id mlxbf2_gpio_acpi_match[] = {
317 MODULE_DEVICE_TABLE(acpi, mlxbf2_gpio_acpi_match);
319 static struct platform_driver mlxbf2_gpio_driver = {
321 .name = "mlxbf2_gpio",
322 .acpi_match_table = ACPI_PTR(mlxbf2_gpio_acpi_match),
324 .probe = mlxbf2_gpio_probe,
326 .suspend = mlxbf2_gpio_suspend,
327 .resume = mlxbf2_gpio_resume,
331 module_platform_driver(mlxbf2_gpio_driver);
333 MODULE_DESCRIPTION("Mellanox BlueField-2 GPIO Driver");
334 MODULE_AUTHOR("Mellanox Technologies");
335 MODULE_LICENSE("GPL v2");