blob: e89a3c6877aa41a6f7e0bbc3a7828e9f14792ab9 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jamie Iles7779b3452014-02-25 17:01:01 -06002/*
3 * Copyright (c) 2011 Jamie Iles
4 *
Jamie Iles7779b3452014-02-25 17:01:01 -06005 * All enquiries to support@picochip.com
6 */
Jiang Qiue6cb3482016-04-28 17:32:03 +08007#include <linux/acpi.h>
Phil Edworthye6bf3772018-03-12 18:30:56 +00008#include <linux/clk.h>
Jamie Iles7779b3452014-02-25 17:01:01 -06009#include <linux/err.h>
Phil Edworthye6bf3772018-03-12 18:30:56 +000010#include <linux/gpio/driver.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060011#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/ioport.h>
15#include <linux/irq.h>
16#include <linux/irqdomain.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
Hoan Trana72b8c42017-02-21 11:32:43 -080020#include <linux/of_device.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060021#include <linux/of_irq.h>
22#include <linux/platform_device.h>
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +080023#include <linux/property.h>
Alan Tull07901a92017-10-11 11:34:44 -050024#include <linux/reset.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060025#include <linux/spinlock.h>
Weike Chen3d2613c2014-09-17 09:18:39 -070026#include <linux/platform_data/gpio-dwapb.h>
27#include <linux/slab.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060028
Jiang Qiue6cb3482016-04-28 17:32:03 +080029#include "gpiolib.h"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030030#include "gpiolib-acpi.h"
Jiang Qiue6cb3482016-04-28 17:32:03 +080031
Jamie Iles7779b3452014-02-25 17:01:01 -060032#define GPIO_SWPORTA_DR 0x00
33#define GPIO_SWPORTA_DDR 0x04
34#define GPIO_SWPORTB_DR 0x0c
35#define GPIO_SWPORTB_DDR 0x10
36#define GPIO_SWPORTC_DR 0x18
37#define GPIO_SWPORTC_DDR 0x1c
38#define GPIO_SWPORTD_DR 0x24
39#define GPIO_SWPORTD_DDR 0x28
40#define GPIO_INTEN 0x30
41#define GPIO_INTMASK 0x34
42#define GPIO_INTTYPE_LEVEL 0x38
43#define GPIO_INT_POLARITY 0x3c
44#define GPIO_INTSTATUS 0x40
Weike Chen5d60d9e2014-09-17 09:18:41 -070045#define GPIO_PORTA_DEBOUNCE 0x48
Jamie Iles7779b3452014-02-25 17:01:01 -060046#define GPIO_PORTA_EOI 0x4c
47#define GPIO_EXT_PORTA 0x50
48#define GPIO_EXT_PORTB 0x54
49#define GPIO_EXT_PORTC 0x58
50#define GPIO_EXT_PORTD 0x5c
51
Andy Shevchenkoc58220c2020-04-15 17:15:21 +030052#define DWAPB_DRIVER_NAME "gpio-dwapb"
Jamie Iles7779b3452014-02-25 17:01:01 -060053#define DWAPB_MAX_PORTS 4
Andy Shevchenkoc58220c2020-04-15 17:15:21 +030054
Linus Walleij89f99fe2018-02-08 17:03:58 +010055#define GPIO_EXT_PORT_STRIDE 0x04 /* register stride 32 bits */
56#define GPIO_SWPORT_DR_STRIDE 0x0c /* register stride 3*32 bits */
57#define GPIO_SWPORT_DDR_STRIDE 0x0c /* register stride 3*32 bits */
Jamie Iles7779b3452014-02-25 17:01:01 -060058
Hoan Trana72b8c42017-02-21 11:32:43 -080059#define GPIO_REG_OFFSET_V2 1
60
61#define GPIO_INTMASK_V2 0x44
62#define GPIO_INTTYPE_LEVEL_V2 0x34
63#define GPIO_INT_POLARITY_V2 0x38
64#define GPIO_INTSTATUS_V2 0x3c
65#define GPIO_PORTA_EOI_V2 0x40
66
Serge Semin5c544c92020-03-23 22:54:00 +030067#define DWAPB_NR_CLOCKS 2
68
Jamie Iles7779b3452014-02-25 17:01:01 -060069struct dwapb_gpio;
70
Weike Chen1e960db2014-09-17 09:18:42 -070071#ifdef CONFIG_PM_SLEEP
72/* Store GPIO context across system-wide suspend/resume transitions */
73struct dwapb_context {
74 u32 data;
75 u32 dir;
76 u32 ext;
77 u32 int_en;
78 u32 int_mask;
79 u32 int_type;
80 u32 int_pol;
81 u32 int_deb;
Hoan Tran6437c7b2017-09-08 15:41:15 -070082 u32 wake_en;
Weike Chen1e960db2014-09-17 09:18:42 -070083};
84#endif
85
Jamie Iles7779b3452014-02-25 17:01:01 -060086struct dwapb_gpio_port {
Linus Walleij0f4630f2015-12-04 14:02:58 +010087 struct gpio_chip gc;
Jamie Iles7779b3452014-02-25 17:01:01 -060088 bool is_registered;
89 struct dwapb_gpio *gpio;
Weike Chen1e960db2014-09-17 09:18:42 -070090#ifdef CONFIG_PM_SLEEP
91 struct dwapb_context *ctx;
92#endif
93 unsigned int idx;
Jamie Iles7779b3452014-02-25 17:01:01 -060094};
95
96struct dwapb_gpio {
97 struct device *dev;
98 void __iomem *regs;
99 struct dwapb_gpio_port *ports;
100 unsigned int nr_ports;
101 struct irq_domain *domain;
Hoan Trana72b8c42017-02-21 11:32:43 -0800102 unsigned int flags;
Alan Tull07901a92017-10-11 11:34:44 -0500103 struct reset_control *rst;
Serge Semin5c544c92020-03-23 22:54:00 +0300104 struct clk_bulk_data clks[DWAPB_NR_CLOCKS];
Jamie Iles7779b3452014-02-25 17:01:01 -0600105};
106
Hoan Trana72b8c42017-02-21 11:32:43 -0800107static inline u32 gpio_reg_v2_convert(unsigned int offset)
108{
109 switch (offset) {
110 case GPIO_INTMASK:
111 return GPIO_INTMASK_V2;
112 case GPIO_INTTYPE_LEVEL:
113 return GPIO_INTTYPE_LEVEL_V2;
114 case GPIO_INT_POLARITY:
115 return GPIO_INT_POLARITY_V2;
116 case GPIO_INTSTATUS:
117 return GPIO_INTSTATUS_V2;
118 case GPIO_PORTA_EOI:
119 return GPIO_PORTA_EOI_V2;
120 }
121
122 return offset;
123}
124
125static inline u32 gpio_reg_convert(struct dwapb_gpio *gpio, unsigned int offset)
126{
127 if (gpio->flags & GPIO_REG_OFFSET_V2)
128 return gpio_reg_v2_convert(offset);
129
130 return offset;
131}
132
Weike Chen67809b92014-09-17 09:18:40 -0700133static inline u32 dwapb_read(struct dwapb_gpio *gpio, unsigned int offset)
134{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100135 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen67809b92014-09-17 09:18:40 -0700136 void __iomem *reg_base = gpio->regs;
137
Hoan Trana72b8c42017-02-21 11:32:43 -0800138 return gc->read_reg(reg_base + gpio_reg_convert(gpio, offset));
Weike Chen67809b92014-09-17 09:18:40 -0700139}
140
141static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
142 u32 val)
143{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100144 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen67809b92014-09-17 09:18:40 -0700145 void __iomem *reg_base = gpio->regs;
146
Hoan Trana72b8c42017-02-21 11:32:43 -0800147 gc->write_reg(reg_base + gpio_reg_convert(gpio, offset), val);
Weike Chen67809b92014-09-17 09:18:40 -0700148}
149
Jamie Iles7779b3452014-02-25 17:01:01 -0600150static int dwapb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
151{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100152 struct dwapb_gpio_port *port = gpiochip_get_data(gc);
Jamie Iles7779b3452014-02-25 17:01:01 -0600153 struct dwapb_gpio *gpio = port->gpio;
154
155 return irq_find_mapping(gpio->domain, offset);
156}
157
Linus Walleij62c16232018-02-08 18:00:05 +0100158static struct dwapb_gpio_port *dwapb_offs_to_port(struct dwapb_gpio *gpio, unsigned int offs)
159{
160 struct dwapb_gpio_port *port;
161 int i;
162
163 for (i = 0; i < gpio->nr_ports; i++) {
164 port = &gpio->ports[i];
165 if (port->idx == offs / 32)
166 return port;
167 }
168
169 return NULL;
170}
171
Jamie Iles7779b3452014-02-25 17:01:01 -0600172static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
173{
Linus Walleij62c16232018-02-08 18:00:05 +0100174 struct dwapb_gpio_port *port = dwapb_offs_to_port(gpio, offs);
175 struct gpio_chip *gc;
176 u32 pol;
177 int val;
Jamie Iles7779b3452014-02-25 17:01:01 -0600178
Linus Walleij62c16232018-02-08 18:00:05 +0100179 if (!port)
180 return;
181 gc = &port->gc;
182
183 pol = dwapb_read(gpio, GPIO_INT_POLARITY);
184 /* Just read the current value right out of the data register */
185 val = gc->get(gc, offs % 32);
186 if (val)
187 pol &= ~BIT(offs);
Jamie Iles7779b3452014-02-25 17:01:01 -0600188 else
Linus Walleij62c16232018-02-08 18:00:05 +0100189 pol |= BIT(offs);
Jamie Iles7779b3452014-02-25 17:01:01 -0600190
Linus Walleij62c16232018-02-08 18:00:05 +0100191 dwapb_write(gpio, GPIO_INT_POLARITY, pol);
Jamie Iles7779b3452014-02-25 17:01:01 -0600192}
193
Weike Chen3d2613c2014-09-17 09:18:39 -0700194static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
Jamie Iles7779b3452014-02-25 17:01:01 -0600195{
Andy Shevchenko038aa1f2020-04-15 17:15:22 +0300196 unsigned long irq_status;
197 int hwirq;
Jamie Iles7779b3452014-02-25 17:01:01 -0600198
Andy Shevchenko038aa1f2020-04-15 17:15:22 +0300199 irq_status = dwapb_read(gpio, GPIO_INTSTATUS);
200 for_each_set_bit(hwirq, &irq_status, 32) {
Jamie Iles7779b3452014-02-25 17:01:01 -0600201 int gpio_irq = irq_find_mapping(gpio->domain, hwirq);
Andy Shevchenko038aa1f2020-04-15 17:15:22 +0300202 u32 irq_type = irq_get_trigger_type(gpio_irq);
Jamie Iles7779b3452014-02-25 17:01:01 -0600203
204 generic_handle_irq(gpio_irq);
Jamie Iles7779b3452014-02-25 17:01:01 -0600205
Andy Shevchenko038aa1f2020-04-15 17:15:22 +0300206 if ((irq_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Jamie Iles7779b3452014-02-25 17:01:01 -0600207 dwapb_toggle_trigger(gpio, hwirq);
208 }
209
Andy Shevchenko038aa1f2020-04-15 17:15:22 +0300210 return irq_status;
Weike Chen3d2613c2014-09-17 09:18:39 -0700211}
212
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200213static void dwapb_irq_handler(struct irq_desc *desc)
Weike Chen3d2613c2014-09-17 09:18:39 -0700214{
Jiang Liu476f8b42015-06-04 12:13:15 +0800215 struct dwapb_gpio *gpio = irq_desc_get_handler_data(desc);
Weike Chen3d2613c2014-09-17 09:18:39 -0700216 struct irq_chip *chip = irq_desc_get_chip(desc);
217
218 dwapb_do_irq(gpio);
219
Jamie Iles7779b3452014-02-25 17:01:01 -0600220 if (chip->irq_eoi)
221 chip->irq_eoi(irq_desc_get_irq_data(desc));
222}
223
224static void dwapb_irq_enable(struct irq_data *d)
225{
226 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
227 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100228 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600229 unsigned long flags;
230 u32 val;
231
Linus Walleij0f4630f2015-12-04 14:02:58 +0100232 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700233 val = dwapb_read(gpio, GPIO_INTEN);
Jamie Iles7779b3452014-02-25 17:01:01 -0600234 val |= BIT(d->hwirq);
Weike Chen67809b92014-09-17 09:18:40 -0700235 dwapb_write(gpio, GPIO_INTEN, val);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100236 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Jamie Iles7779b3452014-02-25 17:01:01 -0600237}
238
239static void dwapb_irq_disable(struct irq_data *d)
240{
241 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
242 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100243 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600244 unsigned long flags;
245 u32 val;
246
Linus Walleij0f4630f2015-12-04 14:02:58 +0100247 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700248 val = dwapb_read(gpio, GPIO_INTEN);
Jamie Iles7779b3452014-02-25 17:01:01 -0600249 val &= ~BIT(d->hwirq);
Weike Chen67809b92014-09-17 09:18:40 -0700250 dwapb_write(gpio, GPIO_INTEN, val);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100251 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Jamie Iles7779b3452014-02-25 17:01:01 -0600252}
253
Linus Walleij57ef0422014-03-14 18:16:20 +0100254static int dwapb_irq_reqres(struct irq_data *d)
Jamie Iles7779b3452014-02-25 17:01:01 -0600255{
256 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
257 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100258 struct gpio_chip *gc = &gpio->ports[0].gc;
Andy Shevchenko10ed3532018-07-30 15:38:33 +0300259 int ret;
Jamie Iles7779b3452014-02-25 17:01:01 -0600260
Andy Shevchenko10ed3532018-07-30 15:38:33 +0300261 ret = gpiochip_lock_as_irq(gc, irqd_to_hwirq(d));
262 if (ret) {
Jamie Iles7779b3452014-02-25 17:01:01 -0600263 dev_err(gpio->dev, "unable to lock HW IRQ %lu for IRQ\n",
264 irqd_to_hwirq(d));
Andy Shevchenko10ed3532018-07-30 15:38:33 +0300265 return ret;
Linus Walleij57ef0422014-03-14 18:16:20 +0100266 }
Jamie Iles7779b3452014-02-25 17:01:01 -0600267 return 0;
268}
269
Linus Walleij57ef0422014-03-14 18:16:20 +0100270static void dwapb_irq_relres(struct irq_data *d)
Jamie Iles7779b3452014-02-25 17:01:01 -0600271{
272 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
273 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100274 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600275
Linus Walleij0f4630f2015-12-04 14:02:58 +0100276 gpiochip_unlock_as_irq(gc, irqd_to_hwirq(d));
Jamie Iles7779b3452014-02-25 17:01:01 -0600277}
278
279static int dwapb_irq_set_type(struct irq_data *d, u32 type)
280{
281 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
282 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100283 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600284 int bit = d->hwirq;
285 unsigned long level, polarity, flags;
286
287 if (type & ~(IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
288 IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
289 return -EINVAL;
290
Linus Walleij0f4630f2015-12-04 14:02:58 +0100291 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700292 level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
293 polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
Jamie Iles7779b3452014-02-25 17:01:01 -0600294
295 switch (type) {
296 case IRQ_TYPE_EDGE_BOTH:
297 level |= BIT(bit);
298 dwapb_toggle_trigger(gpio, bit);
299 break;
300 case IRQ_TYPE_EDGE_RISING:
301 level |= BIT(bit);
302 polarity |= BIT(bit);
303 break;
304 case IRQ_TYPE_EDGE_FALLING:
305 level |= BIT(bit);
306 polarity &= ~BIT(bit);
307 break;
308 case IRQ_TYPE_LEVEL_HIGH:
309 level &= ~BIT(bit);
310 polarity |= BIT(bit);
311 break;
312 case IRQ_TYPE_LEVEL_LOW:
313 level &= ~BIT(bit);
314 polarity &= ~BIT(bit);
315 break;
316 }
317
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200318 irq_setup_alt_chip(d, type);
319
Weike Chen67809b92014-09-17 09:18:40 -0700320 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
Xiaoguang Chenedadced2017-06-02 07:27:15 +0800321 if (type != IRQ_TYPE_EDGE_BOTH)
322 dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100323 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Jamie Iles7779b3452014-02-25 17:01:01 -0600324
325 return 0;
326}
327
Hoan Tran6437c7b2017-09-08 15:41:15 -0700328#ifdef CONFIG_PM_SLEEP
329static int dwapb_irq_set_wake(struct irq_data *d, unsigned int enable)
330{
331 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
332 struct dwapb_gpio *gpio = igc->private;
333 struct dwapb_context *ctx = gpio->ports[0].ctx;
334
335 if (enable)
336 ctx->wake_en |= BIT(d->hwirq);
337 else
338 ctx->wake_en &= ~BIT(d->hwirq);
339
340 return 0;
341}
342#endif
343
Weike Chen5d60d9e2014-09-17 09:18:41 -0700344static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
345 unsigned offset, unsigned debounce)
346{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100347 struct dwapb_gpio_port *port = gpiochip_get_data(gc);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700348 struct dwapb_gpio *gpio = port->gpio;
349 unsigned long flags, val_deb;
Linus Walleijd97a1b52017-10-20 12:26:51 +0200350 unsigned long mask = BIT(offset);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700351
Linus Walleij0f4630f2015-12-04 14:02:58 +0100352 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700353
354 val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
355 if (debounce)
356 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb | mask);
357 else
358 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb & ~mask);
359
Linus Walleij0f4630f2015-12-04 14:02:58 +0100360 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700361
362 return 0;
363}
364
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300365static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
366 unsigned long config)
367{
368 u32 debounce;
369
370 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
371 return -ENOTSUPP;
372
373 debounce = pinconf_to_config_argument(config);
374 return dwapb_gpio_set_debounce(gc, offset, debounce);
375}
376
Weike Chen3d2613c2014-09-17 09:18:39 -0700377static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
378{
379 u32 worked;
380 struct dwapb_gpio *gpio = dev_id;
381
382 worked = dwapb_do_irq(gpio);
383
384 return worked ? IRQ_HANDLED : IRQ_NONE;
385}
386
Jamie Iles7779b3452014-02-25 17:01:01 -0600387static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
Weike Chen3d2613c2014-09-17 09:18:39 -0700388 struct dwapb_gpio_port *port,
389 struct dwapb_port_property *pp)
Jamie Iles7779b3452014-02-25 17:01:01 -0600390{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100391 struct gpio_chip *gc = &port->gc;
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800392 struct fwnode_handle *fwnode = pp->fwnode;
Weike Chen3d2613c2014-09-17 09:18:39 -0700393 struct irq_chip_generic *irq_gc = NULL;
Jamie Iles7779b3452014-02-25 17:01:01 -0600394 unsigned int hwirq, ngpio = gc->ngpio;
395 struct irq_chip_type *ct;
Weike Chen3d2613c2014-09-17 09:18:39 -0700396 int err, i;
Jamie Iles7779b3452014-02-25 17:01:01 -0600397
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800398 gpio->domain = irq_domain_create_linear(fwnode, ngpio,
399 &irq_generic_chip_ops, gpio);
Jamie Iles7779b3452014-02-25 17:01:01 -0600400 if (!gpio->domain)
401 return;
402
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200403 err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
Andy Shevchenkoc58220c2020-04-15 17:15:21 +0300404 DWAPB_DRIVER_NAME, handle_level_irq,
Jamie Iles7779b3452014-02-25 17:01:01 -0600405 IRQ_NOREQUEST, 0,
406 IRQ_GC_INIT_NESTED_LOCK);
407 if (err) {
408 dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
409 irq_domain_remove(gpio->domain);
410 gpio->domain = NULL;
411 return;
412 }
413
414 irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
415 if (!irq_gc) {
416 irq_domain_remove(gpio->domain);
417 gpio->domain = NULL;
418 return;
419 }
420
421 irq_gc->reg_base = gpio->regs;
422 irq_gc->private = gpio;
423
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200424 for (i = 0; i < 2; i++) {
425 ct = &irq_gc->chip_types[i];
426 ct->chip.irq_ack = irq_gc_ack_set_bit;
427 ct->chip.irq_mask = irq_gc_mask_set_bit;
428 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
429 ct->chip.irq_set_type = dwapb_irq_set_type;
430 ct->chip.irq_enable = dwapb_irq_enable;
431 ct->chip.irq_disable = dwapb_irq_disable;
432 ct->chip.irq_request_resources = dwapb_irq_reqres;
433 ct->chip.irq_release_resources = dwapb_irq_relres;
Hoan Tran6437c7b2017-09-08 15:41:15 -0700434#ifdef CONFIG_PM_SLEEP
435 ct->chip.irq_set_wake = dwapb_irq_set_wake;
436#endif
Hoan Trana72b8c42017-02-21 11:32:43 -0800437 ct->regs.ack = gpio_reg_convert(gpio, GPIO_PORTA_EOI);
438 ct->regs.mask = gpio_reg_convert(gpio, GPIO_INTMASK);
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200439 ct->type = IRQ_TYPE_LEVEL_MASK;
440 }
441
442 irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
443 irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
444 irq_gc->chip_types[1].handler = handle_edge_irq;
Jamie Iles7779b3452014-02-25 17:01:01 -0600445
Weike Chen3d2613c2014-09-17 09:18:39 -0700446 if (!pp->irq_shared) {
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100447 int i;
448
449 for (i = 0; i < pp->ngpio; i++) {
Phil Edworthyda069d52018-05-23 09:52:44 +0100450 if (pp->irq[i] >= 0)
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100451 irq_set_chained_handler_and_data(pp->irq[i],
452 dwapb_irq_handler, gpio);
453 }
Weike Chen3d2613c2014-09-17 09:18:39 -0700454 } else {
455 /*
456 * Request a shared IRQ since where MFD would have devices
457 * using the same irq pin
458 */
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100459 err = devm_request_irq(gpio->dev, pp->irq[0],
Weike Chen3d2613c2014-09-17 09:18:39 -0700460 dwapb_irq_handler_mfd,
Andy Shevchenkoc58220c2020-04-15 17:15:21 +0300461 IRQF_SHARED, DWAPB_DRIVER_NAME, gpio);
Weike Chen3d2613c2014-09-17 09:18:39 -0700462 if (err) {
463 dev_err(gpio->dev, "error requesting IRQ\n");
464 irq_domain_remove(gpio->domain);
465 gpio->domain = NULL;
466 return;
467 }
468 }
Jamie Iles7779b3452014-02-25 17:01:01 -0600469
470 for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
471 irq_create_mapping(gpio->domain, hwirq);
472
Linus Walleij0f4630f2015-12-04 14:02:58 +0100473 port->gc.to_irq = dwapb_gpio_to_irq;
Jamie Iles7779b3452014-02-25 17:01:01 -0600474}
475
476static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
477{
478 struct dwapb_gpio_port *port = &gpio->ports[0];
Linus Walleij0f4630f2015-12-04 14:02:58 +0100479 struct gpio_chip *gc = &port->gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600480 unsigned int ngpio = gc->ngpio;
481 irq_hw_number_t hwirq;
482
483 if (!gpio->domain)
484 return;
485
486 for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
487 irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));
488
489 irq_domain_remove(gpio->domain);
490 gpio->domain = NULL;
491}
492
493static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
Weike Chen3d2613c2014-09-17 09:18:39 -0700494 struct dwapb_port_property *pp,
Jamie Iles7779b3452014-02-25 17:01:01 -0600495 unsigned int offs)
496{
497 struct dwapb_gpio_port *port;
Jamie Iles7779b3452014-02-25 17:01:01 -0600498 void __iomem *dat, *set, *dirout;
499 int err;
500
Jamie Iles7779b3452014-02-25 17:01:01 -0600501 port = &gpio->ports[offs];
502 port->gpio = gpio;
Weike Chen1e960db2014-09-17 09:18:42 -0700503 port->idx = pp->idx;
504
505#ifdef CONFIG_PM_SLEEP
506 port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
507 if (!port->ctx)
508 return -ENOMEM;
509#endif
Jamie Iles7779b3452014-02-25 17:01:01 -0600510
Linus Walleij89f99fe2018-02-08 17:03:58 +0100511 dat = gpio->regs + GPIO_EXT_PORTA + (pp->idx * GPIO_EXT_PORT_STRIDE);
512 set = gpio->regs + GPIO_SWPORTA_DR + (pp->idx * GPIO_SWPORT_DR_STRIDE);
Jamie Iles7779b3452014-02-25 17:01:01 -0600513 dirout = gpio->regs + GPIO_SWPORTA_DDR +
Linus Walleij89f99fe2018-02-08 17:03:58 +0100514 (pp->idx * GPIO_SWPORT_DDR_STRIDE);
Jamie Iles7779b3452014-02-25 17:01:01 -0600515
Linus Walleij62c16232018-02-08 18:00:05 +0100516 /* This registers 32 GPIO lines per port */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100517 err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
Linus Walleijd97a1b52017-10-20 12:26:51 +0200518 NULL, 0);
Jamie Iles7779b3452014-02-25 17:01:01 -0600519 if (err) {
Jiang Qiue8159182016-04-28 17:32:01 +0800520 dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
521 port->idx);
Jamie Iles7779b3452014-02-25 17:01:01 -0600522 return err;
523 }
524
Weike Chen3d2613c2014-09-17 09:18:39 -0700525#ifdef CONFIG_OF_GPIO
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800526 port->gc.of_node = to_of_node(pp->fwnode);
Weike Chen3d2613c2014-09-17 09:18:39 -0700527#endif
Linus Walleij0f4630f2015-12-04 14:02:58 +0100528 port->gc.ngpio = pp->ngpio;
529 port->gc.base = pp->gpio_base;
Jamie Iles7779b3452014-02-25 17:01:01 -0600530
Weike Chen5d60d9e2014-09-17 09:18:41 -0700531 /* Only port A support debounce */
532 if (pp->idx == 0)
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300533 port->gc.set_config = dwapb_gpio_set_config;
Weike Chen5d60d9e2014-09-17 09:18:41 -0700534
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100535 if (pp->has_irq)
Weike Chen3d2613c2014-09-17 09:18:39 -0700536 dwapb_configure_irqs(gpio, port, pp);
Jamie Iles7779b3452014-02-25 17:01:01 -0600537
Linus Walleij0f4630f2015-12-04 14:02:58 +0100538 err = gpiochip_add_data(&port->gc, port);
Jamie Iles7779b3452014-02-25 17:01:01 -0600539 if (err)
Jiang Qiue8159182016-04-28 17:32:01 +0800540 dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
541 port->idx);
Jamie Iles7779b3452014-02-25 17:01:01 -0600542 else
543 port->is_registered = true;
544
Jiang Qiue6cb3482016-04-28 17:32:03 +0800545 /* Add GPIO-signaled ACPI event support */
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100546 if (pp->has_irq)
Jiang Qiue6cb3482016-04-28 17:32:03 +0800547 acpi_gpiochip_request_interrupts(&port->gc);
548
Jamie Iles7779b3452014-02-25 17:01:01 -0600549 return err;
550}
551
552static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
553{
554 unsigned int m;
555
556 for (m = 0; m < gpio->nr_ports; ++m)
557 if (gpio->ports[m].is_registered)
Linus Walleij0f4630f2015-12-04 14:02:58 +0100558 gpiochip_remove(&gpio->ports[m].gc);
Jamie Iles7779b3452014-02-25 17:01:01 -0600559}
560
Weike Chen3d2613c2014-09-17 09:18:39 -0700561static struct dwapb_platform_data *
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800562dwapb_gpio_get_pdata(struct device *dev)
Weike Chen3d2613c2014-09-17 09:18:39 -0700563{
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800564 struct fwnode_handle *fwnode;
Weike Chen3d2613c2014-09-17 09:18:39 -0700565 struct dwapb_platform_data *pdata;
566 struct dwapb_port_property *pp;
567 int nports;
Phil Edworthyda069d52018-05-23 09:52:44 +0100568 int i, j;
Weike Chen3d2613c2014-09-17 09:18:39 -0700569
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800570 nports = device_get_child_node_count(dev);
Weike Chen3d2613c2014-09-17 09:18:39 -0700571 if (nports == 0)
572 return ERR_PTR(-ENODEV);
573
Axel Linda9df932014-12-28 15:23:14 +0800574 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Weike Chen3d2613c2014-09-17 09:18:39 -0700575 if (!pdata)
576 return ERR_PTR(-ENOMEM);
577
Axel Linda9df932014-12-28 15:23:14 +0800578 pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
579 if (!pdata->properties)
Weike Chen3d2613c2014-09-17 09:18:39 -0700580 return ERR_PTR(-ENOMEM);
Weike Chen3d2613c2014-09-17 09:18:39 -0700581
582 pdata->nports = nports;
583
584 i = 0;
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800585 device_for_each_child_node(dev, fwnode) {
Phil Edworthyda069d52018-05-23 09:52:44 +0100586 struct device_node *np = NULL;
587
Weike Chen3d2613c2014-09-17 09:18:39 -0700588 pp = &pdata->properties[i++];
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800589 pp->fwnode = fwnode;
Weike Chen3d2613c2014-09-17 09:18:39 -0700590
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800591 if (fwnode_property_read_u32(fwnode, "reg", &pp->idx) ||
Weike Chen3d2613c2014-09-17 09:18:39 -0700592 pp->idx >= DWAPB_MAX_PORTS) {
Jiang Qiue8159182016-04-28 17:32:01 +0800593 dev_err(dev,
594 "missing/invalid port index for port%d\n", i);
Wei Yongjunbfab7c82016-07-10 02:17:36 +0000595 fwnode_handle_put(fwnode);
Weike Chen3d2613c2014-09-17 09:18:39 -0700596 return ERR_PTR(-EINVAL);
597 }
598
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800599 if (fwnode_property_read_u32(fwnode, "snps,nr-gpios",
Weike Chen3d2613c2014-09-17 09:18:39 -0700600 &pp->ngpio)) {
Jiang Qiue8159182016-04-28 17:32:01 +0800601 dev_info(dev,
602 "failed to get number of gpios for port%d\n",
603 i);
Weike Chen3d2613c2014-09-17 09:18:39 -0700604 pp->ngpio = 32;
605 }
606
Phil Edworthyda069d52018-05-23 09:52:44 +0100607 pp->irq_shared = false;
608 pp->gpio_base = -1;
609
Weike Chen3d2613c2014-09-17 09:18:39 -0700610 /*
611 * Only port A can provide interrupts in all configurations of
612 * the IP.
613 */
Phil Edworthyda069d52018-05-23 09:52:44 +0100614 if (pp->idx != 0)
615 continue;
616
617 if (dev->of_node && fwnode_property_read_bool(fwnode,
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800618 "interrupt-controller")) {
Phil Edworthyda069d52018-05-23 09:52:44 +0100619 np = to_of_node(fwnode);
Weike Chen3d2613c2014-09-17 09:18:39 -0700620 }
621
Phil Edworthyda069d52018-05-23 09:52:44 +0100622 for (j = 0; j < pp->ngpio; j++) {
623 pp->irq[j] = -ENXIO;
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100624
Phil Edworthyda069d52018-05-23 09:52:44 +0100625 if (np)
626 pp->irq[j] = of_irq_get(np, j);
627 else if (has_acpi_companion(dev))
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100628 pp->irq[j] = platform_get_irq(to_platform_device(dev), j);
Phil Edworthyda069d52018-05-23 09:52:44 +0100629
630 if (pp->irq[j] >= 0)
631 pp->has_irq = true;
Phil Edworthye6ca26a2018-04-26 17:19:47 +0100632 }
Jiang Qiue6cb3482016-04-28 17:32:03 +0800633
Phil Edworthyda069d52018-05-23 09:52:44 +0100634 if (!pp->has_irq)
635 dev_warn(dev, "no irq for port%d\n", pp->idx);
Weike Chen3d2613c2014-09-17 09:18:39 -0700636 }
637
638 return pdata;
639}
640
Hoan Trana72b8c42017-02-21 11:32:43 -0800641static const struct of_device_id dwapb_of_match[] = {
642 { .compatible = "snps,dw-apb-gpio", .data = (void *)0},
643 { .compatible = "apm,xgene-gpio-v2", .data = (void *)GPIO_REG_OFFSET_V2},
644 { /* Sentinel */ }
645};
646MODULE_DEVICE_TABLE(of, dwapb_of_match);
647
648static const struct acpi_device_id dwapb_acpi_match[] = {
649 {"HISI0181", 0},
650 {"APMC0D07", 0},
651 {"APMC0D81", GPIO_REG_OFFSET_V2},
652 { }
653};
654MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);
655
Jamie Iles7779b3452014-02-25 17:01:01 -0600656static int dwapb_gpio_probe(struct platform_device *pdev)
657{
Weike Chen3d2613c2014-09-17 09:18:39 -0700658 unsigned int i;
Jamie Iles7779b3452014-02-25 17:01:01 -0600659 struct dwapb_gpio *gpio;
Jamie Iles7779b3452014-02-25 17:01:01 -0600660 int err;
Weike Chen3d2613c2014-09-17 09:18:39 -0700661 struct device *dev = &pdev->dev;
662 struct dwapb_platform_data *pdata = dev_get_platdata(dev);
Jamie Iles7779b3452014-02-25 17:01:01 -0600663
Axel Linda9df932014-12-28 15:23:14 +0800664 if (!pdata) {
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800665 pdata = dwapb_gpio_get_pdata(dev);
Weike Chen3d2613c2014-09-17 09:18:39 -0700666 if (IS_ERR(pdata))
667 return PTR_ERR(pdata);
668 }
Jamie Iles7779b3452014-02-25 17:01:01 -0600669
Axel Linda9df932014-12-28 15:23:14 +0800670 if (!pdata->nports)
671 return -ENODEV;
Weike Chen3d2613c2014-09-17 09:18:39 -0700672
673 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
Axel Linda9df932014-12-28 15:23:14 +0800674 if (!gpio)
675 return -ENOMEM;
676
Weike Chen3d2613c2014-09-17 09:18:39 -0700677 gpio->dev = &pdev->dev;
678 gpio->nr_ports = pdata->nports;
679
Alan Tull07901a92017-10-11 11:34:44 -0500680 gpio->rst = devm_reset_control_get_optional_shared(dev, NULL);
681 if (IS_ERR(gpio->rst))
682 return PTR_ERR(gpio->rst);
683
684 reset_control_deassert(gpio->rst);
685
Weike Chen3d2613c2014-09-17 09:18:39 -0700686 gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
Jamie Iles7779b3452014-02-25 17:01:01 -0600687 sizeof(*gpio->ports), GFP_KERNEL);
Axel Linda9df932014-12-28 15:23:14 +0800688 if (!gpio->ports)
689 return -ENOMEM;
Jamie Iles7779b3452014-02-25 17:01:01 -0600690
Enrico Weigelt, metux IT consult2a7194e2019-03-11 19:54:47 +0100691 gpio->regs = devm_platform_ioremap_resource(pdev, 0);
Axel Linda9df932014-12-28 15:23:14 +0800692 if (IS_ERR(gpio->regs))
693 return PTR_ERR(gpio->regs);
Jamie Iles7779b3452014-02-25 17:01:01 -0600694
Serge Semin5c544c92020-03-23 22:54:00 +0300695 /* Optional bus and debounce clocks */
696 gpio->clks[0].id = "bus";
697 gpio->clks[1].id = "db";
698 err = devm_clk_bulk_get_optional(&pdev->dev, DWAPB_NR_CLOCKS,
699 gpio->clks);
700 if (err) {
701 dev_err(&pdev->dev, "Cannot get APB/Debounce clocks\n");
702 return err;
Serge Semin3ea8094c2020-03-23 22:53:59 +0300703 }
704
Serge Semin5c544c92020-03-23 22:54:00 +0300705 err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
Serge Semin3ea8094c2020-03-23 22:53:59 +0300706 if (err) {
Serge Semin5c544c92020-03-23 22:54:00 +0300707 dev_err(&pdev->dev, "Cannot enable APB/Debounce clocks\n");
Serge Semin3ea8094c2020-03-23 22:53:59 +0300708 return err;
Phil Edworthye6bf3772018-03-12 18:30:56 +0000709 }
710
Hoan Trana72b8c42017-02-21 11:32:43 -0800711 gpio->flags = 0;
712 if (dev->of_node) {
Thierry Reding7114b7b2018-04-30 09:38:09 +0200713 gpio->flags = (uintptr_t)of_device_get_match_data(dev);
Hoan Trana72b8c42017-02-21 11:32:43 -0800714 } else if (has_acpi_companion(dev)) {
715 const struct acpi_device_id *acpi_id;
716
717 acpi_id = acpi_match_device(dwapb_acpi_match, dev);
718 if (acpi_id) {
719 if (acpi_id->driver_data)
720 gpio->flags = acpi_id->driver_data;
721 }
722 }
723
Weike Chen3d2613c2014-09-17 09:18:39 -0700724 for (i = 0; i < gpio->nr_ports; i++) {
725 err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
Jamie Iles7779b3452014-02-25 17:01:01 -0600726 if (err)
727 goto out_unregister;
728 }
729 platform_set_drvdata(pdev, gpio);
730
Axel Linda9df932014-12-28 15:23:14 +0800731 return 0;
Jamie Iles7779b3452014-02-25 17:01:01 -0600732
733out_unregister:
734 dwapb_gpio_unregister(gpio);
735 dwapb_irq_teardown(gpio);
Serge Semin5c544c92020-03-23 22:54:00 +0300736 clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
Jamie Iles7779b3452014-02-25 17:01:01 -0600737
Jamie Iles7779b3452014-02-25 17:01:01 -0600738 return err;
739}
740
741static int dwapb_gpio_remove(struct platform_device *pdev)
742{
743 struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
744
745 dwapb_gpio_unregister(gpio);
746 dwapb_irq_teardown(gpio);
Alan Tull07901a92017-10-11 11:34:44 -0500747 reset_control_assert(gpio->rst);
Serge Semin5c544c92020-03-23 22:54:00 +0300748 clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
Jamie Iles7779b3452014-02-25 17:01:01 -0600749
750 return 0;
751}
752
Weike Chen1e960db2014-09-17 09:18:42 -0700753#ifdef CONFIG_PM_SLEEP
754static int dwapb_gpio_suspend(struct device *dev)
755{
Wolfram Sangdeb19ac2018-10-21 21:59:56 +0200756 struct dwapb_gpio *gpio = dev_get_drvdata(dev);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100757 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen1e960db2014-09-17 09:18:42 -0700758 unsigned long flags;
759 int i;
760
Linus Walleij0f4630f2015-12-04 14:02:58 +0100761 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700762 for (i = 0; i < gpio->nr_ports; i++) {
763 unsigned int offset;
764 unsigned int idx = gpio->ports[i].idx;
765 struct dwapb_context *ctx = gpio->ports[i].ctx;
766
Linus Walleij58a3b922014-09-24 13:30:24 +0200767 BUG_ON(!ctx);
Weike Chen1e960db2014-09-17 09:18:42 -0700768
Linus Walleij89f99fe2018-02-08 17:03:58 +0100769 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
Weike Chen1e960db2014-09-17 09:18:42 -0700770 ctx->dir = dwapb_read(gpio, offset);
771
Linus Walleij89f99fe2018-02-08 17:03:58 +0100772 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
Weike Chen1e960db2014-09-17 09:18:42 -0700773 ctx->data = dwapb_read(gpio, offset);
774
Linus Walleij89f99fe2018-02-08 17:03:58 +0100775 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
Weike Chen1e960db2014-09-17 09:18:42 -0700776 ctx->ext = dwapb_read(gpio, offset);
777
778 /* Only port A can provide interrupts */
779 if (idx == 0) {
780 ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
781 ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
782 ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
783 ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
784 ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
785
786 /* Mask out interrupts */
Hoan Tran6437c7b2017-09-08 15:41:15 -0700787 dwapb_write(gpio, GPIO_INTMASK,
788 0xffffffff & ~ctx->wake_en);
Weike Chen1e960db2014-09-17 09:18:42 -0700789 }
790 }
Linus Walleij0f4630f2015-12-04 14:02:58 +0100791 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700792
Serge Semin5c544c92020-03-23 22:54:00 +0300793 clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
Phil Edworthye6bf3772018-03-12 18:30:56 +0000794
Weike Chen1e960db2014-09-17 09:18:42 -0700795 return 0;
796}
797
798static int dwapb_gpio_resume(struct device *dev)
799{
Wolfram Sangdeb19ac2018-10-21 21:59:56 +0200800 struct dwapb_gpio *gpio = dev_get_drvdata(dev);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100801 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen1e960db2014-09-17 09:18:42 -0700802 unsigned long flags;
Serge Semin5c544c92020-03-23 22:54:00 +0300803 int i, err;
Weike Chen1e960db2014-09-17 09:18:42 -0700804
Serge Semin5c544c92020-03-23 22:54:00 +0300805 err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
806 if (err) {
807 dev_err(gpio->dev, "Cannot reenable APB/Debounce clocks\n");
808 return err;
809 }
Phil Edworthye6bf3772018-03-12 18:30:56 +0000810
Linus Walleij0f4630f2015-12-04 14:02:58 +0100811 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700812 for (i = 0; i < gpio->nr_ports; i++) {
813 unsigned int offset;
814 unsigned int idx = gpio->ports[i].idx;
815 struct dwapb_context *ctx = gpio->ports[i].ctx;
816
Linus Walleij58a3b922014-09-24 13:30:24 +0200817 BUG_ON(!ctx);
Weike Chen1e960db2014-09-17 09:18:42 -0700818
Linus Walleij89f99fe2018-02-08 17:03:58 +0100819 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
Weike Chen1e960db2014-09-17 09:18:42 -0700820 dwapb_write(gpio, offset, ctx->data);
821
Linus Walleij89f99fe2018-02-08 17:03:58 +0100822 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
Weike Chen1e960db2014-09-17 09:18:42 -0700823 dwapb_write(gpio, offset, ctx->dir);
824
Linus Walleij89f99fe2018-02-08 17:03:58 +0100825 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
Weike Chen1e960db2014-09-17 09:18:42 -0700826 dwapb_write(gpio, offset, ctx->ext);
827
828 /* Only port A can provide interrupts */
829 if (idx == 0) {
830 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
831 dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
832 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
833 dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
834 dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
835
836 /* Clear out spurious interrupts */
837 dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
838 }
839 }
Linus Walleij0f4630f2015-12-04 14:02:58 +0100840 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700841
842 return 0;
843}
844#endif
845
846static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
847 dwapb_gpio_resume);
848
Jamie Iles7779b3452014-02-25 17:01:01 -0600849static struct platform_driver dwapb_gpio_driver = {
850 .driver = {
Andy Shevchenkoc58220c2020-04-15 17:15:21 +0300851 .name = DWAPB_DRIVER_NAME,
Weike Chen1e960db2014-09-17 09:18:42 -0700852 .pm = &dwapb_gpio_pm_ops,
Jamie Iles7779b3452014-02-25 17:01:01 -0600853 .of_match_table = of_match_ptr(dwapb_of_match),
Jiang Qiue6cb3482016-04-28 17:32:03 +0800854 .acpi_match_table = ACPI_PTR(dwapb_acpi_match),
Jamie Iles7779b3452014-02-25 17:01:01 -0600855 },
856 .probe = dwapb_gpio_probe,
857 .remove = dwapb_gpio_remove,
858};
859
860module_platform_driver(dwapb_gpio_driver);
861
862MODULE_LICENSE("GPL");
863MODULE_AUTHOR("Jamie Iles");
864MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");
Andy Shevchenkoc58220c2020-04-15 17:15:21 +0300865MODULE_ALIAS("platform:" DWAPB_DRIVER_NAME);