1 /*
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/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/export.h>
14 #include <linux/property.h>
15 
16 #include <linux/bcma/bcma.h>
17 
18 #include "bcma_private.h"
19 
20 #define BCMA_GPIO_MAX_PINS	32
21 
22 static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
23 {
24 	struct bcma_drv_cc *cc = gpiochip_get_data(chip);
25 
26 	return !!bcma_chipco_gpio_in(cc, 1 << gpio);
27 }
28 
29 static int bcma_gpio_set_value(struct gpio_chip *chip, unsigned int gpio,
30 			       int value)
31 {
32 	struct bcma_drv_cc *cc = gpiochip_get_data(chip);
33 
34 	bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
35 
36 	return 0;
37 }
38 
39 static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
40 {
41 	struct bcma_drv_cc *cc = gpiochip_get_data(chip);
42 
43 	bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
44 	return 0;
45 }
46 
47 static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
48 				      int value)
49 {
50 	struct bcma_drv_cc *cc = gpiochip_get_data(chip);
51 
52 	bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
53 	bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
54 	return 0;
55 }
56 
57 static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
58 {
59 	struct bcma_drv_cc *cc = gpiochip_get_data(chip);
60 
61 	bcma_chipco_gpio_control(cc, 1 << gpio, 0);
62 	/* clear pulldown */
63 	bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
64 	/* Set pullup */
65 	bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
66 
67 	return 0;
68 }
69 
70 static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
71 {
72 	struct bcma_drv_cc *cc = gpiochip_get_data(chip);
73 
74 	/* clear pullup */
75 	bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
76 }
77 
78 #if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
79 
80 static void bcma_gpio_irq_unmask(struct irq_data *d)
81 {
82 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
83 	struct bcma_drv_cc *cc = gpiochip_get_data(gc);
84 	int gpio = irqd_to_hwirq(d);
85 	u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
86 
87 	gpiochip_enable_irq(gc, gpio);
88 	bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
89 	bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
90 }
91 
92 static void bcma_gpio_irq_mask(struct irq_data *d)
93 {
94 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
95 	struct bcma_drv_cc *cc = gpiochip_get_data(gc);
96 	int gpio = irqd_to_hwirq(d);
97 
98 	bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
99 	gpiochip_disable_irq(gc, gpio);
100 }
101 
102 static const struct irq_chip bcma_gpio_irq_chip = {
103 	.name		= "BCMA-GPIO",
104 	.irq_mask	= bcma_gpio_irq_mask,
105 	.irq_unmask	= bcma_gpio_irq_unmask,
106 	.flags		= IRQCHIP_IMMUTABLE,
107 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
108 };
109 
110 static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
111 {
112 	struct bcma_drv_cc *cc = dev_id;
113 	struct gpio_chip *gc = &cc->gpio;
114 	u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
115 	u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
116 	u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
117 	unsigned long irqs = (val ^ pol) & mask;
118 	int gpio;
119 
120 	if (!irqs)
121 		return IRQ_NONE;
122 
123 	for_each_set_bit(gpio, &irqs, gc->ngpio)
124 		generic_handle_domain_irq_safe(gc->irq.domain, gpio);
125 	bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
126 
127 	return IRQ_HANDLED;
128 }
129 
130 static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
131 {
132 	struct gpio_chip *chip = &cc->gpio;
133 	struct gpio_irq_chip *girq = &chip->irq;
134 	int hwirq, err;
135 
136 	if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
137 		return 0;
138 
139 	hwirq = bcma_core_irq(cc->core, 0);
140 	err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
141 			  cc);
142 	if (err)
143 		return err;
144 
145 	bcma_chipco_gpio_intmask(cc, ~0, 0);
146 	bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
147 
148 	gpio_irq_chip_set_chip(girq, &bcma_gpio_irq_chip);
149 	/* This will let us handle the parent IRQ in the driver */
150 	girq->parent_handler = NULL;
151 	girq->num_parents = 0;
152 	girq->parents = NULL;
153 	girq->default_type = IRQ_TYPE_NONE;
154 	girq->handler = handle_simple_irq;
155 
156 	return 0;
157 }
158 
159 static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
160 {
161 	if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
162 		return;
163 
164 	bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
165 	free_irq(bcma_core_irq(cc->core, 0), cc);
166 }
167 #else
168 static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
169 {
170 	return 0;
171 }
172 
173 static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
174 {
175 }
176 #endif
177 
178 int bcma_gpio_init(struct bcma_drv_cc *cc)
179 {
180 	struct bcma_bus *bus = cc->core->bus;
181 	struct gpio_chip *chip = &cc->gpio;
182 	int err;
183 
184 	chip->label		= "bcma_gpio";
185 	chip->owner		= THIS_MODULE;
186 	chip->request		= bcma_gpio_request;
187 	chip->free		= bcma_gpio_free;
188 	chip->get		= bcma_gpio_get_value;
189 	chip->set_rv		= bcma_gpio_set_value;
190 	chip->direction_input	= bcma_gpio_direction_input;
191 	chip->direction_output	= bcma_gpio_direction_output;
192 	chip->parent		= bus->dev;
193 	chip->fwnode		= dev_fwnode(&cc->core->dev);
194 
195 	switch (bus->chipinfo.id) {
196 	case BCMA_CHIP_ID_BCM4707:
197 	case BCMA_CHIP_ID_BCM5357:
198 	case BCMA_CHIP_ID_BCM53572:
199 	case BCMA_CHIP_ID_BCM53573:
200 	case BCMA_CHIP_ID_BCM47094:
201 		chip->ngpio	= 32;
202 		break;
203 	default:
204 		chip->ngpio	= 16;
205 	}
206 
207 	/*
208 	 * Register SoC GPIO devices with absolute GPIO pin base.
209 	 * On MIPS, we don't have Device Tree and we can't use relative (per chip)
210 	 * GPIO numbers.
211 	 * On some ARM devices, user space may want to access some system GPIO
212 	 * pins directly, which is easier to do with a predictable GPIO base.
213 	 */
214 	if (IS_BUILTIN(CONFIG_BCM47XX) ||
215 	    cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
216 		chip->base		= bus->num * BCMA_GPIO_MAX_PINS;
217 	else
218 		chip->base		= -1;
219 
220 	err = bcma_gpio_irq_init(cc);
221 	if (err)
222 		return err;
223 
224 	err = gpiochip_add_data(chip, cc);
225 	if (err) {
226 		bcma_gpio_irq_exit(cc);
227 		return err;
228 	}
229 
230 	return 0;
231 }
232 
233 int bcma_gpio_unregister(struct bcma_drv_cc *cc)
234 {
235 	bcma_gpio_irq_exit(cc);
236 	gpiochip_remove(&cc->gpio);
237 	return 0;
238 }
239