blob: e432ec887479d32b2be5afcc1a26e295909b04cb [file] [log] [blame]
Ken Xuedbad75d2015-03-10 15:02:19 +08001/*
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.
11 */
12
13#include <linux/err.h>
14#include <linux/bug.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/spinlock.h>
18#include <linux/compiler.h>
19#include <linux/types.h>
20#include <linux/errno.h>
21#include <linux/log2.h>
22#include <linux/io.h>
23#include <linux/gpio.h>
24#include <linux/slab.h>
25#include <linux/platform_device.h>
26#include <linux/mutex.h>
27#include <linux/acpi.h>
28#include <linux/seq_file.h>
29#include <linux/interrupt.h>
30#include <linux/list.h>
31#include <linux/bitops.h>
Ken Xuedbad75d2015-03-10 15:02:19 +080032#include <linux/pinctrl/pinconf.h>
33#include <linux/pinctrl/pinconf-generic.h>
34
35#include "pinctrl-utils.h"
36#include "pinctrl-amd.h"
37
Ken Xuedbad75d2015-03-10 15:02:19 +080038static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
39{
40 unsigned long flags;
41 u32 pin_reg;
Linus Walleij04d36722015-12-08 09:21:38 +010042 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080043
Julia Cartwright229710f2017-03-09 10:22:04 -060044 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080045 pin_reg = readl(gpio_dev->base + offset * 4);
Ken Xuedbad75d2015-03-10 15:02:19 +080046 pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
47 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060048 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080049
50 return 0;
51}
52
53static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
54 int value)
55{
56 u32 pin_reg;
57 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010058 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080059
Julia Cartwright229710f2017-03-09 10:22:04 -060060 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080061 pin_reg = readl(gpio_dev->base + offset * 4);
62 pin_reg |= BIT(OUTPUT_ENABLE_OFF);
63 if (value)
64 pin_reg |= BIT(OUTPUT_VALUE_OFF);
65 else
66 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
67 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060068 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080069
70 return 0;
71}
72
73static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
74{
75 u32 pin_reg;
76 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010077 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080078
Julia Cartwright229710f2017-03-09 10:22:04 -060079 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080080 pin_reg = readl(gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060081 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080082
83 return !!(pin_reg & BIT(PIN_STS_OFF));
84}
85
86static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
87{
88 u32 pin_reg;
89 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010090 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080091
Julia Cartwright229710f2017-03-09 10:22:04 -060092 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080093 pin_reg = readl(gpio_dev->base + offset * 4);
94 if (value)
95 pin_reg |= BIT(OUTPUT_VALUE_OFF);
96 else
97 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
98 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060099 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800100}
101
102static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
103 unsigned debounce)
104{
Ken Xuedbad75d2015-03-10 15:02:19 +0800105 u32 time;
Ken Xue25a853d2015-03-27 17:44:26 +0800106 u32 pin_reg;
107 int ret = 0;
Ken Xuedbad75d2015-03-10 15:02:19 +0800108 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +0100109 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800110
Julia Cartwright229710f2017-03-09 10:22:04 -0600111 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800112 pin_reg = readl(gpio_dev->base + offset * 4);
113
114 if (debounce) {
115 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
116 pin_reg &= ~DB_TMR_OUT_MASK;
117 /*
118 Debounce Debounce Timer Max
119 TmrLarge TmrOutUnit Unit Debounce
120 Time
121 0 0 61 usec (2 RtcClk) 976 usec
122 0 1 244 usec (8 RtcClk) 3.9 msec
123 1 0 15.6 msec (512 RtcClk) 250 msec
124 1 1 62.5 msec (2048 RtcClk) 1 sec
125 */
126
127 if (debounce < 61) {
128 pin_reg |= 1;
129 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
130 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
131 } else if (debounce < 976) {
132 time = debounce / 61;
133 pin_reg |= time & DB_TMR_OUT_MASK;
134 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
135 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
136 } else if (debounce < 3900) {
137 time = debounce / 244;
138 pin_reg |= time & DB_TMR_OUT_MASK;
139 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
140 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
141 } else if (debounce < 250000) {
142 time = debounce / 15600;
143 pin_reg |= time & DB_TMR_OUT_MASK;
144 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
145 pin_reg |= BIT(DB_TMR_LARGE_OFF);
146 } else if (debounce < 1000000) {
147 time = debounce / 62500;
148 pin_reg |= time & DB_TMR_OUT_MASK;
149 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
150 pin_reg |= BIT(DB_TMR_LARGE_OFF);
151 } else {
152 pin_reg &= ~DB_CNTRl_MASK;
Ken Xue25a853d2015-03-27 17:44:26 +0800153 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800154 }
155 } else {
156 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
157 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
158 pin_reg &= ~DB_TMR_OUT_MASK;
159 pin_reg &= ~DB_CNTRl_MASK;
160 }
161 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600162 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800163
Ken Xue25a853d2015-03-27 17:44:26 +0800164 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800165}
166
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300167static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
168 unsigned long config)
169{
170 u32 debounce;
171
172 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
173 return -ENOTSUPP;
174
175 debounce = pinconf_to_config_argument(config);
176 return amd_gpio_set_debounce(gc, offset, debounce);
177}
178
Ken Xuedbad75d2015-03-10 15:02:19 +0800179#ifdef CONFIG_DEBUG_FS
180static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
181{
182 u32 pin_reg;
183 unsigned long flags;
184 unsigned int bank, i, pin_num;
Linus Walleij04d36722015-12-08 09:21:38 +0100185 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800186
187 char *level_trig;
188 char *active_level;
189 char *interrupt_enable;
190 char *interrupt_mask;
191 char *wake_cntrl0;
192 char *wake_cntrl1;
193 char *wake_cntrl2;
194 char *pin_sts;
195 char *pull_up_sel;
196 char *pull_up_enable;
197 char *pull_down_enable;
198 char *output_value;
199 char *output_enable;
200
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530201 for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
Ken Xuedbad75d2015-03-10 15:02:19 +0800202 seq_printf(s, "GPIO bank%d\t", bank);
203
204 switch (bank) {
205 case 0:
206 i = 0;
207 pin_num = AMD_GPIO_PINS_BANK0;
208 break;
209 case 1:
210 i = 64;
211 pin_num = AMD_GPIO_PINS_BANK1 + i;
212 break;
213 case 2:
214 i = 128;
215 pin_num = AMD_GPIO_PINS_BANK2 + i;
216 break;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530217 case 3:
218 i = 192;
219 pin_num = AMD_GPIO_PINS_BANK3 + i;
220 break;
Linus Walleij6ac4c1a2017-01-03 09:18:58 +0100221 default:
222 /* Illegal bank number, ignore */
223 continue;
Ken Xuedbad75d2015-03-10 15:02:19 +0800224 }
Ken Xuedbad75d2015-03-10 15:02:19 +0800225 for (; i < pin_num; i++) {
226 seq_printf(s, "pin%d\t", i);
Julia Cartwright229710f2017-03-09 10:22:04 -0600227 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800228 pin_reg = readl(gpio_dev->base + i * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600229 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800230
231 if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
232 interrupt_enable = "interrupt is enabled|";
233
Dan Carpenter3775dac2017-01-07 09:32:15 +0300234 if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
235 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
Ken Xuedbad75d2015-03-10 15:02:19 +0800236 active_level = "Active low|";
Dan Carpenter3775dac2017-01-07 09:32:15 +0300237 else if (pin_reg & BIT(ACTIVE_LEVEL_OFF) &&
238 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
Ken Xuedbad75d2015-03-10 15:02:19 +0800239 active_level = "Active high|";
Dan Carpenter3775dac2017-01-07 09:32:15 +0300240 else if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
241 pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))
Ken Xuedbad75d2015-03-10 15:02:19 +0800242 active_level = "Active on both|";
243 else
Masanari Iida0a951602016-11-23 22:44:47 +0900244 active_level = "Unknown Active level|";
Ken Xuedbad75d2015-03-10 15:02:19 +0800245
246 if (pin_reg & BIT(LEVEL_TRIG_OFF))
247 level_trig = "Level trigger|";
248 else
249 level_trig = "Edge trigger|";
250
251 } else {
252 interrupt_enable =
253 "interrupt is disabled|";
254 active_level = " ";
255 level_trig = " ";
256 }
257
258 if (pin_reg & BIT(INTERRUPT_MASK_OFF))
259 interrupt_mask =
260 "interrupt is unmasked|";
261 else
262 interrupt_mask =
263 "interrupt is masked|";
264
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530265 if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800266 wake_cntrl0 = "enable wakeup in S0i3 state|";
267 else
268 wake_cntrl0 = "disable wakeup in S0i3 state|";
269
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530270 if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800271 wake_cntrl1 = "enable wakeup in S3 state|";
272 else
273 wake_cntrl1 = "disable wakeup in S3 state|";
274
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530275 if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
Ken Xuedbad75d2015-03-10 15:02:19 +0800276 wake_cntrl2 = "enable wakeup in S4/S5 state|";
277 else
278 wake_cntrl2 = "disable wakeup in S4/S5 state|";
279
280 if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
281 pull_up_enable = "pull-up is enabled|";
282 if (pin_reg & BIT(PULL_UP_SEL_OFF))
283 pull_up_sel = "8k pull-up|";
284 else
285 pull_up_sel = "4k pull-up|";
286 } else {
287 pull_up_enable = "pull-up is disabled|";
288 pull_up_sel = " ";
289 }
290
291 if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
292 pull_down_enable = "pull-down is enabled|";
293 else
294 pull_down_enable = "Pull-down is disabled|";
295
296 if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
297 pin_sts = " ";
298 output_enable = "output is enabled|";
299 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
300 output_value = "output is high|";
301 else
302 output_value = "output is low|";
303 } else {
304 output_enable = "output is disabled|";
305 output_value = " ";
306
307 if (pin_reg & BIT(PIN_STS_OFF))
308 pin_sts = "input is high|";
309 else
310 pin_sts = "input is low|";
311 }
312
313 seq_printf(s, "%s %s %s %s %s %s\n"
314 " %s %s %s %s %s %s %s 0x%x\n",
315 level_trig, active_level, interrupt_enable,
316 interrupt_mask, wake_cntrl0, wake_cntrl1,
317 wake_cntrl2, pin_sts, pull_up_sel,
318 pull_up_enable, pull_down_enable,
319 output_value, output_enable, pin_reg);
320 }
321 }
322}
323#else
324#define amd_gpio_dbg_show NULL
325#endif
326
327static void amd_gpio_irq_enable(struct irq_data *d)
328{
329 u32 pin_reg;
330 unsigned long flags;
331 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100332 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800333
Julia Cartwright229710f2017-03-09 10:22:04 -0600334 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800335 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
Ken Xuedbad75d2015-03-10 15:02:19 +0800336 pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
337 pin_reg |= BIT(INTERRUPT_MASK_OFF);
338 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600339 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800340}
341
342static void amd_gpio_irq_disable(struct irq_data *d)
343{
344 u32 pin_reg;
345 unsigned long flags;
346 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100347 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800348
Julia Cartwright229710f2017-03-09 10:22:04 -0600349 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800350 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
351 pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
352 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
353 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600354 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800355}
356
357static void amd_gpio_irq_mask(struct irq_data *d)
358{
359 u32 pin_reg;
360 unsigned long flags;
361 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100362 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800363
Julia Cartwright229710f2017-03-09 10:22:04 -0600364 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800365 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
366 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
367 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600368 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800369}
370
371static void amd_gpio_irq_unmask(struct irq_data *d)
372{
373 u32 pin_reg;
374 unsigned long flags;
375 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100376 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800377
Julia Cartwright229710f2017-03-09 10:22:04 -0600378 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800379 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
380 pin_reg |= BIT(INTERRUPT_MASK_OFF);
381 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600382 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800383}
384
385static void amd_gpio_irq_eoi(struct irq_data *d)
386{
387 u32 reg;
388 unsigned long flags;
389 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100390 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800391
Julia Cartwright229710f2017-03-09 10:22:04 -0600392 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800393 reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
394 reg |= EOI_MASK;
395 writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600396 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800397}
398
399static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
400{
401 int ret = 0;
402 u32 pin_reg;
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530403 unsigned long flags, irq_flags;
Ken Xuedbad75d2015-03-10 15:02:19 +0800404 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100405 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800406
Julia Cartwright229710f2017-03-09 10:22:04 -0600407 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800408 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
409
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530410 /* Ignore the settings coming from the client and
411 * read the values from the ACPI tables
412 * while setting the trigger type
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000413 */
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000414
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530415 irq_flags = irq_get_trigger_type(d->irq);
416 if (irq_flags != IRQ_TYPE_NONE)
417 type = irq_flags;
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000418
Ken Xuedbad75d2015-03-10 15:02:19 +0800419 switch (type & IRQ_TYPE_SENSE_MASK) {
420 case IRQ_TYPE_EDGE_RISING:
421 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
422 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
423 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
424 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200425 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800426 break;
427
428 case IRQ_TYPE_EDGE_FALLING:
429 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
430 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
431 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
432 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200433 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800434 break;
435
436 case IRQ_TYPE_EDGE_BOTH:
437 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
438 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
439 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
440 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200441 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800442 break;
443
444 case IRQ_TYPE_LEVEL_HIGH:
445 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
446 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
447 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
448 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
449 pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200450 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800451 break;
452
453 case IRQ_TYPE_LEVEL_LOW:
454 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
455 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
456 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
457 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
458 pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200459 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800460 break;
461
462 case IRQ_TYPE_NONE:
463 break;
464
465 default:
466 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
467 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800468 }
469
470 pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
471 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600472 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800473
Ken Xuedbad75d2015-03-10 15:02:19 +0800474 return ret;
475}
476
477static void amd_irq_ack(struct irq_data *d)
478{
479 /*
480 * based on HW design,there is no need to ack HW
481 * before handle current irq. But this routine is
482 * necessary for handle_edge_irq
483 */
484}
485
486static struct irq_chip amd_gpio_irqchip = {
487 .name = "amd_gpio",
488 .irq_ack = amd_irq_ack,
489 .irq_enable = amd_gpio_irq_enable,
490 .irq_disable = amd_gpio_irq_disable,
491 .irq_mask = amd_gpio_irq_mask,
492 .irq_unmask = amd_gpio_irq_unmask,
493 .irq_eoi = amd_gpio_irq_eoi,
494 .irq_set_type = amd_gpio_irq_set_type,
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530495 .flags = IRQCHIP_SKIP_SET_WAKE,
Ken Xuedbad75d2015-03-10 15:02:19 +0800496};
497
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200498#define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
499
500static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
Ken Xuedbad75d2015-03-10 15:02:19 +0800501{
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200502 struct amd_gpio *gpio_dev = dev_id;
503 struct gpio_chip *gc = &gpio_dev->gc;
504 irqreturn_t ret = IRQ_NONE;
505 unsigned int i, irqnr;
Ken Xuedbad75d2015-03-10 15:02:19 +0800506 unsigned long flags;
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200507 u32 *regs, regval;
508 u64 status, mask;
Ken Xuedbad75d2015-03-10 15:02:19 +0800509
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200510 /* Read the wake status */
Julia Cartwright229710f2017-03-09 10:22:04 -0600511 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200512 status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
513 status <<= 32;
514 status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
Julia Cartwright229710f2017-03-09 10:22:04 -0600515 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800516
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200517 /* Bit 0-45 contain the relevant status bits */
518 status &= (1ULL << 46) - 1;
519 regs = gpio_dev->base;
520 for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
521 if (!(status & mask))
522 continue;
523 status &= ~mask;
524
525 /* Each status bit covers four pins */
526 for (i = 0; i < 4; i++) {
527 regval = readl(regs + i);
528 if (!(regval & PIN_IRQ_PENDING))
529 continue;
530 irq = irq_find_mapping(gc->irqdomain, irqnr + i);
531 generic_handle_irq(irq);
532 /* Clear interrupt */
533 writel(regval, regs + i);
534 ret = IRQ_HANDLED;
Ken Xuedbad75d2015-03-10 15:02:19 +0800535 }
536 }
537
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200538 /* Signal EOI to the GPIO unit */
Julia Cartwright229710f2017-03-09 10:22:04 -0600539 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200540 regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
541 regval |= EOI_MASK;
542 writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600543 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800544
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200545 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800546}
547
548static int amd_get_groups_count(struct pinctrl_dev *pctldev)
549{
550 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
551
552 return gpio_dev->ngroups;
553}
554
555static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
556 unsigned group)
557{
558 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
559
560 return gpio_dev->groups[group].name;
561}
562
563static int amd_get_group_pins(struct pinctrl_dev *pctldev,
564 unsigned group,
565 const unsigned **pins,
566 unsigned *num_pins)
567{
568 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
569
570 *pins = gpio_dev->groups[group].pins;
571 *num_pins = gpio_dev->groups[group].npins;
572 return 0;
573}
574
575static const struct pinctrl_ops amd_pinctrl_ops = {
576 .get_groups_count = amd_get_groups_count,
577 .get_group_name = amd_get_group_name,
578 .get_group_pins = amd_get_group_pins,
579#ifdef CONFIG_OF
580 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300581 .dt_free_map = pinctrl_utils_free_map,
Ken Xuedbad75d2015-03-10 15:02:19 +0800582#endif
583};
584
585static int amd_pinconf_get(struct pinctrl_dev *pctldev,
586 unsigned int pin,
587 unsigned long *config)
588{
589 u32 pin_reg;
590 unsigned arg;
591 unsigned long flags;
592 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
593 enum pin_config_param param = pinconf_to_config_param(*config);
594
Julia Cartwright229710f2017-03-09 10:22:04 -0600595 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800596 pin_reg = readl(gpio_dev->base + pin*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600597 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800598 switch (param) {
599 case PIN_CONFIG_INPUT_DEBOUNCE:
600 arg = pin_reg & DB_TMR_OUT_MASK;
601 break;
602
603 case PIN_CONFIG_BIAS_PULL_DOWN:
604 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
605 break;
606
607 case PIN_CONFIG_BIAS_PULL_UP:
608 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
609 break;
610
611 case PIN_CONFIG_DRIVE_STRENGTH:
612 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
613 break;
614
615 default:
616 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
617 param);
618 return -ENOTSUPP;
619 }
620
621 *config = pinconf_to_config_packed(param, arg);
622
623 return 0;
624}
625
626static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
627 unsigned long *configs, unsigned num_configs)
628{
629 int i;
Ken Xuedbad75d2015-03-10 15:02:19 +0800630 u32 arg;
Ken Xue25a853d2015-03-27 17:44:26 +0800631 int ret = 0;
632 u32 pin_reg;
Ken Xuedbad75d2015-03-10 15:02:19 +0800633 unsigned long flags;
634 enum pin_config_param param;
635 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
636
Julia Cartwright229710f2017-03-09 10:22:04 -0600637 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800638 for (i = 0; i < num_configs; i++) {
639 param = pinconf_to_config_param(configs[i]);
640 arg = pinconf_to_config_argument(configs[i]);
641 pin_reg = readl(gpio_dev->base + pin*4);
642
643 switch (param) {
644 case PIN_CONFIG_INPUT_DEBOUNCE:
645 pin_reg &= ~DB_TMR_OUT_MASK;
646 pin_reg |= arg & DB_TMR_OUT_MASK;
647 break;
648
649 case PIN_CONFIG_BIAS_PULL_DOWN:
650 pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
651 pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
652 break;
653
654 case PIN_CONFIG_BIAS_PULL_UP:
655 pin_reg &= ~BIT(PULL_UP_SEL_OFF);
656 pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
657 pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
658 pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
659 break;
660
661 case PIN_CONFIG_DRIVE_STRENGTH:
662 pin_reg &= ~(DRV_STRENGTH_SEL_MASK
663 << DRV_STRENGTH_SEL_OFF);
664 pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
665 << DRV_STRENGTH_SEL_OFF;
666 break;
667
668 default:
669 dev_err(&gpio_dev->pdev->dev,
670 "Invalid config param %04x\n", param);
Ken Xue25a853d2015-03-27 17:44:26 +0800671 ret = -ENOTSUPP;
Ken Xuedbad75d2015-03-10 15:02:19 +0800672 }
673
674 writel(pin_reg, gpio_dev->base + pin*4);
675 }
Julia Cartwright229710f2017-03-09 10:22:04 -0600676 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800677
Ken Xue25a853d2015-03-27 17:44:26 +0800678 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800679}
680
681static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
682 unsigned int group,
683 unsigned long *config)
684{
685 const unsigned *pins;
686 unsigned npins;
687 int ret;
688
689 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
690 if (ret)
691 return ret;
692
693 if (amd_pinconf_get(pctldev, pins[0], config))
694 return -ENOTSUPP;
695
696 return 0;
697}
698
699static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
700 unsigned group, unsigned long *configs,
701 unsigned num_configs)
702{
703 const unsigned *pins;
704 unsigned npins;
705 int i, ret;
706
707 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
708 if (ret)
709 return ret;
710 for (i = 0; i < npins; i++) {
711 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
712 return -ENOTSUPP;
713 }
714 return 0;
715}
716
717static const struct pinconf_ops amd_pinconf_ops = {
718 .pin_config_get = amd_pinconf_get,
719 .pin_config_set = amd_pinconf_set,
720 .pin_config_group_get = amd_pinconf_group_get,
721 .pin_config_group_set = amd_pinconf_group_set,
722};
723
724static struct pinctrl_desc amd_pinctrl_desc = {
725 .pins = kerncz_pins,
726 .npins = ARRAY_SIZE(kerncz_pins),
727 .pctlops = &amd_pinctrl_ops,
728 .confops = &amd_pinconf_ops,
729 .owner = THIS_MODULE,
730};
731
732static int amd_gpio_probe(struct platform_device *pdev)
733{
734 int ret = 0;
Ken Xue25a853d2015-03-27 17:44:26 +0800735 int irq_base;
Ken Xuedbad75d2015-03-10 15:02:19 +0800736 struct resource *res;
737 struct amd_gpio *gpio_dev;
738
739 gpio_dev = devm_kzalloc(&pdev->dev,
740 sizeof(struct amd_gpio), GFP_KERNEL);
741 if (!gpio_dev)
742 return -ENOMEM;
743
Julia Cartwright229710f2017-03-09 10:22:04 -0600744 raw_spin_lock_init(&gpio_dev->lock);
Ken Xuedbad75d2015-03-10 15:02:19 +0800745
746 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
747 if (!res) {
748 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
749 return -EINVAL;
750 }
751
752 gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
753 resource_size(res));
Wei Yongjun424a6c62016-02-06 22:56:36 +0800754 if (!gpio_dev->base)
755 return -ENOMEM;
Ken Xuedbad75d2015-03-10 15:02:19 +0800756
757 irq_base = platform_get_irq(pdev, 0);
758 if (irq_base < 0) {
759 dev_err(&pdev->dev, "Failed to get gpio IRQ.\n");
760 return -EINVAL;
761 }
762
763 gpio_dev->pdev = pdev;
764 gpio_dev->gc.direction_input = amd_gpio_direction_input;
765 gpio_dev->gc.direction_output = amd_gpio_direction_output;
766 gpio_dev->gc.get = amd_gpio_get_value;
767 gpio_dev->gc.set = amd_gpio_set_value;
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300768 gpio_dev->gc.set_config = amd_gpio_set_config;
Ken Xuedbad75d2015-03-10 15:02:19 +0800769 gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
770
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530771 gpio_dev->gc.base = -1;
Ken Xuedbad75d2015-03-10 15:02:19 +0800772 gpio_dev->gc.label = pdev->name;
773 gpio_dev->gc.owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +0100774 gpio_dev->gc.parent = &pdev->dev;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530775 gpio_dev->gc.ngpio = resource_size(res) / 4;
Ken Xuedbad75d2015-03-10 15:02:19 +0800776#if defined(CONFIG_OF_GPIO)
777 gpio_dev->gc.of_node = pdev->dev.of_node;
778#endif
779
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530780 gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
Ken Xuedbad75d2015-03-10 15:02:19 +0800781 gpio_dev->groups = kerncz_groups;
782 gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
783
784 amd_pinctrl_desc.name = dev_name(&pdev->dev);
Laxman Dewangan251e22a2016-02-24 14:44:07 +0530785 gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
786 gpio_dev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900787 if (IS_ERR(gpio_dev->pctrl)) {
Ken Xuedbad75d2015-03-10 15:02:19 +0800788 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900789 return PTR_ERR(gpio_dev->pctrl);
Ken Xuedbad75d2015-03-10 15:02:19 +0800790 }
791
Linus Walleij04d36722015-12-08 09:21:38 +0100792 ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
Ken Xuedbad75d2015-03-10 15:02:19 +0800793 if (ret)
Laxman Dewangan251e22a2016-02-24 14:44:07 +0530794 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800795
796 ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530797 0, 0, gpio_dev->gc.ngpio);
Ken Xuedbad75d2015-03-10 15:02:19 +0800798 if (ret) {
799 dev_err(&pdev->dev, "Failed to add pin range\n");
800 goto out2;
801 }
802
803 ret = gpiochip_irqchip_add(&gpio_dev->gc,
804 &amd_gpio_irqchip,
805 0,
806 handle_simple_irq,
807 IRQ_TYPE_NONE);
808 if (ret) {
809 dev_err(&pdev->dev, "could not add irqchip\n");
810 ret = -ENODEV;
811 goto out2;
812 }
813
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200814 ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler, 0,
815 KBUILD_MODNAME, gpio_dev);
816 if (ret)
817 goto out2;
818
Ken Xuedbad75d2015-03-10 15:02:19 +0800819 platform_set_drvdata(pdev, gpio_dev);
820
821 dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
822 return ret;
823
824out2:
825 gpiochip_remove(&gpio_dev->gc);
826
Ken Xuedbad75d2015-03-10 15:02:19 +0800827 return ret;
828}
829
830static int amd_gpio_remove(struct platform_device *pdev)
831{
832 struct amd_gpio *gpio_dev;
833
834 gpio_dev = platform_get_drvdata(pdev);
835
836 gpiochip_remove(&gpio_dev->gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800837
838 return 0;
839}
840
841static const struct acpi_device_id amd_gpio_acpi_match[] = {
842 { "AMD0030", 0 },
Wang Hongcheng42a44402016-03-11 10:58:42 +0800843 { "AMDI0030", 0},
Ken Xuedbad75d2015-03-10 15:02:19 +0800844 { },
845};
846MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
847
848static struct platform_driver amd_gpio_driver = {
849 .driver = {
850 .name = "amd_gpio",
Ken Xuedbad75d2015-03-10 15:02:19 +0800851 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
852 },
853 .probe = amd_gpio_probe,
854 .remove = amd_gpio_remove,
855};
856
857module_platform_driver(amd_gpio_driver);
858
859MODULE_LICENSE("GPL v2");
860MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
861MODULE_DESCRIPTION("AMD GPIO pinctrl driver");