Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / gpio / gpio-zevio.c
blobd7230fd83f5d68a9d80352b7a57e12cd21c389ce
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * GPIO controller in LSI ZEVIO SoCs.
5 * Author: Fabian Vogt <fabian@ritter-vogt.de>
6 */
8 #include <linux/bitops.h>
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/io.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
18 #include <linux/gpio/driver.h>
21 * Memory layout:
22 * This chip has four gpio sections, each controls 8 GPIOs.
23 * Bit 0 in section 0 is GPIO 0, bit 2 in section 1 is GPIO 10.
24 * Disclaimer: Reverse engineered!
25 * For more information refer to:
26 * http://hackspire.unsads.com/wiki/index.php/Memory-mapped_I/O_ports#90000000_-_General_Purpose_I.2FO_.28GPIO.29
28 * 0x00-0x3F: Section 0
29 * +0x00: Masked interrupt status (read-only)
30 * +0x04: R: Interrupt status W: Reset interrupt status
31 * +0x08: R: Interrupt mask W: Mask interrupt
32 * +0x0C: W: Unmask interrupt (write-only)
33 * +0x10: Direction: I/O=1/0
34 * +0x14: Output
35 * +0x18: Input (read-only)
36 * +0x20: R: Level interrupt W: Set as level interrupt
37 * 0x40-0x7F: Section 1
38 * 0x80-0xBF: Section 2
39 * 0xC0-0xFF: Section 3
42 #define ZEVIO_GPIO_SECTION_SIZE 0x40
44 /* Offsets to various registers */
45 #define ZEVIO_GPIO_INT_MASKED_STATUS 0x00
46 #define ZEVIO_GPIO_INT_STATUS 0x04
47 #define ZEVIO_GPIO_INT_UNMASK 0x08
48 #define ZEVIO_GPIO_INT_MASK 0x0C
49 #define ZEVIO_GPIO_DIRECTION 0x10
50 #define ZEVIO_GPIO_OUTPUT 0x14
51 #define ZEVIO_GPIO_INPUT 0x18
52 #define ZEVIO_GPIO_INT_STICKY 0x20
54 /* Bit number of GPIO in its section */
55 #define ZEVIO_GPIO_BIT(gpio) (gpio&7)
57 struct zevio_gpio {
58 struct gpio_chip chip;
59 spinlock_t lock;
60 void __iomem *regs;
63 static inline u32 zevio_gpio_port_get(struct zevio_gpio *c, unsigned pin,
64 unsigned port_offset)
66 unsigned section_offset = ((pin >> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE;
67 return readl(IOMEM(c->regs + section_offset + port_offset));
70 static inline void zevio_gpio_port_set(struct zevio_gpio *c, unsigned pin,
71 unsigned port_offset, u32 val)
73 unsigned section_offset = ((pin >> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE;
74 writel(val, IOMEM(c->regs + section_offset + port_offset));
77 /* Functions for struct gpio_chip */
78 static int zevio_gpio_get(struct gpio_chip *chip, unsigned pin)
80 struct zevio_gpio *controller = gpiochip_get_data(chip);
81 u32 val, dir;
83 spin_lock(&controller->lock);
84 dir = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION);
85 if (dir & BIT(ZEVIO_GPIO_BIT(pin)))
86 val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_INPUT);
87 else
88 val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT);
89 spin_unlock(&controller->lock);
91 return (val >> ZEVIO_GPIO_BIT(pin)) & 0x1;
94 static void zevio_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
96 struct zevio_gpio *controller = gpiochip_get_data(chip);
97 u32 val;
99 spin_lock(&controller->lock);
100 val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT);
101 if (value)
102 val |= BIT(ZEVIO_GPIO_BIT(pin));
103 else
104 val &= ~BIT(ZEVIO_GPIO_BIT(pin));
106 zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_OUTPUT, val);
107 spin_unlock(&controller->lock);
110 static int zevio_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
112 struct zevio_gpio *controller = gpiochip_get_data(chip);
113 u32 val;
115 spin_lock(&controller->lock);
117 val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION);
118 val |= BIT(ZEVIO_GPIO_BIT(pin));
119 zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_DIRECTION, val);
121 spin_unlock(&controller->lock);
123 return 0;
126 static int zevio_gpio_direction_output(struct gpio_chip *chip,
127 unsigned pin, int value)
129 struct zevio_gpio *controller = gpiochip_get_data(chip);
130 u32 val;
132 spin_lock(&controller->lock);
133 val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT);
134 if (value)
135 val |= BIT(ZEVIO_GPIO_BIT(pin));
136 else
137 val &= ~BIT(ZEVIO_GPIO_BIT(pin));
139 zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_OUTPUT, val);
140 val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION);
141 val &= ~BIT(ZEVIO_GPIO_BIT(pin));
142 zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_DIRECTION, val);
144 spin_unlock(&controller->lock);
146 return 0;
149 static int zevio_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
152 * TODO: Implement IRQs.
153 * Not implemented yet due to weird lockups
156 return -ENXIO;
159 static const struct gpio_chip zevio_gpio_chip = {
160 .direction_input = zevio_gpio_direction_input,
161 .direction_output = zevio_gpio_direction_output,
162 .set = zevio_gpio_set,
163 .get = zevio_gpio_get,
164 .to_irq = zevio_gpio_to_irq,
165 .base = 0,
166 .owner = THIS_MODULE,
167 .ngpio = 32,
170 /* Initialization */
171 static int zevio_gpio_probe(struct platform_device *pdev)
173 struct device *dev = &pdev->dev;
174 struct zevio_gpio *controller;
175 int status, i;
177 controller = devm_kzalloc(&pdev->dev, sizeof(*controller), GFP_KERNEL);
178 if (!controller)
179 return -ENOMEM;
181 /* Copy our reference */
182 controller->chip = zevio_gpio_chip;
183 controller->chip.parent = &pdev->dev;
185 controller->chip.label = devm_kasprintf(dev, GFP_KERNEL, "%pfw", dev_fwnode(dev));
186 if (!controller->chip.label)
187 return -ENOMEM;
189 controller->regs = devm_platform_ioremap_resource(pdev, 0);
190 if (IS_ERR(controller->regs))
191 return dev_err_probe(&pdev->dev, PTR_ERR(controller->regs),
192 "failed to ioremap memory resource\n");
194 status = devm_gpiochip_add_data(&pdev->dev, &controller->chip, controller);
195 if (status) {
196 dev_err(&pdev->dev, "failed to add gpiochip: %d\n", status);
197 return status;
200 spin_lock_init(&controller->lock);
202 /* Disable interrupts, they only cause errors */
203 for (i = 0; i < controller->chip.ngpio; i += 8)
204 zevio_gpio_port_set(controller, i, ZEVIO_GPIO_INT_MASK, 0xFF);
206 dev_dbg(controller->chip.parent, "ZEVIO GPIO controller set up!\n");
208 return 0;
211 static const struct of_device_id zevio_gpio_of_match[] = {
212 { .compatible = "lsi,zevio-gpio", },
213 { },
216 static struct platform_driver zevio_gpio_driver = {
217 .driver = {
218 .name = "gpio-zevio",
219 .of_match_table = zevio_gpio_of_match,
220 .suppress_bind_attrs = true,
222 .probe = zevio_gpio_probe,
224 builtin_platform_driver(zevio_gpio_driver);