1 // SPDX-License-Identifier: GPL-2.0+
3 * Loongson GPIO Support
5 * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/err.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/platform_device.h>
15 #include <linux/bitops.h>
16 #include <asm/types.h>
18 enum loongson_gpio_mode {
23 struct loongson_gpio_chip_data {
25 enum loongson_gpio_mode mode;
26 unsigned int conf_offset;
27 unsigned int out_offset;
28 unsigned int in_offset;
31 struct loongson_gpio_chip {
32 struct gpio_chip chip;
33 struct fwnode_handle *fwnode;
35 void __iomem *reg_base;
36 const struct loongson_gpio_chip_data *chip_data;
39 static inline struct loongson_gpio_chip *to_loongson_gpio_chip(struct gpio_chip *chip)
41 return container_of(chip, struct loongson_gpio_chip, chip);
44 static inline void loongson_commit_direction(struct loongson_gpio_chip *lgpio, unsigned int pin,
47 u8 bval = input ? 1 : 0;
49 writeb(bval, lgpio->reg_base + lgpio->chip_data->conf_offset + pin);
52 static void loongson_commit_level(struct loongson_gpio_chip *lgpio, unsigned int pin, int high)
54 u8 bval = high ? 1 : 0;
56 writeb(bval, lgpio->reg_base + lgpio->chip_data->out_offset + pin);
59 static int loongson_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
62 struct loongson_gpio_chip *lgpio = to_loongson_gpio_chip(chip);
64 spin_lock_irqsave(&lgpio->lock, flags);
65 loongson_commit_direction(lgpio, pin, 1);
66 spin_unlock_irqrestore(&lgpio->lock, flags);
71 static int loongson_gpio_direction_output(struct gpio_chip *chip, unsigned int pin, int value)
74 struct loongson_gpio_chip *lgpio = to_loongson_gpio_chip(chip);
76 spin_lock_irqsave(&lgpio->lock, flags);
77 loongson_commit_level(lgpio, pin, value);
78 loongson_commit_direction(lgpio, pin, 0);
79 spin_unlock_irqrestore(&lgpio->lock, flags);
84 static int loongson_gpio_get(struct gpio_chip *chip, unsigned int pin)
88 struct loongson_gpio_chip *lgpio = to_loongson_gpio_chip(chip);
90 bval = readb(lgpio->reg_base + lgpio->chip_data->in_offset + pin);
96 static int loongson_gpio_get_direction(struct gpio_chip *chip, unsigned int pin)
99 struct loongson_gpio_chip *lgpio = to_loongson_gpio_chip(chip);
101 bval = readb(lgpio->reg_base + lgpio->chip_data->conf_offset + pin);
103 return GPIO_LINE_DIRECTION_IN;
105 return GPIO_LINE_DIRECTION_OUT;
108 static void loongson_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
111 struct loongson_gpio_chip *lgpio = to_loongson_gpio_chip(chip);
113 spin_lock_irqsave(&lgpio->lock, flags);
114 loongson_commit_level(lgpio, pin, value);
115 spin_unlock_irqrestore(&lgpio->lock, flags);
118 static int loongson_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
120 struct platform_device *pdev = to_platform_device(chip->parent);
122 return platform_get_irq(pdev, offset);
125 static int loongson_gpio_init(struct device *dev, struct loongson_gpio_chip *lgpio,
126 struct device_node *np, void __iomem *reg_base)
131 lgpio->reg_base = reg_base;
133 if (lgpio->chip_data->mode == BIT_CTRL_MODE) {
134 ret = bgpio_init(&lgpio->chip, dev, 8,
135 lgpio->reg_base + lgpio->chip_data->in_offset,
136 lgpio->reg_base + lgpio->chip_data->out_offset,
138 lgpio->reg_base + lgpio->chip_data->conf_offset,
141 dev_err(dev, "unable to init generic GPIO\n");
145 lgpio->chip.direction_input = loongson_gpio_direction_input;
146 lgpio->chip.get = loongson_gpio_get;
147 lgpio->chip.get_direction = loongson_gpio_get_direction;
148 lgpio->chip.direction_output = loongson_gpio_direction_output;
149 lgpio->chip.set = loongson_gpio_set;
150 lgpio->chip.parent = dev;
151 spin_lock_init(&lgpio->lock);
154 device_property_read_u32(dev, "ngpios", &ngpios);
156 lgpio->chip.can_sleep = 0;
157 lgpio->chip.ngpio = ngpios;
158 lgpio->chip.label = lgpio->chip_data->label;
159 lgpio->chip.to_irq = loongson_gpio_to_irq;
161 return devm_gpiochip_add_data(dev, &lgpio->chip, lgpio);
164 static int loongson_gpio_probe(struct platform_device *pdev)
166 void __iomem *reg_base;
167 struct loongson_gpio_chip *lgpio;
168 struct device_node *np = pdev->dev.of_node;
169 struct device *dev = &pdev->dev;
171 lgpio = devm_kzalloc(dev, sizeof(*lgpio), GFP_KERNEL);
175 lgpio->chip_data = device_get_match_data(dev);
177 reg_base = devm_platform_ioremap_resource(pdev, 0);
178 if (IS_ERR(reg_base))
179 return PTR_ERR(reg_base);
181 return loongson_gpio_init(dev, lgpio, np, reg_base);
184 static const struct loongson_gpio_chip_data loongson_gpio_ls2k_data = {
185 .label = "ls2k_gpio",
186 .mode = BIT_CTRL_MODE,
192 static const struct loongson_gpio_chip_data loongson_gpio_ls7a_data = {
193 .label = "ls7a_gpio",
194 .mode = BYTE_CTRL_MODE,
195 .conf_offset = 0x800,
200 static const struct of_device_id loongson_gpio_of_match[] = {
202 .compatible = "loongson,ls2k-gpio",
203 .data = &loongson_gpio_ls2k_data,
206 .compatible = "loongson,ls7a-gpio",
207 .data = &loongson_gpio_ls7a_data,
211 MODULE_DEVICE_TABLE(of, loongson_gpio_of_match);
213 static const struct acpi_device_id loongson_gpio_acpi_match[] = {
216 .driver_data = (kernel_ulong_t)&loongson_gpio_ls7a_data,
220 MODULE_DEVICE_TABLE(acpi, loongson_gpio_acpi_match);
222 static struct platform_driver loongson_gpio_driver = {
224 .name = "loongson-gpio",
225 .of_match_table = loongson_gpio_of_match,
226 .acpi_match_table = loongson_gpio_acpi_match,
228 .probe = loongson_gpio_probe,
231 static int __init loongson_gpio_setup(void)
233 return platform_driver_register(&loongson_gpio_driver);
235 postcore_initcall(loongson_gpio_setup);
237 MODULE_DESCRIPTION("Loongson gpio driver");
238 MODULE_LICENSE("GPL");