blob: 2de3896489c84eb764e0e47b75819587f0c184be [file] [log] [blame]
Hauke Mehrtensec43b082012-11-20 22:24:33 +00001/*
2 * Sonics Silicon Backplane
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
Michael Büschb8b60692018-07-31 21:56:38 +020011#include "ssb_private.h"
12
Linus Walleij2d4443b2015-12-08 23:00:46 +010013#include <linux/gpio/driver.h>
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +010014#include <linux/irq.h>
15#include <linux/interrupt.h>
16#include <linux/irqdomain.h>
Hauke Mehrtensec43b082012-11-20 22:24:33 +000017#include <linux/export.h>
18#include <linux/ssb/ssb.h>
19
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +010020
21/**************************************************
22 * Shared
23 **************************************************/
24
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +010025#if IS_ENABLED(CONFIG_SSB_EMBEDDED)
Hugh Sipièrec683ffe2016-06-04 17:17:01 +010026static int ssb_gpio_to_irq(struct gpio_chip *chip, unsigned int gpio)
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +010027{
Linus Walleij2d4443b2015-12-08 23:00:46 +010028 struct ssb_bus *bus = gpiochip_get_data(chip);
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +010029
30 if (bus->bustype == SSB_BUSTYPE_SSB)
31 return irq_find_mapping(bus->irq_domain, gpio);
32 else
33 return -EINVAL;
34}
35#endif
36
37/**************************************************
38 * ChipCommon
39 **************************************************/
40
Hugh Sipièrec683ffe2016-06-04 17:17:01 +010041static int ssb_gpio_chipco_get_value(struct gpio_chip *chip, unsigned int gpio)
Hauke Mehrtensec43b082012-11-20 22:24:33 +000042{
Linus Walleij2d4443b2015-12-08 23:00:46 +010043 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +000044
45 return !!ssb_chipco_gpio_in(&bus->chipco, 1 << gpio);
46}
47
Hugh Sipièrec683ffe2016-06-04 17:17:01 +010048static void ssb_gpio_chipco_set_value(struct gpio_chip *chip, unsigned int gpio,
Hauke Mehrtensec43b082012-11-20 22:24:33 +000049 int value)
50{
Linus Walleij2d4443b2015-12-08 23:00:46 +010051 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +000052
53 ssb_chipco_gpio_out(&bus->chipco, 1 << gpio, value ? 1 << gpio : 0);
54}
55
56static int ssb_gpio_chipco_direction_input(struct gpio_chip *chip,
Hugh Sipièrec683ffe2016-06-04 17:17:01 +010057 unsigned int gpio)
Hauke Mehrtensec43b082012-11-20 22:24:33 +000058{
Linus Walleij2d4443b2015-12-08 23:00:46 +010059 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +000060
61 ssb_chipco_gpio_outen(&bus->chipco, 1 << gpio, 0);
62 return 0;
63}
64
65static int ssb_gpio_chipco_direction_output(struct gpio_chip *chip,
Hugh Sipièrec683ffe2016-06-04 17:17:01 +010066 unsigned int gpio, int value)
Hauke Mehrtensec43b082012-11-20 22:24:33 +000067{
Linus Walleij2d4443b2015-12-08 23:00:46 +010068 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +000069
70 ssb_chipco_gpio_outen(&bus->chipco, 1 << gpio, 1 << gpio);
71 ssb_chipco_gpio_out(&bus->chipco, 1 << gpio, value ? 1 << gpio : 0);
72 return 0;
73}
74
Hugh Sipièrec683ffe2016-06-04 17:17:01 +010075static int ssb_gpio_chipco_request(struct gpio_chip *chip, unsigned int gpio)
Hauke Mehrtensec43b082012-11-20 22:24:33 +000076{
Linus Walleij2d4443b2015-12-08 23:00:46 +010077 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +000078
79 ssb_chipco_gpio_control(&bus->chipco, 1 << gpio, 0);
80 /* clear pulldown */
81 ssb_chipco_gpio_pulldown(&bus->chipco, 1 << gpio, 0);
82 /* Set pullup */
83 ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 1 << gpio);
84
85 return 0;
86}
87
Hugh Sipièrec683ffe2016-06-04 17:17:01 +010088static void ssb_gpio_chipco_free(struct gpio_chip *chip, unsigned int gpio)
Hauke Mehrtensec43b082012-11-20 22:24:33 +000089{
Linus Walleij2d4443b2015-12-08 23:00:46 +010090 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +000091
92 /* clear pullup */
93 ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 0);
94}
95
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +010096#if IS_ENABLED(CONFIG_SSB_EMBEDDED)
97static void ssb_gpio_irq_chipco_mask(struct irq_data *d)
Hauke Mehrtensa6ca2e12013-01-26 21:38:35 +010098{
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +010099 struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
100 int gpio = irqd_to_hwirq(d);
Hauke Mehrtensa6ca2e12013-01-26 21:38:35 +0100101
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100102 ssb_chipco_gpio_intmask(&bus->chipco, BIT(gpio), 0);
Hauke Mehrtensa6ca2e12013-01-26 21:38:35 +0100103}
104
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100105static void ssb_gpio_irq_chipco_unmask(struct irq_data *d)
106{
107 struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
108 int gpio = irqd_to_hwirq(d);
109 u32 val = ssb_chipco_gpio_in(&bus->chipco, BIT(gpio));
110
111 ssb_chipco_gpio_polarity(&bus->chipco, BIT(gpio), val);
112 ssb_chipco_gpio_intmask(&bus->chipco, BIT(gpio), BIT(gpio));
113}
114
115static struct irq_chip ssb_gpio_irq_chipco_chip = {
116 .name = "SSB-GPIO-CC",
117 .irq_mask = ssb_gpio_irq_chipco_mask,
118 .irq_unmask = ssb_gpio_irq_chipco_unmask,
119};
120
121static irqreturn_t ssb_gpio_irq_chipco_handler(int irq, void *dev_id)
122{
123 struct ssb_bus *bus = dev_id;
124 struct ssb_chipcommon *chipco = &bus->chipco;
125 u32 val = chipco_read32(chipco, SSB_CHIPCO_GPIOIN);
126 u32 mask = chipco_read32(chipco, SSB_CHIPCO_GPIOIRQ);
127 u32 pol = chipco_read32(chipco, SSB_CHIPCO_GPIOPOL);
128 unsigned long irqs = (val ^ pol) & mask;
129 int gpio;
130
131 if (!irqs)
132 return IRQ_NONE;
133
134 for_each_set_bit(gpio, &irqs, bus->gpio.ngpio)
135 generic_handle_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
136 ssb_chipco_gpio_polarity(chipco, irqs, val & irqs);
137
138 return IRQ_HANDLED;
139}
140
141static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus *bus)
142{
143 struct ssb_chipcommon *chipco = &bus->chipco;
144 struct gpio_chip *chip = &bus->gpio;
145 int gpio, hwirq, err;
146
147 if (bus->bustype != SSB_BUSTYPE_SSB)
148 return 0;
149
150 bus->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
151 &irq_domain_simple_ops, chipco);
152 if (!bus->irq_domain) {
153 err = -ENODEV;
154 goto err_irq_domain;
155 }
156 for (gpio = 0; gpio < chip->ngpio; gpio++) {
157 int irq = irq_create_mapping(bus->irq_domain, gpio);
158
159 irq_set_chip_data(irq, bus);
160 irq_set_chip_and_handler(irq, &ssb_gpio_irq_chipco_chip,
161 handle_simple_irq);
162 }
163
164 hwirq = ssb_mips_irq(bus->chipco.dev) + 2;
165 err = request_irq(hwirq, ssb_gpio_irq_chipco_handler, IRQF_SHARED,
166 "gpio", bus);
167 if (err)
168 goto err_req_irq;
169
170 ssb_chipco_gpio_intmask(&bus->chipco, ~0, 0);
171 chipco_set32(chipco, SSB_CHIPCO_IRQMASK, SSB_CHIPCO_IRQ_GPIO);
172
173 return 0;
174
175err_req_irq:
176 for (gpio = 0; gpio < chip->ngpio; gpio++) {
177 int irq = irq_find_mapping(bus->irq_domain, gpio);
178
179 irq_dispose_mapping(irq);
180 }
181 irq_domain_remove(bus->irq_domain);
182err_irq_domain:
183 return err;
184}
185
186static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus *bus)
187{
188 struct ssb_chipcommon *chipco = &bus->chipco;
189 struct gpio_chip *chip = &bus->gpio;
190 int gpio;
191
192 if (bus->bustype != SSB_BUSTYPE_SSB)
193 return;
194
195 chipco_mask32(chipco, SSB_CHIPCO_IRQMASK, ~SSB_CHIPCO_IRQ_GPIO);
196 free_irq(ssb_mips_irq(bus->chipco.dev) + 2, chipco);
197 for (gpio = 0; gpio < chip->ngpio; gpio++) {
198 int irq = irq_find_mapping(bus->irq_domain, gpio);
199
200 irq_dispose_mapping(irq);
201 }
202 irq_domain_remove(bus->irq_domain);
203}
204#else
205static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus *bus)
206{
207 return 0;
208}
209
210static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus *bus)
211{
212}
213#endif
214
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000215static int ssb_gpio_chipco_init(struct ssb_bus *bus)
216{
217 struct gpio_chip *chip = &bus->gpio;
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100218 int err;
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000219
220 chip->label = "ssb_chipco_gpio";
221 chip->owner = THIS_MODULE;
222 chip->request = ssb_gpio_chipco_request;
223 chip->free = ssb_gpio_chipco_free;
224 chip->get = ssb_gpio_chipco_get_value;
225 chip->set = ssb_gpio_chipco_set_value;
226 chip->direction_input = ssb_gpio_chipco_direction_input;
227 chip->direction_output = ssb_gpio_chipco_direction_output;
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100228#if IS_ENABLED(CONFIG_SSB_EMBEDDED)
229 chip->to_irq = ssb_gpio_to_irq;
230#endif
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000231 chip->ngpio = 16;
232 /* There is just one SoC in one device and its GPIO addresses should be
233 * deterministic to address them more easily. The other buses could get
Shubhankar Kuranagatti5615eb52021-04-28 21:37:47 +0530234 * a random base number.
235 */
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000236 if (bus->bustype == SSB_BUSTYPE_SSB)
237 chip->base = 0;
238 else
239 chip->base = -1;
240
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100241 err = ssb_gpio_irq_chipco_domain_init(bus);
242 if (err)
243 return err;
244
Linus Walleij2d4443b2015-12-08 23:00:46 +0100245 err = gpiochip_add_data(chip, bus);
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100246 if (err) {
247 ssb_gpio_irq_chipco_domain_exit(bus);
248 return err;
249 }
250
251 return 0;
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000252}
253
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100254/**************************************************
255 * EXTIF
256 **************************************************/
257
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000258#ifdef CONFIG_SSB_DRIVER_EXTIF
259
Hugh Sipièrec683ffe2016-06-04 17:17:01 +0100260static int ssb_gpio_extif_get_value(struct gpio_chip *chip, unsigned int gpio)
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000261{
Linus Walleij2d4443b2015-12-08 23:00:46 +0100262 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000263
264 return !!ssb_extif_gpio_in(&bus->extif, 1 << gpio);
265}
266
Hugh Sipièrec683ffe2016-06-04 17:17:01 +0100267static void ssb_gpio_extif_set_value(struct gpio_chip *chip, unsigned int gpio,
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000268 int value)
269{
Linus Walleij2d4443b2015-12-08 23:00:46 +0100270 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000271
272 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
273}
274
275static int ssb_gpio_extif_direction_input(struct gpio_chip *chip,
Hugh Sipièrec683ffe2016-06-04 17:17:01 +0100276 unsigned int gpio)
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000277{
Linus Walleij2d4443b2015-12-08 23:00:46 +0100278 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000279
280 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 0);
281 return 0;
282}
283
284static int ssb_gpio_extif_direction_output(struct gpio_chip *chip,
Hugh Sipièrec683ffe2016-06-04 17:17:01 +0100285 unsigned int gpio, int value)
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000286{
Linus Walleij2d4443b2015-12-08 23:00:46 +0100287 struct ssb_bus *bus = gpiochip_get_data(chip);
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000288
289 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 1 << gpio);
290 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
291 return 0;
292}
293
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100294#if IS_ENABLED(CONFIG_SSB_EMBEDDED)
295static void ssb_gpio_irq_extif_mask(struct irq_data *d)
Hauke Mehrtensa6ca2e12013-01-26 21:38:35 +0100296{
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100297 struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
298 int gpio = irqd_to_hwirq(d);
Hauke Mehrtensa6ca2e12013-01-26 21:38:35 +0100299
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100300 ssb_extif_gpio_intmask(&bus->extif, BIT(gpio), 0);
Hauke Mehrtensa6ca2e12013-01-26 21:38:35 +0100301}
302
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100303static void ssb_gpio_irq_extif_unmask(struct irq_data *d)
304{
305 struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
306 int gpio = irqd_to_hwirq(d);
307 u32 val = ssb_extif_gpio_in(&bus->extif, BIT(gpio));
308
309 ssb_extif_gpio_polarity(&bus->extif, BIT(gpio), val);
310 ssb_extif_gpio_intmask(&bus->extif, BIT(gpio), BIT(gpio));
311}
312
313static struct irq_chip ssb_gpio_irq_extif_chip = {
314 .name = "SSB-GPIO-EXTIF",
315 .irq_mask = ssb_gpio_irq_extif_mask,
316 .irq_unmask = ssb_gpio_irq_extif_unmask,
317};
318
319static irqreturn_t ssb_gpio_irq_extif_handler(int irq, void *dev_id)
320{
321 struct ssb_bus *bus = dev_id;
322 struct ssb_extif *extif = &bus->extif;
323 u32 val = ssb_read32(extif->dev, SSB_EXTIF_GPIO_IN);
324 u32 mask = ssb_read32(extif->dev, SSB_EXTIF_GPIO_INTMASK);
325 u32 pol = ssb_read32(extif->dev, SSB_EXTIF_GPIO_INTPOL);
326 unsigned long irqs = (val ^ pol) & mask;
327 int gpio;
328
329 if (!irqs)
330 return IRQ_NONE;
331
332 for_each_set_bit(gpio, &irqs, bus->gpio.ngpio)
333 generic_handle_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
334 ssb_extif_gpio_polarity(extif, irqs, val & irqs);
335
336 return IRQ_HANDLED;
337}
338
339static int ssb_gpio_irq_extif_domain_init(struct ssb_bus *bus)
340{
341 struct ssb_extif *extif = &bus->extif;
342 struct gpio_chip *chip = &bus->gpio;
343 int gpio, hwirq, err;
344
345 if (bus->bustype != SSB_BUSTYPE_SSB)
346 return 0;
347
348 bus->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
349 &irq_domain_simple_ops, extif);
350 if (!bus->irq_domain) {
351 err = -ENODEV;
352 goto err_irq_domain;
353 }
354 for (gpio = 0; gpio < chip->ngpio; gpio++) {
355 int irq = irq_create_mapping(bus->irq_domain, gpio);
356
357 irq_set_chip_data(irq, bus);
358 irq_set_chip_and_handler(irq, &ssb_gpio_irq_extif_chip,
359 handle_simple_irq);
360 }
361
362 hwirq = ssb_mips_irq(bus->extif.dev) + 2;
363 err = request_irq(hwirq, ssb_gpio_irq_extif_handler, IRQF_SHARED,
364 "gpio", bus);
365 if (err)
366 goto err_req_irq;
367
368 ssb_extif_gpio_intmask(&bus->extif, ~0, 0);
369
370 return 0;
371
372err_req_irq:
373 for (gpio = 0; gpio < chip->ngpio; gpio++) {
374 int irq = irq_find_mapping(bus->irq_domain, gpio);
375
376 irq_dispose_mapping(irq);
377 }
378 irq_domain_remove(bus->irq_domain);
379err_irq_domain:
380 return err;
381}
382
383static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus *bus)
384{
385 struct ssb_extif *extif = &bus->extif;
386 struct gpio_chip *chip = &bus->gpio;
387 int gpio;
388
389 if (bus->bustype != SSB_BUSTYPE_SSB)
390 return;
391
392 free_irq(ssb_mips_irq(bus->extif.dev) + 2, extif);
393 for (gpio = 0; gpio < chip->ngpio; gpio++) {
394 int irq = irq_find_mapping(bus->irq_domain, gpio);
395
396 irq_dispose_mapping(irq);
397 }
398 irq_domain_remove(bus->irq_domain);
399}
400#else
401static int ssb_gpio_irq_extif_domain_init(struct ssb_bus *bus)
402{
403 return 0;
404}
405
406static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus *bus)
407{
408}
409#endif
410
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000411static int ssb_gpio_extif_init(struct ssb_bus *bus)
412{
413 struct gpio_chip *chip = &bus->gpio;
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100414 int err;
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000415
416 chip->label = "ssb_extif_gpio";
417 chip->owner = THIS_MODULE;
418 chip->get = ssb_gpio_extif_get_value;
419 chip->set = ssb_gpio_extif_set_value;
420 chip->direction_input = ssb_gpio_extif_direction_input;
421 chip->direction_output = ssb_gpio_extif_direction_output;
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100422#if IS_ENABLED(CONFIG_SSB_EMBEDDED)
423 chip->to_irq = ssb_gpio_to_irq;
424#endif
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000425 chip->ngpio = 5;
426 /* There is just one SoC in one device and its GPIO addresses should be
427 * deterministic to address them more easily. The other buses could get
Shubhankar Kuranagatti5615eb52021-04-28 21:37:47 +0530428 * a random base number.
429 */
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000430 if (bus->bustype == SSB_BUSTYPE_SSB)
431 chip->base = 0;
432 else
433 chip->base = -1;
434
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100435 err = ssb_gpio_irq_extif_domain_init(bus);
436 if (err)
437 return err;
438
Linus Walleij2d4443b2015-12-08 23:00:46 +0100439 err = gpiochip_add_data(chip, bus);
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100440 if (err) {
441 ssb_gpio_irq_extif_domain_exit(bus);
442 return err;
443 }
444
445 return 0;
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000446}
447
448#else
449static int ssb_gpio_extif_init(struct ssb_bus *bus)
450{
451 return -ENOTSUPP;
452}
453#endif
454
Rafał Miłecki7c1bc0d2014-01-13 19:56:08 +0100455/**************************************************
456 * Init
457 **************************************************/
458
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000459int ssb_gpio_init(struct ssb_bus *bus)
460{
461 if (ssb_chipco_available(&bus->chipco))
462 return ssb_gpio_chipco_init(bus);
463 else if (ssb_extif_available(&bus->extif))
464 return ssb_gpio_extif_init(bus);
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000465 return -1;
466}
Hauke Mehrtens600485e2013-02-03 23:25:34 +0100467
468int ssb_gpio_unregister(struct ssb_bus *bus)
469{
470 if (ssb_chipco_available(&bus->chipco) ||
471 ssb_extif_available(&bus->extif)) {
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200472 gpiochip_remove(&bus->gpio);
473 return 0;
Hauke Mehrtens600485e2013-02-03 23:25:34 +0100474 }
Hauke Mehrtens600485e2013-02-03 23:25:34 +0100475 return -1;
476}