1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO Driver for Loongson 1 SoC
5 * Copyright (C) 2015-2023 Keguang Zhang <keguang.zhang@gmail.com>
8 #include <linux/module.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/platform_device.h>
11 #include <linux/bitops.h>
13 /* Loongson 1 GPIO Register Definitions */
16 #define GPIO_DATA 0x20
17 #define GPIO_OUTPUT 0x30
19 struct ls1x_gpio_chip {
21 void __iomem *reg_base;
24 static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
26 struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
29 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
30 __raw_writel(__raw_readl(ls1x_gc->reg_base + GPIO_CFG) | BIT(offset),
31 ls1x_gc->reg_base + GPIO_CFG);
32 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
37 static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
39 struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
42 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
43 __raw_writel(__raw_readl(ls1x_gc->reg_base + GPIO_CFG) & ~BIT(offset),
44 ls1x_gc->reg_base + GPIO_CFG);
45 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
48 static int ls1x_gpio_probe(struct platform_device *pdev)
50 struct device *dev = &pdev->dev;
51 struct ls1x_gpio_chip *ls1x_gc;
54 ls1x_gc = devm_kzalloc(dev, sizeof(*ls1x_gc), GFP_KERNEL);
58 ls1x_gc->reg_base = devm_platform_ioremap_resource(pdev, 0);
59 if (IS_ERR(ls1x_gc->reg_base))
60 return PTR_ERR(ls1x_gc->reg_base);
62 ret = bgpio_init(&ls1x_gc->gc, dev, 4, ls1x_gc->reg_base + GPIO_DATA,
63 ls1x_gc->reg_base + GPIO_OUTPUT, NULL,
64 NULL, ls1x_gc->reg_base + GPIO_DIR, 0);
68 ls1x_gc->gc.owner = THIS_MODULE;
69 ls1x_gc->gc.request = ls1x_gpio_request;
70 ls1x_gc->gc.free = ls1x_gpio_free;
72 * Clear ngpio to let gpiolib get the correct number
73 * by reading ngpios property
75 ls1x_gc->gc.ngpio = 0;
77 ret = devm_gpiochip_add_data(dev, &ls1x_gc->gc, ls1x_gc);
81 platform_set_drvdata(pdev, ls1x_gc);
83 dev_info(dev, "GPIO controller registered with %d pins\n",
88 dev_err(dev, "failed to register GPIO controller\n");
92 static const struct of_device_id ls1x_gpio_dt_ids[] = {
93 { .compatible = "loongson,ls1x-gpio" },
96 MODULE_DEVICE_TABLE(of, ls1x_gpio_dt_ids);
98 static struct platform_driver ls1x_gpio_driver = {
99 .probe = ls1x_gpio_probe,
102 .of_match_table = ls1x_gpio_dt_ids,
106 module_platform_driver(ls1x_gpio_driver);
108 MODULE_AUTHOR("Keguang Zhang <keguang.zhang@gmail.com>");
109 MODULE_DESCRIPTION("Loongson1 GPIO driver");
110 MODULE_LICENSE("GPL");