Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / gpio / gpio-loongson1.c
blob6ca3b969db4df231517d021a7b4b5e3ddcf626f7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * GPIO Driver for Loongson 1 SoC
5 * Copyright (C) 2015-2023 Keguang Zhang <keguang.zhang@gmail.com>
6 */
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 */
14 #define GPIO_CFG 0x0
15 #define GPIO_DIR 0x10
16 #define GPIO_DATA 0x20
17 #define GPIO_OUTPUT 0x30
19 struct ls1x_gpio_chip {
20 struct gpio_chip gc;
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);
27 unsigned long flags;
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);
34 return 0;
37 static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
39 struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
40 unsigned long flags;
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;
52 int ret;
54 ls1x_gc = devm_kzalloc(dev, sizeof(*ls1x_gc), GFP_KERNEL);
55 if (!ls1x_gc)
56 return -ENOMEM;
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);
65 if (ret)
66 goto err;
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);
78 if (ret)
79 goto err;
81 platform_set_drvdata(pdev, ls1x_gc);
83 dev_info(dev, "GPIO controller registered with %d pins\n",
84 ls1x_gc->gc.ngpio);
86 return 0;
87 err:
88 dev_err(dev, "failed to register GPIO controller\n");
89 return ret;
92 static const struct of_device_id ls1x_gpio_dt_ids[] = {
93 { .compatible = "loongson,ls1x-gpio" },
94 { /* sentinel */ }
96 MODULE_DEVICE_TABLE(of, ls1x_gpio_dt_ids);
98 static struct platform_driver ls1x_gpio_driver = {
99 .probe = ls1x_gpio_probe,
100 .driver = {
101 .name = "ls1x-gpio",
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");