r8152: fix tx packets accounting
[linux/fpc-iii.git] / drivers / gpio / gpio-zevio.c
blobe23ef7b9451d9546bb6fe7a7dc8e562d9c2a1fe4
1 /*
2 * GPIO controller in LSI ZEVIO SoCs.
4 * Author: Fabian Vogt <fabian@ritter-vogt.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/spinlock.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/bitops.h>
15 #include <linux/io.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/slab.h>
19 #include <linux/gpio.h>
22 * Memory layout:
23 * This chip has four gpio sections, each controls 8 GPIOs.
24 * Bit 0 in section 0 is GPIO 0, bit 2 in section 1 is GPIO 10.
25 * Disclaimer: Reverse engineered!
26 * For more information refer to:
27 * http://hackspire.unsads.com/wiki/index.php/Memory-mapped_I/O_ports#90000000_-_General_Purpose_I.2FO_.28GPIO.29
29 * 0x00-0x3F: Section 0
30 * +0x00: Masked interrupt status (read-only)
31 * +0x04: R: Interrupt status W: Reset interrupt status
32 * +0x08: R: Interrupt mask W: Mask interrupt
33 * +0x0C: W: Unmask interrupt (write-only)
34 * +0x10: Direction: I/O=1/0
35 * +0x14: Output
36 * +0x18: Input (read-only)
37 * +0x20: R: Level interrupt W: Set as level interrupt
38 * 0x40-0x7F: Section 1
39 * 0x80-0xBF: Section 2
40 * 0xC0-0xFF: Section 3
43 #define ZEVIO_GPIO_SECTION_SIZE 0x40
45 /* Offsets to various registers */
46 #define ZEVIO_GPIO_INT_MASKED_STATUS 0x00
47 #define ZEVIO_GPIO_INT_STATUS 0x04
48 #define ZEVIO_GPIO_INT_UNMASK 0x08
49 #define ZEVIO_GPIO_INT_MASK 0x0C
50 #define ZEVIO_GPIO_DIRECTION 0x10
51 #define ZEVIO_GPIO_OUTPUT 0x14
52 #define ZEVIO_GPIO_INPUT 0x18
53 #define ZEVIO_GPIO_INT_STICKY 0x20
55 /* Bit number of GPIO in its section */
56 #define ZEVIO_GPIO_BIT(gpio) (gpio&7)
58 struct zevio_gpio {
59 spinlock_t lock;
60 struct of_mm_gpio_chip chip;
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->chip.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->chip.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 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,
168 .of_gpio_n_cells = 2,
171 /* Initialization */
172 static int zevio_gpio_probe(struct platform_device *pdev)
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 platform_set_drvdata(pdev, controller);
183 /* Copy our reference */
184 controller->chip.gc = zevio_gpio_chip;
185 controller->chip.gc.parent = &pdev->dev;
187 status = of_mm_gpiochip_add_data(pdev->dev.of_node,
188 &(controller->chip),
189 controller);
190 if (status) {
191 dev_err(&pdev->dev, "failed to add gpiochip: %d\n", status);
192 return status;
195 spin_lock_init(&controller->lock);
197 /* Disable interrupts, they only cause errors */
198 for (i = 0; i < controller->chip.gc.ngpio; i += 8)
199 zevio_gpio_port_set(controller, i, ZEVIO_GPIO_INT_MASK, 0xFF);
201 dev_dbg(controller->chip.gc.parent, "ZEVIO GPIO controller set up!\n");
203 return 0;
206 static const struct of_device_id zevio_gpio_of_match[] = {
207 { .compatible = "lsi,zevio-gpio", },
208 { },
211 static struct platform_driver zevio_gpio_driver = {
212 .driver = {
213 .name = "gpio-zevio",
214 .of_match_table = zevio_gpio_of_match,
215 .suppress_bind_attrs = true,
217 .probe = zevio_gpio_probe,
219 builtin_platform_driver(zevio_gpio_driver);