blob: 8a1e4705bc873d303b6b859056d9c06f298d75b8 [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
Linus Walleij74f4e0c2015-08-14 00:21:45 +020011#include <linux/gpio/driver.h>
Rafał Miłecki29976092013-12-12 13:46:03 +010012#include <linux/interrupt.h>
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000013#include <linux/export.h>
14#include <linux/bcma/bcma.h>
15
16#include "bcma_private.h"
17
Rafał Miłecki057fcd42015-03-15 19:43:14 +010018#define BCMA_GPIO_MAX_PINS 32
19
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000020static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
21{
Linus Walleij78d455a2015-12-08 16:17:02 +010022 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000023
24 return !!bcma_chipco_gpio_in(cc, 1 << gpio);
25}
26
27static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
28 int value)
29{
Linus Walleij78d455a2015-12-08 16:17:02 +010030 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000031
32 bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
33}
34
35static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
36{
Linus Walleij78d455a2015-12-08 16:17:02 +010037 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000038
39 bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
40 return 0;
41}
42
43static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
44 int value)
45{
Linus Walleij78d455a2015-12-08 16:17:02 +010046 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000047
48 bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
49 bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
50 return 0;
51}
52
53static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
54{
Linus Walleij78d455a2015-12-08 16:17:02 +010055 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000056
57 bcma_chipco_gpio_control(cc, 1 << gpio, 0);
58 /* clear pulldown */
59 bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
60 /* Set pullup */
61 bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
62
63 return 0;
64}
65
66static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
67{
Linus Walleij78d455a2015-12-08 16:17:02 +010068 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +000069
70 /* clear pullup */
71 bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
72}
73
Rafał Miłecki0cbfc062015-02-20 11:49:05 +010074#if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
Hauke Mehrtens8f1ca262013-01-26 21:39:44 +010075
Rafał Miłecki29976092013-12-12 13:46:03 +010076static void bcma_gpio_irq_unmask(struct irq_data *d)
77{
Linus Walleij74f4e0c2015-08-14 00:21:45 +020078 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij78d455a2015-12-08 16:17:02 +010079 struct bcma_drv_cc *cc = gpiochip_get_data(gc);
Rafał Miłecki29976092013-12-12 13:46:03 +010080 int gpio = irqd_to_hwirq(d);
Hauke Mehrtens978e55d2014-01-02 19:01:08 +010081 u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
Rafał Miłecki29976092013-12-12 13:46:03 +010082
Hauke Mehrtens978e55d2014-01-02 19:01:08 +010083 bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
Rafał Miłecki29976092013-12-12 13:46:03 +010084 bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
85}
86
87static void bcma_gpio_irq_mask(struct irq_data *d)
88{
Linus Walleij74f4e0c2015-08-14 00:21:45 +020089 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij78d455a2015-12-08 16:17:02 +010090 struct bcma_drv_cc *cc = gpiochip_get_data(gc);
Rafał Miłecki29976092013-12-12 13:46:03 +010091 int gpio = irqd_to_hwirq(d);
92
93 bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
94}
95
96static struct irq_chip bcma_gpio_irq_chip = {
97 .name = "BCMA-GPIO",
98 .irq_mask = bcma_gpio_irq_mask,
99 .irq_unmask = bcma_gpio_irq_unmask,
100};
101
102static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
103{
104 struct bcma_drv_cc *cc = dev_id;
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200105 struct gpio_chip *gc = &cc->gpio;
Rafał Miłecki29976092013-12-12 13:46:03 +0100106 u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
107 u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
108 u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
Rafał Miłeckid5ab2ad2014-01-13 20:05:17 +0100109 unsigned long irqs = (val ^ pol) & mask;
Rafał Miłecki29976092013-12-12 13:46:03 +0100110 int gpio;
111
112 if (!irqs)
113 return IRQ_NONE;
114
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200115 for_each_set_bit(gpio, &irqs, gc->ngpio)
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100116 generic_handle_irq(irq_find_mapping(gc->irq.domain, gpio));
Rafał Miłecki29976092013-12-12 13:46:03 +0100117 bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
118
119 return IRQ_HANDLED;
120}
121
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200122static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
Rafał Miłecki29976092013-12-12 13:46:03 +0100123{
124 struct gpio_chip *chip = &cc->gpio;
Linus Walleija080ecb2020-07-22 13:17:25 +0200125 struct gpio_irq_chip *girq = &chip->irq;
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200126 int hwirq, err;
Rafał Miłecki29976092013-12-12 13:46:03 +0100127
128 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
129 return 0;
130
Hauke Mehrtens85eb92e2014-11-01 16:54:55 +0100131 hwirq = bcma_core_irq(cc->core, 0);
Rafał Miłecki29976092013-12-12 13:46:03 +0100132 err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
133 cc);
134 if (err)
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200135 return err;
Rafał Miłecki29976092013-12-12 13:46:03 +0100136
Hauke Mehrtens978e55d2014-01-02 19:01:08 +0100137 bcma_chipco_gpio_intmask(cc, ~0, 0);
Rafał Miłecki29976092013-12-12 13:46:03 +0100138 bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
139
Linus Walleija080ecb2020-07-22 13:17:25 +0200140 girq->chip = &bcma_gpio_irq_chip;
141 /* This will let us handle the parent IRQ in the driver */
142 girq->parent_handler = NULL;
143 girq->num_parents = 0;
144 girq->parents = NULL;
145 girq->default_type = IRQ_TYPE_NONE;
146 girq->handler = handle_simple_irq;
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200147
148 return 0;
Rafał Miłecki29976092013-12-12 13:46:03 +0100149}
150
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200151static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
Rafał Miłecki29976092013-12-12 13:46:03 +0100152{
Rafał Miłecki29976092013-12-12 13:46:03 +0100153 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
154 return;
155
156 bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
Hauke Mehrtens85eb92e2014-11-01 16:54:55 +0100157 free_irq(bcma_core_irq(cc->core, 0), cc);
Rafał Miłecki29976092013-12-12 13:46:03 +0100158}
159#else
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200160static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
Rafał Miłecki29976092013-12-12 13:46:03 +0100161{
162 return 0;
163}
164
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200165static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
Rafał Miłecki29976092013-12-12 13:46:03 +0100166{
167}
168#endif
169
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000170int bcma_gpio_init(struct bcma_drv_cc *cc)
171{
Rafał Miłecki057fcd42015-03-15 19:43:14 +0100172 struct bcma_bus *bus = cc->core->bus;
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000173 struct gpio_chip *chip = &cc->gpio;
Rafał Miłecki29976092013-12-12 13:46:03 +0100174 int err;
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000175
176 chip->label = "bcma_gpio";
177 chip->owner = THIS_MODULE;
178 chip->request = bcma_gpio_request;
179 chip->free = bcma_gpio_free;
180 chip->get = bcma_gpio_get_value;
181 chip->set = bcma_gpio_set_value;
182 chip->direction_input = bcma_gpio_direction_input;
183 chip->direction_output = bcma_gpio_direction_output;
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200184 chip->owner = THIS_MODULE;
Rafał Miłecki5a1c18b2019-01-02 00:00:01 +0100185 chip->parent = bus->dev;
Rafał Miłeckia0196d12014-09-30 12:55:48 +0200186#if IS_BUILTIN(CONFIG_OF)
Rafał Miłeckia4bb5b12017-02-14 23:03:46 +0100187 chip->of_node = cc->core->dev.of_node;
Rafał Miłeckia0196d12014-09-30 12:55:48 +0200188#endif
Rafał Miłecki057fcd42015-03-15 19:43:14 +0100189 switch (bus->chipinfo.id) {
Felix Fietkauf022ea52015-04-15 15:07:53 +0200190 case BCMA_CHIP_ID_BCM4707:
Rafał Miłecki0f8ca0142014-03-20 21:09:07 +0100191 case BCMA_CHIP_ID_BCM5357:
Rafał Miłeckif0db59e2014-06-05 20:20:44 +0200192 case BCMA_CHIP_ID_BCM53572:
Florian Fainelli459c3512017-06-30 12:02:57 -0700193 case BCMA_CHIP_ID_BCM53573:
Rafał Miłecki61dba732016-01-24 16:37:33 +0100194 case BCMA_CHIP_ID_BCM47094:
Rafał Miłecki0f8ca0142014-03-20 21:09:07 +0100195 chip->ngpio = 32;
196 break;
197 default:
198 chip->ngpio = 16;
199 }
200
Rafał Miłecki057fcd42015-03-15 19:43:14 +0100201 /*
Felix Fietkau2d57b712015-04-15 15:07:52 +0200202 * Register SoC GPIO devices with absolute GPIO pin base.
203 * On MIPS, we don't have Device Tree and we can't use relative (per chip)
204 * GPIO numbers.
205 * On some ARM devices, user space may want to access some system GPIO
206 * pins directly, which is easier to do with a predictable GPIO base.
Rafał Miłecki057fcd42015-03-15 19:43:14 +0100207 */
Felix Fietkau2d57b712015-04-15 15:07:52 +0200208 if (IS_BUILTIN(CONFIG_BCM47XX) ||
209 cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
210 chip->base = bus->num * BCMA_GPIO_MAX_PINS;
211 else
212 chip->base = -1;
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000213
Linus Walleija080ecb2020-07-22 13:17:25 +0200214 err = bcma_gpio_irq_init(cc);
Rafał Miłecki29976092013-12-12 13:46:03 +0100215 if (err)
216 return err;
217
Linus Walleija080ecb2020-07-22 13:17:25 +0200218 err = gpiochip_add_data(chip, cc);
Rafał Miłecki29976092013-12-12 13:46:03 +0100219 if (err) {
Linus Walleija080ecb2020-07-22 13:17:25 +0200220 bcma_gpio_irq_exit(cc);
Rafał Miłecki29976092013-12-12 13:46:03 +0100221 return err;
222 }
223
224 return 0;
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000225}
Hauke Mehrtensc50ae942013-02-03 23:25:33 +0100226
227int bcma_gpio_unregister(struct bcma_drv_cc *cc)
228{
Linus Walleij74f4e0c2015-08-14 00:21:45 +0200229 bcma_gpio_irq_exit(cc);
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200230 gpiochip_remove(&cc->gpio);
231 return 0;
Hauke Mehrtensc50ae942013-02-03 23:25:33 +0100232}