Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 2 | /* |
| 3 | * GPIO controller in LSI ZEVIO SoCs. |
| 4 | * |
| 5 | * Author: Fabian Vogt <fabian@ritter-vogt.de> |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/spinlock.h> |
| 9 | #include <linux/errno.h> |
Paul Gortmaker | a90295b | 2016-05-09 19:59:58 -0400 | [diff] [blame] | 10 | #include <linux/init.h> |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 11 | #include <linux/bitops.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/of_device.h> |
| 14 | #include <linux/of_gpio.h> |
| 15 | #include <linux/slab.h> |
Linus Walleij | f091616 | 2018-09-03 00:18:51 +0200 | [diff] [blame] | 16 | #include <linux/gpio/driver.h> |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Memory layout: |
| 20 | * This chip has four gpio sections, each controls 8 GPIOs. |
| 21 | * Bit 0 in section 0 is GPIO 0, bit 2 in section 1 is GPIO 10. |
| 22 | * Disclaimer: Reverse engineered! |
| 23 | * For more information refer to: |
| 24 | * http://hackspire.unsads.com/wiki/index.php/Memory-mapped_I/O_ports#90000000_-_General_Purpose_I.2FO_.28GPIO.29 |
| 25 | * |
| 26 | * 0x00-0x3F: Section 0 |
| 27 | * +0x00: Masked interrupt status (read-only) |
| 28 | * +0x04: R: Interrupt status W: Reset interrupt status |
| 29 | * +0x08: R: Interrupt mask W: Mask interrupt |
| 30 | * +0x0C: W: Unmask interrupt (write-only) |
| 31 | * +0x10: Direction: I/O=1/0 |
| 32 | * +0x14: Output |
| 33 | * +0x18: Input (read-only) |
| 34 | * +0x20: R: Level interrupt W: Set as level interrupt |
| 35 | * 0x40-0x7F: Section 1 |
| 36 | * 0x80-0xBF: Section 2 |
| 37 | * 0xC0-0xFF: Section 3 |
| 38 | */ |
| 39 | |
| 40 | #define ZEVIO_GPIO_SECTION_SIZE 0x40 |
| 41 | |
| 42 | /* Offsets to various registers */ |
| 43 | #define ZEVIO_GPIO_INT_MASKED_STATUS 0x00 |
| 44 | #define ZEVIO_GPIO_INT_STATUS 0x04 |
| 45 | #define ZEVIO_GPIO_INT_UNMASK 0x08 |
| 46 | #define ZEVIO_GPIO_INT_MASK 0x0C |
| 47 | #define ZEVIO_GPIO_DIRECTION 0x10 |
| 48 | #define ZEVIO_GPIO_OUTPUT 0x14 |
| 49 | #define ZEVIO_GPIO_INPUT 0x18 |
| 50 | #define ZEVIO_GPIO_INT_STICKY 0x20 |
| 51 | |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 52 | /* Bit number of GPIO in its section */ |
| 53 | #define ZEVIO_GPIO_BIT(gpio) (gpio&7) |
| 54 | |
| 55 | struct zevio_gpio { |
| 56 | spinlock_t lock; |
| 57 | struct of_mm_gpio_chip chip; |
| 58 | }; |
| 59 | |
| 60 | static inline u32 zevio_gpio_port_get(struct zevio_gpio *c, unsigned pin, |
| 61 | unsigned port_offset) |
| 62 | { |
| 63 | unsigned section_offset = ((pin >> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE; |
| 64 | return readl(IOMEM(c->chip.regs + section_offset + port_offset)); |
| 65 | } |
| 66 | |
| 67 | static inline void zevio_gpio_port_set(struct zevio_gpio *c, unsigned pin, |
| 68 | unsigned port_offset, u32 val) |
| 69 | { |
| 70 | unsigned section_offset = ((pin >> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE; |
| 71 | writel(val, IOMEM(c->chip.regs + section_offset + port_offset)); |
| 72 | } |
| 73 | |
| 74 | /* Functions for struct gpio_chip */ |
| 75 | static int zevio_gpio_get(struct gpio_chip *chip, unsigned pin) |
| 76 | { |
Linus Walleij | 9a3ad66 | 2015-12-07 15:26:02 +0100 | [diff] [blame] | 77 | struct zevio_gpio *controller = gpiochip_get_data(chip); |
Axel Lin | ef7de26 | 2014-04-08 07:59:39 +0800 | [diff] [blame] | 78 | u32 val, dir; |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 79 | |
Axel Lin | ef7de26 | 2014-04-08 07:59:39 +0800 | [diff] [blame] | 80 | spin_lock(&controller->lock); |
| 81 | dir = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION); |
| 82 | if (dir & BIT(ZEVIO_GPIO_BIT(pin))) |
| 83 | val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_INPUT); |
| 84 | else |
| 85 | val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT); |
| 86 | spin_unlock(&controller->lock); |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 87 | |
| 88 | return (val >> ZEVIO_GPIO_BIT(pin)) & 0x1; |
| 89 | } |
| 90 | |
| 91 | static void zevio_gpio_set(struct gpio_chip *chip, unsigned pin, int value) |
| 92 | { |
Linus Walleij | 9a3ad66 | 2015-12-07 15:26:02 +0100 | [diff] [blame] | 93 | struct zevio_gpio *controller = gpiochip_get_data(chip); |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 94 | u32 val; |
| 95 | |
| 96 | spin_lock(&controller->lock); |
| 97 | val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT); |
| 98 | if (value) |
| 99 | val |= BIT(ZEVIO_GPIO_BIT(pin)); |
| 100 | else |
| 101 | val &= ~BIT(ZEVIO_GPIO_BIT(pin)); |
| 102 | |
| 103 | zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_OUTPUT, val); |
| 104 | spin_unlock(&controller->lock); |
| 105 | } |
| 106 | |
| 107 | static int zevio_gpio_direction_input(struct gpio_chip *chip, unsigned pin) |
| 108 | { |
Linus Walleij | 9a3ad66 | 2015-12-07 15:26:02 +0100 | [diff] [blame] | 109 | struct zevio_gpio *controller = gpiochip_get_data(chip); |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 110 | u32 val; |
| 111 | |
| 112 | spin_lock(&controller->lock); |
| 113 | |
| 114 | val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION); |
| 115 | val |= BIT(ZEVIO_GPIO_BIT(pin)); |
| 116 | zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_DIRECTION, val); |
| 117 | |
| 118 | spin_unlock(&controller->lock); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int zevio_gpio_direction_output(struct gpio_chip *chip, |
| 124 | unsigned pin, int value) |
| 125 | { |
Linus Walleij | 9a3ad66 | 2015-12-07 15:26:02 +0100 | [diff] [blame] | 126 | struct zevio_gpio *controller = gpiochip_get_data(chip); |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 127 | u32 val; |
| 128 | |
| 129 | spin_lock(&controller->lock); |
| 130 | val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT); |
| 131 | if (value) |
| 132 | val |= BIT(ZEVIO_GPIO_BIT(pin)); |
| 133 | else |
| 134 | val &= ~BIT(ZEVIO_GPIO_BIT(pin)); |
| 135 | |
| 136 | zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_OUTPUT, val); |
| 137 | val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION); |
| 138 | val &= ~BIT(ZEVIO_GPIO_BIT(pin)); |
| 139 | zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_DIRECTION, val); |
| 140 | |
| 141 | spin_unlock(&controller->lock); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int zevio_gpio_to_irq(struct gpio_chip *chip, unsigned pin) |
| 147 | { |
| 148 | /* |
| 149 | * TODO: Implement IRQs. |
| 150 | * Not implemented yet due to weird lockups |
| 151 | */ |
| 152 | |
| 153 | return -ENXIO; |
| 154 | } |
| 155 | |
Bhumika Goyal | 4a14af4 | 2017-08-20 23:57:46 +0530 | [diff] [blame] | 156 | static const struct gpio_chip zevio_gpio_chip = { |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 157 | .direction_input = zevio_gpio_direction_input, |
| 158 | .direction_output = zevio_gpio_direction_output, |
| 159 | .set = zevio_gpio_set, |
| 160 | .get = zevio_gpio_get, |
| 161 | .to_irq = zevio_gpio_to_irq, |
| 162 | .base = 0, |
| 163 | .owner = THIS_MODULE, |
| 164 | .ngpio = 32, |
| 165 | .of_gpio_n_cells = 2, |
| 166 | }; |
| 167 | |
| 168 | /* Initialization */ |
| 169 | static int zevio_gpio_probe(struct platform_device *pdev) |
| 170 | { |
| 171 | struct zevio_gpio *controller; |
| 172 | int status, i; |
| 173 | |
| 174 | controller = devm_kzalloc(&pdev->dev, sizeof(*controller), GFP_KERNEL); |
Jingoo Han | 50908d6 | 2014-04-29 17:46:22 +0900 | [diff] [blame] | 175 | if (!controller) |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 176 | return -ENOMEM; |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 177 | |
Ricardo Ribalda Delgado | ff00be6 | 2015-01-18 12:39:31 +0100 | [diff] [blame] | 178 | platform_set_drvdata(pdev, controller); |
| 179 | |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 180 | /* Copy our reference */ |
| 181 | controller->chip.gc = zevio_gpio_chip; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 182 | controller->chip.gc.parent = &pdev->dev; |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 183 | |
Linus Walleij | 9a3ad66 | 2015-12-07 15:26:02 +0100 | [diff] [blame] | 184 | status = of_mm_gpiochip_add_data(pdev->dev.of_node, |
| 185 | &(controller->chip), |
| 186 | controller); |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 187 | if (status) { |
| 188 | dev_err(&pdev->dev, "failed to add gpiochip: %d\n", status); |
| 189 | return status; |
| 190 | } |
| 191 | |
| 192 | spin_lock_init(&controller->lock); |
| 193 | |
| 194 | /* Disable interrupts, they only cause errors */ |
| 195 | for (i = 0; i < controller->chip.gc.ngpio; i += 8) |
| 196 | zevio_gpio_port_set(controller, i, ZEVIO_GPIO_INT_MASK, 0xFF); |
| 197 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 198 | dev_dbg(controller->chip.gc.parent, "ZEVIO GPIO controller set up!\n"); |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Jingoo Han | a49f2e7 | 2014-05-07 18:09:01 +0900 | [diff] [blame] | 203 | static const struct of_device_id zevio_gpio_of_match[] = { |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 204 | { .compatible = "lsi,zevio-gpio", }, |
| 205 | { }, |
| 206 | }; |
| 207 | |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 208 | static struct platform_driver zevio_gpio_driver = { |
| 209 | .driver = { |
| 210 | .name = "gpio-zevio", |
Axel Lin | 9ea8d81 | 2014-04-08 11:44:49 +0800 | [diff] [blame] | 211 | .of_match_table = zevio_gpio_of_match, |
Paul Gortmaker | a90295b | 2016-05-09 19:59:58 -0400 | [diff] [blame] | 212 | .suppress_bind_attrs = true, |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 213 | }, |
| 214 | .probe = zevio_gpio_probe, |
Fabian Vogt | 9af4d80 | 2014-02-24 20:54:58 +0100 | [diff] [blame] | 215 | }; |
Paul Gortmaker | a90295b | 2016-05-09 19:59:58 -0400 | [diff] [blame] | 216 | builtin_platform_driver(zevio_gpio_driver); |