xref: /openbmc/linux/drivers/gpio/gpio-loongson1.c (revision 2a598d0b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * GPIO Driver for Loongson 1 SoC
4  *
5  * Copyright (C) 2015-2023 Keguang Zhang <keguang.zhang@gmail.com>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/platform_device.h>
11 #include <linux/bitops.h>
12 
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
18 
19 struct ls1x_gpio_chip {
20 	struct gpio_chip gc;
21 	void __iomem *reg_base;
22 };
23 
24 static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
25 {
26 	struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
27 	unsigned long flags;
28 
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);
33 
34 	return 0;
35 }
36 
37 static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
38 {
39 	struct ls1x_gpio_chip *ls1x_gc = gpiochip_get_data(gc);
40 	unsigned long flags;
41 
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);
46 }
47 
48 static int ls1x_gpio_probe(struct platform_device *pdev)
49 {
50 	struct device *dev = &pdev->dev;
51 	struct ls1x_gpio_chip *ls1x_gc;
52 	int ret;
53 
54 	ls1x_gc = devm_kzalloc(dev, sizeof(*ls1x_gc), GFP_KERNEL);
55 	if (!ls1x_gc)
56 		return -ENOMEM;
57 
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);
61 
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;
67 
68 	ls1x_gc->gc.owner = THIS_MODULE;
69 	ls1x_gc->gc.request = ls1x_gpio_request;
70 	ls1x_gc->gc.free = ls1x_gpio_free;
71 	/*
72 	 * Clear ngpio to let gpiolib get the correct number
73 	 * by reading ngpios property
74 	 */
75 	ls1x_gc->gc.ngpio = 0;
76 
77 	ret = devm_gpiochip_add_data(dev, &ls1x_gc->gc, ls1x_gc);
78 	if (ret)
79 		goto err;
80 
81 	platform_set_drvdata(pdev, ls1x_gc);
82 
83 	dev_info(dev, "GPIO controller registered with %d pins\n",
84 		 ls1x_gc->gc.ngpio);
85 
86 	return 0;
87 err:
88 	dev_err(dev, "failed to register GPIO controller\n");
89 	return ret;
90 }
91 
92 static const struct of_device_id ls1x_gpio_dt_ids[] = {
93 	{ .compatible = "loongson,ls1x-gpio" },
94 	{ /* sentinel */ }
95 };
96 MODULE_DEVICE_TABLE(of, ls1x_gpio_dt_ids);
97 
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,
103 	},
104 };
105 
106 module_platform_driver(ls1x_gpio_driver);
107 
108 MODULE_AUTHOR("Keguang Zhang <keguang.zhang@gmail.com>");
109 MODULE_DESCRIPTION("Loongson1 GPIO driver");
110 MODULE_LICENSE("GPL");
111