1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/acpi.h>
4 #include <linux/bitops.h>
5 #include <linux/device.h>
6 #include <linux/gpio/driver.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
12 #include <linux/resource.h>
13 #include <linux/types.h>
15 /* Number of pins on BlueField */
16 #define MLXBF_GPIO_NR 54
18 /* Pad Electrical Controls. */
19 #define MLXBF_GPIO_PAD_CONTROL_FIRST_WORD 0x0700
20 #define MLXBF_GPIO_PAD_CONTROL_1_FIRST_WORD 0x0708
21 #define MLXBF_GPIO_PAD_CONTROL_2_FIRST_WORD 0x0710
22 #define MLXBF_GPIO_PAD_CONTROL_3_FIRST_WORD 0x0718
24 #define MLXBF_GPIO_PIN_DIR_I 0x1040
25 #define MLXBF_GPIO_PIN_DIR_O 0x1048
26 #define MLXBF_GPIO_PIN_STATE 0x1000
27 #define MLXBF_GPIO_SCRATCHPAD 0x20
30 struct mlxbf_gpio_context_save_regs
{
32 u64 pad_control
[MLXBF_GPIO_NR
];
38 /* Device state structure. */
39 struct mlxbf_gpio_state
{
46 struct mlxbf_gpio_context_save_regs csave_regs
;
50 static int mlxbf_gpio_probe(struct platform_device
*pdev
)
52 struct mlxbf_gpio_state
*gs
;
53 struct device
*dev
= &pdev
->dev
;
57 gs
= devm_kzalloc(&pdev
->dev
, sizeof(*gs
), GFP_KERNEL
);
61 gs
->base
= devm_platform_ioremap_resource(pdev
, 0);
63 return PTR_ERR(gs
->base
);
66 ret
= bgpio_init(gc
, dev
, 8,
67 gs
->base
+ MLXBF_GPIO_PIN_STATE
,
70 gs
->base
+ MLXBF_GPIO_PIN_DIR_O
,
71 gs
->base
+ MLXBF_GPIO_PIN_DIR_I
,
76 gc
->owner
= THIS_MODULE
;
77 gc
->ngpio
= MLXBF_GPIO_NR
;
79 ret
= devm_gpiochip_add_data(dev
, &gs
->gc
, gs
);
81 dev_err(&pdev
->dev
, "Failed adding memory mapped gpiochip\n");
85 platform_set_drvdata(pdev
, gs
);
86 dev_info(&pdev
->dev
, "registered Mellanox BlueField GPIO");
91 static int mlxbf_gpio_suspend(struct platform_device
*pdev
, pm_message_t state
)
93 struct mlxbf_gpio_state
*gs
= platform_get_drvdata(pdev
);
95 gs
->csave_regs
.scratchpad
= readq(gs
->base
+ MLXBF_GPIO_SCRATCHPAD
);
96 gs
->csave_regs
.pad_control
[0] =
97 readq(gs
->base
+ MLXBF_GPIO_PAD_CONTROL_FIRST_WORD
);
98 gs
->csave_regs
.pad_control
[1] =
99 readq(gs
->base
+ MLXBF_GPIO_PAD_CONTROL_1_FIRST_WORD
);
100 gs
->csave_regs
.pad_control
[2] =
101 readq(gs
->base
+ MLXBF_GPIO_PAD_CONTROL_2_FIRST_WORD
);
102 gs
->csave_regs
.pad_control
[3] =
103 readq(gs
->base
+ MLXBF_GPIO_PAD_CONTROL_3_FIRST_WORD
);
104 gs
->csave_regs
.pin_dir_i
= readq(gs
->base
+ MLXBF_GPIO_PIN_DIR_I
);
105 gs
->csave_regs
.pin_dir_o
= readq(gs
->base
+ MLXBF_GPIO_PIN_DIR_O
);
110 static int mlxbf_gpio_resume(struct platform_device
*pdev
)
112 struct mlxbf_gpio_state
*gs
= platform_get_drvdata(pdev
);
114 writeq(gs
->csave_regs
.scratchpad
, gs
->base
+ MLXBF_GPIO_SCRATCHPAD
);
115 writeq(gs
->csave_regs
.pad_control
[0],
116 gs
->base
+ MLXBF_GPIO_PAD_CONTROL_FIRST_WORD
);
117 writeq(gs
->csave_regs
.pad_control
[1],
118 gs
->base
+ MLXBF_GPIO_PAD_CONTROL_1_FIRST_WORD
);
119 writeq(gs
->csave_regs
.pad_control
[2],
120 gs
->base
+ MLXBF_GPIO_PAD_CONTROL_2_FIRST_WORD
);
121 writeq(gs
->csave_regs
.pad_control
[3],
122 gs
->base
+ MLXBF_GPIO_PAD_CONTROL_3_FIRST_WORD
);
123 writeq(gs
->csave_regs
.pin_dir_i
, gs
->base
+ MLXBF_GPIO_PIN_DIR_I
);
124 writeq(gs
->csave_regs
.pin_dir_o
, gs
->base
+ MLXBF_GPIO_PIN_DIR_O
);
130 static const struct acpi_device_id mlxbf_gpio_acpi_match
[] = {
134 MODULE_DEVICE_TABLE(acpi
, mlxbf_gpio_acpi_match
);
136 static struct platform_driver mlxbf_gpio_driver
= {
138 .name
= "mlxbf_gpio",
139 .acpi_match_table
= ACPI_PTR(mlxbf_gpio_acpi_match
),
141 .probe
= mlxbf_gpio_probe
,
143 .suspend
= mlxbf_gpio_suspend
,
144 .resume
= mlxbf_gpio_resume
,
148 module_platform_driver(mlxbf_gpio_driver
);
150 MODULE_DESCRIPTION("Mellanox BlueField GPIO Driver");
151 MODULE_AUTHOR("Mellanox Technologies");
152 MODULE_LICENSE("GPL");