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