blob: 38af1ec2df0c2b9c172915906f0a63864b65f1b5 [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.
Shyam Sundar S Kadd7bfc2017-05-03 11:59:11 +053011 *
12 * Contact Information: Nehal Shah <Nehal-bakulchandra.Shah@amd.com>
13 * Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
14 *
Ken Xuedbad75d2015-03-10 15:02:19 +080015 */
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 Xuedbad75d2015-03-10 15:02:19 +080036#include <linux/pinctrl/pinconf.h>
37#include <linux/pinctrl/pinconf-generic.h>
38
39#include "pinctrl-utils.h"
40#include "pinctrl-amd.h"
41
Ken Xuedbad75d2015-03-10 15:02:19 +080042static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
43{
44 unsigned long flags;
45 u32 pin_reg;
Linus Walleij04d36722015-12-08 09:21:38 +010046 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080047
Julia Cartwright229710f2017-03-09 10:22:04 -060048 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080049 pin_reg = readl(gpio_dev->base + offset * 4);
Ken Xuedbad75d2015-03-10 15:02:19 +080050 pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
51 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060052 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080053
54 return 0;
55}
56
57static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
58 int value)
59{
60 u32 pin_reg;
61 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010062 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080063
Julia Cartwright229710f2017-03-09 10:22:04 -060064 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080065 pin_reg = readl(gpio_dev->base + offset * 4);
66 pin_reg |= BIT(OUTPUT_ENABLE_OFF);
67 if (value)
68 pin_reg |= BIT(OUTPUT_VALUE_OFF);
69 else
70 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
71 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060072 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080073
74 return 0;
75}
76
77static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
78{
79 u32 pin_reg;
80 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010081 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080082
Julia Cartwright229710f2017-03-09 10:22:04 -060083 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080084 pin_reg = readl(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 !!(pin_reg & BIT(PIN_STS_OFF));
88}
89
90static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
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);
98 if (value)
99 pin_reg |= BIT(OUTPUT_VALUE_OFF);
100 else
101 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
102 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600103 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800104}
105
106static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
107 unsigned debounce)
108{
Ken Xuedbad75d2015-03-10 15:02:19 +0800109 u32 time;
Ken Xue25a853d2015-03-27 17:44:26 +0800110 u32 pin_reg;
111 int ret = 0;
Ken Xuedbad75d2015-03-10 15:02:19 +0800112 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +0100113 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800114
Julia Cartwright229710f2017-03-09 10:22:04 -0600115 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800116 pin_reg = readl(gpio_dev->base + offset * 4);
117
118 if (debounce) {
119 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
120 pin_reg &= ~DB_TMR_OUT_MASK;
121 /*
122 Debounce Debounce Timer Max
123 TmrLarge TmrOutUnit Unit Debounce
124 Time
125 0 0 61 usec (2 RtcClk) 976 usec
126 0 1 244 usec (8 RtcClk) 3.9 msec
127 1 0 15.6 msec (512 RtcClk) 250 msec
128 1 1 62.5 msec (2048 RtcClk) 1 sec
129 */
130
131 if (debounce < 61) {
132 pin_reg |= 1;
133 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
134 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
135 } else if (debounce < 976) {
136 time = debounce / 61;
137 pin_reg |= time & DB_TMR_OUT_MASK;
138 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
139 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
140 } else if (debounce < 3900) {
141 time = debounce / 244;
142 pin_reg |= time & DB_TMR_OUT_MASK;
143 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
144 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
145 } else if (debounce < 250000) {
146 time = debounce / 15600;
147 pin_reg |= time & DB_TMR_OUT_MASK;
148 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
149 pin_reg |= BIT(DB_TMR_LARGE_OFF);
150 } else if (debounce < 1000000) {
151 time = debounce / 62500;
152 pin_reg |= time & DB_TMR_OUT_MASK;
153 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
154 pin_reg |= BIT(DB_TMR_LARGE_OFF);
155 } else {
156 pin_reg &= ~DB_CNTRl_MASK;
Ken Xue25a853d2015-03-27 17:44:26 +0800157 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800158 }
159 } else {
160 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
161 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
162 pin_reg &= ~DB_TMR_OUT_MASK;
163 pin_reg &= ~DB_CNTRl_MASK;
164 }
165 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600166 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800167
Ken Xue25a853d2015-03-27 17:44:26 +0800168 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800169}
170
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300171static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
172 unsigned long config)
173{
174 u32 debounce;
175
176 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
177 return -ENOTSUPP;
178
179 debounce = pinconf_to_config_argument(config);
180 return amd_gpio_set_debounce(gc, offset, debounce);
181}
182
Ken Xuedbad75d2015-03-10 15:02:19 +0800183#ifdef CONFIG_DEBUG_FS
184static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
185{
186 u32 pin_reg;
187 unsigned long flags;
188 unsigned int bank, i, pin_num;
Linus Walleij04d36722015-12-08 09:21:38 +0100189 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800190
191 char *level_trig;
192 char *active_level;
193 char *interrupt_enable;
194 char *interrupt_mask;
195 char *wake_cntrl0;
196 char *wake_cntrl1;
197 char *wake_cntrl2;
198 char *pin_sts;
199 char *pull_up_sel;
200 char *pull_up_enable;
201 char *pull_down_enable;
202 char *output_value;
203 char *output_enable;
204
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530205 for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
Ken Xuedbad75d2015-03-10 15:02:19 +0800206 seq_printf(s, "GPIO bank%d\t", bank);
207
208 switch (bank) {
209 case 0:
210 i = 0;
211 pin_num = AMD_GPIO_PINS_BANK0;
212 break;
213 case 1:
214 i = 64;
215 pin_num = AMD_GPIO_PINS_BANK1 + i;
216 break;
217 case 2:
218 i = 128;
219 pin_num = AMD_GPIO_PINS_BANK2 + i;
220 break;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530221 case 3:
222 i = 192;
223 pin_num = AMD_GPIO_PINS_BANK3 + i;
224 break;
Linus Walleij6ac4c1a2017-01-03 09:18:58 +0100225 default:
226 /* Illegal bank number, ignore */
227 continue;
Ken Xuedbad75d2015-03-10 15:02:19 +0800228 }
Ken Xuedbad75d2015-03-10 15:02:19 +0800229 for (; i < pin_num; i++) {
230 seq_printf(s, "pin%d\t", i);
Julia Cartwright229710f2017-03-09 10:22:04 -0600231 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800232 pin_reg = readl(gpio_dev->base + i * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600233 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800234
235 if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
236 interrupt_enable = "interrupt is enabled|";
237
Dan Carpenter3775dac2017-01-07 09:32:15 +0300238 if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
239 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
Ken Xuedbad75d2015-03-10 15:02:19 +0800240 active_level = "Active low|";
Dan Carpenter3775dac2017-01-07 09:32:15 +0300241 else if (pin_reg & BIT(ACTIVE_LEVEL_OFF) &&
242 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
Ken Xuedbad75d2015-03-10 15:02:19 +0800243 active_level = "Active high|";
Dan Carpenter3775dac2017-01-07 09:32:15 +0300244 else if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
245 pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))
Ken Xuedbad75d2015-03-10 15:02:19 +0800246 active_level = "Active on both|";
247 else
Masanari Iida0a951602016-11-23 22:44:47 +0900248 active_level = "Unknown Active level|";
Ken Xuedbad75d2015-03-10 15:02:19 +0800249
250 if (pin_reg & BIT(LEVEL_TRIG_OFF))
251 level_trig = "Level trigger|";
252 else
253 level_trig = "Edge trigger|";
254
255 } else {
256 interrupt_enable =
257 "interrupt is disabled|";
258 active_level = " ";
259 level_trig = " ";
260 }
261
262 if (pin_reg & BIT(INTERRUPT_MASK_OFF))
263 interrupt_mask =
264 "interrupt is unmasked|";
265 else
266 interrupt_mask =
267 "interrupt is masked|";
268
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530269 if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800270 wake_cntrl0 = "enable wakeup in S0i3 state|";
271 else
272 wake_cntrl0 = "disable wakeup in S0i3 state|";
273
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530274 if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800275 wake_cntrl1 = "enable wakeup in S3 state|";
276 else
277 wake_cntrl1 = "disable wakeup in S3 state|";
278
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530279 if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
Ken Xuedbad75d2015-03-10 15:02:19 +0800280 wake_cntrl2 = "enable wakeup in S4/S5 state|";
281 else
282 wake_cntrl2 = "disable wakeup in S4/S5 state|";
283
284 if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
285 pull_up_enable = "pull-up is enabled|";
286 if (pin_reg & BIT(PULL_UP_SEL_OFF))
287 pull_up_sel = "8k pull-up|";
288 else
289 pull_up_sel = "4k pull-up|";
290 } else {
291 pull_up_enable = "pull-up is disabled|";
292 pull_up_sel = " ";
293 }
294
295 if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
296 pull_down_enable = "pull-down is enabled|";
297 else
298 pull_down_enable = "Pull-down is disabled|";
299
300 if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
301 pin_sts = " ";
302 output_enable = "output is enabled|";
303 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
304 output_value = "output is high|";
305 else
306 output_value = "output is low|";
307 } else {
308 output_enable = "output is disabled|";
309 output_value = " ";
310
311 if (pin_reg & BIT(PIN_STS_OFF))
312 pin_sts = "input is high|";
313 else
314 pin_sts = "input is low|";
315 }
316
317 seq_printf(s, "%s %s %s %s %s %s\n"
318 " %s %s %s %s %s %s %s 0x%x\n",
319 level_trig, active_level, interrupt_enable,
320 interrupt_mask, wake_cntrl0, wake_cntrl1,
321 wake_cntrl2, pin_sts, pull_up_sel,
322 pull_up_enable, pull_down_enable,
323 output_value, output_enable, pin_reg);
324 }
325 }
326}
327#else
328#define amd_gpio_dbg_show NULL
329#endif
330
331static void amd_gpio_irq_enable(struct irq_data *d)
332{
333 u32 pin_reg;
334 unsigned long flags;
335 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100336 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800337
Julia Cartwright229710f2017-03-09 10:22:04 -0600338 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800339 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
Ken Xuedbad75d2015-03-10 15:02:19 +0800340 pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
341 pin_reg |= BIT(INTERRUPT_MASK_OFF);
342 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600343 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800344}
345
346static void amd_gpio_irq_disable(struct irq_data *d)
347{
348 u32 pin_reg;
349 unsigned long flags;
350 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100351 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800352
Julia Cartwright229710f2017-03-09 10:22:04 -0600353 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800354 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
355 pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
356 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
357 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600358 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800359}
360
361static void amd_gpio_irq_mask(struct irq_data *d)
362{
363 u32 pin_reg;
364 unsigned long flags;
365 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100366 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800367
Julia Cartwright229710f2017-03-09 10:22:04 -0600368 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800369 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
370 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
371 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600372 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800373}
374
375static void amd_gpio_irq_unmask(struct irq_data *d)
376{
377 u32 pin_reg;
378 unsigned long flags;
379 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100380 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800381
Julia Cartwright229710f2017-03-09 10:22:04 -0600382 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800383 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 Cartwright229710f2017-03-09 10:22:04 -0600386 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800387}
388
389static void amd_gpio_irq_eoi(struct irq_data *d)
390{
391 u32 reg;
392 unsigned long flags;
393 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100394 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800395
Julia Cartwright229710f2017-03-09 10:22:04 -0600396 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800397 reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
398 reg |= EOI_MASK;
399 writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600400 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800401}
402
403static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
404{
405 int ret = 0;
406 u32 pin_reg;
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530407 unsigned long flags, irq_flags;
Ken Xuedbad75d2015-03-10 15:02:19 +0800408 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100409 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800410
Julia Cartwright229710f2017-03-09 10:22:04 -0600411 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800412 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
413
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530414 /* Ignore the settings coming from the client and
415 * read the values from the ACPI tables
416 * while setting the trigger type
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000417 */
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000418
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530419 irq_flags = irq_get_trigger_type(d->irq);
420 if (irq_flags != IRQ_TYPE_NONE)
421 type = irq_flags;
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000422
Ken Xuedbad75d2015-03-10 15:02:19 +0800423 switch (type & IRQ_TYPE_SENSE_MASK) {
424 case IRQ_TYPE_EDGE_RISING:
425 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
426 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
427 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
428 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200429 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800430 break;
431
432 case IRQ_TYPE_EDGE_FALLING:
433 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
434 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
435 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
436 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200437 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800438 break;
439
440 case IRQ_TYPE_EDGE_BOTH:
441 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
442 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
443 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
444 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200445 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800446 break;
447
448 case IRQ_TYPE_LEVEL_HIGH:
449 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
450 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
451 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
452 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
453 pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200454 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800455 break;
456
457 case IRQ_TYPE_LEVEL_LOW:
458 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
459 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
460 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
461 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
462 pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200463 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800464 break;
465
466 case IRQ_TYPE_NONE:
467 break;
468
469 default:
470 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
471 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800472 }
473
474 pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
475 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600476 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800477
Ken Xuedbad75d2015-03-10 15:02:19 +0800478 return ret;
479}
480
481static void amd_irq_ack(struct irq_data *d)
482{
483 /*
484 * based on HW design,there is no need to ack HW
485 * before handle current irq. But this routine is
486 * necessary for handle_edge_irq
487 */
488}
489
490static struct irq_chip amd_gpio_irqchip = {
491 .name = "amd_gpio",
492 .irq_ack = amd_irq_ack,
493 .irq_enable = amd_gpio_irq_enable,
494 .irq_disable = amd_gpio_irq_disable,
495 .irq_mask = amd_gpio_irq_mask,
496 .irq_unmask = amd_gpio_irq_unmask,
497 .irq_eoi = amd_gpio_irq_eoi,
498 .irq_set_type = amd_gpio_irq_set_type,
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530499 .flags = IRQCHIP_SKIP_SET_WAKE,
Ken Xuedbad75d2015-03-10 15:02:19 +0800500};
501
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200502#define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
503
504static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
Ken Xuedbad75d2015-03-10 15:02:19 +0800505{
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200506 struct amd_gpio *gpio_dev = dev_id;
507 struct gpio_chip *gc = &gpio_dev->gc;
508 irqreturn_t ret = IRQ_NONE;
509 unsigned int i, irqnr;
Ken Xuedbad75d2015-03-10 15:02:19 +0800510 unsigned long flags;
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200511 u32 *regs, regval;
512 u64 status, mask;
Ken Xuedbad75d2015-03-10 15:02:19 +0800513
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200514 /* Read the wake status */
Julia Cartwright229710f2017-03-09 10:22:04 -0600515 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200516 status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
517 status <<= 32;
518 status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
Julia Cartwright229710f2017-03-09 10:22:04 -0600519 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800520
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200521 /* Bit 0-45 contain the relevant status bits */
522 status &= (1ULL << 46) - 1;
523 regs = gpio_dev->base;
524 for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
525 if (!(status & mask))
526 continue;
527 status &= ~mask;
528
529 /* Each status bit covers four pins */
530 for (i = 0; i < 4; i++) {
531 regval = readl(regs + i);
532 if (!(regval & PIN_IRQ_PENDING))
533 continue;
534 irq = irq_find_mapping(gc->irqdomain, irqnr + i);
535 generic_handle_irq(irq);
536 /* Clear interrupt */
537 writel(regval, regs + i);
538 ret = IRQ_HANDLED;
Ken Xuedbad75d2015-03-10 15:02:19 +0800539 }
540 }
541
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200542 /* Signal EOI to the GPIO unit */
Julia Cartwright229710f2017-03-09 10:22:04 -0600543 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200544 regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
545 regval |= EOI_MASK;
546 writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600547 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800548
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200549 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800550}
551
552static int amd_get_groups_count(struct pinctrl_dev *pctldev)
553{
554 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
555
556 return gpio_dev->ngroups;
557}
558
559static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
560 unsigned group)
561{
562 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
563
564 return gpio_dev->groups[group].name;
565}
566
567static int amd_get_group_pins(struct pinctrl_dev *pctldev,
568 unsigned group,
569 const unsigned **pins,
570 unsigned *num_pins)
571{
572 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
573
574 *pins = gpio_dev->groups[group].pins;
575 *num_pins = gpio_dev->groups[group].npins;
576 return 0;
577}
578
579static const struct pinctrl_ops amd_pinctrl_ops = {
580 .get_groups_count = amd_get_groups_count,
581 .get_group_name = amd_get_group_name,
582 .get_group_pins = amd_get_group_pins,
583#ifdef CONFIG_OF
584 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300585 .dt_free_map = pinctrl_utils_free_map,
Ken Xuedbad75d2015-03-10 15:02:19 +0800586#endif
587};
588
589static int amd_pinconf_get(struct pinctrl_dev *pctldev,
590 unsigned int pin,
591 unsigned long *config)
592{
593 u32 pin_reg;
594 unsigned arg;
595 unsigned long flags;
596 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
597 enum pin_config_param param = pinconf_to_config_param(*config);
598
Julia Cartwright229710f2017-03-09 10:22:04 -0600599 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800600 pin_reg = readl(gpio_dev->base + pin*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600601 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800602 switch (param) {
603 case PIN_CONFIG_INPUT_DEBOUNCE:
604 arg = pin_reg & DB_TMR_OUT_MASK;
605 break;
606
607 case PIN_CONFIG_BIAS_PULL_DOWN:
608 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
609 break;
610
611 case PIN_CONFIG_BIAS_PULL_UP:
612 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
613 break;
614
615 case PIN_CONFIG_DRIVE_STRENGTH:
616 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
617 break;
618
619 default:
620 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
621 param);
622 return -ENOTSUPP;
623 }
624
625 *config = pinconf_to_config_packed(param, arg);
626
627 return 0;
628}
629
630static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
631 unsigned long *configs, unsigned num_configs)
632{
633 int i;
Ken Xuedbad75d2015-03-10 15:02:19 +0800634 u32 arg;
Ken Xue25a853d2015-03-27 17:44:26 +0800635 int ret = 0;
636 u32 pin_reg;
Ken Xuedbad75d2015-03-10 15:02:19 +0800637 unsigned long flags;
638 enum pin_config_param param;
639 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
640
Julia Cartwright229710f2017-03-09 10:22:04 -0600641 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800642 for (i = 0; i < num_configs; i++) {
643 param = pinconf_to_config_param(configs[i]);
644 arg = pinconf_to_config_argument(configs[i]);
645 pin_reg = readl(gpio_dev->base + pin*4);
646
647 switch (param) {
648 case PIN_CONFIG_INPUT_DEBOUNCE:
649 pin_reg &= ~DB_TMR_OUT_MASK;
650 pin_reg |= arg & DB_TMR_OUT_MASK;
651 break;
652
653 case PIN_CONFIG_BIAS_PULL_DOWN:
654 pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
655 pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
656 break;
657
658 case PIN_CONFIG_BIAS_PULL_UP:
659 pin_reg &= ~BIT(PULL_UP_SEL_OFF);
660 pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
661 pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
662 pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
663 break;
664
665 case PIN_CONFIG_DRIVE_STRENGTH:
666 pin_reg &= ~(DRV_STRENGTH_SEL_MASK
667 << DRV_STRENGTH_SEL_OFF);
668 pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
669 << DRV_STRENGTH_SEL_OFF;
670 break;
671
672 default:
673 dev_err(&gpio_dev->pdev->dev,
674 "Invalid config param %04x\n", param);
Ken Xue25a853d2015-03-27 17:44:26 +0800675 ret = -ENOTSUPP;
Ken Xuedbad75d2015-03-10 15:02:19 +0800676 }
677
678 writel(pin_reg, gpio_dev->base + pin*4);
679 }
Julia Cartwright229710f2017-03-09 10:22:04 -0600680 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800681
Ken Xue25a853d2015-03-27 17:44:26 +0800682 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800683}
684
685static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
686 unsigned int group,
687 unsigned long *config)
688{
689 const unsigned *pins;
690 unsigned npins;
691 int ret;
692
693 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
694 if (ret)
695 return ret;
696
697 if (amd_pinconf_get(pctldev, pins[0], config))
698 return -ENOTSUPP;
699
700 return 0;
701}
702
703static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
704 unsigned group, unsigned long *configs,
705 unsigned num_configs)
706{
707 const unsigned *pins;
708 unsigned npins;
709 int i, ret;
710
711 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
712 if (ret)
713 return ret;
714 for (i = 0; i < npins; i++) {
715 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
716 return -ENOTSUPP;
717 }
718 return 0;
719}
720
721static const struct pinconf_ops amd_pinconf_ops = {
722 .pin_config_get = amd_pinconf_get,
723 .pin_config_set = amd_pinconf_set,
724 .pin_config_group_get = amd_pinconf_group_get,
725 .pin_config_group_set = amd_pinconf_group_set,
726};
727
728static struct pinctrl_desc amd_pinctrl_desc = {
729 .pins = kerncz_pins,
730 .npins = ARRAY_SIZE(kerncz_pins),
731 .pctlops = &amd_pinctrl_ops,
732 .confops = &amd_pinconf_ops,
733 .owner = THIS_MODULE,
734};
735
736static int amd_gpio_probe(struct platform_device *pdev)
737{
738 int ret = 0;
Ken Xue25a853d2015-03-27 17:44:26 +0800739 int irq_base;
Ken Xuedbad75d2015-03-10 15:02:19 +0800740 struct resource *res;
741 struct amd_gpio *gpio_dev;
742
743 gpio_dev = devm_kzalloc(&pdev->dev,
744 sizeof(struct amd_gpio), GFP_KERNEL);
745 if (!gpio_dev)
746 return -ENOMEM;
747
Julia Cartwright229710f2017-03-09 10:22:04 -0600748 raw_spin_lock_init(&gpio_dev->lock);
Ken Xuedbad75d2015-03-10 15:02:19 +0800749
750 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
751 if (!res) {
752 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
753 return -EINVAL;
754 }
755
756 gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
757 resource_size(res));
Wei Yongjun424a6c62016-02-06 22:56:36 +0800758 if (!gpio_dev->base)
759 return -ENOMEM;
Ken Xuedbad75d2015-03-10 15:02:19 +0800760
761 irq_base = platform_get_irq(pdev, 0);
762 if (irq_base < 0) {
Gustavo A. R. Silva2e6424ab2017-08-09 11:09:33 -0500763 dev_err(&pdev->dev, "Failed to get gpio IRQ: %d\n", irq_base);
764 return irq_base;
Ken Xuedbad75d2015-03-10 15:02:19 +0800765 }
766
767 gpio_dev->pdev = pdev;
768 gpio_dev->gc.direction_input = amd_gpio_direction_input;
769 gpio_dev->gc.direction_output = amd_gpio_direction_output;
770 gpio_dev->gc.get = amd_gpio_get_value;
771 gpio_dev->gc.set = amd_gpio_set_value;
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300772 gpio_dev->gc.set_config = amd_gpio_set_config;
Ken Xuedbad75d2015-03-10 15:02:19 +0800773 gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
774
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530775 gpio_dev->gc.base = -1;
Ken Xuedbad75d2015-03-10 15:02:19 +0800776 gpio_dev->gc.label = pdev->name;
777 gpio_dev->gc.owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +0100778 gpio_dev->gc.parent = &pdev->dev;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530779 gpio_dev->gc.ngpio = resource_size(res) / 4;
Ken Xuedbad75d2015-03-10 15:02:19 +0800780#if defined(CONFIG_OF_GPIO)
781 gpio_dev->gc.of_node = pdev->dev.of_node;
782#endif
783
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530784 gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
Ken Xuedbad75d2015-03-10 15:02:19 +0800785 gpio_dev->groups = kerncz_groups;
786 gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
787
788 amd_pinctrl_desc.name = dev_name(&pdev->dev);
Laxman Dewangan251e22a2016-02-24 14:44:07 +0530789 gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
790 gpio_dev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900791 if (IS_ERR(gpio_dev->pctrl)) {
Ken Xuedbad75d2015-03-10 15:02:19 +0800792 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900793 return PTR_ERR(gpio_dev->pctrl);
Ken Xuedbad75d2015-03-10 15:02:19 +0800794 }
795
Linus Walleij04d36722015-12-08 09:21:38 +0100796 ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
Ken Xuedbad75d2015-03-10 15:02:19 +0800797 if (ret)
Laxman Dewangan251e22a2016-02-24 14:44:07 +0530798 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800799
800 ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530801 0, 0, gpio_dev->gc.ngpio);
Ken Xuedbad75d2015-03-10 15:02:19 +0800802 if (ret) {
803 dev_err(&pdev->dev, "Failed to add pin range\n");
804 goto out2;
805 }
806
807 ret = gpiochip_irqchip_add(&gpio_dev->gc,
808 &amd_gpio_irqchip,
809 0,
810 handle_simple_irq,
811 IRQ_TYPE_NONE);
812 if (ret) {
813 dev_err(&pdev->dev, "could not add irqchip\n");
814 ret = -ENODEV;
815 goto out2;
816 }
817
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200818 ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler, 0,
819 KBUILD_MODNAME, gpio_dev);
820 if (ret)
821 goto out2;
822
Ken Xuedbad75d2015-03-10 15:02:19 +0800823 platform_set_drvdata(pdev, gpio_dev);
824
825 dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
826 return ret;
827
828out2:
829 gpiochip_remove(&gpio_dev->gc);
830
Ken Xuedbad75d2015-03-10 15:02:19 +0800831 return ret;
832}
833
834static int amd_gpio_remove(struct platform_device *pdev)
835{
836 struct amd_gpio *gpio_dev;
837
838 gpio_dev = platform_get_drvdata(pdev);
839
840 gpiochip_remove(&gpio_dev->gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800841
842 return 0;
843}
844
845static const struct acpi_device_id amd_gpio_acpi_match[] = {
846 { "AMD0030", 0 },
Wang Hongcheng42a44402016-03-11 10:58:42 +0800847 { "AMDI0030", 0},
Ken Xuedbad75d2015-03-10 15:02:19 +0800848 { },
849};
850MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
851
852static struct platform_driver amd_gpio_driver = {
853 .driver = {
854 .name = "amd_gpio",
Ken Xuedbad75d2015-03-10 15:02:19 +0800855 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
856 },
857 .probe = amd_gpio_probe,
858 .remove = amd_gpio_remove,
859};
860
861module_platform_driver(amd_gpio_driver);
862
863MODULE_LICENSE("GPL v2");
864MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
865MODULE_DESCRIPTION("AMD GPIO pinctrl driver");