Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GPIO driver for AMD |
| 3 | * |
| 4 | * Copyright (c) 2014,2015 AMD Corporation. |
| 5 | * Authors: Ken Xue <Ken.Xue@amd.com> |
| 6 | * Wu, Jeff <Jeff.Wu@amd.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms and conditions of the GNU General Public License, |
| 10 | * version 2, as published by the Free Software Foundation. |
Shyam Sundar S K | add7bfc | 2017-05-03 11:59:11 +0530 | [diff] [blame] | 11 | * |
| 12 | * Contact Information: Nehal Shah <Nehal-bakulchandra.Shah@amd.com> |
| 13 | * Shyam Sundar S K <Shyam-sundar.S-k@amd.com> |
| 14 | * |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/bug.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/compiler.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/log2.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/mutex.h> |
| 31 | #include <linux/acpi.h> |
| 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/list.h> |
| 35 | #include <linux/bitops.h> |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 36 | #include <linux/pinctrl/pinconf.h> |
| 37 | #include <linux/pinctrl/pinconf-generic.h> |
| 38 | |
| 39 | #include "pinctrl-utils.h" |
| 40 | #include "pinctrl-amd.h" |
| 41 | |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 42 | static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 43 | { |
| 44 | unsigned long flags; |
| 45 | u32 pin_reg; |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 46 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 47 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 48 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 49 | pin_reg = readl(gpio_dev->base + offset * 4); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 50 | pin_reg &= ~BIT(OUTPUT_ENABLE_OFF); |
| 51 | writel(pin_reg, gpio_dev->base + offset * 4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 52 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset, |
| 58 | int value) |
| 59 | { |
| 60 | u32 pin_reg; |
| 61 | unsigned long flags; |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 62 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 63 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 64 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 65 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 66 | pin_reg |= BIT(OUTPUT_ENABLE_OFF); |
| 67 | if (value) |
| 68 | pin_reg |= BIT(OUTPUT_VALUE_OFF); |
| 69 | else |
| 70 | pin_reg &= ~BIT(OUTPUT_VALUE_OFF); |
| 71 | writel(pin_reg, gpio_dev->base + offset * 4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 72 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset) |
| 78 | { |
| 79 | u32 pin_reg; |
| 80 | unsigned long flags; |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 81 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 82 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 83 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 84 | pin_reg = readl(gpio_dev->base + offset * 4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 85 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 86 | |
| 87 | return !!(pin_reg & BIT(PIN_STS_OFF)); |
| 88 | } |
| 89 | |
| 90 | static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value) |
| 91 | { |
| 92 | u32 pin_reg; |
| 93 | unsigned long flags; |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 94 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 95 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 96 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 97 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 98 | if (value) |
| 99 | pin_reg |= BIT(OUTPUT_VALUE_OFF); |
| 100 | else |
| 101 | pin_reg &= ~BIT(OUTPUT_VALUE_OFF); |
| 102 | writel(pin_reg, gpio_dev->base + offset * 4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 103 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset, |
| 107 | unsigned debounce) |
| 108 | { |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 109 | u32 time; |
Ken Xue | 25a853d | 2015-03-27 17:44:26 +0800 | [diff] [blame] | 110 | u32 pin_reg; |
| 111 | int ret = 0; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 112 | unsigned long flags; |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 113 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 114 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 115 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 116 | pin_reg = readl(gpio_dev->base + offset * 4); |
| 117 | |
| 118 | if (debounce) { |
| 119 | pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF; |
| 120 | pin_reg &= ~DB_TMR_OUT_MASK; |
| 121 | /* |
| 122 | Debounce Debounce Timer Max |
| 123 | TmrLarge TmrOutUnit Unit Debounce |
| 124 | Time |
| 125 | 0 0 61 usec (2 RtcClk) 976 usec |
| 126 | 0 1 244 usec (8 RtcClk) 3.9 msec |
| 127 | 1 0 15.6 msec (512 RtcClk) 250 msec |
| 128 | 1 1 62.5 msec (2048 RtcClk) 1 sec |
| 129 | */ |
| 130 | |
| 131 | if (debounce < 61) { |
| 132 | pin_reg |= 1; |
| 133 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 134 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 135 | } else if (debounce < 976) { |
| 136 | time = debounce / 61; |
| 137 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 138 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 139 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 140 | } else if (debounce < 3900) { |
| 141 | time = debounce / 244; |
| 142 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 143 | pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF); |
| 144 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 145 | } else if (debounce < 250000) { |
| 146 | time = debounce / 15600; |
| 147 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 148 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 149 | pin_reg |= BIT(DB_TMR_LARGE_OFF); |
| 150 | } else if (debounce < 1000000) { |
| 151 | time = debounce / 62500; |
| 152 | pin_reg |= time & DB_TMR_OUT_MASK; |
| 153 | pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF); |
| 154 | pin_reg |= BIT(DB_TMR_LARGE_OFF); |
| 155 | } else { |
| 156 | pin_reg &= ~DB_CNTRl_MASK; |
Ken Xue | 25a853d | 2015-03-27 17:44:26 +0800 | [diff] [blame] | 157 | ret = -EINVAL; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 158 | } |
| 159 | } else { |
| 160 | pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF); |
| 161 | pin_reg &= ~BIT(DB_TMR_LARGE_OFF); |
| 162 | pin_reg &= ~DB_TMR_OUT_MASK; |
| 163 | pin_reg &= ~DB_CNTRl_MASK; |
| 164 | } |
| 165 | writel(pin_reg, gpio_dev->base + offset * 4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 166 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 167 | |
Ken Xue | 25a853d | 2015-03-27 17:44:26 +0800 | [diff] [blame] | 168 | return ret; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 171 | static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset, |
| 172 | unsigned long config) |
| 173 | { |
| 174 | u32 debounce; |
| 175 | |
| 176 | if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE) |
| 177 | return -ENOTSUPP; |
| 178 | |
| 179 | debounce = pinconf_to_config_argument(config); |
| 180 | return amd_gpio_set_debounce(gc, offset, debounce); |
| 181 | } |
| 182 | |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 183 | #ifdef CONFIG_DEBUG_FS |
| 184 | static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc) |
| 185 | { |
| 186 | u32 pin_reg; |
| 187 | unsigned long flags; |
| 188 | unsigned int bank, i, pin_num; |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 189 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 190 | |
| 191 | char *level_trig; |
| 192 | char *active_level; |
| 193 | char *interrupt_enable; |
| 194 | char *interrupt_mask; |
| 195 | char *wake_cntrl0; |
| 196 | char *wake_cntrl1; |
| 197 | char *wake_cntrl2; |
| 198 | char *pin_sts; |
| 199 | char *pull_up_sel; |
| 200 | char *pull_up_enable; |
| 201 | char *pull_down_enable; |
| 202 | char *output_value; |
| 203 | char *output_enable; |
| 204 | |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 205 | for (bank = 0; bank < gpio_dev->hwbank_num; bank++) { |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 206 | seq_printf(s, "GPIO bank%d\t", bank); |
| 207 | |
| 208 | switch (bank) { |
| 209 | case 0: |
| 210 | i = 0; |
| 211 | pin_num = AMD_GPIO_PINS_BANK0; |
| 212 | break; |
| 213 | case 1: |
| 214 | i = 64; |
| 215 | pin_num = AMD_GPIO_PINS_BANK1 + i; |
| 216 | break; |
| 217 | case 2: |
| 218 | i = 128; |
| 219 | pin_num = AMD_GPIO_PINS_BANK2 + i; |
| 220 | break; |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 221 | case 3: |
| 222 | i = 192; |
| 223 | pin_num = AMD_GPIO_PINS_BANK3 + i; |
| 224 | break; |
Linus Walleij | 6ac4c1a | 2017-01-03 09:18:58 +0100 | [diff] [blame] | 225 | default: |
| 226 | /* Illegal bank number, ignore */ |
| 227 | continue; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 228 | } |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 229 | for (; i < pin_num; i++) { |
| 230 | seq_printf(s, "pin%d\t", i); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 231 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 232 | pin_reg = readl(gpio_dev->base + i * 4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 233 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 234 | |
| 235 | if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) { |
| 236 | interrupt_enable = "interrupt is enabled|"; |
| 237 | |
Dan Carpenter | 3775dac | 2017-01-07 09:32:15 +0300 | [diff] [blame] | 238 | if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) && |
| 239 | !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))) |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 240 | active_level = "Active low|"; |
Dan Carpenter | 3775dac | 2017-01-07 09:32:15 +0300 | [diff] [blame] | 241 | else if (pin_reg & BIT(ACTIVE_LEVEL_OFF) && |
| 242 | !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))) |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 243 | active_level = "Active high|"; |
Dan Carpenter | 3775dac | 2017-01-07 09:32:15 +0300 | [diff] [blame] | 244 | else if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) && |
| 245 | pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)) |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 246 | active_level = "Active on both|"; |
| 247 | else |
Masanari Iida | 0a95160 | 2016-11-23 22:44:47 +0900 | [diff] [blame] | 248 | active_level = "Unknown Active level|"; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 249 | |
| 250 | if (pin_reg & BIT(LEVEL_TRIG_OFF)) |
| 251 | level_trig = "Level trigger|"; |
| 252 | else |
| 253 | level_trig = "Edge trigger|"; |
| 254 | |
| 255 | } else { |
| 256 | interrupt_enable = |
| 257 | "interrupt is disabled|"; |
| 258 | active_level = " "; |
| 259 | level_trig = " "; |
| 260 | } |
| 261 | |
| 262 | if (pin_reg & BIT(INTERRUPT_MASK_OFF)) |
| 263 | interrupt_mask = |
| 264 | "interrupt is unmasked|"; |
| 265 | else |
| 266 | interrupt_mask = |
| 267 | "interrupt is masked|"; |
| 268 | |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 269 | if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3)) |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 270 | wake_cntrl0 = "enable wakeup in S0i3 state|"; |
| 271 | else |
| 272 | wake_cntrl0 = "disable wakeup in S0i3 state|"; |
| 273 | |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 274 | if (pin_reg & BIT(WAKE_CNTRL_OFF_S3)) |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 275 | wake_cntrl1 = "enable wakeup in S3 state|"; |
| 276 | else |
| 277 | wake_cntrl1 = "disable wakeup in S3 state|"; |
| 278 | |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 279 | if (pin_reg & BIT(WAKE_CNTRL_OFF_S4)) |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 280 | wake_cntrl2 = "enable wakeup in S4/S5 state|"; |
| 281 | else |
| 282 | wake_cntrl2 = "disable wakeup in S4/S5 state|"; |
| 283 | |
| 284 | if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) { |
| 285 | pull_up_enable = "pull-up is enabled|"; |
| 286 | if (pin_reg & BIT(PULL_UP_SEL_OFF)) |
| 287 | pull_up_sel = "8k pull-up|"; |
| 288 | else |
| 289 | pull_up_sel = "4k pull-up|"; |
| 290 | } else { |
| 291 | pull_up_enable = "pull-up is disabled|"; |
| 292 | pull_up_sel = " "; |
| 293 | } |
| 294 | |
| 295 | if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF)) |
| 296 | pull_down_enable = "pull-down is enabled|"; |
| 297 | else |
| 298 | pull_down_enable = "Pull-down is disabled|"; |
| 299 | |
| 300 | if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) { |
| 301 | pin_sts = " "; |
| 302 | output_enable = "output is enabled|"; |
| 303 | if (pin_reg & BIT(OUTPUT_VALUE_OFF)) |
| 304 | output_value = "output is high|"; |
| 305 | else |
| 306 | output_value = "output is low|"; |
| 307 | } else { |
| 308 | output_enable = "output is disabled|"; |
| 309 | output_value = " "; |
| 310 | |
| 311 | if (pin_reg & BIT(PIN_STS_OFF)) |
| 312 | pin_sts = "input is high|"; |
| 313 | else |
| 314 | pin_sts = "input is low|"; |
| 315 | } |
| 316 | |
| 317 | seq_printf(s, "%s %s %s %s %s %s\n" |
| 318 | " %s %s %s %s %s %s %s 0x%x\n", |
| 319 | level_trig, active_level, interrupt_enable, |
| 320 | interrupt_mask, wake_cntrl0, wake_cntrl1, |
| 321 | wake_cntrl2, pin_sts, pull_up_sel, |
| 322 | pull_up_enable, pull_down_enable, |
| 323 | output_value, output_enable, pin_reg); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | #else |
| 328 | #define amd_gpio_dbg_show NULL |
| 329 | #endif |
| 330 | |
| 331 | static void amd_gpio_irq_enable(struct irq_data *d) |
| 332 | { |
| 333 | u32 pin_reg; |
| 334 | unsigned long flags; |
| 335 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 336 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 337 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 338 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 339 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 340 | pin_reg |= BIT(INTERRUPT_ENABLE_OFF); |
| 341 | pin_reg |= BIT(INTERRUPT_MASK_OFF); |
| 342 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 343 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void amd_gpio_irq_disable(struct irq_data *d) |
| 347 | { |
| 348 | u32 pin_reg; |
| 349 | unsigned long flags; |
| 350 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 351 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 352 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 353 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 354 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 355 | pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF); |
| 356 | pin_reg &= ~BIT(INTERRUPT_MASK_OFF); |
| 357 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 358 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static void amd_gpio_irq_mask(struct irq_data *d) |
| 362 | { |
| 363 | u32 pin_reg; |
| 364 | unsigned long flags; |
| 365 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 366 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 367 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 368 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 369 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 370 | pin_reg &= ~BIT(INTERRUPT_MASK_OFF); |
| 371 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 372 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void amd_gpio_irq_unmask(struct irq_data *d) |
| 376 | { |
| 377 | u32 pin_reg; |
| 378 | unsigned long flags; |
| 379 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 380 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 381 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 382 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 383 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 384 | pin_reg |= BIT(INTERRUPT_MASK_OFF); |
| 385 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 386 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void amd_gpio_irq_eoi(struct irq_data *d) |
| 390 | { |
| 391 | u32 reg; |
| 392 | unsigned long flags; |
| 393 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 394 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 395 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 396 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 397 | reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG); |
| 398 | reg |= EOI_MASK; |
| 399 | writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 400 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 404 | { |
| 405 | int ret = 0; |
| 406 | u32 pin_reg; |
Shyam Sundar S K | 2983f29 | 2016-12-08 17:31:14 +0530 | [diff] [blame] | 407 | unsigned long flags, irq_flags; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 408 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 409 | struct amd_gpio *gpio_dev = gpiochip_get_data(gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 410 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 411 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 412 | pin_reg = readl(gpio_dev->base + (d->hwirq)*4); |
| 413 | |
Shyam Sundar S K | 2983f29 | 2016-12-08 17:31:14 +0530 | [diff] [blame] | 414 | /* Ignore the settings coming from the client and |
| 415 | * read the values from the ACPI tables |
| 416 | * while setting the trigger type |
Agrawal, Nitesh-kumar | 499c719 | 2016-08-31 08:50:49 +0000 | [diff] [blame] | 417 | */ |
Agrawal, Nitesh-kumar | 499c719 | 2016-08-31 08:50:49 +0000 | [diff] [blame] | 418 | |
Shyam Sundar S K | 2983f29 | 2016-12-08 17:31:14 +0530 | [diff] [blame] | 419 | irq_flags = irq_get_trigger_type(d->irq); |
| 420 | if (irq_flags != IRQ_TYPE_NONE) |
| 421 | type = irq_flags; |
Agrawal, Nitesh-kumar | 499c719 | 2016-08-31 08:50:49 +0000 | [diff] [blame] | 422 | |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 423 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 424 | case IRQ_TYPE_EDGE_RISING: |
| 425 | pin_reg &= ~BIT(LEVEL_TRIG_OFF); |
| 426 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 427 | pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF; |
| 428 | pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF; |
Thomas Gleixner | 9d82931 | 2015-06-23 15:52:47 +0200 | [diff] [blame] | 429 | irq_set_handler_locked(d, handle_edge_irq); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 430 | break; |
| 431 | |
| 432 | case IRQ_TYPE_EDGE_FALLING: |
| 433 | pin_reg &= ~BIT(LEVEL_TRIG_OFF); |
| 434 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 435 | pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF; |
| 436 | pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF; |
Thomas Gleixner | 9d82931 | 2015-06-23 15:52:47 +0200 | [diff] [blame] | 437 | irq_set_handler_locked(d, handle_edge_irq); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 438 | break; |
| 439 | |
| 440 | case IRQ_TYPE_EDGE_BOTH: |
| 441 | pin_reg &= ~BIT(LEVEL_TRIG_OFF); |
| 442 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 443 | pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF; |
| 444 | pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF; |
Thomas Gleixner | 9d82931 | 2015-06-23 15:52:47 +0200 | [diff] [blame] | 445 | irq_set_handler_locked(d, handle_edge_irq); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 446 | break; |
| 447 | |
| 448 | case IRQ_TYPE_LEVEL_HIGH: |
| 449 | pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF; |
| 450 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 451 | pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF; |
| 452 | pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF); |
| 453 | pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF; |
Thomas Gleixner | 9d82931 | 2015-06-23 15:52:47 +0200 | [diff] [blame] | 454 | irq_set_handler_locked(d, handle_level_irq); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | case IRQ_TYPE_LEVEL_LOW: |
| 458 | pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF; |
| 459 | pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF); |
| 460 | pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF; |
| 461 | pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF); |
| 462 | pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF; |
Thomas Gleixner | 9d82931 | 2015-06-23 15:52:47 +0200 | [diff] [blame] | 463 | irq_set_handler_locked(d, handle_level_irq); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 464 | break; |
| 465 | |
| 466 | case IRQ_TYPE_NONE: |
| 467 | break; |
| 468 | |
| 469 | default: |
| 470 | dev_err(&gpio_dev->pdev->dev, "Invalid type value\n"); |
| 471 | ret = -EINVAL; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF; |
| 475 | writel(pin_reg, gpio_dev->base + (d->hwirq)*4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 476 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 477 | |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | static void amd_irq_ack(struct irq_data *d) |
| 482 | { |
| 483 | /* |
| 484 | * based on HW design,there is no need to ack HW |
| 485 | * before handle current irq. But this routine is |
| 486 | * necessary for handle_edge_irq |
| 487 | */ |
| 488 | } |
| 489 | |
| 490 | static struct irq_chip amd_gpio_irqchip = { |
| 491 | .name = "amd_gpio", |
| 492 | .irq_ack = amd_irq_ack, |
| 493 | .irq_enable = amd_gpio_irq_enable, |
| 494 | .irq_disable = amd_gpio_irq_disable, |
| 495 | .irq_mask = amd_gpio_irq_mask, |
| 496 | .irq_unmask = amd_gpio_irq_unmask, |
| 497 | .irq_eoi = amd_gpio_irq_eoi, |
| 498 | .irq_set_type = amd_gpio_irq_set_type, |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 499 | .flags = IRQCHIP_SKIP_SET_WAKE, |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 500 | }; |
| 501 | |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 502 | #define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF)) |
| 503 | |
| 504 | static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id) |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 505 | { |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 506 | struct amd_gpio *gpio_dev = dev_id; |
| 507 | struct gpio_chip *gc = &gpio_dev->gc; |
| 508 | irqreturn_t ret = IRQ_NONE; |
| 509 | unsigned int i, irqnr; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 510 | unsigned long flags; |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 511 | u32 *regs, regval; |
| 512 | u64 status, mask; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 513 | |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 514 | /* Read the wake status */ |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 515 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 516 | status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1); |
| 517 | status <<= 32; |
| 518 | status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 519 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 520 | |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 521 | /* Bit 0-45 contain the relevant status bits */ |
| 522 | status &= (1ULL << 46) - 1; |
| 523 | regs = gpio_dev->base; |
| 524 | for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) { |
| 525 | if (!(status & mask)) |
| 526 | continue; |
| 527 | status &= ~mask; |
| 528 | |
| 529 | /* Each status bit covers four pins */ |
| 530 | for (i = 0; i < 4; i++) { |
| 531 | regval = readl(regs + i); |
| 532 | if (!(regval & PIN_IRQ_PENDING)) |
| 533 | continue; |
| 534 | irq = irq_find_mapping(gc->irqdomain, irqnr + i); |
| 535 | generic_handle_irq(irq); |
| 536 | /* Clear interrupt */ |
| 537 | writel(regval, regs + i); |
| 538 | ret = IRQ_HANDLED; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 542 | /* Signal EOI to the GPIO unit */ |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 543 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 544 | regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG); |
| 545 | regval |= EOI_MASK; |
| 546 | writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 547 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 548 | |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 549 | return ret; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static int amd_get_groups_count(struct pinctrl_dev *pctldev) |
| 553 | { |
| 554 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 555 | |
| 556 | return gpio_dev->ngroups; |
| 557 | } |
| 558 | |
| 559 | static const char *amd_get_group_name(struct pinctrl_dev *pctldev, |
| 560 | unsigned group) |
| 561 | { |
| 562 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 563 | |
| 564 | return gpio_dev->groups[group].name; |
| 565 | } |
| 566 | |
| 567 | static int amd_get_group_pins(struct pinctrl_dev *pctldev, |
| 568 | unsigned group, |
| 569 | const unsigned **pins, |
| 570 | unsigned *num_pins) |
| 571 | { |
| 572 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 573 | |
| 574 | *pins = gpio_dev->groups[group].pins; |
| 575 | *num_pins = gpio_dev->groups[group].npins; |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static const struct pinctrl_ops amd_pinctrl_ops = { |
| 580 | .get_groups_count = amd_get_groups_count, |
| 581 | .get_group_name = amd_get_group_name, |
| 582 | .get_group_pins = amd_get_group_pins, |
| 583 | #ifdef CONFIG_OF |
| 584 | .dt_node_to_map = pinconf_generic_dt_node_to_map_group, |
Irina Tirdea | d32f7fd | 2016-03-31 14:44:42 +0300 | [diff] [blame] | 585 | .dt_free_map = pinctrl_utils_free_map, |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 586 | #endif |
| 587 | }; |
| 588 | |
| 589 | static int amd_pinconf_get(struct pinctrl_dev *pctldev, |
| 590 | unsigned int pin, |
| 591 | unsigned long *config) |
| 592 | { |
| 593 | u32 pin_reg; |
| 594 | unsigned arg; |
| 595 | unsigned long flags; |
| 596 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 597 | enum pin_config_param param = pinconf_to_config_param(*config); |
| 598 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 599 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 600 | pin_reg = readl(gpio_dev->base + pin*4); |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 601 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 602 | switch (param) { |
| 603 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 604 | arg = pin_reg & DB_TMR_OUT_MASK; |
| 605 | break; |
| 606 | |
| 607 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 608 | arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0); |
| 609 | break; |
| 610 | |
| 611 | case PIN_CONFIG_BIAS_PULL_UP: |
| 612 | arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1)); |
| 613 | break; |
| 614 | |
| 615 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 616 | arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK; |
| 617 | break; |
| 618 | |
| 619 | default: |
| 620 | dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n", |
| 621 | param); |
| 622 | return -ENOTSUPP; |
| 623 | } |
| 624 | |
| 625 | *config = pinconf_to_config_packed(param, arg); |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 631 | unsigned long *configs, unsigned num_configs) |
| 632 | { |
| 633 | int i; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 634 | u32 arg; |
Ken Xue | 25a853d | 2015-03-27 17:44:26 +0800 | [diff] [blame] | 635 | int ret = 0; |
| 636 | u32 pin_reg; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 637 | unsigned long flags; |
| 638 | enum pin_config_param param; |
| 639 | struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev); |
| 640 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 641 | raw_spin_lock_irqsave(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 642 | for (i = 0; i < num_configs; i++) { |
| 643 | param = pinconf_to_config_param(configs[i]); |
| 644 | arg = pinconf_to_config_argument(configs[i]); |
| 645 | pin_reg = readl(gpio_dev->base + pin*4); |
| 646 | |
| 647 | switch (param) { |
| 648 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 649 | pin_reg &= ~DB_TMR_OUT_MASK; |
| 650 | pin_reg |= arg & DB_TMR_OUT_MASK; |
| 651 | break; |
| 652 | |
| 653 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 654 | pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF); |
| 655 | pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF; |
| 656 | break; |
| 657 | |
| 658 | case PIN_CONFIG_BIAS_PULL_UP: |
| 659 | pin_reg &= ~BIT(PULL_UP_SEL_OFF); |
| 660 | pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF; |
| 661 | pin_reg &= ~BIT(PULL_UP_ENABLE_OFF); |
| 662 | pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF; |
| 663 | break; |
| 664 | |
| 665 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 666 | pin_reg &= ~(DRV_STRENGTH_SEL_MASK |
| 667 | << DRV_STRENGTH_SEL_OFF); |
| 668 | pin_reg |= (arg & DRV_STRENGTH_SEL_MASK) |
| 669 | << DRV_STRENGTH_SEL_OFF; |
| 670 | break; |
| 671 | |
| 672 | default: |
| 673 | dev_err(&gpio_dev->pdev->dev, |
| 674 | "Invalid config param %04x\n", param); |
Ken Xue | 25a853d | 2015-03-27 17:44:26 +0800 | [diff] [blame] | 675 | ret = -ENOTSUPP; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | writel(pin_reg, gpio_dev->base + pin*4); |
| 679 | } |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 680 | raw_spin_unlock_irqrestore(&gpio_dev->lock, flags); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 681 | |
Ken Xue | 25a853d | 2015-03-27 17:44:26 +0800 | [diff] [blame] | 682 | return ret; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static int amd_pinconf_group_get(struct pinctrl_dev *pctldev, |
| 686 | unsigned int group, |
| 687 | unsigned long *config) |
| 688 | { |
| 689 | const unsigned *pins; |
| 690 | unsigned npins; |
| 691 | int ret; |
| 692 | |
| 693 | ret = amd_get_group_pins(pctldev, group, &pins, &npins); |
| 694 | if (ret) |
| 695 | return ret; |
| 696 | |
| 697 | if (amd_pinconf_get(pctldev, pins[0], config)) |
| 698 | return -ENOTSUPP; |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static int amd_pinconf_group_set(struct pinctrl_dev *pctldev, |
| 704 | unsigned group, unsigned long *configs, |
| 705 | unsigned num_configs) |
| 706 | { |
| 707 | const unsigned *pins; |
| 708 | unsigned npins; |
| 709 | int i, ret; |
| 710 | |
| 711 | ret = amd_get_group_pins(pctldev, group, &pins, &npins); |
| 712 | if (ret) |
| 713 | return ret; |
| 714 | for (i = 0; i < npins; i++) { |
| 715 | if (amd_pinconf_set(pctldev, pins[i], configs, num_configs)) |
| 716 | return -ENOTSUPP; |
| 717 | } |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static const struct pinconf_ops amd_pinconf_ops = { |
| 722 | .pin_config_get = amd_pinconf_get, |
| 723 | .pin_config_set = amd_pinconf_set, |
| 724 | .pin_config_group_get = amd_pinconf_group_get, |
| 725 | .pin_config_group_set = amd_pinconf_group_set, |
| 726 | }; |
| 727 | |
| 728 | static struct pinctrl_desc amd_pinctrl_desc = { |
| 729 | .pins = kerncz_pins, |
| 730 | .npins = ARRAY_SIZE(kerncz_pins), |
| 731 | .pctlops = &amd_pinctrl_ops, |
| 732 | .confops = &amd_pinconf_ops, |
| 733 | .owner = THIS_MODULE, |
| 734 | }; |
| 735 | |
| 736 | static int amd_gpio_probe(struct platform_device *pdev) |
| 737 | { |
| 738 | int ret = 0; |
Ken Xue | 25a853d | 2015-03-27 17:44:26 +0800 | [diff] [blame] | 739 | int irq_base; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 740 | struct resource *res; |
| 741 | struct amd_gpio *gpio_dev; |
| 742 | |
| 743 | gpio_dev = devm_kzalloc(&pdev->dev, |
| 744 | sizeof(struct amd_gpio), GFP_KERNEL); |
| 745 | if (!gpio_dev) |
| 746 | return -ENOMEM; |
| 747 | |
Julia Cartwright | 229710f | 2017-03-09 10:22:04 -0600 | [diff] [blame] | 748 | raw_spin_lock_init(&gpio_dev->lock); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 749 | |
| 750 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 751 | if (!res) { |
| 752 | dev_err(&pdev->dev, "Failed to get gpio io resource.\n"); |
| 753 | return -EINVAL; |
| 754 | } |
| 755 | |
| 756 | gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start, |
| 757 | resource_size(res)); |
Wei Yongjun | 424a6c6 | 2016-02-06 22:56:36 +0800 | [diff] [blame] | 758 | if (!gpio_dev->base) |
| 759 | return -ENOMEM; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 760 | |
| 761 | irq_base = platform_get_irq(pdev, 0); |
| 762 | if (irq_base < 0) { |
Gustavo A. R. Silva | 2e6424ab | 2017-08-09 11:09:33 -0500 | [diff] [blame^] | 763 | dev_err(&pdev->dev, "Failed to get gpio IRQ: %d\n", irq_base); |
| 764 | return irq_base; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | gpio_dev->pdev = pdev; |
| 768 | gpio_dev->gc.direction_input = amd_gpio_direction_input; |
| 769 | gpio_dev->gc.direction_output = amd_gpio_direction_output; |
| 770 | gpio_dev->gc.get = amd_gpio_get_value; |
| 771 | gpio_dev->gc.set = amd_gpio_set_value; |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 772 | gpio_dev->gc.set_config = amd_gpio_set_config; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 773 | gpio_dev->gc.dbg_show = amd_gpio_dbg_show; |
| 774 | |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 775 | gpio_dev->gc.base = -1; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 776 | gpio_dev->gc.label = pdev->name; |
| 777 | gpio_dev->gc.owner = THIS_MODULE; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 778 | gpio_dev->gc.parent = &pdev->dev; |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 779 | gpio_dev->gc.ngpio = resource_size(res) / 4; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 780 | #if defined(CONFIG_OF_GPIO) |
| 781 | gpio_dev->gc.of_node = pdev->dev.of_node; |
| 782 | #endif |
| 783 | |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 784 | gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 785 | gpio_dev->groups = kerncz_groups; |
| 786 | gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups); |
| 787 | |
| 788 | amd_pinctrl_desc.name = dev_name(&pdev->dev); |
Laxman Dewangan | 251e22a | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 789 | gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc, |
| 790 | gpio_dev); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 791 | if (IS_ERR(gpio_dev->pctrl)) { |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 792 | dev_err(&pdev->dev, "Couldn't register pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 793 | return PTR_ERR(gpio_dev->pctrl); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 794 | } |
| 795 | |
Linus Walleij | 04d3672 | 2015-12-08 09:21:38 +0100 | [diff] [blame] | 796 | ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 797 | if (ret) |
Laxman Dewangan | 251e22a | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 798 | return ret; |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 799 | |
| 800 | ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev), |
Shah, Nehal-bakulchandra | 3bfd443 | 2016-12-06 12:17:48 +0530 | [diff] [blame] | 801 | 0, 0, gpio_dev->gc.ngpio); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 802 | if (ret) { |
| 803 | dev_err(&pdev->dev, "Failed to add pin range\n"); |
| 804 | goto out2; |
| 805 | } |
| 806 | |
| 807 | ret = gpiochip_irqchip_add(&gpio_dev->gc, |
| 808 | &amd_gpio_irqchip, |
| 809 | 0, |
| 810 | handle_simple_irq, |
| 811 | IRQ_TYPE_NONE); |
| 812 | if (ret) { |
| 813 | dev_err(&pdev->dev, "could not add irqchip\n"); |
| 814 | ret = -ENODEV; |
| 815 | goto out2; |
| 816 | } |
| 817 | |
Thomas Gleixner | ba714a9 | 2017-05-23 23:23:32 +0200 | [diff] [blame] | 818 | ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler, 0, |
| 819 | KBUILD_MODNAME, gpio_dev); |
| 820 | if (ret) |
| 821 | goto out2; |
| 822 | |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 823 | platform_set_drvdata(pdev, gpio_dev); |
| 824 | |
| 825 | dev_dbg(&pdev->dev, "amd gpio driver loaded\n"); |
| 826 | return ret; |
| 827 | |
| 828 | out2: |
| 829 | gpiochip_remove(&gpio_dev->gc); |
| 830 | |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 831 | return ret; |
| 832 | } |
| 833 | |
| 834 | static int amd_gpio_remove(struct platform_device *pdev) |
| 835 | { |
| 836 | struct amd_gpio *gpio_dev; |
| 837 | |
| 838 | gpio_dev = platform_get_drvdata(pdev); |
| 839 | |
| 840 | gpiochip_remove(&gpio_dev->gc); |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static const struct acpi_device_id amd_gpio_acpi_match[] = { |
| 846 | { "AMD0030", 0 }, |
Wang Hongcheng | 42a4440 | 2016-03-11 10:58:42 +0800 | [diff] [blame] | 847 | { "AMDI0030", 0}, |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 848 | { }, |
| 849 | }; |
| 850 | MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match); |
| 851 | |
| 852 | static struct platform_driver amd_gpio_driver = { |
| 853 | .driver = { |
| 854 | .name = "amd_gpio", |
Ken Xue | dbad75d | 2015-03-10 15:02:19 +0800 | [diff] [blame] | 855 | .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match), |
| 856 | }, |
| 857 | .probe = amd_gpio_probe, |
| 858 | .remove = amd_gpio_remove, |
| 859 | }; |
| 860 | |
| 861 | module_platform_driver(amd_gpio_driver); |
| 862 | |
| 863 | MODULE_LICENSE("GPL v2"); |
| 864 | MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>"); |
| 865 | MODULE_DESCRIPTION("AMD GPIO pinctrl driver"); |