blob: f051c4552af570257b92bf5d7f80b2a706cbf721 [file] [log] [blame]
Jamie Iles7779b3452014-02-25 17:01:01 -06001/*
2 * Copyright (c) 2011 Jamie Iles
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * All enquiries to support@picochip.com
9 */
Jiang Qiue6cb3482016-04-28 17:32:03 +080010#include <linux/acpi.h>
Linus Walleij0f4630f2015-12-04 14:02:58 +010011#include <linux/gpio/driver.h>
12/* FIXME: for gpio_get_value(), replace this with direct register read */
13#include <linux/gpio.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060014#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/ioport.h>
19#include <linux/irq.h>
20#include <linux/irqdomain.h>
21#include <linux/module.h>
22#include <linux/of.h>
23#include <linux/of_address.h>
Hoan Trana72b8c42017-02-21 11:32:43 -080024#include <linux/of_device.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060025#include <linux/of_irq.h>
26#include <linux/platform_device.h>
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +080027#include <linux/property.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060028#include <linux/spinlock.h>
Weike Chen3d2613c2014-09-17 09:18:39 -070029#include <linux/platform_data/gpio-dwapb.h>
30#include <linux/slab.h>
Jamie Iles7779b3452014-02-25 17:01:01 -060031
Jiang Qiue6cb3482016-04-28 17:32:03 +080032#include "gpiolib.h"
33
Jamie Iles7779b3452014-02-25 17:01:01 -060034#define GPIO_SWPORTA_DR 0x00
35#define GPIO_SWPORTA_DDR 0x04
36#define GPIO_SWPORTB_DR 0x0c
37#define GPIO_SWPORTB_DDR 0x10
38#define GPIO_SWPORTC_DR 0x18
39#define GPIO_SWPORTC_DDR 0x1c
40#define GPIO_SWPORTD_DR 0x24
41#define GPIO_SWPORTD_DDR 0x28
42#define GPIO_INTEN 0x30
43#define GPIO_INTMASK 0x34
44#define GPIO_INTTYPE_LEVEL 0x38
45#define GPIO_INT_POLARITY 0x3c
46#define GPIO_INTSTATUS 0x40
Weike Chen5d60d9e2014-09-17 09:18:41 -070047#define GPIO_PORTA_DEBOUNCE 0x48
Jamie Iles7779b3452014-02-25 17:01:01 -060048#define GPIO_PORTA_EOI 0x4c
49#define GPIO_EXT_PORTA 0x50
50#define GPIO_EXT_PORTB 0x54
51#define GPIO_EXT_PORTC 0x58
52#define GPIO_EXT_PORTD 0x5c
53
54#define DWAPB_MAX_PORTS 4
55#define GPIO_EXT_PORT_SIZE (GPIO_EXT_PORTB - GPIO_EXT_PORTA)
56#define GPIO_SWPORT_DR_SIZE (GPIO_SWPORTB_DR - GPIO_SWPORTA_DR)
57#define GPIO_SWPORT_DDR_SIZE (GPIO_SWPORTB_DDR - GPIO_SWPORTA_DDR)
58
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
Jamie Iles7779b3452014-02-25 17:01:01 -060067struct dwapb_gpio;
68
Weike Chen1e960db2014-09-17 09:18:42 -070069#ifdef CONFIG_PM_SLEEP
70/* Store GPIO context across system-wide suspend/resume transitions */
71struct dwapb_context {
72 u32 data;
73 u32 dir;
74 u32 ext;
75 u32 int_en;
76 u32 int_mask;
77 u32 int_type;
78 u32 int_pol;
79 u32 int_deb;
80};
81#endif
82
Jamie Iles7779b3452014-02-25 17:01:01 -060083struct dwapb_gpio_port {
Linus Walleij0f4630f2015-12-04 14:02:58 +010084 struct gpio_chip gc;
Jamie Iles7779b3452014-02-25 17:01:01 -060085 bool is_registered;
86 struct dwapb_gpio *gpio;
Weike Chen1e960db2014-09-17 09:18:42 -070087#ifdef CONFIG_PM_SLEEP
88 struct dwapb_context *ctx;
89#endif
90 unsigned int idx;
Jamie Iles7779b3452014-02-25 17:01:01 -060091};
92
93struct dwapb_gpio {
94 struct device *dev;
95 void __iomem *regs;
96 struct dwapb_gpio_port *ports;
97 unsigned int nr_ports;
98 struct irq_domain *domain;
Hoan Trana72b8c42017-02-21 11:32:43 -080099 unsigned int flags;
Jamie Iles7779b3452014-02-25 17:01:01 -0600100};
101
Hoan Trana72b8c42017-02-21 11:32:43 -0800102static inline u32 gpio_reg_v2_convert(unsigned int offset)
103{
104 switch (offset) {
105 case GPIO_INTMASK:
106 return GPIO_INTMASK_V2;
107 case GPIO_INTTYPE_LEVEL:
108 return GPIO_INTTYPE_LEVEL_V2;
109 case GPIO_INT_POLARITY:
110 return GPIO_INT_POLARITY_V2;
111 case GPIO_INTSTATUS:
112 return GPIO_INTSTATUS_V2;
113 case GPIO_PORTA_EOI:
114 return GPIO_PORTA_EOI_V2;
115 }
116
117 return offset;
118}
119
120static inline u32 gpio_reg_convert(struct dwapb_gpio *gpio, unsigned int offset)
121{
122 if (gpio->flags & GPIO_REG_OFFSET_V2)
123 return gpio_reg_v2_convert(offset);
124
125 return offset;
126}
127
Weike Chen67809b92014-09-17 09:18:40 -0700128static inline u32 dwapb_read(struct dwapb_gpio *gpio, unsigned int offset)
129{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100130 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen67809b92014-09-17 09:18:40 -0700131 void __iomem *reg_base = gpio->regs;
132
Hoan Trana72b8c42017-02-21 11:32:43 -0800133 return gc->read_reg(reg_base + gpio_reg_convert(gpio, offset));
Weike Chen67809b92014-09-17 09:18:40 -0700134}
135
136static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
137 u32 val)
138{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100139 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen67809b92014-09-17 09:18:40 -0700140 void __iomem *reg_base = gpio->regs;
141
Hoan Trana72b8c42017-02-21 11:32:43 -0800142 gc->write_reg(reg_base + gpio_reg_convert(gpio, offset), val);
Weike Chen67809b92014-09-17 09:18:40 -0700143}
144
Jamie Iles7779b3452014-02-25 17:01:01 -0600145static int dwapb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
146{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100147 struct dwapb_gpio_port *port = gpiochip_get_data(gc);
Jamie Iles7779b3452014-02-25 17:01:01 -0600148 struct dwapb_gpio *gpio = port->gpio;
149
150 return irq_find_mapping(gpio->domain, offset);
151}
152
153static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
154{
Weike Chen67809b92014-09-17 09:18:40 -0700155 u32 v = dwapb_read(gpio, GPIO_INT_POLARITY);
Jamie Iles7779b3452014-02-25 17:01:01 -0600156
Linus Walleij0f4630f2015-12-04 14:02:58 +0100157 if (gpio_get_value(gpio->ports[0].gc.base + offs))
Jamie Iles7779b3452014-02-25 17:01:01 -0600158 v &= ~BIT(offs);
159 else
160 v |= BIT(offs);
161
Weike Chen67809b92014-09-17 09:18:40 -0700162 dwapb_write(gpio, GPIO_INT_POLARITY, v);
Jamie Iles7779b3452014-02-25 17:01:01 -0600163}
164
Weike Chen3d2613c2014-09-17 09:18:39 -0700165static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
Jamie Iles7779b3452014-02-25 17:01:01 -0600166{
Jisheng Zhang5664aa12017-04-13 17:46:39 +0800167 u32 irq_status = dwapb_read(gpio, GPIO_INTSTATUS);
Weike Chen3d2613c2014-09-17 09:18:39 -0700168 u32 ret = irq_status;
Jamie Iles7779b3452014-02-25 17:01:01 -0600169
170 while (irq_status) {
171 int hwirq = fls(irq_status) - 1;
172 int gpio_irq = irq_find_mapping(gpio->domain, hwirq);
173
174 generic_handle_irq(gpio_irq);
175 irq_status &= ~BIT(hwirq);
176
177 if ((irq_get_trigger_type(gpio_irq) & IRQ_TYPE_SENSE_MASK)
178 == IRQ_TYPE_EDGE_BOTH)
179 dwapb_toggle_trigger(gpio, hwirq);
180 }
181
Weike Chen3d2613c2014-09-17 09:18:39 -0700182 return ret;
183}
184
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200185static void dwapb_irq_handler(struct irq_desc *desc)
Weike Chen3d2613c2014-09-17 09:18:39 -0700186{
Jiang Liu476f8b42015-06-04 12:13:15 +0800187 struct dwapb_gpio *gpio = irq_desc_get_handler_data(desc);
Weike Chen3d2613c2014-09-17 09:18:39 -0700188 struct irq_chip *chip = irq_desc_get_chip(desc);
189
190 dwapb_do_irq(gpio);
191
Jamie Iles7779b3452014-02-25 17:01:01 -0600192 if (chip->irq_eoi)
193 chip->irq_eoi(irq_desc_get_irq_data(desc));
194}
195
196static void dwapb_irq_enable(struct irq_data *d)
197{
198 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
199 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100200 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600201 unsigned long flags;
202 u32 val;
203
Linus Walleij0f4630f2015-12-04 14:02:58 +0100204 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700205 val = dwapb_read(gpio, GPIO_INTEN);
Jamie Iles7779b3452014-02-25 17:01:01 -0600206 val |= BIT(d->hwirq);
Weike Chen67809b92014-09-17 09:18:40 -0700207 dwapb_write(gpio, GPIO_INTEN, val);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100208 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Jamie Iles7779b3452014-02-25 17:01:01 -0600209}
210
211static void dwapb_irq_disable(struct irq_data *d)
212{
213 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
214 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100215 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600216 unsigned long flags;
217 u32 val;
218
Linus Walleij0f4630f2015-12-04 14:02:58 +0100219 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700220 val = dwapb_read(gpio, GPIO_INTEN);
Jamie Iles7779b3452014-02-25 17:01:01 -0600221 val &= ~BIT(d->hwirq);
Weike Chen67809b92014-09-17 09:18:40 -0700222 dwapb_write(gpio, GPIO_INTEN, val);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100223 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Jamie Iles7779b3452014-02-25 17:01:01 -0600224}
225
Linus Walleij57ef0422014-03-14 18:16:20 +0100226static int dwapb_irq_reqres(struct irq_data *d)
Jamie Iles7779b3452014-02-25 17:01:01 -0600227{
228 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
229 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100230 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600231
Linus Walleij0f4630f2015-12-04 14:02:58 +0100232 if (gpiochip_lock_as_irq(gc, irqd_to_hwirq(d))) {
Jamie Iles7779b3452014-02-25 17:01:01 -0600233 dev_err(gpio->dev, "unable to lock HW IRQ %lu for IRQ\n",
234 irqd_to_hwirq(d));
Linus Walleij57ef0422014-03-14 18:16:20 +0100235 return -EINVAL;
236 }
Jamie Iles7779b3452014-02-25 17:01:01 -0600237 return 0;
238}
239
Linus Walleij57ef0422014-03-14 18:16:20 +0100240static void dwapb_irq_relres(struct irq_data *d)
Jamie Iles7779b3452014-02-25 17:01:01 -0600241{
242 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
243 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100244 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600245
Linus Walleij0f4630f2015-12-04 14:02:58 +0100246 gpiochip_unlock_as_irq(gc, irqd_to_hwirq(d));
Jamie Iles7779b3452014-02-25 17:01:01 -0600247}
248
249static int dwapb_irq_set_type(struct irq_data *d, u32 type)
250{
251 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
252 struct dwapb_gpio *gpio = igc->private;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100253 struct gpio_chip *gc = &gpio->ports[0].gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600254 int bit = d->hwirq;
255 unsigned long level, polarity, flags;
256
257 if (type & ~(IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
258 IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
259 return -EINVAL;
260
Linus Walleij0f4630f2015-12-04 14:02:58 +0100261 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700262 level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
263 polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
Jamie Iles7779b3452014-02-25 17:01:01 -0600264
265 switch (type) {
266 case IRQ_TYPE_EDGE_BOTH:
267 level |= BIT(bit);
268 dwapb_toggle_trigger(gpio, bit);
269 break;
270 case IRQ_TYPE_EDGE_RISING:
271 level |= BIT(bit);
272 polarity |= BIT(bit);
273 break;
274 case IRQ_TYPE_EDGE_FALLING:
275 level |= BIT(bit);
276 polarity &= ~BIT(bit);
277 break;
278 case IRQ_TYPE_LEVEL_HIGH:
279 level &= ~BIT(bit);
280 polarity |= BIT(bit);
281 break;
282 case IRQ_TYPE_LEVEL_LOW:
283 level &= ~BIT(bit);
284 polarity &= ~BIT(bit);
285 break;
286 }
287
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200288 irq_setup_alt_chip(d, type);
289
Weike Chen67809b92014-09-17 09:18:40 -0700290 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
291 dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100292 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Jamie Iles7779b3452014-02-25 17:01:01 -0600293
294 return 0;
295}
296
Weike Chen5d60d9e2014-09-17 09:18:41 -0700297static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
298 unsigned offset, unsigned debounce)
299{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100300 struct dwapb_gpio_port *port = gpiochip_get_data(gc);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700301 struct dwapb_gpio *gpio = port->gpio;
302 unsigned long flags, val_deb;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100303 unsigned long mask = gc->pin2mask(gc, offset);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700304
Linus Walleij0f4630f2015-12-04 14:02:58 +0100305 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700306
307 val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
308 if (debounce)
309 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb | mask);
310 else
311 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb & ~mask);
312
Linus Walleij0f4630f2015-12-04 14:02:58 +0100313 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700314
315 return 0;
316}
317
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300318static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
319 unsigned long config)
320{
321 u32 debounce;
322
323 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
324 return -ENOTSUPP;
325
326 debounce = pinconf_to_config_argument(config);
327 return dwapb_gpio_set_debounce(gc, offset, debounce);
328}
329
Weike Chen3d2613c2014-09-17 09:18:39 -0700330static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
331{
332 u32 worked;
333 struct dwapb_gpio *gpio = dev_id;
334
335 worked = dwapb_do_irq(gpio);
336
337 return worked ? IRQ_HANDLED : IRQ_NONE;
338}
339
Jamie Iles7779b3452014-02-25 17:01:01 -0600340static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
Weike Chen3d2613c2014-09-17 09:18:39 -0700341 struct dwapb_gpio_port *port,
342 struct dwapb_port_property *pp)
Jamie Iles7779b3452014-02-25 17:01:01 -0600343{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100344 struct gpio_chip *gc = &port->gc;
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800345 struct fwnode_handle *fwnode = pp->fwnode;
Weike Chen3d2613c2014-09-17 09:18:39 -0700346 struct irq_chip_generic *irq_gc = NULL;
Jamie Iles7779b3452014-02-25 17:01:01 -0600347 unsigned int hwirq, ngpio = gc->ngpio;
348 struct irq_chip_type *ct;
Weike Chen3d2613c2014-09-17 09:18:39 -0700349 int err, i;
Jamie Iles7779b3452014-02-25 17:01:01 -0600350
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800351 gpio->domain = irq_domain_create_linear(fwnode, ngpio,
352 &irq_generic_chip_ops, gpio);
Jamie Iles7779b3452014-02-25 17:01:01 -0600353 if (!gpio->domain)
354 return;
355
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200356 err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
Jamie Iles7779b3452014-02-25 17:01:01 -0600357 "gpio-dwapb", handle_level_irq,
358 IRQ_NOREQUEST, 0,
359 IRQ_GC_INIT_NESTED_LOCK);
360 if (err) {
361 dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
362 irq_domain_remove(gpio->domain);
363 gpio->domain = NULL;
364 return;
365 }
366
367 irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
368 if (!irq_gc) {
369 irq_domain_remove(gpio->domain);
370 gpio->domain = NULL;
371 return;
372 }
373
374 irq_gc->reg_base = gpio->regs;
375 irq_gc->private = gpio;
376
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200377 for (i = 0; i < 2; i++) {
378 ct = &irq_gc->chip_types[i];
379 ct->chip.irq_ack = irq_gc_ack_set_bit;
380 ct->chip.irq_mask = irq_gc_mask_set_bit;
381 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
382 ct->chip.irq_set_type = dwapb_irq_set_type;
383 ct->chip.irq_enable = dwapb_irq_enable;
384 ct->chip.irq_disable = dwapb_irq_disable;
385 ct->chip.irq_request_resources = dwapb_irq_reqres;
386 ct->chip.irq_release_resources = dwapb_irq_relres;
Hoan Trana72b8c42017-02-21 11:32:43 -0800387 ct->regs.ack = gpio_reg_convert(gpio, GPIO_PORTA_EOI);
388 ct->regs.mask = gpio_reg_convert(gpio, GPIO_INTMASK);
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200389 ct->type = IRQ_TYPE_LEVEL_MASK;
390 }
391
392 irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
393 irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
394 irq_gc->chip_types[1].handler = handle_edge_irq;
Jamie Iles7779b3452014-02-25 17:01:01 -0600395
Weike Chen3d2613c2014-09-17 09:18:39 -0700396 if (!pp->irq_shared) {
Thomas Gleixner6218b882015-06-21 20:16:05 +0200397 irq_set_chained_handler_and_data(pp->irq, dwapb_irq_handler,
398 gpio);
Weike Chen3d2613c2014-09-17 09:18:39 -0700399 } else {
400 /*
401 * Request a shared IRQ since where MFD would have devices
402 * using the same irq pin
403 */
404 err = devm_request_irq(gpio->dev, pp->irq,
405 dwapb_irq_handler_mfd,
406 IRQF_SHARED, "gpio-dwapb-mfd", gpio);
407 if (err) {
408 dev_err(gpio->dev, "error requesting IRQ\n");
409 irq_domain_remove(gpio->domain);
410 gpio->domain = NULL;
411 return;
412 }
413 }
Jamie Iles7779b3452014-02-25 17:01:01 -0600414
415 for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
416 irq_create_mapping(gpio->domain, hwirq);
417
Linus Walleij0f4630f2015-12-04 14:02:58 +0100418 port->gc.to_irq = dwapb_gpio_to_irq;
Jamie Iles7779b3452014-02-25 17:01:01 -0600419}
420
421static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
422{
423 struct dwapb_gpio_port *port = &gpio->ports[0];
Linus Walleij0f4630f2015-12-04 14:02:58 +0100424 struct gpio_chip *gc = &port->gc;
Jamie Iles7779b3452014-02-25 17:01:01 -0600425 unsigned int ngpio = gc->ngpio;
426 irq_hw_number_t hwirq;
427
428 if (!gpio->domain)
429 return;
430
431 for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
432 irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));
433
434 irq_domain_remove(gpio->domain);
435 gpio->domain = NULL;
436}
437
438static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
Weike Chen3d2613c2014-09-17 09:18:39 -0700439 struct dwapb_port_property *pp,
Jamie Iles7779b3452014-02-25 17:01:01 -0600440 unsigned int offs)
441{
442 struct dwapb_gpio_port *port;
Jamie Iles7779b3452014-02-25 17:01:01 -0600443 void __iomem *dat, *set, *dirout;
444 int err;
445
Jamie Iles7779b3452014-02-25 17:01:01 -0600446 port = &gpio->ports[offs];
447 port->gpio = gpio;
Weike Chen1e960db2014-09-17 09:18:42 -0700448 port->idx = pp->idx;
449
450#ifdef CONFIG_PM_SLEEP
451 port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
452 if (!port->ctx)
453 return -ENOMEM;
454#endif
Jamie Iles7779b3452014-02-25 17:01:01 -0600455
Weike Chen3d2613c2014-09-17 09:18:39 -0700456 dat = gpio->regs + GPIO_EXT_PORTA + (pp->idx * GPIO_EXT_PORT_SIZE);
457 set = gpio->regs + GPIO_SWPORTA_DR + (pp->idx * GPIO_SWPORT_DR_SIZE);
Jamie Iles7779b3452014-02-25 17:01:01 -0600458 dirout = gpio->regs + GPIO_SWPORTA_DDR +
Weike Chen3d2613c2014-09-17 09:18:39 -0700459 (pp->idx * GPIO_SWPORT_DDR_SIZE);
Jamie Iles7779b3452014-02-25 17:01:01 -0600460
Linus Walleij0f4630f2015-12-04 14:02:58 +0100461 err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
Jamie Iles7779b3452014-02-25 17:01:01 -0600462 NULL, false);
463 if (err) {
Jiang Qiue8159182016-04-28 17:32:01 +0800464 dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
465 port->idx);
Jamie Iles7779b3452014-02-25 17:01:01 -0600466 return err;
467 }
468
Weike Chen3d2613c2014-09-17 09:18:39 -0700469#ifdef CONFIG_OF_GPIO
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800470 port->gc.of_node = to_of_node(pp->fwnode);
Weike Chen3d2613c2014-09-17 09:18:39 -0700471#endif
Linus Walleij0f4630f2015-12-04 14:02:58 +0100472 port->gc.ngpio = pp->ngpio;
473 port->gc.base = pp->gpio_base;
Jamie Iles7779b3452014-02-25 17:01:01 -0600474
Weike Chen5d60d9e2014-09-17 09:18:41 -0700475 /* Only port A support debounce */
476 if (pp->idx == 0)
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300477 port->gc.set_config = dwapb_gpio_set_config;
Weike Chen5d60d9e2014-09-17 09:18:41 -0700478
Weike Chen3d2613c2014-09-17 09:18:39 -0700479 if (pp->irq)
480 dwapb_configure_irqs(gpio, port, pp);
Jamie Iles7779b3452014-02-25 17:01:01 -0600481
Linus Walleij0f4630f2015-12-04 14:02:58 +0100482 err = gpiochip_add_data(&port->gc, port);
Jamie Iles7779b3452014-02-25 17:01:01 -0600483 if (err)
Jiang Qiue8159182016-04-28 17:32:01 +0800484 dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
485 port->idx);
Jamie Iles7779b3452014-02-25 17:01:01 -0600486 else
487 port->is_registered = true;
488
Jiang Qiue6cb3482016-04-28 17:32:03 +0800489 /* Add GPIO-signaled ACPI event support */
490 if (pp->irq)
491 acpi_gpiochip_request_interrupts(&port->gc);
492
Jamie Iles7779b3452014-02-25 17:01:01 -0600493 return err;
494}
495
496static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
497{
498 unsigned int m;
499
500 for (m = 0; m < gpio->nr_ports; ++m)
501 if (gpio->ports[m].is_registered)
Linus Walleij0f4630f2015-12-04 14:02:58 +0100502 gpiochip_remove(&gpio->ports[m].gc);
Jamie Iles7779b3452014-02-25 17:01:01 -0600503}
504
Weike Chen3d2613c2014-09-17 09:18:39 -0700505static struct dwapb_platform_data *
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800506dwapb_gpio_get_pdata(struct device *dev)
Weike Chen3d2613c2014-09-17 09:18:39 -0700507{
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800508 struct fwnode_handle *fwnode;
Weike Chen3d2613c2014-09-17 09:18:39 -0700509 struct dwapb_platform_data *pdata;
510 struct dwapb_port_property *pp;
511 int nports;
512 int i;
513
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800514 nports = device_get_child_node_count(dev);
Weike Chen3d2613c2014-09-17 09:18:39 -0700515 if (nports == 0)
516 return ERR_PTR(-ENODEV);
517
Axel Linda9df932014-12-28 15:23:14 +0800518 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Weike Chen3d2613c2014-09-17 09:18:39 -0700519 if (!pdata)
520 return ERR_PTR(-ENOMEM);
521
Axel Linda9df932014-12-28 15:23:14 +0800522 pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
523 if (!pdata->properties)
Weike Chen3d2613c2014-09-17 09:18:39 -0700524 return ERR_PTR(-ENOMEM);
Weike Chen3d2613c2014-09-17 09:18:39 -0700525
526 pdata->nports = nports;
527
528 i = 0;
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800529 device_for_each_child_node(dev, fwnode) {
Weike Chen3d2613c2014-09-17 09:18:39 -0700530 pp = &pdata->properties[i++];
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800531 pp->fwnode = fwnode;
Weike Chen3d2613c2014-09-17 09:18:39 -0700532
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800533 if (fwnode_property_read_u32(fwnode, "reg", &pp->idx) ||
Weike Chen3d2613c2014-09-17 09:18:39 -0700534 pp->idx >= DWAPB_MAX_PORTS) {
Jiang Qiue8159182016-04-28 17:32:01 +0800535 dev_err(dev,
536 "missing/invalid port index for port%d\n", i);
Wei Yongjunbfab7c82016-07-10 02:17:36 +0000537 fwnode_handle_put(fwnode);
Weike Chen3d2613c2014-09-17 09:18:39 -0700538 return ERR_PTR(-EINVAL);
539 }
540
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800541 if (fwnode_property_read_u32(fwnode, "snps,nr-gpios",
Weike Chen3d2613c2014-09-17 09:18:39 -0700542 &pp->ngpio)) {
Jiang Qiue8159182016-04-28 17:32:01 +0800543 dev_info(dev,
544 "failed to get number of gpios for port%d\n",
545 i);
Weike Chen3d2613c2014-09-17 09:18:39 -0700546 pp->ngpio = 32;
547 }
548
549 /*
550 * Only port A can provide interrupts in all configurations of
551 * the IP.
552 */
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800553 if (dev->of_node && pp->idx == 0 &&
554 fwnode_property_read_bool(fwnode,
555 "interrupt-controller")) {
556 pp->irq = irq_of_parse_and_map(to_of_node(fwnode), 0);
Jiang Qiue8159182016-04-28 17:32:01 +0800557 if (!pp->irq)
558 dev_warn(dev, "no irq for port%d\n", pp->idx);
Weike Chen3d2613c2014-09-17 09:18:39 -0700559 }
560
Jiang Qiue6cb3482016-04-28 17:32:03 +0800561 if (has_acpi_companion(dev) && pp->idx == 0)
562 pp->irq = platform_get_irq(to_platform_device(dev), 0);
563
Weike Chen3d2613c2014-09-17 09:18:39 -0700564 pp->irq_shared = false;
565 pp->gpio_base = -1;
Weike Chen3d2613c2014-09-17 09:18:39 -0700566 }
567
568 return pdata;
569}
570
Hoan Trana72b8c42017-02-21 11:32:43 -0800571static const struct of_device_id dwapb_of_match[] = {
572 { .compatible = "snps,dw-apb-gpio", .data = (void *)0},
573 { .compatible = "apm,xgene-gpio-v2", .data = (void *)GPIO_REG_OFFSET_V2},
574 { /* Sentinel */ }
575};
576MODULE_DEVICE_TABLE(of, dwapb_of_match);
577
578static const struct acpi_device_id dwapb_acpi_match[] = {
579 {"HISI0181", 0},
580 {"APMC0D07", 0},
581 {"APMC0D81", GPIO_REG_OFFSET_V2},
582 { }
583};
584MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);
585
Jamie Iles7779b3452014-02-25 17:01:01 -0600586static int dwapb_gpio_probe(struct platform_device *pdev)
587{
Weike Chen3d2613c2014-09-17 09:18:39 -0700588 unsigned int i;
Jamie Iles7779b3452014-02-25 17:01:01 -0600589 struct resource *res;
590 struct dwapb_gpio *gpio;
Jamie Iles7779b3452014-02-25 17:01:01 -0600591 int err;
Weike Chen3d2613c2014-09-17 09:18:39 -0700592 struct device *dev = &pdev->dev;
593 struct dwapb_platform_data *pdata = dev_get_platdata(dev);
Jamie Iles7779b3452014-02-25 17:01:01 -0600594
Axel Linda9df932014-12-28 15:23:14 +0800595 if (!pdata) {
Jiang Qiu4ba8cfa2016-04-28 17:32:02 +0800596 pdata = dwapb_gpio_get_pdata(dev);
Weike Chen3d2613c2014-09-17 09:18:39 -0700597 if (IS_ERR(pdata))
598 return PTR_ERR(pdata);
599 }
Jamie Iles7779b3452014-02-25 17:01:01 -0600600
Axel Linda9df932014-12-28 15:23:14 +0800601 if (!pdata->nports)
602 return -ENODEV;
Weike Chen3d2613c2014-09-17 09:18:39 -0700603
604 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
Axel Linda9df932014-12-28 15:23:14 +0800605 if (!gpio)
606 return -ENOMEM;
607
Weike Chen3d2613c2014-09-17 09:18:39 -0700608 gpio->dev = &pdev->dev;
609 gpio->nr_ports = pdata->nports;
610
611 gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
Jamie Iles7779b3452014-02-25 17:01:01 -0600612 sizeof(*gpio->ports), GFP_KERNEL);
Axel Linda9df932014-12-28 15:23:14 +0800613 if (!gpio->ports)
614 return -ENOMEM;
Jamie Iles7779b3452014-02-25 17:01:01 -0600615
616 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
617 gpio->regs = devm_ioremap_resource(&pdev->dev, res);
Axel Linda9df932014-12-28 15:23:14 +0800618 if (IS_ERR(gpio->regs))
619 return PTR_ERR(gpio->regs);
Jamie Iles7779b3452014-02-25 17:01:01 -0600620
Hoan Trana72b8c42017-02-21 11:32:43 -0800621 gpio->flags = 0;
622 if (dev->of_node) {
623 const struct of_device_id *of_devid;
624
625 of_devid = of_match_device(dwapb_of_match, dev);
626 if (of_devid) {
627 if (of_devid->data)
628 gpio->flags = (uintptr_t)of_devid->data;
629 }
630 } else if (has_acpi_companion(dev)) {
631 const struct acpi_device_id *acpi_id;
632
633 acpi_id = acpi_match_device(dwapb_acpi_match, dev);
634 if (acpi_id) {
635 if (acpi_id->driver_data)
636 gpio->flags = acpi_id->driver_data;
637 }
638 }
639
Weike Chen3d2613c2014-09-17 09:18:39 -0700640 for (i = 0; i < gpio->nr_ports; i++) {
641 err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
Jamie Iles7779b3452014-02-25 17:01:01 -0600642 if (err)
643 goto out_unregister;
644 }
645 platform_set_drvdata(pdev, gpio);
646
Axel Linda9df932014-12-28 15:23:14 +0800647 return 0;
Jamie Iles7779b3452014-02-25 17:01:01 -0600648
649out_unregister:
650 dwapb_gpio_unregister(gpio);
651 dwapb_irq_teardown(gpio);
652
Jamie Iles7779b3452014-02-25 17:01:01 -0600653 return err;
654}
655
656static int dwapb_gpio_remove(struct platform_device *pdev)
657{
658 struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
659
660 dwapb_gpio_unregister(gpio);
661 dwapb_irq_teardown(gpio);
662
663 return 0;
664}
665
Weike Chen1e960db2014-09-17 09:18:42 -0700666#ifdef CONFIG_PM_SLEEP
667static int dwapb_gpio_suspend(struct device *dev)
668{
669 struct platform_device *pdev = to_platform_device(dev);
670 struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100671 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen1e960db2014-09-17 09:18:42 -0700672 unsigned long flags;
673 int i;
674
Linus Walleij0f4630f2015-12-04 14:02:58 +0100675 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700676 for (i = 0; i < gpio->nr_ports; i++) {
677 unsigned int offset;
678 unsigned int idx = gpio->ports[i].idx;
679 struct dwapb_context *ctx = gpio->ports[i].ctx;
680
Linus Walleij58a3b922014-09-24 13:30:24 +0200681 BUG_ON(!ctx);
Weike Chen1e960db2014-09-17 09:18:42 -0700682
683 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
684 ctx->dir = dwapb_read(gpio, offset);
685
686 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
687 ctx->data = dwapb_read(gpio, offset);
688
689 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
690 ctx->ext = dwapb_read(gpio, offset);
691
692 /* Only port A can provide interrupts */
693 if (idx == 0) {
694 ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
695 ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
696 ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
697 ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
698 ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
699
700 /* Mask out interrupts */
701 dwapb_write(gpio, GPIO_INTMASK, 0xffffffff);
702 }
703 }
Linus Walleij0f4630f2015-12-04 14:02:58 +0100704 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700705
706 return 0;
707}
708
709static int dwapb_gpio_resume(struct device *dev)
710{
711 struct platform_device *pdev = to_platform_device(dev);
712 struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100713 struct gpio_chip *gc = &gpio->ports[0].gc;
Weike Chen1e960db2014-09-17 09:18:42 -0700714 unsigned long flags;
715 int i;
716
Linus Walleij0f4630f2015-12-04 14:02:58 +0100717 spin_lock_irqsave(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700718 for (i = 0; i < gpio->nr_ports; i++) {
719 unsigned int offset;
720 unsigned int idx = gpio->ports[i].idx;
721 struct dwapb_context *ctx = gpio->ports[i].ctx;
722
Linus Walleij58a3b922014-09-24 13:30:24 +0200723 BUG_ON(!ctx);
Weike Chen1e960db2014-09-17 09:18:42 -0700724
725 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
726 dwapb_write(gpio, offset, ctx->data);
727
728 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
729 dwapb_write(gpio, offset, ctx->dir);
730
731 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
732 dwapb_write(gpio, offset, ctx->ext);
733
734 /* Only port A can provide interrupts */
735 if (idx == 0) {
736 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
737 dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
738 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
739 dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
740 dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
741
742 /* Clear out spurious interrupts */
743 dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
744 }
745 }
Linus Walleij0f4630f2015-12-04 14:02:58 +0100746 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Weike Chen1e960db2014-09-17 09:18:42 -0700747
748 return 0;
749}
750#endif
751
752static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
753 dwapb_gpio_resume);
754
Jamie Iles7779b3452014-02-25 17:01:01 -0600755static struct platform_driver dwapb_gpio_driver = {
756 .driver = {
757 .name = "gpio-dwapb",
Weike Chen1e960db2014-09-17 09:18:42 -0700758 .pm = &dwapb_gpio_pm_ops,
Jamie Iles7779b3452014-02-25 17:01:01 -0600759 .of_match_table = of_match_ptr(dwapb_of_match),
Jiang Qiue6cb3482016-04-28 17:32:03 +0800760 .acpi_match_table = ACPI_PTR(dwapb_acpi_match),
Jamie Iles7779b3452014-02-25 17:01:01 -0600761 },
762 .probe = dwapb_gpio_probe,
763 .remove = dwapb_gpio_remove,
764};
765
766module_platform_driver(dwapb_gpio_driver);
767
768MODULE_LICENSE("GPL");
769MODULE_AUTHOR("Jamie Iles");
770MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");