blob: 281b700fe7e9c92115a48b86857751f8e5b4ab0f [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
Daniel Drake79d2c8b2017-09-11 14:11:56 +080039#include "core.h"
Ken Xuedbad75d2015-03-10 15:02:19 +080040#include "pinctrl-utils.h"
41#include "pinctrl-amd.h"
42
Daniel Kurtz12b10f42018-02-16 12:12:43 -070043static int amd_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
44{
45 unsigned long flags;
46 u32 pin_reg;
47 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
48
49 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
50 pin_reg = readl(gpio_dev->base + offset * 4);
51 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
52
53 return !(pin_reg & BIT(OUTPUT_ENABLE_OFF));
54}
55
Ken Xuedbad75d2015-03-10 15:02:19 +080056static int amd_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
57{
58 unsigned long flags;
59 u32 pin_reg;
Linus Walleij04d36722015-12-08 09:21:38 +010060 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080061
Julia Cartwright229710f2017-03-09 10:22:04 -060062 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080063 pin_reg = readl(gpio_dev->base + offset * 4);
Ken Xuedbad75d2015-03-10 15:02:19 +080064 pin_reg &= ~BIT(OUTPUT_ENABLE_OFF);
65 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060066 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080067
68 return 0;
69}
70
71static int amd_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
72 int value)
73{
74 u32 pin_reg;
75 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010076 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080077
Julia Cartwright229710f2017-03-09 10:22:04 -060078 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080079 pin_reg = readl(gpio_dev->base + offset * 4);
80 pin_reg |= BIT(OUTPUT_ENABLE_OFF);
81 if (value)
82 pin_reg |= BIT(OUTPUT_VALUE_OFF);
83 else
84 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
85 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -060086 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080087
88 return 0;
89}
90
91static int amd_gpio_get_value(struct gpio_chip *gc, unsigned offset)
92{
93 u32 pin_reg;
94 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +010095 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +080096
Julia Cartwright229710f2017-03-09 10:22:04 -060097 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +080098 pin_reg = readl(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 return !!(pin_reg & BIT(PIN_STS_OFF));
102}
103
104static void amd_gpio_set_value(struct gpio_chip *gc, unsigned offset, int value)
105{
106 u32 pin_reg;
107 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +0100108 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800109
Julia Cartwright229710f2017-03-09 10:22:04 -0600110 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800111 pin_reg = readl(gpio_dev->base + offset * 4);
112 if (value)
113 pin_reg |= BIT(OUTPUT_VALUE_OFF);
114 else
115 pin_reg &= ~BIT(OUTPUT_VALUE_OFF);
116 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600117 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800118}
119
120static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
121 unsigned debounce)
122{
Ken Xuedbad75d2015-03-10 15:02:19 +0800123 u32 time;
Ken Xue25a853d2015-03-27 17:44:26 +0800124 u32 pin_reg;
125 int ret = 0;
Ken Xuedbad75d2015-03-10 15:02:19 +0800126 unsigned long flags;
Linus Walleij04d36722015-12-08 09:21:38 +0100127 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800128
Julia Cartwright229710f2017-03-09 10:22:04 -0600129 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800130 pin_reg = readl(gpio_dev->base + offset * 4);
131
132 if (debounce) {
133 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
134 pin_reg &= ~DB_TMR_OUT_MASK;
135 /*
136 Debounce Debounce Timer Max
137 TmrLarge TmrOutUnit Unit Debounce
138 Time
139 0 0 61 usec (2 RtcClk) 976 usec
140 0 1 244 usec (8 RtcClk) 3.9 msec
141 1 0 15.6 msec (512 RtcClk) 250 msec
142 1 1 62.5 msec (2048 RtcClk) 1 sec
143 */
144
145 if (debounce < 61) {
146 pin_reg |= 1;
147 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
148 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
149 } else if (debounce < 976) {
150 time = debounce / 61;
151 pin_reg |= time & DB_TMR_OUT_MASK;
152 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
153 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
154 } else if (debounce < 3900) {
155 time = debounce / 244;
156 pin_reg |= time & DB_TMR_OUT_MASK;
157 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
158 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
159 } else if (debounce < 250000) {
160 time = debounce / 15600;
161 pin_reg |= time & DB_TMR_OUT_MASK;
162 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
163 pin_reg |= BIT(DB_TMR_LARGE_OFF);
164 } else if (debounce < 1000000) {
165 time = debounce / 62500;
166 pin_reg |= time & DB_TMR_OUT_MASK;
167 pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
168 pin_reg |= BIT(DB_TMR_LARGE_OFF);
169 } else {
170 pin_reg &= ~DB_CNTRl_MASK;
Ken Xue25a853d2015-03-27 17:44:26 +0800171 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800172 }
173 } else {
174 pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
175 pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
176 pin_reg &= ~DB_TMR_OUT_MASK;
177 pin_reg &= ~DB_CNTRl_MASK;
178 }
179 writel(pin_reg, gpio_dev->base + offset * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600180 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800181
Ken Xue25a853d2015-03-27 17:44:26 +0800182 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800183}
184
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300185static int amd_gpio_set_config(struct gpio_chip *gc, unsigned offset,
186 unsigned long config)
187{
188 u32 debounce;
189
190 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
191 return -ENOTSUPP;
192
193 debounce = pinconf_to_config_argument(config);
194 return amd_gpio_set_debounce(gc, offset, debounce);
195}
196
Ken Xuedbad75d2015-03-10 15:02:19 +0800197#ifdef CONFIG_DEBUG_FS
198static void amd_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gc)
199{
200 u32 pin_reg;
201 unsigned long flags;
202 unsigned int bank, i, pin_num;
Linus Walleij04d36722015-12-08 09:21:38 +0100203 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800204
205 char *level_trig;
206 char *active_level;
207 char *interrupt_enable;
208 char *interrupt_mask;
209 char *wake_cntrl0;
210 char *wake_cntrl1;
211 char *wake_cntrl2;
212 char *pin_sts;
213 char *pull_up_sel;
214 char *pull_up_enable;
215 char *pull_down_enable;
216 char *output_value;
217 char *output_enable;
218
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530219 for (bank = 0; bank < gpio_dev->hwbank_num; bank++) {
Ken Xuedbad75d2015-03-10 15:02:19 +0800220 seq_printf(s, "GPIO bank%d\t", bank);
221
222 switch (bank) {
223 case 0:
224 i = 0;
225 pin_num = AMD_GPIO_PINS_BANK0;
226 break;
227 case 1:
228 i = 64;
229 pin_num = AMD_GPIO_PINS_BANK1 + i;
230 break;
231 case 2:
232 i = 128;
233 pin_num = AMD_GPIO_PINS_BANK2 + i;
234 break;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530235 case 3:
236 i = 192;
237 pin_num = AMD_GPIO_PINS_BANK3 + i;
238 break;
Linus Walleij6ac4c1a2017-01-03 09:18:58 +0100239 default:
240 /* Illegal bank number, ignore */
241 continue;
Ken Xuedbad75d2015-03-10 15:02:19 +0800242 }
Ken Xuedbad75d2015-03-10 15:02:19 +0800243 for (; i < pin_num; i++) {
244 seq_printf(s, "pin%d\t", i);
Julia Cartwright229710f2017-03-09 10:22:04 -0600245 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800246 pin_reg = readl(gpio_dev->base + i * 4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600247 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800248
249 if (pin_reg & BIT(INTERRUPT_ENABLE_OFF)) {
250 interrupt_enable = "interrupt is enabled|";
251
Dan Carpenter3775dac2017-01-07 09:32:15 +0300252 if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
253 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
Ken Xuedbad75d2015-03-10 15:02:19 +0800254 active_level = "Active low|";
Dan Carpenter3775dac2017-01-07 09:32:15 +0300255 else if (pin_reg & BIT(ACTIVE_LEVEL_OFF) &&
256 !(pin_reg & BIT(ACTIVE_LEVEL_OFF + 1)))
Ken Xuedbad75d2015-03-10 15:02:19 +0800257 active_level = "Active high|";
Dan Carpenter3775dac2017-01-07 09:32:15 +0300258 else if (!(pin_reg & BIT(ACTIVE_LEVEL_OFF)) &&
259 pin_reg & BIT(ACTIVE_LEVEL_OFF + 1))
Ken Xuedbad75d2015-03-10 15:02:19 +0800260 active_level = "Active on both|";
261 else
Masanari Iida0a951602016-11-23 22:44:47 +0900262 active_level = "Unknown Active level|";
Ken Xuedbad75d2015-03-10 15:02:19 +0800263
264 if (pin_reg & BIT(LEVEL_TRIG_OFF))
265 level_trig = "Level trigger|";
266 else
267 level_trig = "Edge trigger|";
268
269 } else {
270 interrupt_enable =
271 "interrupt is disabled|";
272 active_level = " ";
273 level_trig = " ";
274 }
275
276 if (pin_reg & BIT(INTERRUPT_MASK_OFF))
277 interrupt_mask =
278 "interrupt is unmasked|";
279 else
280 interrupt_mask =
281 "interrupt is masked|";
282
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530283 if (pin_reg & BIT(WAKE_CNTRL_OFF_S0I3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800284 wake_cntrl0 = "enable wakeup in S0i3 state|";
285 else
286 wake_cntrl0 = "disable wakeup in S0i3 state|";
287
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530288 if (pin_reg & BIT(WAKE_CNTRL_OFF_S3))
Ken Xuedbad75d2015-03-10 15:02:19 +0800289 wake_cntrl1 = "enable wakeup in S3 state|";
290 else
291 wake_cntrl1 = "disable wakeup in S3 state|";
292
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530293 if (pin_reg & BIT(WAKE_CNTRL_OFF_S4))
Ken Xuedbad75d2015-03-10 15:02:19 +0800294 wake_cntrl2 = "enable wakeup in S4/S5 state|";
295 else
296 wake_cntrl2 = "disable wakeup in S4/S5 state|";
297
298 if (pin_reg & BIT(PULL_UP_ENABLE_OFF)) {
299 pull_up_enable = "pull-up is enabled|";
300 if (pin_reg & BIT(PULL_UP_SEL_OFF))
301 pull_up_sel = "8k pull-up|";
302 else
303 pull_up_sel = "4k pull-up|";
304 } else {
305 pull_up_enable = "pull-up is disabled|";
306 pull_up_sel = " ";
307 }
308
309 if (pin_reg & BIT(PULL_DOWN_ENABLE_OFF))
310 pull_down_enable = "pull-down is enabled|";
311 else
312 pull_down_enable = "Pull-down is disabled|";
313
314 if (pin_reg & BIT(OUTPUT_ENABLE_OFF)) {
315 pin_sts = " ";
316 output_enable = "output is enabled|";
317 if (pin_reg & BIT(OUTPUT_VALUE_OFF))
318 output_value = "output is high|";
319 else
320 output_value = "output is low|";
321 } else {
322 output_enable = "output is disabled|";
323 output_value = " ";
324
325 if (pin_reg & BIT(PIN_STS_OFF))
326 pin_sts = "input is high|";
327 else
328 pin_sts = "input is low|";
329 }
330
331 seq_printf(s, "%s %s %s %s %s %s\n"
332 " %s %s %s %s %s %s %s 0x%x\n",
333 level_trig, active_level, interrupt_enable,
334 interrupt_mask, wake_cntrl0, wake_cntrl1,
335 wake_cntrl2, pin_sts, pull_up_sel,
336 pull_up_enable, pull_down_enable,
337 output_value, output_enable, pin_reg);
338 }
339 }
340}
341#else
342#define amd_gpio_dbg_show NULL
343#endif
344
345static void amd_gpio_irq_enable(struct irq_data *d)
346{
347 u32 pin_reg;
348 unsigned long flags;
349 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100350 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800351
Julia Cartwright229710f2017-03-09 10:22:04 -0600352 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800353 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
Ken Xuedbad75d2015-03-10 15:02:19 +0800354 pin_reg |= BIT(INTERRUPT_ENABLE_OFF);
355 pin_reg |= BIT(INTERRUPT_MASK_OFF);
356 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600357 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800358}
359
360static void amd_gpio_irq_disable(struct irq_data *d)
361{
362 u32 pin_reg;
363 unsigned long flags;
364 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100365 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800366
Julia Cartwright229710f2017-03-09 10:22:04 -0600367 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800368 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
369 pin_reg &= ~BIT(INTERRUPT_ENABLE_OFF);
370 pin_reg &= ~BIT(INTERRUPT_MASK_OFF);
371 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia 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_mask(struct irq_data *d)
376{
377 u32 pin_reg;
378 unsigned long flags;
379 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus 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_unmask(struct irq_data *d)
390{
391 u32 pin_reg;
392 unsigned long flags;
393 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus 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 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
398 pin_reg |= BIT(INTERRUPT_MASK_OFF);
399 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia 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 void amd_gpio_irq_eoi(struct irq_data *d)
404{
405 u32 reg;
406 unsigned long flags;
407 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100408 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800409
Julia Cartwright229710f2017-03-09 10:22:04 -0600410 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800411 reg = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
412 reg |= EOI_MASK;
413 writel(reg, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600414 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800415}
416
417static int amd_gpio_irq_set_type(struct irq_data *d, unsigned int type)
418{
419 int ret = 0;
420 u32 pin_reg;
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530421 unsigned long flags, irq_flags;
Ken Xuedbad75d2015-03-10 15:02:19 +0800422 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij04d36722015-12-08 09:21:38 +0100423 struct amd_gpio *gpio_dev = gpiochip_get_data(gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800424
Julia Cartwright229710f2017-03-09 10:22:04 -0600425 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800426 pin_reg = readl(gpio_dev->base + (d->hwirq)*4);
427
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530428 /* Ignore the settings coming from the client and
429 * read the values from the ACPI tables
430 * while setting the trigger type
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000431 */
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000432
Shyam Sundar S K2983f292016-12-08 17:31:14 +0530433 irq_flags = irq_get_trigger_type(d->irq);
434 if (irq_flags != IRQ_TYPE_NONE)
435 type = irq_flags;
Agrawal, Nitesh-kumar499c7192016-08-31 08:50:49 +0000436
Ken Xuedbad75d2015-03-10 15:02:19 +0800437 switch (type & IRQ_TYPE_SENSE_MASK) {
438 case IRQ_TYPE_EDGE_RISING:
439 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
440 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
441 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
442 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200443 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800444 break;
445
446 case IRQ_TYPE_EDGE_FALLING:
447 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
448 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
449 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
450 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200451 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800452 break;
453
454 case IRQ_TYPE_EDGE_BOTH:
455 pin_reg &= ~BIT(LEVEL_TRIG_OFF);
456 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
457 pin_reg |= BOTH_EADGE << ACTIVE_LEVEL_OFF;
458 pin_reg |= DB_TYPE_REMOVE_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200459 irq_set_handler_locked(d, handle_edge_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800460 break;
461
462 case IRQ_TYPE_LEVEL_HIGH:
463 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
464 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
465 pin_reg |= ACTIVE_HIGH << ACTIVE_LEVEL_OFF;
466 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
467 pin_reg |= DB_TYPE_PRESERVE_LOW_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200468 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800469 break;
470
471 case IRQ_TYPE_LEVEL_LOW:
472 pin_reg |= LEVEL_TRIGGER << LEVEL_TRIG_OFF;
473 pin_reg &= ~(ACTIVE_LEVEL_MASK << ACTIVE_LEVEL_OFF);
474 pin_reg |= ACTIVE_LOW << ACTIVE_LEVEL_OFF;
475 pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
476 pin_reg |= DB_TYPE_PRESERVE_HIGH_GLITCH << DB_CNTRL_OFF;
Thomas Gleixner9d829312015-06-23 15:52:47 +0200477 irq_set_handler_locked(d, handle_level_irq);
Ken Xuedbad75d2015-03-10 15:02:19 +0800478 break;
479
480 case IRQ_TYPE_NONE:
481 break;
482
483 default:
484 dev_err(&gpio_dev->pdev->dev, "Invalid type value\n");
485 ret = -EINVAL;
Ken Xuedbad75d2015-03-10 15:02:19 +0800486 }
487
488 pin_reg |= CLR_INTR_STAT << INTERRUPT_STS_OFF;
489 writel(pin_reg, gpio_dev->base + (d->hwirq)*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600490 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800491
Ken Xuedbad75d2015-03-10 15:02:19 +0800492 return ret;
493}
494
495static void amd_irq_ack(struct irq_data *d)
496{
497 /*
498 * based on HW design,there is no need to ack HW
499 * before handle current irq. But this routine is
500 * necessary for handle_edge_irq
501 */
502}
503
504static struct irq_chip amd_gpio_irqchip = {
505 .name = "amd_gpio",
506 .irq_ack = amd_irq_ack,
507 .irq_enable = amd_gpio_irq_enable,
508 .irq_disable = amd_gpio_irq_disable,
509 .irq_mask = amd_gpio_irq_mask,
510 .irq_unmask = amd_gpio_irq_unmask,
511 .irq_eoi = amd_gpio_irq_eoi,
512 .irq_set_type = amd_gpio_irq_set_type,
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530513 .flags = IRQCHIP_SKIP_SET_WAKE,
Ken Xuedbad75d2015-03-10 15:02:19 +0800514};
515
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200516#define PIN_IRQ_PENDING (BIT(INTERRUPT_STS_OFF) | BIT(WAKE_STS_OFF))
517
518static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
Ken Xuedbad75d2015-03-10 15:02:19 +0800519{
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200520 struct amd_gpio *gpio_dev = dev_id;
521 struct gpio_chip *gc = &gpio_dev->gc;
522 irqreturn_t ret = IRQ_NONE;
523 unsigned int i, irqnr;
Ken Xuedbad75d2015-03-10 15:02:19 +0800524 unsigned long flags;
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200525 u32 *regs, regval;
526 u64 status, mask;
Ken Xuedbad75d2015-03-10 15:02:19 +0800527
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200528 /* Read the wake status */
Julia Cartwright229710f2017-03-09 10:22:04 -0600529 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200530 status = readl(gpio_dev->base + WAKE_INT_STATUS_REG1);
531 status <<= 32;
532 status |= readl(gpio_dev->base + WAKE_INT_STATUS_REG0);
Julia Cartwright229710f2017-03-09 10:22:04 -0600533 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800534
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200535 /* Bit 0-45 contain the relevant status bits */
536 status &= (1ULL << 46) - 1;
537 regs = gpio_dev->base;
538 for (mask = 1, irqnr = 0; status; mask <<= 1, regs += 4, irqnr += 4) {
539 if (!(status & mask))
540 continue;
541 status &= ~mask;
542
543 /* Each status bit covers four pins */
544 for (i = 0; i < 4; i++) {
545 regval = readl(regs + i);
546 if (!(regval & PIN_IRQ_PENDING))
547 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100548 irq = irq_find_mapping(gc->irq.domain, irqnr + i);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200549 generic_handle_irq(irq);
Daniel Drake6afb1022017-10-02 12:00:54 +0800550
551 /* Clear interrupt.
552 * We must read the pin register again, in case the
553 * value was changed while executing
554 * generic_handle_irq() above.
555 */
556 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
557 regval = readl(regs + i);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200558 writel(regval, regs + i);
Daniel Drake6afb1022017-10-02 12:00:54 +0800559 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200560 ret = IRQ_HANDLED;
Ken Xuedbad75d2015-03-10 15:02:19 +0800561 }
562 }
563
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200564 /* Signal EOI to the GPIO unit */
Julia Cartwright229710f2017-03-09 10:22:04 -0600565 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200566 regval = readl(gpio_dev->base + WAKE_INT_MASTER_REG);
567 regval |= EOI_MASK;
568 writel(regval, gpio_dev->base + WAKE_INT_MASTER_REG);
Julia Cartwright229710f2017-03-09 10:22:04 -0600569 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800570
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200571 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800572}
573
574static int amd_get_groups_count(struct pinctrl_dev *pctldev)
575{
576 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
577
578 return gpio_dev->ngroups;
579}
580
581static const char *amd_get_group_name(struct pinctrl_dev *pctldev,
582 unsigned group)
583{
584 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
585
586 return gpio_dev->groups[group].name;
587}
588
589static int amd_get_group_pins(struct pinctrl_dev *pctldev,
590 unsigned group,
591 const unsigned **pins,
592 unsigned *num_pins)
593{
594 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
595
596 *pins = gpio_dev->groups[group].pins;
597 *num_pins = gpio_dev->groups[group].npins;
598 return 0;
599}
600
601static const struct pinctrl_ops amd_pinctrl_ops = {
602 .get_groups_count = amd_get_groups_count,
603 .get_group_name = amd_get_group_name,
604 .get_group_pins = amd_get_group_pins,
605#ifdef CONFIG_OF
606 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300607 .dt_free_map = pinctrl_utils_free_map,
Ken Xuedbad75d2015-03-10 15:02:19 +0800608#endif
609};
610
611static int amd_pinconf_get(struct pinctrl_dev *pctldev,
612 unsigned int pin,
613 unsigned long *config)
614{
615 u32 pin_reg;
616 unsigned arg;
617 unsigned long flags;
618 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
619 enum pin_config_param param = pinconf_to_config_param(*config);
620
Julia Cartwright229710f2017-03-09 10:22:04 -0600621 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800622 pin_reg = readl(gpio_dev->base + pin*4);
Julia Cartwright229710f2017-03-09 10:22:04 -0600623 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800624 switch (param) {
625 case PIN_CONFIG_INPUT_DEBOUNCE:
626 arg = pin_reg & DB_TMR_OUT_MASK;
627 break;
628
629 case PIN_CONFIG_BIAS_PULL_DOWN:
630 arg = (pin_reg >> PULL_DOWN_ENABLE_OFF) & BIT(0);
631 break;
632
633 case PIN_CONFIG_BIAS_PULL_UP:
634 arg = (pin_reg >> PULL_UP_SEL_OFF) & (BIT(0) | BIT(1));
635 break;
636
637 case PIN_CONFIG_DRIVE_STRENGTH:
638 arg = (pin_reg >> DRV_STRENGTH_SEL_OFF) & DRV_STRENGTH_SEL_MASK;
639 break;
640
641 default:
642 dev_err(&gpio_dev->pdev->dev, "Invalid config param %04x\n",
643 param);
644 return -ENOTSUPP;
645 }
646
647 *config = pinconf_to_config_packed(param, arg);
648
649 return 0;
650}
651
652static int amd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
653 unsigned long *configs, unsigned num_configs)
654{
655 int i;
Ken Xuedbad75d2015-03-10 15:02:19 +0800656 u32 arg;
Ken Xue25a853d2015-03-27 17:44:26 +0800657 int ret = 0;
658 u32 pin_reg;
Ken Xuedbad75d2015-03-10 15:02:19 +0800659 unsigned long flags;
660 enum pin_config_param param;
661 struct amd_gpio *gpio_dev = pinctrl_dev_get_drvdata(pctldev);
662
Julia Cartwright229710f2017-03-09 10:22:04 -0600663 raw_spin_lock_irqsave(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800664 for (i = 0; i < num_configs; i++) {
665 param = pinconf_to_config_param(configs[i]);
666 arg = pinconf_to_config_argument(configs[i]);
667 pin_reg = readl(gpio_dev->base + pin*4);
668
669 switch (param) {
670 case PIN_CONFIG_INPUT_DEBOUNCE:
671 pin_reg &= ~DB_TMR_OUT_MASK;
672 pin_reg |= arg & DB_TMR_OUT_MASK;
673 break;
674
675 case PIN_CONFIG_BIAS_PULL_DOWN:
676 pin_reg &= ~BIT(PULL_DOWN_ENABLE_OFF);
677 pin_reg |= (arg & BIT(0)) << PULL_DOWN_ENABLE_OFF;
678 break;
679
680 case PIN_CONFIG_BIAS_PULL_UP:
681 pin_reg &= ~BIT(PULL_UP_SEL_OFF);
682 pin_reg |= (arg & BIT(0)) << PULL_UP_SEL_OFF;
683 pin_reg &= ~BIT(PULL_UP_ENABLE_OFF);
684 pin_reg |= ((arg>>1) & BIT(0)) << PULL_UP_ENABLE_OFF;
685 break;
686
687 case PIN_CONFIG_DRIVE_STRENGTH:
688 pin_reg &= ~(DRV_STRENGTH_SEL_MASK
689 << DRV_STRENGTH_SEL_OFF);
690 pin_reg |= (arg & DRV_STRENGTH_SEL_MASK)
691 << DRV_STRENGTH_SEL_OFF;
692 break;
693
694 default:
695 dev_err(&gpio_dev->pdev->dev,
696 "Invalid config param %04x\n", param);
Ken Xue25a853d2015-03-27 17:44:26 +0800697 ret = -ENOTSUPP;
Ken Xuedbad75d2015-03-10 15:02:19 +0800698 }
699
700 writel(pin_reg, gpio_dev->base + pin*4);
701 }
Julia Cartwright229710f2017-03-09 10:22:04 -0600702 raw_spin_unlock_irqrestore(&gpio_dev->lock, flags);
Ken Xuedbad75d2015-03-10 15:02:19 +0800703
Ken Xue25a853d2015-03-27 17:44:26 +0800704 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800705}
706
707static int amd_pinconf_group_get(struct pinctrl_dev *pctldev,
708 unsigned int group,
709 unsigned long *config)
710{
711 const unsigned *pins;
712 unsigned npins;
713 int ret;
714
715 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
716 if (ret)
717 return ret;
718
719 if (amd_pinconf_get(pctldev, pins[0], config))
720 return -ENOTSUPP;
721
722 return 0;
723}
724
725static int amd_pinconf_group_set(struct pinctrl_dev *pctldev,
726 unsigned group, unsigned long *configs,
727 unsigned num_configs)
728{
729 const unsigned *pins;
730 unsigned npins;
731 int i, ret;
732
733 ret = amd_get_group_pins(pctldev, group, &pins, &npins);
734 if (ret)
735 return ret;
736 for (i = 0; i < npins; i++) {
737 if (amd_pinconf_set(pctldev, pins[i], configs, num_configs))
738 return -ENOTSUPP;
739 }
740 return 0;
741}
742
743static const struct pinconf_ops amd_pinconf_ops = {
744 .pin_config_get = amd_pinconf_get,
745 .pin_config_set = amd_pinconf_set,
746 .pin_config_group_get = amd_pinconf_group_get,
747 .pin_config_group_set = amd_pinconf_group_set,
748};
749
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800750#ifdef CONFIG_PM_SLEEP
751static bool amd_gpio_should_save(struct amd_gpio *gpio_dev, unsigned int pin)
752{
753 const struct pin_desc *pd = pin_desc_get(gpio_dev->pctrl, pin);
754
755 if (!pd)
756 return false;
757
758 /*
759 * Only restore the pin if it is actually in use by the kernel (or
760 * by userspace).
761 */
762 if (pd->mux_owner || pd->gpio_owner ||
763 gpiochip_line_is_irq(&gpio_dev->gc, pin))
764 return true;
765
766 return false;
767}
768
Colin Ian King2d71dfa2017-09-13 17:15:01 +0100769static int amd_gpio_suspend(struct device *dev)
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800770{
771 struct platform_device *pdev = to_platform_device(dev);
772 struct amd_gpio *gpio_dev = platform_get_drvdata(pdev);
773 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
774 int i;
775
776 for (i = 0; i < desc->npins; i++) {
777 int pin = desc->pins[i].number;
778
779 if (!amd_gpio_should_save(gpio_dev, pin))
780 continue;
781
782 gpio_dev->saved_regs[i] = readl(gpio_dev->base + pin*4);
783 }
784
785 return 0;
786}
787
Colin Ian King2d71dfa2017-09-13 17:15:01 +0100788static int amd_gpio_resume(struct device *dev)
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800789{
790 struct platform_device *pdev = to_platform_device(dev);
791 struct amd_gpio *gpio_dev = platform_get_drvdata(pdev);
792 struct pinctrl_desc *desc = gpio_dev->pctrl->desc;
793 int i;
794
795 for (i = 0; i < desc->npins; i++) {
796 int pin = desc->pins[i].number;
797
798 if (!amd_gpio_should_save(gpio_dev, pin))
799 continue;
800
801 writel(gpio_dev->saved_regs[i], gpio_dev->base + pin*4);
802 }
803
804 return 0;
805}
806
807static const struct dev_pm_ops amd_gpio_pm_ops = {
808 SET_LATE_SYSTEM_SLEEP_PM_OPS(amd_gpio_suspend,
809 amd_gpio_resume)
810};
811#endif
812
Ken Xuedbad75d2015-03-10 15:02:19 +0800813static struct pinctrl_desc amd_pinctrl_desc = {
814 .pins = kerncz_pins,
815 .npins = ARRAY_SIZE(kerncz_pins),
816 .pctlops = &amd_pinctrl_ops,
817 .confops = &amd_pinconf_ops,
818 .owner = THIS_MODULE,
819};
820
821static int amd_gpio_probe(struct platform_device *pdev)
822{
823 int ret = 0;
Ken Xue25a853d2015-03-27 17:44:26 +0800824 int irq_base;
Ken Xuedbad75d2015-03-10 15:02:19 +0800825 struct resource *res;
826 struct amd_gpio *gpio_dev;
827
828 gpio_dev = devm_kzalloc(&pdev->dev,
829 sizeof(struct amd_gpio), GFP_KERNEL);
830 if (!gpio_dev)
831 return -ENOMEM;
832
Julia Cartwright229710f2017-03-09 10:22:04 -0600833 raw_spin_lock_init(&gpio_dev->lock);
Ken Xuedbad75d2015-03-10 15:02:19 +0800834
835 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
836 if (!res) {
837 dev_err(&pdev->dev, "Failed to get gpio io resource.\n");
838 return -EINVAL;
839 }
840
841 gpio_dev->base = devm_ioremap_nocache(&pdev->dev, res->start,
842 resource_size(res));
Wei Yongjun424a6c62016-02-06 22:56:36 +0800843 if (!gpio_dev->base)
844 return -ENOMEM;
Ken Xuedbad75d2015-03-10 15:02:19 +0800845
846 irq_base = platform_get_irq(pdev, 0);
847 if (irq_base < 0) {
Gustavo A. R. Silva2e6424ab2017-08-09 11:09:33 -0500848 dev_err(&pdev->dev, "Failed to get gpio IRQ: %d\n", irq_base);
849 return irq_base;
Ken Xuedbad75d2015-03-10 15:02:19 +0800850 }
851
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800852#ifdef CONFIG_PM_SLEEP
853 gpio_dev->saved_regs = devm_kcalloc(&pdev->dev, amd_pinctrl_desc.npins,
854 sizeof(*gpio_dev->saved_regs),
855 GFP_KERNEL);
856 if (!gpio_dev->saved_regs)
857 return -ENOMEM;
858#endif
859
Ken Xuedbad75d2015-03-10 15:02:19 +0800860 gpio_dev->pdev = pdev;
Daniel Kurtz12b10f42018-02-16 12:12:43 -0700861 gpio_dev->gc.get_direction = amd_gpio_get_direction;
Ken Xuedbad75d2015-03-10 15:02:19 +0800862 gpio_dev->gc.direction_input = amd_gpio_direction_input;
863 gpio_dev->gc.direction_output = amd_gpio_direction_output;
864 gpio_dev->gc.get = amd_gpio_get_value;
865 gpio_dev->gc.set = amd_gpio_set_value;
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300866 gpio_dev->gc.set_config = amd_gpio_set_config;
Ken Xuedbad75d2015-03-10 15:02:19 +0800867 gpio_dev->gc.dbg_show = amd_gpio_dbg_show;
868
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530869 gpio_dev->gc.base = -1;
Ken Xuedbad75d2015-03-10 15:02:19 +0800870 gpio_dev->gc.label = pdev->name;
871 gpio_dev->gc.owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +0100872 gpio_dev->gc.parent = &pdev->dev;
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530873 gpio_dev->gc.ngpio = resource_size(res) / 4;
Ken Xuedbad75d2015-03-10 15:02:19 +0800874#if defined(CONFIG_OF_GPIO)
875 gpio_dev->gc.of_node = pdev->dev.of_node;
876#endif
877
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530878 gpio_dev->hwbank_num = gpio_dev->gc.ngpio / 64;
Ken Xuedbad75d2015-03-10 15:02:19 +0800879 gpio_dev->groups = kerncz_groups;
880 gpio_dev->ngroups = ARRAY_SIZE(kerncz_groups);
881
882 amd_pinctrl_desc.name = dev_name(&pdev->dev);
Laxman Dewangan251e22a2016-02-24 14:44:07 +0530883 gpio_dev->pctrl = devm_pinctrl_register(&pdev->dev, &amd_pinctrl_desc,
884 gpio_dev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900885 if (IS_ERR(gpio_dev->pctrl)) {
Ken Xuedbad75d2015-03-10 15:02:19 +0800886 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900887 return PTR_ERR(gpio_dev->pctrl);
Ken Xuedbad75d2015-03-10 15:02:19 +0800888 }
889
Linus Walleij04d36722015-12-08 09:21:38 +0100890 ret = gpiochip_add_data(&gpio_dev->gc, gpio_dev);
Ken Xuedbad75d2015-03-10 15:02:19 +0800891 if (ret)
Laxman Dewangan251e22a2016-02-24 14:44:07 +0530892 return ret;
Ken Xuedbad75d2015-03-10 15:02:19 +0800893
894 ret = gpiochip_add_pin_range(&gpio_dev->gc, dev_name(&pdev->dev),
Shah, Nehal-bakulchandra3bfd4432016-12-06 12:17:48 +0530895 0, 0, gpio_dev->gc.ngpio);
Ken Xuedbad75d2015-03-10 15:02:19 +0800896 if (ret) {
897 dev_err(&pdev->dev, "Failed to add pin range\n");
898 goto out2;
899 }
900
901 ret = gpiochip_irqchip_add(&gpio_dev->gc,
902 &amd_gpio_irqchip,
903 0,
904 handle_simple_irq,
905 IRQ_TYPE_NONE);
906 if (ret) {
907 dev_err(&pdev->dev, "could not add irqchip\n");
908 ret = -ENODEV;
909 goto out2;
910 }
911
Thomas Gleixnerba714a92017-05-23 23:23:32 +0200912 ret = devm_request_irq(&pdev->dev, irq_base, amd_gpio_irq_handler, 0,
913 KBUILD_MODNAME, gpio_dev);
914 if (ret)
915 goto out2;
916
Ken Xuedbad75d2015-03-10 15:02:19 +0800917 platform_set_drvdata(pdev, gpio_dev);
918
919 dev_dbg(&pdev->dev, "amd gpio driver loaded\n");
920 return ret;
921
922out2:
923 gpiochip_remove(&gpio_dev->gc);
924
Ken Xuedbad75d2015-03-10 15:02:19 +0800925 return ret;
926}
927
928static int amd_gpio_remove(struct platform_device *pdev)
929{
930 struct amd_gpio *gpio_dev;
931
932 gpio_dev = platform_get_drvdata(pdev);
933
934 gpiochip_remove(&gpio_dev->gc);
Ken Xuedbad75d2015-03-10 15:02:19 +0800935
936 return 0;
937}
938
939static const struct acpi_device_id amd_gpio_acpi_match[] = {
940 { "AMD0030", 0 },
Wang Hongcheng42a44402016-03-11 10:58:42 +0800941 { "AMDI0030", 0},
Ken Xuedbad75d2015-03-10 15:02:19 +0800942 { },
943};
944MODULE_DEVICE_TABLE(acpi, amd_gpio_acpi_match);
945
946static struct platform_driver amd_gpio_driver = {
947 .driver = {
948 .name = "amd_gpio",
Ken Xuedbad75d2015-03-10 15:02:19 +0800949 .acpi_match_table = ACPI_PTR(amd_gpio_acpi_match),
Daniel Drake79d2c8b2017-09-11 14:11:56 +0800950#ifdef CONFIG_PM_SLEEP
951 .pm = &amd_gpio_pm_ops,
952#endif
Ken Xuedbad75d2015-03-10 15:02:19 +0800953 },
954 .probe = amd_gpio_probe,
955 .remove = amd_gpio_remove,
956};
957
958module_platform_driver(amd_gpio_driver);
959
960MODULE_LICENSE("GPL v2");
961MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>");
962MODULE_DESCRIPTION("AMD GPIO pinctrl driver");