blob: 1a7d686494ffbd26f2eefde41d69630bd496c27b [file] [log] [blame]
Thomas Gleixner75a6faf2019-06-01 10:08:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Ken Xuedbad75d2015-03-10 15:02:19 +08002/*
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 *
Shyam Sundar S Kadd7bfc2017-05-03 11:59:11 +05309 * Contact Information: Nehal Shah <Nehal-bakulchandra.Shah@amd.com>
10 * Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
Ken Xuedbad75d2015-03-10 15:02:19 +080011 */
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>
Linus Walleij1c5fb662018-09-13 13:58:21 +020023#include <linux/gpio/driver.h>
Ken Xuedbad75d2015-03-10 15:02:19 +080024#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>
Ken Xuedbad75d2015-03-10 15:02:19 +080032#include <linux/pinctrl/pinconf.h>
33#include <linux/pinctrl/pinconf-generic.h>
34
Daniel Drake79d2c8b2017-09-11 14:11:56 +080035#include "core.h"
Ken Xuedbad75d2015-03-10 15:02:19 +080036#include "pinctrl-utils.h"
37#include "pinctrl-amd.h"
38
Daniel Kurtz12b10f42018-02-16 12:12:43 -070039static 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
Matti Vaittinen3c827872020-02-14 15:57:12 +020049 if (pin_reg & BIT(OUTPUT_ENABLE_OFF))
50 return GPIO_LINE_DIRECTION_OUT;
51
52 return GPIO_LINE_DIRECTION_IN;
Daniel Kurtz12b10f42018-02-16 12:12:43 -070053}
54
Ken Xuedbad75d2015-03-10 15:02:19 +080055static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
56{
57 unsigned long flags;
58 u32 pin_reg;
Linus Walleij04d36722015-12-08 09:21:38 +010059 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080060
Julia Cartwright229710f2017-03-09 10:22:04 -060061 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080062 pin_reg = readl(gpio_dev->base + offset * 4);
Ken Xuedbad75d2015-03-10 15:02:19 +080063 pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
64 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060065 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080066
67 return 0;
68}
69
70static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
71 int value)
72{
73 u32 pin_reg;
74 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010075 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080076
Julia Cartwright229710f2017-03-09 10:22:04 -060077 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080078 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);
Julia Cartwright229710f2017-03-09 10:22:04 -060085 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080086
87 return 0;
88}
89
90static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
91{
92 u32 pin_reg;
93 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010094 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080095
Julia Cartwright229710f2017-03-09 10:22:04 -060096 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080097 pin_reg = readl(gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060098 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080099
100 return !!(pin_reg & BIT(PIN_STS_OFF));
101}
102
103static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
104{
105 u32 pin_reg;
106 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +0100107 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800108
Julia Cartwright229710f2017-03-09 10:22:04 -0600109 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800110 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);
Julia Cartwright229710f2017-03-09 10:22:04 -0600116 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800117}
118
119static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
120 unsigned debounce)
121{
Ken Xuedbad75d2015-03-10 15:02:19 +0800122 u32 time;
Ken Xue25a853d2015-03-27 17:44:26 +0800123 u32 pin_reg;
124 int ret = 0;
Ken Xuedbad75d2015-03-10 15:02:19 +0800125 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +0100126 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800127
Julia Cartwright229710f2017-03-09 10:22:04 -0600128 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800129 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) {
Coiby Xuc64a6a02020-11-06 07:19:10 +0800159 time = debounce / 15625;
Ken Xuedbad75d2015-03-10 15:02:19 +0800160 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 {
Coiby Xu06abe822020-11-06 07:19:09 +0800169 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
Ken Xue25a853d2015-03-27 17:44:26 +0800170 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800171 }
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;
Coiby Xu06abe822020-11-06 07:19:09 +0800176 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
Ken Xuedbad75d2015-03-10 15:02:19 +0800177 }
178 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600179 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800180
Ken Xue25a853d2015-03-27 17:44:26 +0800181 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800182}
183
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300184static 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
Ken Xuedbad75d2015-03-10 15:02:19 +0800196#ifdef CONFIG_DEBUG_FS
197static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
198{
199 u32 pin_reg;
Coiby Xu39cc1d32020-11-06 07:19:11 +0800200 u32 db_cntrl;
Ken Xuedbad75d2015-03-10 15:02:19 +0800201 unsigned long flags;
202 unsigned int bank, i, pin_num;
Linus Walleij04d36722015-12-08 09:21:38 +0100203 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800204
Coiby Xu39cc1d32020-11-06 07:19:11 +0800205 bool tmr_out_unit;
206 unsigned int time;
207 unsigned int unit;
208 bool tmr_large;
209
Ken Xuedbad75d2015-03-10 15:02:19 +0800210 char *level_trig;
211 char *active_level;
212 char *interrupt_enable;
213 char *interrupt_mask;
214 char *wake_cntrl0;
215 char *wake_cntrl1;
216 char *wake_cntrl2;
217 char *pin_sts;
218 char *pull_up_sel;
219 char *pull_up_enable;
220 char *pull_down_enable;
221 char *output_value;
222 char *output_enable;
Coiby Xu39cc1d32020-11-06 07:19:11 +0800223 char debounce_value[40];
224 char *debounce_enable;
Ken Xuedbad75d2015-03-10 15:02:19 +0800225
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530226 for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
Ken Xuedbad75d2015-03-10 15:02:19 +0800227 seq_printf(s, "GPIO bank%d\t", bank);
228
229 switch (bank) {
230 case 0:
231 i = 0;
232 pin_num = AMD_GPIO_PINS_BANK0;
233 break;
234 case 1:
235 i = 64;
236 pin_num = AMD_GPIO_PINS_BANK1 + i;
237 break;
238 case 2:
239 i = 128;
240 pin_num = AMD_GPIO_PINS_BANK2 + i;
241 break;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530242 case 3:
243 i = 192;
244 pin_num = AMD_GPIO_PINS_BANK3 + i;
245 break;
Linus Walleij6ac4c1a2017-01-03 09:18:58 +0100246 default:
247 /* Illegal bank number, ignore */
248 continue;
Ken Xuedbad75d2015-03-10 15:02:19 +0800249 }
Ken Xuedbad75d2015-03-10 15:02:19 +0800250 for (; i < pin_num; i++) {
251 seq_printf(s, "pin%d\t", i);
Julia Cartwright229710f2017-03-09 10:22:04 -0600252 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800253 pin_reg = readl(gpio_dev->base + i * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600254 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800255
256 if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
Daniel Kurtz1766e4b2018-07-16 19:07:41 -0600257 u8 level = (pin_reg >> ACTIVE_LEVEL_OFF) &
258 ACTIVE_LEVEL_MASK;
Ken Xuedbad75d2015-03-10 15:02:19 +0800259 interrupt_enable = "interrupt is enabled|";
260
Daniel Kurtz1766e4b2018-07-16 19:07:41 -0600261 if (level == ACTIVE_LEVEL_HIGH)
Ken Xuedbad75d2015-03-10 15:02:19 +0800262 active_level = "Active high|";
Daniel Kurtz1766e4b2018-07-16 19:07:41 -0600263 else if (level == ACTIVE_LEVEL_LOW)
264 active_level = "Active low|";
265 else if (!(pin_reg & BIT(LEVEL_TRIG_OFF)) &&
266 level == ACTIVE_LEVEL_BOTH)
Ken Xuedbad75d2015-03-10 15:02:19 +0800267 active_level = "Active on both|";
268 else
Masanari Iida0a951602016-11-23 22:44:47 +0900269 active_level = "Unknown Active level|";
Ken Xuedbad75d2015-03-10 15:02:19 +0800270
271 if (pin_reg & BIT(LEVEL_TRIG_OFF))
272 level_trig = "Level trigger|";
273 else
274 level_trig = "Edge trigger|";
275
276 } else {
277 interrupt_enable =
278 "interrupt is disabled|";
279 active_level = " ";
280 level_trig = " ";
281 }
282
283 if (pin_reg & BIT(INTERRUPT_MASK_OFF))
284 interrupt_mask =
285 "interrupt is unmasked|";
286 else
287 interrupt_mask =
288 "interrupt is masked|";
289
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530290 if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800291 wake_cntrl0 = "enable wakeup in S0i3 state|";
292 else
293 wake_cntrl0 = "disable wakeup in S0i3 state|";
294
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530295 if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800296 wake_cntrl1 = "enable wakeup in S3 state|";
297 else
298 wake_cntrl1 = "disable wakeup in S3 state|";
299
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530300 if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
Ken Xuedbad75d2015-03-10 15:02:19 +0800301 wake_cntrl2 = "enable wakeup in S4/S5 state|";
302 else
303 wake_cntrl2 = "disable wakeup in S4/S5 state|";
304
305 if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
306 pull_up_enable = "pull-up is enabled|";
307 if (pin_reg & BIT(PULL_UP_SEL_OFF))
308 pull_up_sel = "8k pull-up|";
309 else
310 pull_up_sel = "4k pull-up|";
311 } else {
312 pull_up_enable = "pull-up is disabled|";
313 pull_up_sel = " ";
314 }
315
316 if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
317 pull_down_enable = "pull-down is enabled|";
318 else
319 pull_down_enable = "Pull-down is disabled|";
320
321 if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
322 pin_sts = " ";
323 output_enable = "output is enabled|";
324 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
325 output_value = "output is high|";
326 else
327 output_value = "output is low|";
328 } else {
329 output_enable = "output is disabled|";
330 output_value = " ";
331
332 if (pin_reg & BIT(PIN_STS_OFF))
333 pin_sts = "input is high|";
334 else
335 pin_sts = "input is low|";
336 }
337
Coiby Xu39cc1d32020-11-06 07:19:11 +0800338 db_cntrl = (DB_CNTRl_MASK << DB_CNTRL_OFF) & pin_reg;
339 if (db_cntrl) {
340 tmr_out_unit = pin_reg & BIT(DB_TMR_OUT_UNIT_OFF);
341 tmr_large = pin_reg & BIT(DB_TMR_LARGE_OFF);
342 time = pin_reg & DB_TMR_OUT_MASK;
343 if (tmr_large) {
344 if (tmr_out_unit)
345 unit = 62500;
346 else
347 unit = 15625;
348 } else {
349 if (tmr_out_unit)
350 unit = 244;
351 else
352 unit = 61;
353 }
354 if ((DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF) == db_cntrl)
355 debounce_enable = "debouncing filter (high and low) enabled|";
356 else if ((DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF) == db_cntrl)
357 debounce_enable = "debouncing filter (low) enabled|";
358 else
359 debounce_enable = "debouncing filter (high) enabled|";
360
361 snprintf(debounce_value, sizeof(debounce_value),
362 "debouncing timeout is %u (us)|", time * unit);
363 } else {
364 debounce_enable = "debouncing filter disabled|";
365 snprintf(debounce_value, sizeof(debounce_value), " ");
366 }
367
Ken Xuedbad75d2015-03-10 15:02:19 +0800368 seq_printf(s, "%s %s %s %s %s %s\n"
Coiby Xu39cc1d32020-11-06 07:19:11 +0800369 " %s %s %s %s %s %s %s %s %s 0x%x\n",
Ken Xuedbad75d2015-03-10 15:02:19 +0800370 level_trig, active_level, interrupt_enable,
371 interrupt_mask, wake_cntrl0, wake_cntrl1,
372 wake_cntrl2, pin_sts, pull_up_sel,
373 pull_up_enable, pull_down_enable,
Coiby Xu39cc1d32020-11-06 07:19:11 +0800374 output_value, output_enable,
375 debounce_enable, debounce_value, pin_reg);
Ken Xuedbad75d2015-03-10 15:02:19 +0800376 }
377 }
378}
379#else
380#define amd_gpio_dbg_show NULL
381#endif
382
383static void amd_gpio_irq_enable(struct irq_data *d)
384{
385 u32 pin_reg;
386 unsigned long flags;
387 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100388 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800389
Julia Cartwright229710f2017-03-09 10:22:04 -0600390 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800391 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
Ken Xuedbad75d2015-03-10 15:02:19 +0800392 pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
393 pin_reg |= BIT(INTERRUPT_MASK_OFF);
394 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600395 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800396}
397
398static void amd_gpio_irq_disable(struct irq_data *d)
399{
400 u32 pin_reg;
401 unsigned long flags;
402 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100403 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800404
Julia Cartwright229710f2017-03-09 10:22:04 -0600405 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800406 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
407 pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
408 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
409 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600410 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800411}
412
413static void amd_gpio_irq_mask(struct irq_data *d)
414{
415 u32 pin_reg;
416 unsigned long flags;
417 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100418 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800419
Julia Cartwright229710f2017-03-09 10:22:04 -0600420 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800421 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
422 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
423 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600424 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800425}
426
427static void amd_gpio_irq_unmask(struct irq_data *d)
428{
429 u32 pin_reg;
430 unsigned long flags;
431 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100432 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800433
Julia Cartwright229710f2017-03-09 10:22:04 -0600434 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800435 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
436 pin_reg |= BIT(INTERRUPT_MASK_OFF);
437 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600438 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800439}
440
Raul E Rangeld62bd5c2021-04-29 16:34:24 -0600441static int amd_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
442{
443 u32 pin_reg;
444 unsigned long flags;
445 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
446 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Mario Limoncielloc4b68e52021-08-09 15:15:13 -0500447 u32 wake_mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3);
Basavaraj Natikaracd47b92021-08-31 17:36:13 +0530448 int err;
Raul E Rangeld62bd5c2021-04-29 16:34:24 -0600449
450 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
451 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
452
453 if (on)
454 pin_reg |= wake_mask;
455 else
456 pin_reg &= ~wake_mask;
457
458 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
459 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
460
Basavaraj Natikaracd47b92021-08-31 17:36:13 +0530461 if (on)
462 err = enable_irq_wake(gpio_dev->irq);
463 else
464 err = disable_irq_wake(gpio_dev->irq);
465
466 if (err)
467 dev_err(&gpio_dev->pdev->dev, "failed to %s wake-up interrupt\n",
468 on ? "enable" : "disable");
469
Raul E Rangeld62bd5c2021-04-29 16:34:24 -0600470 return 0;
471}
472
Ken Xuedbad75d2015-03-10 15:02:19 +0800473static void amd_gpio_irq_eoi(struct irq_data *d)
474{
475 u32 reg;
476 unsigned long flags;
477 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100478 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800479
Julia Cartwright229710f2017-03-09 10:22:04 -0600480 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800481 reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
482 reg |= EOI_MASK;
483 writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600484 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800485}
486
487static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
488{
489 int ret = 0;
Daniel Kurtzb85bfa22018-09-22 13:58:26 -0600490 u32 pin_reg, pin_reg_irq_en, mask;
Furquan Shaikh5f4962d2020-06-26 14:10:26 -0700491 unsigned long flags;
Ken Xuedbad75d2015-03-10 15:02:19 +0800492 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100493 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800494
Julia Cartwright229710f2017-03-09 10:22:04 -0600495 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800496 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
497
498 switch (type & IRQ_TYPE_SENSE_MASK) {
499 case IRQ_TYPE_EDGE_RISING:
500 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
501 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
502 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200503 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800504 break;
505
506 case IRQ_TYPE_EDGE_FALLING:
507 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
508 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
509 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200510 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800511 break;
512
513 case IRQ_TYPE_EDGE_BOTH:
514 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
515 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
516 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200517 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800518 break;
519
520 case IRQ_TYPE_LEVEL_HIGH:
521 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
522 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
523 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200524 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800525 break;
526
527 case IRQ_TYPE_LEVEL_LOW:
528 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
529 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
530 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200531 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800532 break;
533
534 case IRQ_TYPE_NONE:
535 break;
536
537 default:
538 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
539 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800540 }
541
542 pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
Daniel Kurtzb85bfa22018-09-22 13:58:26 -0600543 /*
544 * If WAKE_INT_MASTER_REG.MaskStsEn is set, a software write to the
545 * debounce registers of any GPIO will block wake/interrupt status
Matteo Croce48c67f12019-01-04 22:49:12 +0100546 * generation for *all* GPIOs for a length of time that depends on
Daniel Kurtzb85bfa22018-09-22 13:58:26 -0600547 * WAKE_INT_MASTER_REG.MaskStsLength[11:0]. During this period the
548 * INTERRUPT_ENABLE bit will read as 0.
549 *
550 * We temporarily enable irq for the GPIO whose configuration is
551 * changing, and then wait for it to read back as 1 to know when
552 * debounce has settled and then disable the irq again.
553 * We do this polling with the spinlock held to ensure other GPIO
554 * access routines do not read an incorrect value for the irq enable
555 * bit of other GPIOs. We keep the GPIO masked while polling to avoid
556 * spurious irqs, and disable the irq again after polling.
557 */
558 mask = BIT(INTERRUPT_ENABLE_OFF);
559 pin_reg_irq_en = pin_reg;
560 pin_reg_irq_en |= mask;
561 pin_reg_irq_en &= ~BIT(INTERRUPT_MASK_OFF);
562 writel(pin_reg_irq_en, gpio_dev->base + (d->hwirq)*4);
563 while ((readl(gpio_dev->base + (d->hwirq)*4) & mask) != mask)
564 continue;
Ken Xuedbad75d2015-03-10 15:02:19 +0800565 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600566 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800567
Ken Xuedbad75d2015-03-10 15:02:19 +0800568 return ret;
569}
570
571static void amd_irq_ack(struct irq_data *d)
572{
573 /*
574 * based on HW design,there is no need to ack HW
575 * before handle current irq. But this routine is
576 * necessary for handle_edge_irq
577 */
578}
579
580static struct irq_chip amd_gpio_irqchip = {
581 .name = "amd_gpio",
582 .irq_ack = amd_irq_ack,
583 .irq_enable = amd_gpio_irq_enable,
584 .irq_disable = amd_gpio_irq_disable,
585 .irq_mask = amd_gpio_irq_mask,
586 .irq_unmask = amd_gpio_irq_unmask,
Raul E Rangeld62bd5c2021-04-29 16:34:24 -0600587 .irq_set_wake = amd_gpio_irq_set_wake,
Ken Xuedbad75d2015-03-10 15:02:19 +0800588 .irq_eoi = amd_gpio_irq_eoi,
589 .irq_set_type = amd_gpio_irq_set_type,
Raul E Rangeld62bd5c2021-04-29 16:34:24 -0600590 /*
591 * We need to set IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND so that a wake event
592 * also generates an IRQ. We need the IRQ so the irq_handler can clear
593 * the wake event. Otherwise the wake event will never clear and
594 * prevent the system from suspending.
595 */
596 .flags = IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND,
Ken Xuedbad75d2015-03-10 15:02:19 +0800597};
598
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200599#define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
600
Mario Limonciello2d540672021-10-31 20:48:53 -0500601static bool do_amd_gpio_irq_handler(int irq, void *dev_id)
Ken Xuedbad75d2015-03-10 15:02:19 +0800602{
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200603 struct amd_gpio *gpio_dev = dev_id;
604 struct gpio_chip *gc = &gpio_dev->gc;
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200605 unsigned int i, irqnr;
Ken Xuedbad75d2015-03-10 15:02:19 +0800606 unsigned long flags;
Ben Dooks (Codethink)10ff58a2019-10-22 16:11:54 +0100607 u32 __iomem *regs;
Mario Limonciello2d540672021-10-31 20:48:53 -0500608 bool ret = false;
Ben Dooks (Codethink)10ff58a2019-10-22 16:11:54 +0100609 u32 regval;
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200610 u64 status, mask;
Ken Xuedbad75d2015-03-10 15:02:19 +0800611
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200612 /* Read the wake status */
Julia Cartwright229710f2017-03-09 10:22:04 -0600613 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200614 status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
615 status <<= 32;
616 status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
Julia Cartwright229710f2017-03-09 10:22:04 -0600617 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800618
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200619 /* Bit 0-45 contain the relevant status bits */
620 status &= (1ULL << 46) - 1;
621 regs = gpio_dev->base;
622 for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
623 if (!(status & mask))
624 continue;
625 status &= ~mask;
626
627 /* Each status bit covers four pins */
628 for (i = 0; i < 4; i++) {
629 regval = readl(regs + i);
Mario Limonciello2d540672021-10-31 20:48:53 -0500630 /* caused wake on resume context for shared IRQ */
631 if (irq < 0 && (regval & BIT(WAKE_STS_OFF))) {
632 dev_dbg(&gpio_dev->pdev->dev,
633 "Waking due to GPIO %d: 0x%x",
634 irqnr + i, regval);
635 return true;
636 }
637
Daniel Kurtz8bbed1e2018-07-16 18:57:18 -0600638 if (!(regval & PIN_IRQ_PENDING) ||
639 !(regval & BIT(INTERRUPT_MASK_OFF)))
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200640 continue;
Marc Zyngiera9cb09b2021-05-04 17:42:18 +0100641 generic_handle_domain_irq(gc->irq.domain, irqnr + i);
Daniel Drake6afb1022017-10-02 12:00:54 +0800642
643 /* Clear interrupt.
644 * We must read the pin register again, in case the
645 * value was changed while executing
Marc Zyngiera9cb09b2021-05-04 17:42:18 +0100646 * generic_handle_domain_irq() above.
Daniel Draked21b8ad2019-08-14 17:05:40 +0800647 * If we didn't find a mapping for the interrupt,
648 * disable it in order to avoid a system hang caused
649 * by an interrupt storm.
Daniel Drake6afb1022017-10-02 12:00:54 +0800650 */
651 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
652 regval = readl(regs + i);
Daniel Draked21b8ad2019-08-14 17:05:40 +0800653 if (irq == 0) {
654 regval &= ~BIT(INTERRUPT_ENABLE_OFF);
655 dev_dbg(&gpio_dev->pdev->dev,
656 "Disabling spurious GPIO IRQ %d\n",
657 irqnr + i);
658 }
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200659 writel(regval, regs + i);
Daniel Drake6afb1022017-10-02 12:00:54 +0800660 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Mario Limonciello2d540672021-10-31 20:48:53 -0500661 ret = true;
Ken Xuedbad75d2015-03-10 15:02:19 +0800662 }
663 }
Mario Limonciello2d540672021-10-31 20:48:53 -0500664 /* did not cause wake on resume context for shared IRQ */
665 if (irq < 0)
666 return false;
Ken Xuedbad75d2015-03-10 15:02:19 +0800667
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200668 /* Signal EOI to the GPIO unit */
Julia Cartwright229710f2017-03-09 10:22:04 -0600669 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200670 regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
671 regval |= EOI_MASK;
672 writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600673 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800674
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200675 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800676}
677
Mario Limonciello2d540672021-10-31 20:48:53 -0500678static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
679{
680 return IRQ_RETVAL(do_amd_gpio_irq_handler(irq, dev_id));
681}
682
683static bool __maybe_unused amd_gpio_check_wake(void *dev_id)
684{
685 return do_amd_gpio_irq_handler(-1, dev_id);
686}
687
Ken Xuedbad75d2015-03-10 15:02:19 +0800688static int amd_get_groups_count(struct pinctrl_dev *pctldev)
689{
690 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
691
692 return gpio_dev->ngroups;
693}
694
695static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
696 unsigned group)
697{
698 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
699
700 return gpio_dev->groups[group].name;
701}
702
703static int amd_get_group_pins(struct pinctrl_dev *pctldev,
704 unsigned group,
705 const unsigned **pins,
706 unsigned *num_pins)
707{
708 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
709
710 *pins = gpio_dev->groups[group].pins;
711 *num_pins = gpio_dev->groups[group].npins;
712 return 0;
713}
714
715static const struct pinctrl_ops amd_pinctrl_ops = {
716 .get_groups_count = amd_get_groups_count,
717 .get_group_name = amd_get_group_name,
718 .get_group_pins = amd_get_group_pins,
719#ifdef CONFIG_OF
720 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300721 .dt_free_map = pinctrl_utils_free_map,
Ken Xuedbad75d2015-03-10 15:02:19 +0800722#endif
723};
724
725static int amd_pinconf_get(struct pinctrl_dev *pctldev,
726 unsigned int pin,
727 unsigned long *config)
728{
729 u32 pin_reg;
730 unsigned arg;
731 unsigned long flags;
732 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
733 enum pin_config_param param = pinconf_to_config_param(*config);
734
Julia Cartwright229710f2017-03-09 10:22:04 -0600735 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800736 pin_reg = readl(gpio_dev->base + pin*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600737 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800738 switch (param) {
739 case PIN_CONFIG_INPUT_DEBOUNCE:
740 arg = pin_reg & DB_TMR_OUT_MASK;
741 break;
742
743 case PIN_CONFIG_BIAS_PULL_DOWN:
744 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
745 break;
746
747 case PIN_CONFIG_BIAS_PULL_UP:
748 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
749 break;
750
751 case PIN_CONFIG_DRIVE_STRENGTH:
752 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
753 break;
754
755 default:
756 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
757 param);
758 return -ENOTSUPP;
759 }
760
761 *config = pinconf_to_config_packed(param, arg);
762
763 return 0;
764}
765
766static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
767 unsigned long *configs, unsigned num_configs)
768{
769 int i;
Ken Xuedbad75d2015-03-10 15:02:19 +0800770 u32 arg;
Ken Xue25a853d2015-03-27 17:44:26 +0800771 int ret = 0;
772 u32 pin_reg;
Ken Xuedbad75d2015-03-10 15:02:19 +0800773 unsigned long flags;
774 enum pin_config_param param;
775 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
776
Julia Cartwright229710f2017-03-09 10:22:04 -0600777 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800778 for (i = 0; i < num_configs; i++) {
779 param = pinconf_to_config_param(configs[i]);
780 arg = pinconf_to_config_argument(configs[i]);
781 pin_reg = readl(gpio_dev->base + pin*4);
782
783 switch (param) {
784 case PIN_CONFIG_INPUT_DEBOUNCE:
785 pin_reg &= ~DB_TMR_OUT_MASK;
786 pin_reg |= arg & DB_TMR_OUT_MASK;
787 break;
788
789 case PIN_CONFIG_BIAS_PULL_DOWN:
790 pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
791 pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
792 break;
793
794 case PIN_CONFIG_BIAS_PULL_UP:
795 pin_reg &= ~BIT(PULL_UP_SEL_OFF);
796 pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
797 pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
798 pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
799 break;
800
801 case PIN_CONFIG_DRIVE_STRENGTH:
802 pin_reg &= ~(DRV_STRENGTH_SEL_MASK
803 << DRV_STRENGTH_SEL_OFF);
804 pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
805 << DRV_STRENGTH_SEL_OFF;
806 break;
807
808 default:
809 dev_err(&gpio_dev->pdev->dev,
810 "Invalid config param %04x\n", param);
Ken Xue25a853d2015-03-27 17:44:26 +0800811 ret = -ENOTSUPP;
Ken Xuedbad75d2015-03-10 15:02:19 +0800812 }
813
814 writel(pin_reg, gpio_dev->base + pin*4);
815 }
Julia Cartwright229710f2017-03-09 10:22:04 -0600816 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800817
Ken Xue25a853d2015-03-27 17:44:26 +0800818 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800819}
820
821static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
822 unsigned int group,
823 unsigned long *config)
824{
825 const unsigned *pins;
826 unsigned npins;
827 int ret;
828
829 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
830 if (ret)
831 return ret;
832
833 if (amd_pinconf_get(pctldev, pins[0], config))
834 return -ENOTSUPP;
835
836 return 0;
837}
838
839static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
840 unsigned group, unsigned long *configs,
841 unsigned num_configs)
842{
843 const unsigned *pins;
844 unsigned npins;
845 int i, ret;
846
847 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
848 if (ret)
849 return ret;
850 for (i = 0; i < npins; i++) {
851 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
852 return -ENOTSUPP;
853 }
854 return 0;
855}
856
857static const struct pinconf_ops amd_pinconf_ops = {
858 .pin_config_get = amd_pinconf_get,
859 .pin_config_set = amd_pinconf_set,
860 .pin_config_group_get = amd_pinconf_group_get,
861 .pin_config_group_set = amd_pinconf_group_set,
862};
863
Sachi King4e5a04b2021-10-09 14:32:40 +1100864static void amd_gpio_irq_init(struct amd_gpio *gpio_dev)
865{
866 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
867 unsigned long flags;
868 u32 pin_reg, mask;
869 int i;
870
871 mask = BIT(WAKE_CNTRL_OFF_S0I3) | BIT(WAKE_CNTRL_OFF_S3) |
872 BIT(INTERRUPT_MASK_OFF) | BIT(INTERRUPT_ENABLE_OFF) |
873 BIT(WAKE_CNTRL_OFF_S4);
874
875 for (i = 0; i < desc->npins; i++) {
876 int pin = desc->pins[i].number;
877 const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
878
879 if (!pd)
880 continue;
881
882 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
883
884 pin_reg = readl(gpio_dev->base + i * 4);
885 pin_reg &= ~mask;
886 writel(pin_reg, gpio_dev->base + i * 4);
887
888 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
889 }
890}
891
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800892#ifdef CONFIG_PM_SLEEP
893static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin)
894{
895 const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
896
897 if (!pd)
898 return false;
899
900 /*
901 * Only restore the pin if it is actually in use by the kernel (or
902 * by userspace).
903 */
904 if (pd->mux_owner || pd->gpio_owner ||
905 gpiochip_line_is_irq(&gpio_dev->gc, pin))
906 return true;
907
908 return false;
909}
910
Colin Ian King2d71dfa2017-09-13 17:15:01 +0100911static int amd_gpio_suspend(struct device *dev)
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800912{
Wolfram Sang9f540c32018-10-21 22:00:30 +0200913 struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800914 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
915 int i;
916
917 for (i = 0; i < desc->npins; i++) {
918 int pin = desc->pins[i].number;
919
920 if (!amd_gpio_should_save(gpio_dev, pin))
921 continue;
922
923 gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin*4);
924 }
925
926 return 0;
927}
928
Colin Ian King2d71dfa2017-09-13 17:15:01 +0100929static int amd_gpio_resume(struct device *dev)
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800930{
Wolfram Sang9f540c32018-10-21 22:00:30 +0200931 struct amd_gpio *gpio_dev = dev_get_drvdata(dev);
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800932 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
933 int i;
934
935 for (i = 0; i < desc->npins; i++) {
936 int pin = desc->pins[i].number;
937
938 if (!amd_gpio_should_save(gpio_dev, pin))
939 continue;
940
941 writel(gpio_dev->saved_regs[i], gpio_dev->base + pin*4);
942 }
943
944 return 0;
945}
946
947static const struct dev_pm_ops amd_gpio_pm_ops = {
948 SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend,
949 amd_gpio_resume)
950};
951#endif
952
Ken Xuedbad75d2015-03-10 15:02:19 +0800953static struct pinctrl_desc amd_pinctrl_desc = {
954 .pins = kerncz_pins,
955 .npins = ARRAY_SIZE(kerncz_pins),
956 .pctlops = &amd_pinctrl_ops,
957 .confops = &amd_pinconf_ops,
958 .owner = THIS_MODULE,
959};
960
961static int amd_gpio_probe(struct platform_device *pdev)
962{
963 int ret = 0;
Ken Xuedbad75d2015-03-10 15:02:19 +0800964 struct resource *res;
965 struct amd_gpio *gpio_dev;
Linus Walleije81376eb2020-07-22 12:15:45 +0200966 struct gpio_irq_chip *girq;
Ken Xuedbad75d2015-03-10 15:02:19 +0800967
968 gpio_dev = devm_kzalloc(&pdev->dev,
969 sizeof(struct amd_gpio), GFP_KERNEL);
970 if (!gpio_dev)
971 return -ENOMEM;
972
Julia Cartwright229710f2017-03-09 10:22:04 -0600973 raw_spin_lock_init(&gpio_dev->lock);
Ken Xuedbad75d2015-03-10 15:02:19 +0800974
975 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
976 if (!res) {
977 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
978 return -EINVAL;
979 }
980
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100981 gpio_dev->base = devm_ioremap(&pdev->dev, res->start,
Ken Xuedbad75d2015-03-10 15:02:19 +0800982 resource_size(res));
Wei Yongjun424a6c62016-02-06 22:56:36 +0800983 if (!gpio_dev->base)
984 return -ENOMEM;
Ken Xuedbad75d2015-03-10 15:02:19 +0800985
Basavaraj Natikar7e6f8d62021-08-31 17:36:12 +0530986 gpio_dev->irq = platform_get_irq(pdev, 0);
987 if (gpio_dev->irq < 0)
988 return gpio_dev->irq;
Ken Xuedbad75d2015-03-10 15:02:19 +0800989
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800990#ifdef CONFIG_PM_SLEEP
991 gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
992 sizeof(*gpio_dev->saved_regs),
993 GFP_KERNEL);
994 if (!gpio_dev->saved_regs)
995 return -ENOMEM;
996#endif
997
Ken Xuedbad75d2015-03-10 15:02:19 +0800998 gpio_dev->pdev = pdev;
Daniel Kurtz12b10f42018-02-16 12:12:43 -0700999 gpio_dev->gc.get_direction = amd_gpio_get_direction;
Ken Xuedbad75d2015-03-10 15:02:19 +08001000 gpio_dev->gc.direction_input = amd_gpio_direction_input;
1001 gpio_dev->gc.direction_output = amd_gpio_direction_output;
1002 gpio_dev->gc.get = amd_gpio_get_value;
1003 gpio_dev->gc.set = amd_gpio_set_value;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001004 gpio_dev->gc.set_config = amd_gpio_set_config;
Ken Xuedbad75d2015-03-10 15:02:19 +08001005 gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
1006
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +05301007 gpio_dev->gc.base = -1;
Ken Xuedbad75d2015-03-10 15:02:19 +08001008 gpio_dev->gc.label = pdev->name;
1009 gpio_dev->gc.owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +01001010 gpio_dev->gc.parent = &pdev->dev;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +05301011 gpio_dev->gc.ngpio = resource_size(res) / 4;
Ken Xuedbad75d2015-03-10 15:02:19 +08001012
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +05301013 gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
Ken Xuedbad75d2015-03-10 15:02:19 +08001014 gpio_dev->groups = kerncz_groups;
1015 gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
1016
1017 amd_pinctrl_desc.name = dev_name(&pdev->dev);
Laxman Dewangan251e22a2016-02-24 14:44:07 +05301018 gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
1019 gpio_dev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001020 if (IS_ERR(gpio_dev->pctrl)) {
Ken Xuedbad75d2015-03-10 15:02:19 +08001021 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001022 return PTR_ERR(gpio_dev->pctrl);
Ken Xuedbad75d2015-03-10 15:02:19 +08001023 }
1024
Sachi King4e5a04b2021-10-09 14:32:40 +11001025 /* Disable and mask interrupts */
1026 amd_gpio_irq_init(gpio_dev);
1027
Linus Walleije81376eb2020-07-22 12:15:45 +02001028 girq = &gpio_dev->gc.irq;
1029 girq->chip = &amd_gpio_irqchip;
1030 /* This will let us handle the parent IRQ in the driver */
1031 girq->parent_handler = NULL;
1032 girq->num_parents = 0;
1033 girq->parents = NULL;
1034 girq->default_type = IRQ_TYPE_NONE;
1035 girq->handler = handle_simple_irq;
1036
Linus Walleij04d36722015-12-08 09:21:38 +01001037 ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
Ken Xuedbad75d2015-03-10 15:02:19 +08001038 if (ret)
Laxman Dewangan251e22a2016-02-24 14:44:07 +05301039 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +08001040
1041 ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +05301042 0, 0, gpio_dev->gc.ngpio);
Ken Xuedbad75d2015-03-10 15:02:19 +08001043 if (ret) {
1044 dev_err(&pdev->dev, "Failed to add pin range\n");
1045 goto out2;
1046 }
1047
Basavaraj Natikar7e6f8d62021-08-31 17:36:12 +05301048 ret = devm_request_irq(&pdev->dev, gpio_dev->irq, amd_gpio_irq_handler,
Sandeep Singh279ffaf2019-04-04 13:16:26 +00001049 IRQF_SHARED, KBUILD_MODNAME, gpio_dev);
Thomas Gleixnerba714a92017-05-23 23:23:32 +02001050 if (ret)
1051 goto out2;
1052
Ken Xuedbad75d2015-03-10 15:02:19 +08001053 platform_set_drvdata(pdev, gpio_dev);
Mario Limonciello2d540672021-10-31 20:48:53 -05001054 acpi_register_wakeup_handler(gpio_dev->irq, amd_gpio_check_wake, gpio_dev);
Ken Xuedbad75d2015-03-10 15:02:19 +08001055
1056 dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
1057 return ret;
1058
1059out2:
1060 gpiochip_remove(&gpio_dev->gc);
1061
Ken Xuedbad75d2015-03-10 15:02:19 +08001062 return ret;
1063}
1064
1065static int amd_gpio_remove(struct platform_device *pdev)
1066{
1067 struct amd_gpio *gpio_dev;
1068
1069 gpio_dev = platform_get_drvdata(pdev);
1070
1071 gpiochip_remove(&gpio_dev->gc);
Mario Limonciello2d540672021-10-31 20:48:53 -05001072 acpi_unregister_wakeup_handler(amd_gpio_check_wake, gpio_dev);
Ken Xuedbad75d2015-03-10 15:02:19 +08001073
1074 return 0;
1075}
1076
Lee Jonesde4334f2020-07-13 15:49:30 +01001077#ifdef CONFIG_ACPI
Ken Xuedbad75d2015-03-10 15:02:19 +08001078static const struct acpi_device_id amd_gpio_acpi_match[] = {
1079 { "AMD0030", 0 },
Wang Hongcheng42a44402016-03-11 10:58:42 +08001080 { "AMDI0030", 0},
Maximilian Luz1ca46d32021-05-12 23:03:16 +02001081 { "AMDI0031", 0},
Ken Xuedbad75d2015-03-10 15:02:19 +08001082 { },
1083};
1084MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
Lee Jonesde4334f2020-07-13 15:49:30 +01001085#endif
Ken Xuedbad75d2015-03-10 15:02:19 +08001086
1087static struct platform_driver amd_gpio_driver = {
1088 .driver = {
1089 .name = "amd_gpio",
Ken Xuedbad75d2015-03-10 15:02:19 +08001090 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
Daniel Drake79d2c8b2017-09-11 14:11:56 +08001091#ifdef CONFIG_PM_SLEEP
1092 .pm = &amd_gpio_pm_ops,
1093#endif
Ken Xuedbad75d2015-03-10 15:02:19 +08001094 },
1095 .probe = amd_gpio_probe,
1096 .remove = amd_gpio_remove,
1097};
1098
1099module_platform_driver(amd_gpio_driver);
1100
1101MODULE_LICENSE("GPL v2");
1102MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
1103MODULE_DESCRIPTION("AMD GPIO pinctrl driver");