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