Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 IBM Corp. |
| 3 | * |
| 4 | * Joel Stanley <joel@jms.id.au> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 12 | #include <asm/div64.h> |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/gpio/driver.h> |
| 15 | #include <linux/hashtable.h> |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/io.h> |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 20 | #include <linux/pinctrl/consumer.h> |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/string.h> |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 24 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 25 | struct aspeed_bank_props { |
| 26 | unsigned int bank; |
| 27 | u32 input; |
| 28 | u32 output; |
| 29 | }; |
| 30 | |
| 31 | struct aspeed_gpio_config { |
| 32 | unsigned int nr_gpios; |
| 33 | const struct aspeed_bank_props *props; |
| 34 | }; |
| 35 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 36 | /* |
| 37 | * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled |
| 38 | * @timer_users: Tracks the number of users for each timer |
| 39 | * |
| 40 | * The @timer_users has four elements but the first element is unused. This is |
| 41 | * to simplify accounting and indexing, as a zero value in @offset_timer |
| 42 | * represents disabled debouncing for the GPIO. Any other value for an element |
| 43 | * of @offset_timer is used as an index into @timer_users. This behaviour of |
| 44 | * the zero value aligns with the behaviour of zero built from the timer |
| 45 | * configuration registers (i.e. debouncing is disabled). |
| 46 | */ |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 47 | struct aspeed_gpio { |
| 48 | struct gpio_chip chip; |
| 49 | spinlock_t lock; |
| 50 | void __iomem *base; |
| 51 | int irq; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 52 | const struct aspeed_gpio_config *config; |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 53 | |
| 54 | u8 *offset_timer; |
| 55 | unsigned int timer_users[4]; |
| 56 | struct clk *clk; |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 57 | |
| 58 | u32 *dcache; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | struct aspeed_gpio_bank { |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 62 | uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch |
| 63 | * +4: Rd/Wr: Direction (0=in, 1=out) |
| 64 | */ |
| 65 | uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */ |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 66 | uint16_t irq_regs; |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 67 | uint16_t debounce_regs; |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 68 | uint16_t tolerance_regs; |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 69 | const char names[4][3]; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 70 | }; |
| 71 | |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 72 | /* |
| 73 | * Note: The "value" register returns the input value sampled on the |
| 74 | * line even when the GPIO is configured as an output. Since |
| 75 | * that input goes through synchronizers, writing, then reading |
| 76 | * back may not return the written value right away. |
| 77 | * |
| 78 | * The "rdata" register returns the content of the write latch |
| 79 | * and thus can be used to read back what was last written |
| 80 | * reliably. |
| 81 | */ |
| 82 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 83 | static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 }; |
| 84 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 85 | static const struct aspeed_gpio_bank aspeed_gpio_banks[] = { |
| 86 | { |
| 87 | .val_regs = 0x0000, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 88 | .rdata_reg = 0x00c0, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 89 | .irq_regs = 0x0008, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 90 | .debounce_regs = 0x0040, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 91 | .tolerance_regs = 0x001c, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 92 | .names = { "A", "B", "C", "D" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 93 | }, |
| 94 | { |
| 95 | .val_regs = 0x0020, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 96 | .rdata_reg = 0x00c4, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 97 | .irq_regs = 0x0028, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 98 | .debounce_regs = 0x0048, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 99 | .tolerance_regs = 0x003c, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 100 | .names = { "E", "F", "G", "H" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 101 | }, |
| 102 | { |
| 103 | .val_regs = 0x0070, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 104 | .rdata_reg = 0x00c8, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 105 | .irq_regs = 0x0098, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 106 | .debounce_regs = 0x00b0, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 107 | .tolerance_regs = 0x00ac, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 108 | .names = { "I", "J", "K", "L" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 109 | }, |
| 110 | { |
| 111 | .val_regs = 0x0078, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 112 | .rdata_reg = 0x00cc, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 113 | .irq_regs = 0x00e8, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 114 | .debounce_regs = 0x0100, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 115 | .tolerance_regs = 0x00fc, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 116 | .names = { "M", "N", "O", "P" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 117 | }, |
| 118 | { |
| 119 | .val_regs = 0x0080, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 120 | .rdata_reg = 0x00d0, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 121 | .irq_regs = 0x0118, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 122 | .debounce_regs = 0x0130, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 123 | .tolerance_regs = 0x012c, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 124 | .names = { "Q", "R", "S", "T" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 125 | }, |
| 126 | { |
| 127 | .val_regs = 0x0088, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 128 | .rdata_reg = 0x00d4, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 129 | .irq_regs = 0x0148, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 130 | .debounce_regs = 0x0160, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 131 | .tolerance_regs = 0x015c, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 132 | .names = { "U", "V", "W", "X" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 133 | }, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 134 | { |
| 135 | .val_regs = 0x01E0, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 136 | .rdata_reg = 0x00d8, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 137 | .irq_regs = 0x0178, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 138 | .debounce_regs = 0x0190, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 139 | .tolerance_regs = 0x018c, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 140 | .names = { "Y", "Z", "AA", "AB" }, |
| 141 | }, |
| 142 | { |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 143 | .val_regs = 0x01e8, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 144 | .rdata_reg = 0x00dc, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 145 | .irq_regs = 0x01a8, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 146 | .debounce_regs = 0x01c0, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 147 | .tolerance_regs = 0x01bc, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 148 | .names = { "AC", "", "", "" }, |
| 149 | }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 150 | }; |
| 151 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 152 | enum aspeed_gpio_reg { |
| 153 | reg_val, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 154 | reg_rdata, |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 155 | reg_dir, |
| 156 | reg_irq_enable, |
| 157 | reg_irq_type0, |
| 158 | reg_irq_type1, |
| 159 | reg_irq_type2, |
| 160 | reg_irq_status, |
| 161 | reg_debounce_sel1, |
| 162 | reg_debounce_sel2, |
| 163 | reg_tolerance, |
| 164 | }; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 165 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 166 | #define GPIO_VAL_VALUE 0x00 |
| 167 | #define GPIO_VAL_DIR 0x04 |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 168 | |
| 169 | #define GPIO_IRQ_ENABLE 0x00 |
| 170 | #define GPIO_IRQ_TYPE0 0x04 |
| 171 | #define GPIO_IRQ_TYPE1 0x08 |
| 172 | #define GPIO_IRQ_TYPE2 0x0c |
| 173 | #define GPIO_IRQ_STATUS 0x10 |
| 174 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 175 | #define GPIO_DEBOUNCE_SEL1 0x00 |
| 176 | #define GPIO_DEBOUNCE_SEL2 0x04 |
| 177 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 178 | /* This will be resolved at compile time */ |
| 179 | static inline void __iomem *bank_reg(struct aspeed_gpio *gpio, |
| 180 | const struct aspeed_gpio_bank *bank, |
| 181 | const enum aspeed_gpio_reg reg) |
| 182 | { |
| 183 | switch (reg) { |
| 184 | case reg_val: |
| 185 | return gpio->base + bank->val_regs + GPIO_VAL_VALUE; |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 186 | case reg_rdata: |
| 187 | return gpio->base + bank->rdata_reg; |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 188 | case reg_dir: |
| 189 | return gpio->base + bank->val_regs + GPIO_VAL_DIR; |
| 190 | case reg_irq_enable: |
| 191 | return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE; |
| 192 | case reg_irq_type0: |
| 193 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0; |
| 194 | case reg_irq_type1: |
| 195 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1; |
| 196 | case reg_irq_type2: |
| 197 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2; |
| 198 | case reg_irq_status: |
| 199 | return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS; |
| 200 | case reg_debounce_sel1: |
| 201 | return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1; |
| 202 | case reg_debounce_sel2: |
| 203 | return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2; |
| 204 | case reg_tolerance: |
| 205 | return gpio->base + bank->tolerance_regs; |
| 206 | } |
| 207 | BUG_ON(1); |
| 208 | } |
| 209 | |
| 210 | #define GPIO_BANK(x) ((x) >> 5) |
| 211 | #define GPIO_OFFSET(x) ((x) & 0x1f) |
| 212 | #define GPIO_BIT(x) BIT(GPIO_OFFSET(x)) |
| 213 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 214 | #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o)) |
| 215 | #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1) |
| 216 | #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0) |
| 217 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 218 | static const struct aspeed_gpio_bank *to_bank(unsigned int offset) |
| 219 | { |
| 220 | unsigned int bank = GPIO_BANK(offset); |
| 221 | |
Vasyl Gomonovych | fe13862 | 2017-12-21 16:55:10 +0100 | [diff] [blame] | 222 | WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 223 | return &aspeed_gpio_banks[bank]; |
| 224 | } |
| 225 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 226 | static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props) |
| 227 | { |
| 228 | return !(props->input || props->output); |
| 229 | } |
| 230 | |
| 231 | static inline const struct aspeed_bank_props *find_bank_props( |
| 232 | struct aspeed_gpio *gpio, unsigned int offset) |
| 233 | { |
| 234 | const struct aspeed_bank_props *props = gpio->config->props; |
| 235 | |
| 236 | while (!is_bank_props_sentinel(props)) { |
| 237 | if (props->bank == GPIO_BANK(offset)) |
| 238 | return props; |
| 239 | props++; |
| 240 | } |
| 241 | |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset) |
| 246 | { |
| 247 | const struct aspeed_bank_props *props = find_bank_props(gpio, offset); |
| 248 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 249 | unsigned int group = GPIO_OFFSET(offset) / 8; |
| 250 | |
| 251 | return bank->names[group][0] != '\0' && |
| 252 | (!props || ((props->input | props->output) & GPIO_BIT(offset))); |
| 253 | } |
| 254 | |
| 255 | static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset) |
| 256 | { |
| 257 | const struct aspeed_bank_props *props = find_bank_props(gpio, offset); |
| 258 | |
| 259 | return !props || (props->input & GPIO_BIT(offset)); |
| 260 | } |
| 261 | |
| 262 | #define have_irq(g, o) have_input((g), (o)) |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 263 | #define have_debounce(g, o) have_input((g), (o)) |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 264 | |
| 265 | static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset) |
| 266 | { |
| 267 | const struct aspeed_bank_props *props = find_bank_props(gpio, offset); |
| 268 | |
| 269 | return !props || (props->output & GPIO_BIT(offset)); |
| 270 | } |
| 271 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 272 | static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset) |
| 273 | { |
| 274 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 275 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 276 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 277 | return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, |
| 281 | int val) |
| 282 | { |
| 283 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 284 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 285 | void __iomem *addr; |
| 286 | u32 reg; |
| 287 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 288 | addr = bank_reg(gpio, bank, reg_val); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 289 | reg = gpio->dcache[GPIO_BANK(offset)]; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 290 | |
| 291 | if (val) |
| 292 | reg |= GPIO_BIT(offset); |
| 293 | else |
| 294 | reg &= ~GPIO_BIT(offset); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 295 | gpio->dcache[GPIO_BANK(offset)] = reg; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 296 | |
| 297 | iowrite32(reg, addr); |
| 298 | } |
| 299 | |
| 300 | static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, |
| 301 | int val) |
| 302 | { |
| 303 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 304 | unsigned long flags; |
| 305 | |
| 306 | spin_lock_irqsave(&gpio->lock, flags); |
| 307 | |
| 308 | __aspeed_gpio_set(gc, offset, val); |
| 309 | |
| 310 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 311 | } |
| 312 | |
| 313 | static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset) |
| 314 | { |
| 315 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 316 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 317 | unsigned long flags; |
| 318 | u32 reg; |
| 319 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 320 | if (!have_input(gpio, offset)) |
| 321 | return -ENOTSUPP; |
| 322 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 323 | spin_lock_irqsave(&gpio->lock, flags); |
| 324 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 325 | reg = ioread32(bank_reg(gpio, bank, reg_dir)); |
| 326 | iowrite32(reg & ~GPIO_BIT(offset), bank_reg(gpio, bank, reg_dir)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 327 | |
| 328 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int aspeed_gpio_dir_out(struct gpio_chip *gc, |
| 334 | unsigned int offset, int val) |
| 335 | { |
| 336 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 337 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 338 | unsigned long flags; |
| 339 | u32 reg; |
| 340 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 341 | if (!have_output(gpio, offset)) |
| 342 | return -ENOTSUPP; |
| 343 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 344 | spin_lock_irqsave(&gpio->lock, flags); |
| 345 | |
Benjamin Herrenschmidt | af79492 | 2018-05-17 18:11:56 +1000 | [diff] [blame] | 346 | __aspeed_gpio_set(gc, offset, val); |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 347 | reg = ioread32(bank_reg(gpio, bank, reg_dir)); |
| 348 | iowrite32(reg | GPIO_BIT(offset), bank_reg(gpio, bank, reg_dir)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 349 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 350 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) |
| 356 | { |
| 357 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 358 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 359 | unsigned long flags; |
| 360 | u32 val; |
| 361 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 362 | if (!have_input(gpio, offset)) |
Andrew Jeffery | 619e96f | 2017-02-02 14:58:17 +1030 | [diff] [blame] | 363 | return 0; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 364 | |
| 365 | if (!have_output(gpio, offset)) |
Andrew Jeffery | 619e96f | 2017-02-02 14:58:17 +1030 | [diff] [blame] | 366 | return 1; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 367 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 368 | spin_lock_irqsave(&gpio->lock, flags); |
| 369 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 370 | val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 371 | |
| 372 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 373 | |
| 374 | return !val; |
| 375 | |
| 376 | } |
| 377 | |
| 378 | static inline int irqd_to_aspeed_gpio_data(struct irq_data *d, |
| 379 | struct aspeed_gpio **gpio, |
| 380 | const struct aspeed_gpio_bank **bank, |
| 381 | u32 *bit) |
| 382 | { |
| 383 | int offset; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 384 | struct aspeed_gpio *internal; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 385 | |
| 386 | offset = irqd_to_hwirq(d); |
| 387 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 388 | internal = irq_data_get_irq_chip_data(d); |
| 389 | |
| 390 | /* This might be a bit of a questionable place to check */ |
| 391 | if (!have_irq(internal, offset)) |
| 392 | return -ENOTSUPP; |
| 393 | |
| 394 | *gpio = internal; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 395 | *bank = to_bank(offset); |
| 396 | *bit = GPIO_BIT(offset); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static void aspeed_gpio_irq_ack(struct irq_data *d) |
| 402 | { |
| 403 | const struct aspeed_gpio_bank *bank; |
| 404 | struct aspeed_gpio *gpio; |
| 405 | unsigned long flags; |
| 406 | void __iomem *status_addr; |
| 407 | u32 bit; |
| 408 | int rc; |
| 409 | |
| 410 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit); |
| 411 | if (rc) |
| 412 | return; |
| 413 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 414 | status_addr = bank_reg(gpio, bank, reg_irq_status); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 415 | |
| 416 | spin_lock_irqsave(&gpio->lock, flags); |
| 417 | iowrite32(bit, status_addr); |
| 418 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 419 | } |
| 420 | |
| 421 | static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set) |
| 422 | { |
| 423 | const struct aspeed_gpio_bank *bank; |
| 424 | struct aspeed_gpio *gpio; |
| 425 | unsigned long flags; |
| 426 | u32 reg, bit; |
| 427 | void __iomem *addr; |
| 428 | int rc; |
| 429 | |
| 430 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit); |
| 431 | if (rc) |
| 432 | return; |
| 433 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 434 | addr = bank_reg(gpio, bank, reg_irq_enable); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 435 | |
| 436 | spin_lock_irqsave(&gpio->lock, flags); |
| 437 | |
| 438 | reg = ioread32(addr); |
| 439 | if (set) |
| 440 | reg |= bit; |
| 441 | else |
Govert Overgaauw | f241632 | 2018-04-06 14:41:35 +0200 | [diff] [blame] | 442 | reg &= ~bit; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 443 | iowrite32(reg, addr); |
| 444 | |
| 445 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 446 | } |
| 447 | |
| 448 | static void aspeed_gpio_irq_mask(struct irq_data *d) |
| 449 | { |
| 450 | aspeed_gpio_irq_set_mask(d, false); |
| 451 | } |
| 452 | |
| 453 | static void aspeed_gpio_irq_unmask(struct irq_data *d) |
| 454 | { |
| 455 | aspeed_gpio_irq_set_mask(d, true); |
| 456 | } |
| 457 | |
| 458 | static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type) |
| 459 | { |
| 460 | u32 type0 = 0; |
| 461 | u32 type1 = 0; |
| 462 | u32 type2 = 0; |
| 463 | u32 bit, reg; |
| 464 | const struct aspeed_gpio_bank *bank; |
| 465 | irq_flow_handler_t handler; |
| 466 | struct aspeed_gpio *gpio; |
| 467 | unsigned long flags; |
| 468 | void __iomem *addr; |
| 469 | int rc; |
| 470 | |
| 471 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit); |
| 472 | if (rc) |
| 473 | return -EINVAL; |
| 474 | |
| 475 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 476 | case IRQ_TYPE_EDGE_BOTH: |
| 477 | type2 |= bit; |
Gustavo A. R. Silva | e80df7b | 2017-10-13 15:43:53 -0500 | [diff] [blame] | 478 | /* fall through */ |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 479 | case IRQ_TYPE_EDGE_RISING: |
| 480 | type0 |= bit; |
Gustavo A. R. Silva | e80df7b | 2017-10-13 15:43:53 -0500 | [diff] [blame] | 481 | /* fall through */ |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 482 | case IRQ_TYPE_EDGE_FALLING: |
| 483 | handler = handle_edge_irq; |
| 484 | break; |
| 485 | case IRQ_TYPE_LEVEL_HIGH: |
| 486 | type0 |= bit; |
Gustavo A. R. Silva | e80df7b | 2017-10-13 15:43:53 -0500 | [diff] [blame] | 487 | /* fall through */ |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 488 | case IRQ_TYPE_LEVEL_LOW: |
| 489 | type1 |= bit; |
| 490 | handler = handle_level_irq; |
| 491 | break; |
| 492 | default: |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | |
| 496 | spin_lock_irqsave(&gpio->lock, flags); |
| 497 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 498 | addr = bank_reg(gpio, bank, reg_irq_type0); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 499 | reg = ioread32(addr); |
| 500 | reg = (reg & ~bit) | type0; |
| 501 | iowrite32(reg, addr); |
| 502 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 503 | addr = bank_reg(gpio, bank, reg_irq_type1); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 504 | reg = ioread32(addr); |
| 505 | reg = (reg & ~bit) | type1; |
| 506 | iowrite32(reg, addr); |
| 507 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 508 | addr = bank_reg(gpio, bank, reg_irq_type2); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 509 | reg = ioread32(addr); |
| 510 | reg = (reg & ~bit) | type2; |
| 511 | iowrite32(reg, addr); |
| 512 | |
| 513 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 514 | |
| 515 | irq_set_handler_locked(d, handler); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static void aspeed_gpio_irq_handler(struct irq_desc *desc) |
| 521 | { |
| 522 | struct gpio_chip *gc = irq_desc_get_handler_data(desc); |
| 523 | struct irq_chip *ic = irq_desc_get_chip(desc); |
| 524 | struct aspeed_gpio *data = gpiochip_get_data(gc); |
| 525 | unsigned int i, p, girq; |
| 526 | unsigned long reg; |
| 527 | |
| 528 | chained_irq_enter(ic, desc); |
| 529 | |
| 530 | for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) { |
| 531 | const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i]; |
| 532 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 533 | reg = ioread32(bank_reg(data, bank, reg_irq_status)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 534 | |
| 535 | for_each_set_bit(p, ®, 32) { |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 536 | girq = irq_find_mapping(gc->irq.domain, i * 32 + p); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 537 | generic_handle_irq(girq); |
| 538 | } |
| 539 | |
| 540 | } |
| 541 | |
| 542 | chained_irq_exit(ic, desc); |
| 543 | } |
| 544 | |
| 545 | static struct irq_chip aspeed_gpio_irqchip = { |
| 546 | .name = "aspeed-gpio", |
| 547 | .irq_ack = aspeed_gpio_irq_ack, |
| 548 | .irq_mask = aspeed_gpio_irq_mask, |
| 549 | .irq_unmask = aspeed_gpio_irq_unmask, |
| 550 | .irq_set_type = aspeed_gpio_set_type, |
| 551 | }; |
| 552 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 553 | static void set_irq_valid_mask(struct aspeed_gpio *gpio) |
| 554 | { |
| 555 | const struct aspeed_bank_props *props = gpio->config->props; |
| 556 | |
| 557 | while (!is_bank_props_sentinel(props)) { |
| 558 | unsigned int offset; |
| 559 | const unsigned long int input = props->input; |
| 560 | |
| 561 | /* Pretty crummy approach, but similar to GPIO core */ |
| 562 | for_each_clear_bit(offset, &input, 32) { |
| 563 | unsigned int i = props->bank * 32 + offset; |
| 564 | |
| 565 | if (i >= gpio->config->nr_gpios) |
| 566 | break; |
| 567 | |
Thierry Reding | dc7b038 | 2017-11-07 19:15:52 +0100 | [diff] [blame] | 568 | clear_bit(i, gpio->chip.irq.valid_mask); |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | props++; |
| 572 | } |
| 573 | } |
| 574 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 575 | static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio, |
| 576 | struct platform_device *pdev) |
| 577 | { |
| 578 | int rc; |
| 579 | |
| 580 | rc = platform_get_irq(pdev, 0); |
| 581 | if (rc < 0) |
| 582 | return rc; |
| 583 | |
| 584 | gpio->irq = rc; |
| 585 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 586 | set_irq_valid_mask(gpio); |
| 587 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 588 | rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip, |
| 589 | 0, handle_bad_irq, IRQ_TYPE_NONE); |
| 590 | if (rc) { |
| 591 | dev_info(&pdev->dev, "Could not add irqchip\n"); |
| 592 | return rc; |
| 593 | } |
| 594 | |
| 595 | gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip, |
| 596 | gpio->irq, aspeed_gpio_irq_handler); |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 601 | static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip, |
| 602 | unsigned int offset, bool enable) |
| 603 | { |
| 604 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 605 | unsigned long flags; |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 606 | void __iomem *treg; |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 607 | u32 val; |
| 608 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 609 | treg = bank_reg(gpio, to_bank(offset), reg_tolerance); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 610 | |
| 611 | spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 612 | val = readl(treg); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 613 | |
| 614 | if (enable) |
| 615 | val |= GPIO_BIT(offset); |
| 616 | else |
| 617 | val &= ~GPIO_BIT(offset); |
| 618 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 619 | writel(val, treg); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 620 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 625 | static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset) |
| 626 | { |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 627 | if (!have_gpio(gpiochip_get_data(chip), offset)) |
| 628 | return -ENODEV; |
| 629 | |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 630 | return pinctrl_gpio_request(chip->base + offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset) |
| 634 | { |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 635 | pinctrl_gpio_free(chip->base + offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 636 | } |
| 637 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 638 | static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs, |
| 639 | u32 *cycles) |
| 640 | { |
| 641 | u64 rate; |
| 642 | u64 n; |
| 643 | u32 r; |
| 644 | |
| 645 | rate = clk_get_rate(gpio->clk); |
| 646 | if (!rate) |
| 647 | return -ENOTSUPP; |
| 648 | |
| 649 | n = rate * usecs; |
| 650 | r = do_div(n, 1000000); |
| 651 | |
| 652 | if (n >= U32_MAX) |
| 653 | return -ERANGE; |
| 654 | |
| 655 | /* At least as long as the requested time */ |
| 656 | *cycles = n + (!!r); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | /* Call under gpio->lock */ |
| 662 | static int register_allocated_timer(struct aspeed_gpio *gpio, |
| 663 | unsigned int offset, unsigned int timer) |
| 664 | { |
| 665 | if (WARN(gpio->offset_timer[offset] != 0, |
| 666 | "Offset %d already allocated timer %d\n", |
| 667 | offset, gpio->offset_timer[offset])) |
| 668 | return -EINVAL; |
| 669 | |
| 670 | if (WARN(gpio->timer_users[timer] == UINT_MAX, |
| 671 | "Timer user count would overflow\n")) |
| 672 | return -EPERM; |
| 673 | |
| 674 | gpio->offset_timer[offset] = timer; |
| 675 | gpio->timer_users[timer]++; |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | /* Call under gpio->lock */ |
| 681 | static int unregister_allocated_timer(struct aspeed_gpio *gpio, |
| 682 | unsigned int offset) |
| 683 | { |
| 684 | if (WARN(gpio->offset_timer[offset] == 0, |
| 685 | "No timer allocated to offset %d\n", offset)) |
| 686 | return -EINVAL; |
| 687 | |
| 688 | if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0, |
| 689 | "No users recorded for timer %d\n", |
| 690 | gpio->offset_timer[offset])) |
| 691 | return -EINVAL; |
| 692 | |
| 693 | gpio->timer_users[gpio->offset_timer[offset]]--; |
| 694 | gpio->offset_timer[offset] = 0; |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* Call under gpio->lock */ |
| 700 | static inline bool timer_allocation_registered(struct aspeed_gpio *gpio, |
| 701 | unsigned int offset) |
| 702 | { |
| 703 | return gpio->offset_timer[offset] > 0; |
| 704 | } |
| 705 | |
| 706 | /* Call under gpio->lock */ |
| 707 | static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset, |
| 708 | unsigned int timer) |
| 709 | { |
| 710 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 711 | const u32 mask = GPIO_BIT(offset); |
| 712 | void __iomem *addr; |
| 713 | u32 val; |
| 714 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 715 | addr = bank_reg(gpio, bank, reg_debounce_sel1); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 716 | val = ioread32(addr); |
| 717 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr); |
| 718 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 719 | addr = bank_reg(gpio, bank, reg_debounce_sel2); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 720 | val = ioread32(addr); |
| 721 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr); |
| 722 | } |
| 723 | |
| 724 | static int enable_debounce(struct gpio_chip *chip, unsigned int offset, |
| 725 | unsigned long usecs) |
| 726 | { |
| 727 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 728 | u32 requested_cycles; |
| 729 | unsigned long flags; |
| 730 | int rc; |
| 731 | int i; |
| 732 | |
Joel Stanley | df563c8 | 2017-05-02 15:38:24 +0930 | [diff] [blame] | 733 | if (!gpio->clk) |
| 734 | return -EINVAL; |
| 735 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 736 | rc = usecs_to_cycles(gpio, usecs, &requested_cycles); |
| 737 | if (rc < 0) { |
| 738 | dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n", |
| 739 | usecs, clk_get_rate(gpio->clk), rc); |
| 740 | return rc; |
| 741 | } |
| 742 | |
| 743 | spin_lock_irqsave(&gpio->lock, flags); |
| 744 | |
| 745 | if (timer_allocation_registered(gpio, offset)) { |
| 746 | rc = unregister_allocated_timer(gpio, offset); |
| 747 | if (rc < 0) |
| 748 | goto out; |
| 749 | } |
| 750 | |
| 751 | /* Try to find a timer already configured for the debounce period */ |
| 752 | for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) { |
| 753 | u32 cycles; |
| 754 | |
| 755 | cycles = ioread32(gpio->base + debounce_timers[i]); |
| 756 | if (requested_cycles == cycles) |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | if (i == ARRAY_SIZE(debounce_timers)) { |
| 761 | int j; |
| 762 | |
| 763 | /* |
| 764 | * As there are no timers configured for the requested debounce |
| 765 | * period, find an unused timer instead |
| 766 | */ |
| 767 | for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) { |
| 768 | if (gpio->timer_users[j] == 0) |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | if (j == ARRAY_SIZE(gpio->timer_users)) { |
| 773 | dev_warn(chip->parent, |
| 774 | "Debounce timers exhausted, cannot debounce for period %luus\n", |
| 775 | usecs); |
| 776 | |
| 777 | rc = -EPERM; |
| 778 | |
| 779 | /* |
| 780 | * We already adjusted the accounting to remove @offset |
| 781 | * as a user of its previous timer, so also configure |
| 782 | * the hardware so @offset has timers disabled for |
| 783 | * consistency. |
| 784 | */ |
| 785 | configure_timer(gpio, offset, 0); |
| 786 | goto out; |
| 787 | } |
| 788 | |
| 789 | i = j; |
| 790 | |
| 791 | iowrite32(requested_cycles, gpio->base + debounce_timers[i]); |
| 792 | } |
| 793 | |
| 794 | if (WARN(i == 0, "Cannot register index of disabled timer\n")) { |
| 795 | rc = -EINVAL; |
| 796 | goto out; |
| 797 | } |
| 798 | |
| 799 | register_allocated_timer(gpio, offset, i); |
| 800 | configure_timer(gpio, offset, i); |
| 801 | |
| 802 | out: |
| 803 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 804 | |
| 805 | return rc; |
| 806 | } |
| 807 | |
| 808 | static int disable_debounce(struct gpio_chip *chip, unsigned int offset) |
| 809 | { |
| 810 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 811 | unsigned long flags; |
| 812 | int rc; |
| 813 | |
| 814 | spin_lock_irqsave(&gpio->lock, flags); |
| 815 | |
| 816 | rc = unregister_allocated_timer(gpio, offset); |
| 817 | if (!rc) |
| 818 | configure_timer(gpio, offset, 0); |
| 819 | |
| 820 | spin_unlock_irqrestore(&gpio->lock, flags); |
| 821 | |
| 822 | return rc; |
| 823 | } |
| 824 | |
| 825 | static int set_debounce(struct gpio_chip *chip, unsigned int offset, |
| 826 | unsigned long usecs) |
| 827 | { |
| 828 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 829 | |
| 830 | if (!have_debounce(gpio, offset)) |
| 831 | return -ENOTSUPP; |
| 832 | |
| 833 | if (usecs) |
| 834 | return enable_debounce(chip, offset, usecs); |
| 835 | |
| 836 | return disable_debounce(chip, offset); |
| 837 | } |
| 838 | |
| 839 | static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset, |
| 840 | unsigned long config) |
| 841 | { |
| 842 | unsigned long param = pinconf_to_config_param(config); |
| 843 | u32 arg = pinconf_to_config_argument(config); |
| 844 | |
| 845 | if (param == PIN_CONFIG_INPUT_DEBOUNCE) |
| 846 | return set_debounce(chip, offset, arg); |
| 847 | else if (param == PIN_CONFIG_BIAS_DISABLE || |
| 848 | param == PIN_CONFIG_BIAS_PULL_DOWN || |
| 849 | param == PIN_CONFIG_DRIVE_STRENGTH) |
| 850 | return pinctrl_gpio_set_config(offset, config); |
Andrew Jeffery | c3bafe0 | 2017-04-07 22:29:02 +0930 | [diff] [blame] | 851 | else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN || |
| 852 | param == PIN_CONFIG_DRIVE_OPEN_SOURCE) |
| 853 | /* Return -ENOTSUPP to trigger emulation, as per datasheet */ |
| 854 | return -ENOTSUPP; |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 855 | else if (param == PIN_CONFIG_PERSIST_STATE) |
| 856 | return aspeed_gpio_reset_tolerance(chip, offset, arg); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 857 | |
| 858 | return -ENOTSUPP; |
| 859 | } |
| 860 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 861 | /* |
| 862 | * Any banks not specified in a struct aspeed_bank_props array are assumed to |
| 863 | * have the properties: |
| 864 | * |
| 865 | * { .input = 0xffffffff, .output = 0xffffffff } |
| 866 | */ |
| 867 | |
| 868 | static const struct aspeed_bank_props ast2400_bank_props[] = { |
| 869 | /* input output */ |
| 870 | { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */ |
| 871 | { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */ |
| 872 | { }, |
| 873 | }; |
| 874 | |
| 875 | static const struct aspeed_gpio_config ast2400_config = |
| 876 | /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */ |
| 877 | { .nr_gpios = 220, .props = ast2400_bank_props, }; |
| 878 | |
| 879 | static const struct aspeed_bank_props ast2500_bank_props[] = { |
| 880 | /* input output */ |
| 881 | { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */ |
| 882 | { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */ |
| 883 | { 7, 0x000000ff, 0x000000ff }, /* AC */ |
| 884 | { }, |
| 885 | }; |
| 886 | |
| 887 | static const struct aspeed_gpio_config ast2500_config = |
| 888 | /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */ |
| 889 | { .nr_gpios = 232, .props = ast2500_bank_props, }; |
| 890 | |
| 891 | static const struct of_device_id aspeed_gpio_of_table[] = { |
| 892 | { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, }, |
| 893 | { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, }, |
| 894 | {} |
| 895 | }; |
| 896 | MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table); |
| 897 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 898 | static int __init aspeed_gpio_probe(struct platform_device *pdev) |
| 899 | { |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 900 | const struct of_device_id *gpio_id; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 901 | struct aspeed_gpio *gpio; |
| 902 | struct resource *res; |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 903 | int rc, i, banks; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 904 | |
| 905 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); |
| 906 | if (!gpio) |
| 907 | return -ENOMEM; |
| 908 | |
| 909 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 910 | gpio->base = devm_ioremap_resource(&pdev->dev, res); |
Wei Yongjun | 7f8b965 | 2016-09-15 01:30:32 +0000 | [diff] [blame] | 911 | if (IS_ERR(gpio->base)) |
| 912 | return PTR_ERR(gpio->base); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 913 | |
| 914 | spin_lock_init(&gpio->lock); |
| 915 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 916 | gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node); |
| 917 | if (!gpio_id) |
| 918 | return -EINVAL; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 919 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 920 | gpio->clk = of_clk_get(pdev->dev.of_node, 0); |
| 921 | if (IS_ERR(gpio->clk)) { |
| 922 | dev_warn(&pdev->dev, |
Andrew Jeffery | 754c045 | 2017-08-08 15:37:36 +0930 | [diff] [blame] | 923 | "Failed to get clock from devicetree, debouncing disabled\n"); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 924 | gpio->clk = NULL; |
| 925 | } |
| 926 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 927 | gpio->config = gpio_id->data; |
| 928 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 929 | gpio->chip.parent = &pdev->dev; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 930 | gpio->chip.ngpio = gpio->config->nr_gpios; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 931 | gpio->chip.parent = &pdev->dev; |
| 932 | gpio->chip.direction_input = aspeed_gpio_dir_in; |
| 933 | gpio->chip.direction_output = aspeed_gpio_dir_out; |
| 934 | gpio->chip.get_direction = aspeed_gpio_get_direction; |
| 935 | gpio->chip.request = aspeed_gpio_request; |
| 936 | gpio->chip.free = aspeed_gpio_free; |
| 937 | gpio->chip.get = aspeed_gpio_get; |
| 938 | gpio->chip.set = aspeed_gpio_set; |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 939 | gpio->chip.set_config = aspeed_gpio_set_config; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 940 | gpio->chip.label = dev_name(&pdev->dev); |
| 941 | gpio->chip.base = -1; |
Thierry Reding | dc7b038 | 2017-11-07 19:15:52 +0100 | [diff] [blame] | 942 | gpio->chip.irq.need_valid_mask = true; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 943 | |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 944 | /* Allocate a cache of the output registers */ |
| 945 | banks = gpio->config->nr_gpios >> 5; |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 946 | gpio->dcache = devm_kcalloc(&pdev->dev, |
| 947 | banks, sizeof(u32), GFP_KERNEL); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 948 | if (!gpio->dcache) |
| 949 | return -ENOMEM; |
| 950 | |
| 951 | /* Populate it with initial values read from the HW */ |
| 952 | for (i = 0; i < banks; i++) { |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame^] | 953 | void __iomem *addr = bank_reg(gpio, &aspeed_gpio_banks[i], reg_rdata); |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 954 | gpio->dcache[i] = ioread32(addr); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 955 | } |
| 956 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 957 | rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); |
| 958 | if (rc < 0) |
| 959 | return rc; |
| 960 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 961 | gpio->offset_timer = |
| 962 | devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL); |
| 963 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 964 | return aspeed_gpio_setup_irqs(gpio, pdev); |
| 965 | } |
| 966 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 967 | static struct platform_driver aspeed_gpio_driver = { |
| 968 | .driver = { |
| 969 | .name = KBUILD_MODNAME, |
| 970 | .of_match_table = aspeed_gpio_of_table, |
| 971 | }, |
| 972 | }; |
| 973 | |
| 974 | module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe); |
| 975 | |
| 976 | MODULE_DESCRIPTION("Aspeed GPIO Driver"); |
Linus Walleij | e50237c | 2016-09-13 13:43:34 +0200 | [diff] [blame] | 977 | MODULE_LICENSE("GPL"); |