David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * GPIO driver for AMD |
| 4 | * |
| 5 | * Copyright (c) 2014,2015 AMD Corporation. |
| 6 | * Authors: Ken Xue <Ken.Xue@amd.com> |
| 7 | * Wu, Jeff <Jeff.Wu@amd.com> |
| 8 | * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | * Contact Information: Nehal Shah <Nehal-bakulchandra.Shah@amd.com> |
| 10 | * Shyam Sundar S K <Shyam-sundar.S-k@amd.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/bug.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/compiler.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/log2.h> |
| 22 | #include <linux/io.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 23 | #include <linux/gpio/driver.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/acpi.h> |
| 28 | #include <linux/seq_file.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/list.h> |
| 31 | #include <linux/bitops.h> |
| 32 | #include <linux/pinctrl/pinconf.h> |
| 33 | #include <linux/pinctrl/pinconf-generic.h> |
| 34 | |
| 35 | #include "core.h" |
| 36 | #include "pinctrl-utils.h" |
| 37 | #include "pinctrl-amd.h" |
| 38 | |
| 39 | static int amd_gpio_get_direction(struct gpio_chip *gc, unsigned offset) |
| 40 | { |
| 41 | unsigned long flags; |
| 42 | u32 pin_reg; |
| 43 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 44 | |
| 45 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 46 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 47 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 48 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 49 | if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) |
| 50 | return GPIO_LINE_DIRECTION_OUT; |
| 51 | |
| 52 | return GPIO_LINE_DIRECTION_IN; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 56 | { |
| 57 | unsigned long flags; |
| 58 | u32 pin_reg; |
| 59 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 60 | |
| 61 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 62 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 63 | pin_reg &= ~BIT(OUTPUT_ENABLE_OFF); |
| 64 | writel(pin_reg, gpio_dev->base + offset * 4); |
| 65 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset, |
| 71 | int value) |
| 72 | { |
| 73 | u32 pin_reg; |
| 74 | unsigned long flags; |
| 75 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 76 | |
| 77 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 78 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 79 | pin_reg |= BIT(OUTPUT_ENABLE_OFF); |
| 80 | if (value) |
| 81 | pin_reg |= BIT(OUTPUT_VALUE_OFF); |
| 82 | else |
| 83 | pin_reg &= ~BIT(OUTPUT_VALUE_OFF); |
| 84 | writel(pin_reg, gpio_dev->base + offset * 4); |
| 85 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset) |
| 91 | { |
| 92 | u32 pin_reg; |
| 93 | unsigned long flags; |
| 94 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 95 | |
| 96 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 97 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 98 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 99 | |
| 100 | return !!(pin_reg & BIT(PIN_STS_OFF)); |
| 101 | } |
| 102 | |
| 103 | static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value) |
| 104 | { |
| 105 | u32 pin_reg; |
| 106 | unsigned long flags; |
| 107 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 108 | |
| 109 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 110 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 111 | if (value) |
| 112 | pin_reg |= BIT(OUTPUT_VALUE_OFF); |
| 113 | else |
| 114 | pin_reg &= ~BIT(OUTPUT_VALUE_OFF); |
| 115 | writel(pin_reg, gpio_dev->base + offset * 4); |
| 116 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 117 | } |
| 118 | |
| 119 | static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset, |
| 120 | unsigned debounce) |
| 121 | { |
| 122 | u32 time; |
| 123 | u32 pin_reg; |
| 124 | int ret = 0; |
| 125 | unsigned long flags; |
| 126 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 127 | |
| 128 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 129 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 130 | |
| 131 | if (debounce) { |
| 132 | pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF; |
| 133 | pin_reg &= ~DB_TMR_OUT_MASK; |
| 134 | /* |
| 135 | Debounce Debounce Timer Max |
| 136 | TmrLarge TmrOutUnit Unit Debounce |
| 137 | Time |
| 138 | 0 0 61 usec (2 RtcClk) 976 usec |
| 139 | 0 1 244 usec (8 RtcClk) 3.9 msec |
| 140 | 1 0 15.6 msec (512 RtcClk) 250 msec |
| 141 | 1 1 62.5 msec (2048 RtcClk) 1 sec |
| 142 | */ |
| 143 | |
| 144 | if (debounce < 61) { |
| 145 | pin_reg |= 1; |
| 146 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 147 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 148 | } else if (debounce < 976) { |
| 149 | time = debounce / 61; |
| 150 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 151 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 152 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 153 | } else if (debounce < 3900) { |
| 154 | time = debounce / 244; |
| 155 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 156 | pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF); |
| 157 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 158 | } else if (debounce < 250000) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 159 | time = debounce / 15625; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 160 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 161 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 162 | pin_reg |= BIT(DB_TMR_LARGE_OFF); |
| 163 | } else if (debounce < 1000000) { |
| 164 | time = debounce / 62500; |
| 165 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 166 | pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF); |
| 167 | pin_reg |= BIT(DB_TMR_LARGE_OFF); |
| 168 | } else { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 169 | pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 170 | ret = -EINVAL; |
| 171 | } |
| 172 | } else { |
| 173 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 174 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 175 | pin_reg &= ~DB_TMR_OUT_MASK; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 176 | pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 177 | } |
| 178 | writel(pin_reg, gpio_dev->base + offset * 4); |
| 179 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset, |
| 185 | unsigned long config) |
| 186 | { |
| 187 | u32 debounce; |
| 188 | |
| 189 | if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE) |
| 190 | return -ENOTSUPP; |
| 191 | |
| 192 | debounce = pinconf_to_config_argument(config); |
| 193 | return amd_gpio_set_debounce(gc, offset, debounce); |
| 194 | } |
| 195 | |
| 196 | #ifdef CONFIG_DEBUG_FS |
| 197 | static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc) |
| 198 | { |
| 199 | u32 pin_reg; |
| 200 | unsigned long flags; |
| 201 | unsigned int bank, i, pin_num; |
| 202 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 203 | |
| 204 | char *level_trig; |
| 205 | char *active_level; |
| 206 | char *interrupt_enable; |
| 207 | char *interrupt_mask; |
| 208 | char *wake_cntrl0; |
| 209 | char *wake_cntrl1; |
| 210 | char *wake_cntrl2; |
| 211 | char *pin_sts; |
| 212 | char *pull_up_sel; |
| 213 | char *pull_up_enable; |
| 214 | char *pull_down_enable; |
| 215 | char *output_value; |
| 216 | char *output_enable; |
| 217 | |
| 218 | for (bank = 0; bank < gpio_dev->hwbank_num; bank++) { |
| 219 | seq_printf(s, "GPIO bank%d\t", bank); |
| 220 | |
| 221 | switch (bank) { |
| 222 | case 0: |
| 223 | i = 0; |
| 224 | pin_num = AMD_GPIO_PINS_BANK0; |
| 225 | break; |
| 226 | case 1: |
| 227 | i = 64; |
| 228 | pin_num = AMD_GPIO_PINS_BANK1 + i; |
| 229 | break; |
| 230 | case 2: |
| 231 | i = 128; |
| 232 | pin_num = AMD_GPIO_PINS_BANK2 + i; |
| 233 | break; |
| 234 | case 3: |
| 235 | i = 192; |
| 236 | pin_num = AMD_GPIO_PINS_BANK3 + i; |
| 237 | break; |
| 238 | default: |
| 239 | /* Illegal bank number, ignore */ |
| 240 | continue; |
| 241 | } |
| 242 | for (; i < pin_num; i++) { |
| 243 | seq_printf(s, "pin%d\t", i); |
| 244 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 245 | pin_reg = readl(gpio_dev->base + i * 4); |
| 246 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 247 | |
| 248 | if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) { |
| 249 | u8 level = (pin_reg >> ACTIVE_LEVEL_OFF) & |
| 250 | ACTIVE_LEVEL_MASK; |
| 251 | interrupt_enable = "interrupt is enabled|"; |
| 252 | |
| 253 | if (level == ACTIVE_LEVEL_HIGH) |
| 254 | active_level = "Active high|"; |
| 255 | else if (level == ACTIVE_LEVEL_LOW) |
| 256 | active_level = "Active low|"; |
| 257 | else if (!(pin_reg & BIT(LEVEL_TRIG_OFF)) && |
| 258 | level == ACTIVE_LEVEL_BOTH) |
| 259 | active_level = "Active on both|"; |
| 260 | else |
| 261 | active_level = "Unknown Active level|"; |
| 262 | |
| 263 | if (pin_reg & BIT(LEVEL_TRIG_OFF)) |
| 264 | level_trig = "Level trigger|"; |
| 265 | else |
| 266 | level_trig = "Edge trigger|"; |
| 267 | |
| 268 | } else { |
| 269 | interrupt_enable = |
| 270 | "interrupt is disabled|"; |
| 271 | active_level = " "; |
| 272 | level_trig = " "; |
| 273 | } |
| 274 | |
| 275 | if (pin_reg & BIT(INTERRUPT_MASK_OFF)) |
| 276 | interrupt_mask = |
| 277 | "interrupt is unmasked|"; |
| 278 | else |
| 279 | interrupt_mask = |
| 280 | "interrupt is masked|"; |
| 281 | |
| 282 | if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3)) |
| 283 | wake_cntrl0 = "enable wakeup in S0i3 state|"; |
| 284 | else |
| 285 | wake_cntrl0 = "disable wakeup in S0i3 state|"; |
| 286 | |
| 287 | if (pin_reg & BIT(WAKE_CNTRL_OFF_S3)) |
| 288 | wake_cntrl1 = "enable wakeup in S3 state|"; |
| 289 | else |
| 290 | wake_cntrl1 = "disable wakeup in S3 state|"; |
| 291 | |
| 292 | if (pin_reg & BIT(WAKE_CNTRL_OFF_S4)) |
| 293 | wake_cntrl2 = "enable wakeup in S4/S5 state|"; |
| 294 | else |
| 295 | wake_cntrl2 = "disable wakeup in S4/S5 state|"; |
| 296 | |
| 297 | if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) { |
| 298 | pull_up_enable = "pull-up is enabled|"; |
| 299 | if (pin_reg & BIT(PULL_UP_SEL_OFF)) |
| 300 | pull_up_sel = "8k pull-up|"; |
| 301 | else |
| 302 | pull_up_sel = "4k pull-up|"; |
| 303 | } else { |
| 304 | pull_up_enable = "pull-up is disabled|"; |
| 305 | pull_up_sel = " "; |
| 306 | } |
| 307 | |
| 308 | if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF)) |
| 309 | pull_down_enable = "pull-down is enabled|"; |
| 310 | else |
| 311 | pull_down_enable = "Pull-down is disabled|"; |
| 312 | |
| 313 | if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) { |
| 314 | pin_sts = " "; |
| 315 | output_enable = "output is enabled|"; |
| 316 | if (pin_reg & BIT(OUTPUT_VALUE_OFF)) |
| 317 | output_value = "output is high|"; |
| 318 | else |
| 319 | output_value = "output is low|"; |
| 320 | } else { |
| 321 | output_enable = "output is disabled|"; |
| 322 | output_value = " "; |
| 323 | |
| 324 | if (pin_reg & BIT(PIN_STS_OFF)) |
| 325 | pin_sts = "input is high|"; |
| 326 | else |
| 327 | pin_sts = "input is low|"; |
| 328 | } |
| 329 | |
| 330 | seq_printf(s, "%s %s %s %s %s %s\n" |
| 331 | " %s %s %s %s %s %s %s 0x%x\n", |
| 332 | level_trig, active_level, interrupt_enable, |
| 333 | interrupt_mask, wake_cntrl0, wake_cntrl1, |
| 334 | wake_cntrl2, pin_sts, pull_up_sel, |
| 335 | pull_up_enable, pull_down_enable, |
| 336 | output_value, output_enable, pin_reg); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | #else |
| 341 | #define amd_gpio_dbg_show NULL |
| 342 | #endif |
| 343 | |
| 344 | static void amd_gpio_irq_enable(struct irq_data *d) |
| 345 | { |
| 346 | u32 pin_reg; |
| 347 | unsigned long flags; |
| 348 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 349 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 350 | |
| 351 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 352 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 353 | pin_reg |= BIT(INTERRUPT_ENABLE_OFF); |
| 354 | pin_reg |= BIT(INTERRUPT_MASK_OFF); |
| 355 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
| 356 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 357 | } |
| 358 | |
| 359 | static void amd_gpio_irq_disable(struct irq_data *d) |
| 360 | { |
| 361 | u32 pin_reg; |
| 362 | unsigned long flags; |
| 363 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 364 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 365 | |
| 366 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 367 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 368 | pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF); |
| 369 | pin_reg &= ~BIT(INTERRUPT_MASK_OFF); |
| 370 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
| 371 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 372 | } |
| 373 | |
| 374 | static void amd_gpio_irq_mask(struct irq_data *d) |
| 375 | { |
| 376 | u32 pin_reg; |
| 377 | unsigned long flags; |
| 378 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 379 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 380 | |
| 381 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 382 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 383 | pin_reg &= ~BIT(INTERRUPT_MASK_OFF); |
| 384 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
| 385 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 386 | } |
| 387 | |
| 388 | static void amd_gpio_irq_unmask(struct irq_data *d) |
| 389 | { |
| 390 | u32 pin_reg; |
| 391 | unsigned long flags; |
| 392 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 393 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 394 | |
| 395 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 396 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 397 | pin_reg |= BIT(INTERRUPT_MASK_OFF); |
| 398 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
| 399 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 400 | } |
| 401 | |
| 402 | static void amd_gpio_irq_eoi(struct irq_data *d) |
| 403 | { |
| 404 | u32 reg; |
| 405 | unsigned long flags; |
| 406 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 407 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 408 | |
| 409 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 410 | reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG); |
| 411 | reg |= EOI_MASK; |
| 412 | writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG); |
| 413 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 414 | } |
| 415 | |
| 416 | static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 417 | { |
| 418 | int ret = 0; |
| 419 | u32 pin_reg, pin_reg_irq_en, mask; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 420 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 421 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 422 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
| 423 | |
| 424 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 425 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 426 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 427 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 428 | case IRQ_TYPE_EDGE_RISING: |
| 429 | pin_reg &= ~BIT(LEVEL_TRIG_OFF); |
| 430 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 431 | pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 432 | irq_set_handler_locked(d, handle_edge_irq); |
| 433 | break; |
| 434 | |
| 435 | case IRQ_TYPE_EDGE_FALLING: |
| 436 | pin_reg &= ~BIT(LEVEL_TRIG_OFF); |
| 437 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 438 | pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 439 | irq_set_handler_locked(d, handle_edge_irq); |
| 440 | break; |
| 441 | |
| 442 | case IRQ_TYPE_EDGE_BOTH: |
| 443 | pin_reg &= ~BIT(LEVEL_TRIG_OFF); |
| 444 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 445 | pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 446 | irq_set_handler_locked(d, handle_edge_irq); |
| 447 | break; |
| 448 | |
| 449 | case IRQ_TYPE_LEVEL_HIGH: |
| 450 | pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF; |
| 451 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 452 | pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 453 | irq_set_handler_locked(d, handle_level_irq); |
| 454 | break; |
| 455 | |
| 456 | case IRQ_TYPE_LEVEL_LOW: |
| 457 | pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF; |
| 458 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 459 | pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 460 | irq_set_handler_locked(d, handle_level_irq); |
| 461 | break; |
| 462 | |
| 463 | case IRQ_TYPE_NONE: |
| 464 | break; |
| 465 | |
| 466 | default: |
| 467 | dev_err(&gpio_dev->pdev->dev, "Invalid type value\n"); |
| 468 | ret = -EINVAL; |
| 469 | } |
| 470 | |
| 471 | pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF; |
| 472 | /* |
| 473 | * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the |
| 474 | * debounce registers of any GPIO will block wake/interrupt status |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 475 | * generation for *all* GPIOs for a length of time that depends on |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 476 | * WAKE_INT_MASTER_REG.MaskStsLength[11:0]. During this period the |
| 477 | * INTERRUPT_ENABLE bit will read as 0. |
| 478 | * |
| 479 | * We temporarily enable irq for the GPIO whose configuration is |
| 480 | * changing, and then wait for it to read back as 1 to know when |
| 481 | * debounce has settled and then disable the irq again. |
| 482 | * We do this polling with the spinlock held to ensure other GPIO |
| 483 | * access routines do not read an incorrect value for the irq enable |
| 484 | * bit of other GPIOs. We keep the GPIO masked while polling to avoid |
| 485 | * spurious irqs, and disable the irq again after polling. |
| 486 | */ |
| 487 | mask = BIT(INTERRUPT_ENABLE_OFF); |
| 488 | pin_reg_irq_en = pin_reg; |
| 489 | pin_reg_irq_en |= mask; |
| 490 | pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF); |
| 491 | writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4); |
| 492 | while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask) |
| 493 | continue; |
| 494 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
| 495 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static void amd_irq_ack(struct irq_data *d) |
| 501 | { |
| 502 | /* |
| 503 | * based on HW design,there is no need to ack HW |
| 504 | * before handle current irq. But this routine is |
| 505 | * necessary for handle_edge_irq |
| 506 | */ |
| 507 | } |
| 508 | |
| 509 | static struct irq_chip amd_gpio_irqchip = { |
| 510 | .name = "amd_gpio", |
| 511 | .irq_ack = amd_irq_ack, |
| 512 | .irq_enable = amd_gpio_irq_enable, |
| 513 | .irq_disable = amd_gpio_irq_disable, |
| 514 | .irq_mask = amd_gpio_irq_mask, |
| 515 | .irq_unmask = amd_gpio_irq_unmask, |
| 516 | .irq_eoi = amd_gpio_irq_eoi, |
| 517 | .irq_set_type = amd_gpio_irq_set_type, |
| 518 | .flags = IRQCHIP_SKIP_SET_WAKE, |
| 519 | }; |
| 520 | |
| 521 | #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF)) |
| 522 | |
| 523 | static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id) |
| 524 | { |
| 525 | struct amd_gpio *gpio_dev = dev_id; |
| 526 | struct gpio_chip *gc = &gpio_dev->gc; |
| 527 | irqreturn_t ret = IRQ_NONE; |
| 528 | unsigned int i, irqnr; |
| 529 | unsigned long flags; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 530 | u32 __iomem *regs; |
| 531 | u32 regval; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 532 | u64 status, mask; |
| 533 | |
| 534 | /* Read the wake status */ |
| 535 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 536 | status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1); |
| 537 | status <<= 32; |
| 538 | status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0); |
| 539 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 540 | |
| 541 | /* Bit 0-45 contain the relevant status bits */ |
| 542 | status &= (1ULL << 46) - 1; |
| 543 | regs = gpio_dev->base; |
| 544 | for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) { |
| 545 | if (!(status & mask)) |
| 546 | continue; |
| 547 | status &= ~mask; |
| 548 | |
| 549 | /* Each status bit covers four pins */ |
| 550 | for (i = 0; i < 4; i++) { |
| 551 | regval = readl(regs + i); |
| 552 | if (!(regval & PIN_IRQ_PENDING) || |
| 553 | !(regval & BIT(INTERRUPT_MASK_OFF))) |
| 554 | continue; |
| 555 | irq = irq_find_mapping(gc->irq.domain, irqnr + i); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 556 | if (irq != 0) |
| 557 | generic_handle_irq(irq); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 558 | |
| 559 | /* Clear interrupt. |
| 560 | * We must read the pin register again, in case the |
| 561 | * value was changed while executing |
| 562 | * generic_handle_irq() above. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 563 | * If we didn't find a mapping for the interrupt, |
| 564 | * disable it in order to avoid a system hang caused |
| 565 | * by an interrupt storm. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 566 | */ |
| 567 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 568 | regval = readl(regs + i); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 569 | if (irq == 0) { |
| 570 | regval &= ~BIT(INTERRUPT_ENABLE_OFF); |
| 571 | dev_dbg(&gpio_dev->pdev->dev, |
| 572 | "Disabling spurious GPIO IRQ %d\n", |
| 573 | irqnr + i); |
| 574 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 575 | writel(regval, regs + i); |
| 576 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 577 | ret = IRQ_HANDLED; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /* Signal EOI to the GPIO unit */ |
| 582 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 583 | regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG); |
| 584 | regval |= EOI_MASK; |
| 585 | writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG); |
| 586 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 587 | |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | static int amd_get_groups_count(struct pinctrl_dev *pctldev) |
| 592 | { |
| 593 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 594 | |
| 595 | return gpio_dev->ngroups; |
| 596 | } |
| 597 | |
| 598 | static const char *amd_get_group_name(struct pinctrl_dev *pctldev, |
| 599 | unsigned group) |
| 600 | { |
| 601 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 602 | |
| 603 | return gpio_dev->groups[group].name; |
| 604 | } |
| 605 | |
| 606 | static int amd_get_group_pins(struct pinctrl_dev *pctldev, |
| 607 | unsigned group, |
| 608 | const unsigned **pins, |
| 609 | unsigned *num_pins) |
| 610 | { |
| 611 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 612 | |
| 613 | *pins = gpio_dev->groups[group].pins; |
| 614 | *num_pins = gpio_dev->groups[group].npins; |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static const struct pinctrl_ops amd_pinctrl_ops = { |
| 619 | .get_groups_count = amd_get_groups_count, |
| 620 | .get_group_name = amd_get_group_name, |
| 621 | .get_group_pins = amd_get_group_pins, |
| 622 | #ifdef CONFIG_OF |
| 623 | .dt_node_to_map = pinconf_generic_dt_node_to_map_group, |
| 624 | .dt_free_map = pinctrl_utils_free_map, |
| 625 | #endif |
| 626 | }; |
| 627 | |
| 628 | static int amd_pinconf_get(struct pinctrl_dev *pctldev, |
| 629 | unsigned int pin, |
| 630 | unsigned long *config) |
| 631 | { |
| 632 | u32 pin_reg; |
| 633 | unsigned arg; |
| 634 | unsigned long flags; |
| 635 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 636 | enum pin_config_param param = pinconf_to_config_param(*config); |
| 637 | |
| 638 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 639 | pin_reg = readl(gpio_dev->base + pin*4); |
| 640 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 641 | switch (param) { |
| 642 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 643 | arg = pin_reg & DB_TMR_OUT_MASK; |
| 644 | break; |
| 645 | |
| 646 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 647 | arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0); |
| 648 | break; |
| 649 | |
| 650 | case PIN_CONFIG_BIAS_PULL_UP: |
| 651 | arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1)); |
| 652 | break; |
| 653 | |
| 654 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 655 | arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK; |
| 656 | break; |
| 657 | |
| 658 | default: |
| 659 | dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n", |
| 660 | param); |
| 661 | return -ENOTSUPP; |
| 662 | } |
| 663 | |
| 664 | *config = pinconf_to_config_packed(param, arg); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 670 | unsigned long *configs, unsigned num_configs) |
| 671 | { |
| 672 | int i; |
| 673 | u32 arg; |
| 674 | int ret = 0; |
| 675 | u32 pin_reg; |
| 676 | unsigned long flags; |
| 677 | enum pin_config_param param; |
| 678 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 679 | |
| 680 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 681 | for (i = 0; i < num_configs; i++) { |
| 682 | param = pinconf_to_config_param(configs[i]); |
| 683 | arg = pinconf_to_config_argument(configs[i]); |
| 684 | pin_reg = readl(gpio_dev->base + pin*4); |
| 685 | |
| 686 | switch (param) { |
| 687 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 688 | pin_reg &= ~DB_TMR_OUT_MASK; |
| 689 | pin_reg |= arg & DB_TMR_OUT_MASK; |
| 690 | break; |
| 691 | |
| 692 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 693 | pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF); |
| 694 | pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF; |
| 695 | break; |
| 696 | |
| 697 | case PIN_CONFIG_BIAS_PULL_UP: |
| 698 | pin_reg &= ~BIT(PULL_UP_SEL_OFF); |
| 699 | pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF; |
| 700 | pin_reg &= ~BIT(PULL_UP_ENABLE_OFF); |
| 701 | pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF; |
| 702 | break; |
| 703 | |
| 704 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 705 | pin_reg &= ~(DRV_STRENGTH_SEL_MASK |
| 706 | << DRV_STRENGTH_SEL_OFF); |
| 707 | pin_reg |= (arg & DRV_STRENGTH_SEL_MASK) |
| 708 | << DRV_STRENGTH_SEL_OFF; |
| 709 | break; |
| 710 | |
| 711 | default: |
| 712 | dev_err(&gpio_dev->pdev->dev, |
| 713 | "Invalid config param %04x\n", param); |
| 714 | ret = -ENOTSUPP; |
| 715 | } |
| 716 | |
| 717 | writel(pin_reg, gpio_dev->base + pin*4); |
| 718 | } |
| 719 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 720 | |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | static int amd_pinconf_group_get(struct pinctrl_dev *pctldev, |
| 725 | unsigned int group, |
| 726 | unsigned long *config) |
| 727 | { |
| 728 | const unsigned *pins; |
| 729 | unsigned npins; |
| 730 | int ret; |
| 731 | |
| 732 | ret = amd_get_group_pins(pctldev, group, &pins, &npins); |
| 733 | if (ret) |
| 734 | return ret; |
| 735 | |
| 736 | if (amd_pinconf_get(pctldev, pins[0], config)) |
| 737 | return -ENOTSUPP; |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | static int amd_pinconf_group_set(struct pinctrl_dev *pctldev, |
| 743 | unsigned group, unsigned long *configs, |
| 744 | unsigned num_configs) |
| 745 | { |
| 746 | const unsigned *pins; |
| 747 | unsigned npins; |
| 748 | int i, ret; |
| 749 | |
| 750 | ret = amd_get_group_pins(pctldev, group, &pins, &npins); |
| 751 | if (ret) |
| 752 | return ret; |
| 753 | for (i = 0; i < npins; i++) { |
| 754 | if (amd_pinconf_set(pctldev, pins[i], configs, num_configs)) |
| 755 | return -ENOTSUPP; |
| 756 | } |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static const struct pinconf_ops amd_pinconf_ops = { |
| 761 | .pin_config_get = amd_pinconf_get, |
| 762 | .pin_config_set = amd_pinconf_set, |
| 763 | .pin_config_group_get = amd_pinconf_group_get, |
| 764 | .pin_config_group_set = amd_pinconf_group_set, |
| 765 | }; |
| 766 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 767 | static void amd_gpio_irq_init(struct amd_gpio *gpio_dev) |
| 768 | { |
| 769 | struct pinctrl_desc *desc = gpio_dev->pctrl->desc; |
| 770 | unsigned long flags; |
| 771 | u32 pin_reg, mask; |
| 772 | int i; |
| 773 | |
| 774 | mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3) | |
| 775 | BIT(INTERRUPT_MASK_OFF) | BIT(INTERRUPT_ENABLE_OFF) | |
| 776 | BIT(WAKE_CNTRL_OFF_S4); |
| 777 | |
| 778 | for (i = 0; i < desc->npins; i++) { |
| 779 | int pin = desc->pins[i].number; |
| 780 | const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin); |
| 781 | |
| 782 | if (!pd) |
| 783 | continue; |
| 784 | |
| 785 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 786 | |
| 787 | pin_reg = readl(gpio_dev->base + i * 4); |
| 788 | pin_reg &= ~mask; |
| 789 | writel(pin_reg, gpio_dev->base + i * 4); |
| 790 | |
| 791 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
| 792 | } |
| 793 | } |
| 794 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 795 | #ifdef CONFIG_PM_SLEEP |
| 796 | static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin) |
| 797 | { |
| 798 | const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin); |
| 799 | |
| 800 | if (!pd) |
| 801 | return false; |
| 802 | |
| 803 | /* |
| 804 | * Only restore the pin if it is actually in use by the kernel (or |
| 805 | * by userspace). |
| 806 | */ |
| 807 | if (pd->mux_owner || pd->gpio_owner || |
| 808 | gpiochip_line_is_irq(&gpio_dev->gc, pin)) |
| 809 | return true; |
| 810 | |
| 811 | return false; |
| 812 | } |
| 813 | |
| 814 | static int amd_gpio_suspend(struct device *dev) |
| 815 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 816 | struct amd_gpio *gpio_dev = dev_get_drvdata(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 817 | struct pinctrl_desc *desc = gpio_dev->pctrl->desc; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 818 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 819 | int i; |
| 820 | |
| 821 | for (i = 0; i < desc->npins; i++) { |
| 822 | int pin = desc->pins[i].number; |
| 823 | |
| 824 | if (!amd_gpio_should_save(gpio_dev, pin)) |
| 825 | continue; |
| 826 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 827 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 828 | gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin * 4) & ~PIN_IRQ_PENDING; |
| 829 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | static int amd_gpio_resume(struct device *dev) |
| 836 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 837 | struct amd_gpio *gpio_dev = dev_get_drvdata(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 838 | struct pinctrl_desc *desc = gpio_dev->pctrl->desc; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 839 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 840 | int i; |
| 841 | |
| 842 | for (i = 0; i < desc->npins; i++) { |
| 843 | int pin = desc->pins[i].number; |
| 844 | |
| 845 | if (!amd_gpio_should_save(gpio_dev, pin)) |
| 846 | continue; |
| 847 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 848 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
| 849 | gpio_dev->saved_regs[i] |= readl(gpio_dev->base + pin * 4) & PIN_IRQ_PENDING; |
| 850 | writel(gpio_dev->saved_regs[i], gpio_dev->base + pin * 4); |
| 851 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static const struct dev_pm_ops amd_gpio_pm_ops = { |
| 858 | SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend, |
| 859 | amd_gpio_resume) |
| 860 | }; |
| 861 | #endif |
| 862 | |
| 863 | static struct pinctrl_desc amd_pinctrl_desc = { |
| 864 | .pins = kerncz_pins, |
| 865 | .npins = ARRAY_SIZE(kerncz_pins), |
| 866 | .pctlops = &amd_pinctrl_ops, |
| 867 | .confops = &amd_pinconf_ops, |
| 868 | .owner = THIS_MODULE, |
| 869 | }; |
| 870 | |
| 871 | static int amd_gpio_probe(struct platform_device *pdev) |
| 872 | { |
| 873 | int ret = 0; |
| 874 | int irq_base; |
| 875 | struct resource *res; |
| 876 | struct amd_gpio *gpio_dev; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 877 | struct gpio_irq_chip *girq; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 878 | |
| 879 | gpio_dev = devm_kzalloc(&pdev->dev, |
| 880 | sizeof(struct amd_gpio), GFP_KERNEL); |
| 881 | if (!gpio_dev) |
| 882 | return -ENOMEM; |
| 883 | |
| 884 | raw_spin_lock_init(&gpio_dev->lock); |
| 885 | |
| 886 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 887 | if (!res) { |
| 888 | dev_err(&pdev->dev, "Failed to get gpio io resource.\n"); |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 892 | gpio_dev->base = devm_ioremap(&pdev->dev, res->start, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 893 | resource_size(res)); |
| 894 | if (!gpio_dev->base) |
| 895 | return -ENOMEM; |
| 896 | |
| 897 | irq_base = platform_get_irq(pdev, 0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 898 | if (irq_base < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 899 | return irq_base; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 900 | |
| 901 | #ifdef CONFIG_PM_SLEEP |
| 902 | gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins, |
| 903 | sizeof(*gpio_dev->saved_regs), |
| 904 | GFP_KERNEL); |
| 905 | if (!gpio_dev->saved_regs) |
| 906 | return -ENOMEM; |
| 907 | #endif |
| 908 | |
| 909 | gpio_dev->pdev = pdev; |
| 910 | gpio_dev->gc.get_direction = amd_gpio_get_direction; |
| 911 | gpio_dev->gc.direction_input = amd_gpio_direction_input; |
| 912 | gpio_dev->gc.direction_output = amd_gpio_direction_output; |
| 913 | gpio_dev->gc.get = amd_gpio_get_value; |
| 914 | gpio_dev->gc.set = amd_gpio_set_value; |
| 915 | gpio_dev->gc.set_config = amd_gpio_set_config; |
| 916 | gpio_dev->gc.dbg_show = amd_gpio_dbg_show; |
| 917 | |
| 918 | gpio_dev->gc.base = -1; |
| 919 | gpio_dev->gc.label = pdev->name; |
| 920 | gpio_dev->gc.owner = THIS_MODULE; |
| 921 | gpio_dev->gc.parent = &pdev->dev; |
| 922 | gpio_dev->gc.ngpio = resource_size(res) / 4; |
| 923 | #if defined(CONFIG_OF_GPIO) |
| 924 | gpio_dev->gc.of_node = pdev->dev.of_node; |
| 925 | #endif |
| 926 | |
| 927 | gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64; |
| 928 | gpio_dev->groups = kerncz_groups; |
| 929 | gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups); |
| 930 | |
| 931 | amd_pinctrl_desc.name = dev_name(&pdev->dev); |
| 932 | gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc, |
| 933 | gpio_dev); |
| 934 | if (IS_ERR(gpio_dev->pctrl)) { |
| 935 | dev_err(&pdev->dev, "Couldn't register pinctrl driver\n"); |
| 936 | return PTR_ERR(gpio_dev->pctrl); |
| 937 | } |
| 938 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 939 | /* Disable and mask interrupts */ |
| 940 | amd_gpio_irq_init(gpio_dev); |
| 941 | |
| 942 | girq = &gpio_dev->gc.irq; |
| 943 | girq->chip = &amd_gpio_irqchip; |
| 944 | /* This will let us handle the parent IRQ in the driver */ |
| 945 | girq->parent_handler = NULL; |
| 946 | girq->num_parents = 0; |
| 947 | girq->parents = NULL; |
| 948 | girq->default_type = IRQ_TYPE_NONE; |
| 949 | girq->handler = handle_simple_irq; |
| 950 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 951 | ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev); |
| 952 | if (ret) |
| 953 | return ret; |
| 954 | |
| 955 | ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev), |
| 956 | 0, 0, gpio_dev->gc.ngpio); |
| 957 | if (ret) { |
| 958 | dev_err(&pdev->dev, "Failed to add pin range\n"); |
| 959 | goto out2; |
| 960 | } |
| 961 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 962 | ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler, |
| 963 | IRQF_SHARED, KBUILD_MODNAME, gpio_dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 964 | if (ret) |
| 965 | goto out2; |
| 966 | |
| 967 | platform_set_drvdata(pdev, gpio_dev); |
| 968 | |
| 969 | dev_dbg(&pdev->dev, "amd gpio driver loaded\n"); |
| 970 | return ret; |
| 971 | |
| 972 | out2: |
| 973 | gpiochip_remove(&gpio_dev->gc); |
| 974 | |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | static int amd_gpio_remove(struct platform_device *pdev) |
| 979 | { |
| 980 | struct amd_gpio *gpio_dev; |
| 981 | |
| 982 | gpio_dev = platform_get_drvdata(pdev); |
| 983 | |
| 984 | gpiochip_remove(&gpio_dev->gc); |
| 985 | |
| 986 | return 0; |
| 987 | } |
| 988 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 989 | #ifdef CONFIG_ACPI |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 990 | static const struct acpi_device_id amd_gpio_acpi_match[] = { |
| 991 | { "AMD0030", 0 }, |
| 992 | { "AMDI0030", 0}, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 993 | { "AMDI0031", 0}, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 994 | { }, |
| 995 | }; |
| 996 | MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 997 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 998 | |
| 999 | static struct platform_driver amd_gpio_driver = { |
| 1000 | .driver = { |
| 1001 | .name = "amd_gpio", |
| 1002 | .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match), |
| 1003 | #ifdef CONFIG_PM_SLEEP |
| 1004 | .pm = &amd_gpio_pm_ops, |
| 1005 | #endif |
| 1006 | }, |
| 1007 | .probe = amd_gpio_probe, |
| 1008 | .remove = amd_gpio_remove, |
| 1009 | }; |
| 1010 | |
| 1011 | module_platform_driver(amd_gpio_driver); |
| 1012 | |
| 1013 | MODULE_LICENSE("GPL v2"); |
| 1014 | MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>"); |
| 1015 | MODULE_DESCRIPTION("AMD GPIO pinctrl driver"); |