blob: 710fa62dd5ae2649d4fb3524d35fd8871ba87409 [file] [log] [blame]
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +00001/*
2 * Broadcom specific AMBA
3 * GPIO driver
4 *
5 * Copyright 2011, Broadcom Corporation
6 * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
7 *
8 * Licensed under the GNU/GPL. See COPYING for details.
9 */
10
11#include <linux/gpio.h>
Rafał Miłecki29976092013-12-12 13:46:03 +010012#include <linux/irq.h>
13#include <linux/interrupt.h>
14#include <linux/irqdomain.h>
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000015#include <linux/export.h>
16#include <linux/bcma/bcma.h>
17
18#include "bcma_private.h"
19
20static inline struct bcma_drv_cc *bcma_gpio_get_cc(struct gpio_chip *chip)
21{
22 return container_of(chip, struct bcma_drv_cc, gpio);
23}
24
25static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
26{
27 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
28
29 return !!bcma_chipco_gpio_in(cc, 1 << gpio);
30}
31
32static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
33 int value)
34{
35 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
36
37 bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
38}
39
40static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
41{
42 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
43
44 bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
45 return 0;
46}
47
48static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
49 int value)
50{
51 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
52
53 bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
54 bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
55 return 0;
56}
57
58static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
59{
60 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
61
62 bcma_chipco_gpio_control(cc, 1 << gpio, 0);
63 /* clear pulldown */
64 bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
65 /* Set pullup */
66 bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
67
68 return 0;
69}
70
71static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
72{
73 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
74
75 /* clear pullup */
76 bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
77}
78
Rafał Miłecki29976092013-12-12 13:46:03 +010079#if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
Hauke Mehrtens8f1ca262013-01-26 21:39:44 +010080static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
81{
82 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
83
84 if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
Rafał Miłecki29976092013-12-12 13:46:03 +010085 return irq_find_mapping(cc->irq_domain, gpio);
Hauke Mehrtens8f1ca262013-01-26 21:39:44 +010086 else
87 return -EINVAL;
88}
89
Rafał Miłecki29976092013-12-12 13:46:03 +010090static void bcma_gpio_irq_unmask(struct irq_data *d)
91{
92 struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
93 int gpio = irqd_to_hwirq(d);
Hauke Mehrtens978e55d2014-01-02 19:01:08 +010094 u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
Rafał Miłecki29976092013-12-12 13:46:03 +010095
Hauke Mehrtens978e55d2014-01-02 19:01:08 +010096 bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
Rafał Miłecki29976092013-12-12 13:46:03 +010097 bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
98}
99
100static void bcma_gpio_irq_mask(struct irq_data *d)
101{
102 struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
103 int gpio = irqd_to_hwirq(d);
104
105 bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
106}
107
108static struct irq_chip bcma_gpio_irq_chip = {
109 .name = "BCMA-GPIO",
110 .irq_mask = bcma_gpio_irq_mask,
111 .irq_unmask = bcma_gpio_irq_unmask,
112};
113
114static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
115{
116 struct bcma_drv_cc *cc = dev_id;
117 u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
118 u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
119 u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
Rafał Miłeckid5ab2ad2014-01-13 20:05:17 +0100120 unsigned long irqs = (val ^ pol) & mask;
Rafał Miłecki29976092013-12-12 13:46:03 +0100121 int gpio;
122
123 if (!irqs)
124 return IRQ_NONE;
125
Rafał Miłeckid5ab2ad2014-01-13 20:05:17 +0100126 for_each_set_bit(gpio, &irqs, cc->gpio.ngpio)
Rafał Miłecki29976092013-12-12 13:46:03 +0100127 generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
128 bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
129
130 return IRQ_HANDLED;
131}
132
133static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
134{
135 struct gpio_chip *chip = &cc->gpio;
136 int gpio, hwirq, err;
137
138 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
139 return 0;
140
141 cc->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
142 &irq_domain_simple_ops, cc);
143 if (!cc->irq_domain) {
144 err = -ENODEV;
145 goto err_irq_domain;
146 }
147 for (gpio = 0; gpio < chip->ngpio; gpio++) {
148 int irq = irq_create_mapping(cc->irq_domain, gpio);
149
150 irq_set_chip_data(irq, cc);
151 irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
152 handle_simple_irq);
153 }
154
155 hwirq = bcma_core_irq(cc->core);
156 err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
157 cc);
158 if (err)
159 goto err_req_irq;
160
Hauke Mehrtens978e55d2014-01-02 19:01:08 +0100161 bcma_chipco_gpio_intmask(cc, ~0, 0);
Rafał Miłecki29976092013-12-12 13:46:03 +0100162 bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
163
164 return 0;
165
166err_req_irq:
167 for (gpio = 0; gpio < chip->ngpio; gpio++) {
168 int irq = irq_find_mapping(cc->irq_domain, gpio);
169
170 irq_dispose_mapping(irq);
171 }
172 irq_domain_remove(cc->irq_domain);
173err_irq_domain:
174 return err;
175}
176
177static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
178{
179 struct gpio_chip *chip = &cc->gpio;
180 int gpio;
181
182 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
183 return;
184
185 bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
186 free_irq(bcma_core_irq(cc->core), cc);
187 for (gpio = 0; gpio < chip->ngpio; gpio++) {
188 int irq = irq_find_mapping(cc->irq_domain, gpio);
189
190 irq_dispose_mapping(irq);
191 }
192 irq_domain_remove(cc->irq_domain);
193}
194#else
195static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
196{
197 return 0;
198}
199
200static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
201{
202}
203#endif
204
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000205int bcma_gpio_init(struct bcma_drv_cc *cc)
206{
207 struct gpio_chip *chip = &cc->gpio;
Rafał Miłecki29976092013-12-12 13:46:03 +0100208 int err;
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000209
210 chip->label = "bcma_gpio";
211 chip->owner = THIS_MODULE;
212 chip->request = bcma_gpio_request;
213 chip->free = bcma_gpio_free;
214 chip->get = bcma_gpio_get_value;
215 chip->set = bcma_gpio_set_value;
216 chip->direction_input = bcma_gpio_direction_input;
217 chip->direction_output = bcma_gpio_direction_output;
Rafał Miłecki29976092013-12-12 13:46:03 +0100218#if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
Hauke Mehrtens8f1ca262013-01-26 21:39:44 +0100219 chip->to_irq = bcma_gpio_to_irq;
Rafał Miłecki29976092013-12-12 13:46:03 +0100220#endif
Rafał Miłecki0f8ca012014-03-20 21:09:07 +0100221 switch (cc->core->bus->chipinfo.id) {
222 case BCMA_CHIP_ID_BCM5357:
Rafał Miłeckif0db59e2014-06-05 20:20:44 +0200223 case BCMA_CHIP_ID_BCM53572:
Rafał Miłecki0f8ca012014-03-20 21:09:07 +0100224 chip->ngpio = 32;
225 break;
226 default:
227 chip->ngpio = 16;
228 }
229
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000230 /* There is just one SoC in one device and its GPIO addresses should be
231 * deterministic to address them more easily. The other buses could get
232 * a random base number. */
233 if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
234 chip->base = 0;
235 else
236 chip->base = -1;
237
Rafał Miłecki29976092013-12-12 13:46:03 +0100238 err = bcma_gpio_irq_domain_init(cc);
239 if (err)
240 return err;
241
242 err = gpiochip_add(chip);
243 if (err) {
244 bcma_gpio_irq_domain_exit(cc);
245 return err;
246 }
247
248 return 0;
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000249}
Hauke Mehrtensc50ae942013-02-03 23:25:33 +0100250
251int bcma_gpio_unregister(struct bcma_drv_cc *cc)
252{
Rafał Miłecki29976092013-12-12 13:46:03 +0100253 bcma_gpio_irq_domain_exit(cc);
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200254 gpiochip_remove(&cc->gpio);
255 return 0;
Hauke Mehrtensc50ae942013-02-03 23:25:33 +0100256}