xref: /linux/drivers/gpio/gpio-xlp.c (revision 529f75d8ca214aa52ed609d1a909bbe3a71504bf)
1ff718800SKamlakant Patel /*
2ff718800SKamlakant Patel  * Copyright (C) 2003-2015 Broadcom Corporation
3ff718800SKamlakant Patel  * All Rights Reserved
4ff718800SKamlakant Patel  *
5ff718800SKamlakant Patel  * This program is free software; you can redistribute it and/or modify
6ff718800SKamlakant Patel  * it under the terms of the GNU General Public License version 2 as
7ff718800SKamlakant Patel  * published by the Free Software Foundation.
8ff718800SKamlakant Patel  *
9ff718800SKamlakant Patel  * This program is distributed in the hope that it will be useful,
10ff718800SKamlakant Patel  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11ff718800SKamlakant Patel  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12ff718800SKamlakant Patel  * GNU General Public License for more details.
13ff718800SKamlakant Patel  */
14ff718800SKamlakant Patel 
15ff718800SKamlakant Patel #include <linux/gpio.h>
16ff718800SKamlakant Patel #include <linux/platform_device.h>
17ff718800SKamlakant Patel #include <linux/of_device.h>
18ff718800SKamlakant Patel #include <linux/module.h>
19ff718800SKamlakant Patel #include <linux/irq.h>
20ff718800SKamlakant Patel #include <linux/interrupt.h>
2183ea24fdSKamlakant Patel #include <linux/irqchip/chained_irq.h>
22baa1b920SKamlakant Patel #include <linux/acpi.h>
23ff718800SKamlakant Patel 
24ff718800SKamlakant Patel /*
25ff718800SKamlakant Patel  * XLP GPIO has multiple 32 bit registers for each feature where each register
26ff718800SKamlakant Patel  * controls 32 pins. So, pins up to 64 require 2 32-bit registers and up to 96
27ff718800SKamlakant Patel  * require 3 32-bit registers for each feature.
28ff718800SKamlakant Patel  * Here we only define offset of the first register for each feature. Offset of
29ff718800SKamlakant Patel  * the registers for pins greater than 32 can be calculated as following(Use
30ff718800SKamlakant Patel  * GPIO_INT_STAT as example):
31ff718800SKamlakant Patel  *
32ff718800SKamlakant Patel  * offset = (gpio / XLP_GPIO_REGSZ) * 4;
33ff718800SKamlakant Patel  * reg_addr = addr + offset;
34ff718800SKamlakant Patel  *
35ff718800SKamlakant Patel  * where addr is base address of the that feature register and gpio is the pin.
36ff718800SKamlakant Patel  */
37ff718800SKamlakant Patel #define GPIO_OUTPUT_EN		0x00
38ff718800SKamlakant Patel #define GPIO_PADDRV		0x08
39ff718800SKamlakant Patel #define GPIO_INT_EN00		0x18
40ff718800SKamlakant Patel #define GPIO_INT_EN10		0x20
41ff718800SKamlakant Patel #define GPIO_INT_EN20		0x28
42ff718800SKamlakant Patel #define GPIO_INT_EN30		0x30
43ff718800SKamlakant Patel #define GPIO_INT_POL		0x38
44ff718800SKamlakant Patel #define GPIO_INT_TYPE		0x40
45ff718800SKamlakant Patel #define GPIO_INT_STAT		0x48
46ff718800SKamlakant Patel 
47ff718800SKamlakant Patel #define GPIO_9XX_BYTESWAP	0X00
48ff718800SKamlakant Patel #define GPIO_9XX_CTRL		0X04
49ff718800SKamlakant Patel #define GPIO_9XX_OUTPUT_EN	0x14
50ff718800SKamlakant Patel #define GPIO_9XX_PADDRV		0x24
51ff718800SKamlakant Patel /*
52ff718800SKamlakant Patel  * Only for 4 interrupt enable reg are defined for now,
53ff718800SKamlakant Patel  * total reg available are 12.
54ff718800SKamlakant Patel  */
55ff718800SKamlakant Patel #define GPIO_9XX_INT_EN00	0x44
56ff718800SKamlakant Patel #define GPIO_9XX_INT_EN10	0x54
57ff718800SKamlakant Patel #define GPIO_9XX_INT_EN20	0x64
58ff718800SKamlakant Patel #define GPIO_9XX_INT_EN30	0x74
59ff718800SKamlakant Patel #define GPIO_9XX_INT_POL	0x104
60ff718800SKamlakant Patel #define GPIO_9XX_INT_TYPE	0x114
61ff718800SKamlakant Patel #define GPIO_9XX_INT_STAT	0x124
62ff718800SKamlakant Patel 
63ff718800SKamlakant Patel #define GPIO_3XX_INT_EN00	0x18
64ff718800SKamlakant Patel #define GPIO_3XX_INT_EN10	0x20
65ff718800SKamlakant Patel #define GPIO_3XX_INT_EN20	0x28
66ff718800SKamlakant Patel #define GPIO_3XX_INT_EN30	0x30
67ff718800SKamlakant Patel #define GPIO_3XX_INT_POL	0x78
68ff718800SKamlakant Patel #define GPIO_3XX_INT_TYPE	0x80
69ff718800SKamlakant Patel #define GPIO_3XX_INT_STAT	0x88
70ff718800SKamlakant Patel 
71ff718800SKamlakant Patel /* Interrupt type register mask */
72ff718800SKamlakant Patel #define XLP_GPIO_IRQ_TYPE_LVL	0x0
73ff718800SKamlakant Patel #define XLP_GPIO_IRQ_TYPE_EDGE	0x1
74ff718800SKamlakant Patel 
75ff718800SKamlakant Patel /* Interrupt polarity register mask */
76ff718800SKamlakant Patel #define XLP_GPIO_IRQ_POL_HIGH	0x0
77ff718800SKamlakant Patel #define XLP_GPIO_IRQ_POL_LOW	0x1
78ff718800SKamlakant Patel 
79ff718800SKamlakant Patel #define XLP_GPIO_REGSZ		32
80ff718800SKamlakant Patel #define XLP_GPIO_IRQ_BASE	768
81ff718800SKamlakant Patel #define XLP_MAX_NR_GPIO		96
82ff718800SKamlakant Patel 
83ff718800SKamlakant Patel /* XLP variants supported by this driver */
84ff718800SKamlakant Patel enum {
85ff718800SKamlakant Patel 	XLP_GPIO_VARIANT_XLP832 = 1,
86ff718800SKamlakant Patel 	XLP_GPIO_VARIANT_XLP316,
87ff718800SKamlakant Patel 	XLP_GPIO_VARIANT_XLP208,
88ff718800SKamlakant Patel 	XLP_GPIO_VARIANT_XLP980,
89dd98756dSKamlakant Patel 	XLP_GPIO_VARIANT_XLP532,
90dd98756dSKamlakant Patel 	GPIO_VARIANT_VULCAN
91ff718800SKamlakant Patel };
92ff718800SKamlakant Patel 
93ff718800SKamlakant Patel struct xlp_gpio_priv {
94ff718800SKamlakant Patel 	struct gpio_chip chip;
95ff718800SKamlakant Patel 	DECLARE_BITMAP(gpio_enabled_mask, XLP_MAX_NR_GPIO);
96ff718800SKamlakant Patel 	void __iomem *gpio_intr_en;	/* pointer to first intr enable reg */
97ff718800SKamlakant Patel 	void __iomem *gpio_intr_stat;	/* pointer to first intr status reg */
98ff718800SKamlakant Patel 	void __iomem *gpio_intr_type;	/* pointer to first intr type reg */
99ff718800SKamlakant Patel 	void __iomem *gpio_intr_pol;	/* pointer to first intr polarity reg */
100ff718800SKamlakant Patel 	void __iomem *gpio_out_en;	/* pointer to first output enable reg */
101ff718800SKamlakant Patel 	void __iomem *gpio_paddrv;	/* pointer to first pad drive reg */
102ff718800SKamlakant Patel 	spinlock_t lock;
103ff718800SKamlakant Patel };
104ff718800SKamlakant Patel 
105ff718800SKamlakant Patel static int xlp_gpio_get_reg(void __iomem *addr, unsigned gpio)
106ff718800SKamlakant Patel {
107ff718800SKamlakant Patel 	u32 pos, regset;
108ff718800SKamlakant Patel 
109ff718800SKamlakant Patel 	pos = gpio % XLP_GPIO_REGSZ;
110ff718800SKamlakant Patel 	regset = (gpio / XLP_GPIO_REGSZ) * 4;
111ff718800SKamlakant Patel 	return !!(readl(addr + regset) & BIT(pos));
112ff718800SKamlakant Patel }
113ff718800SKamlakant Patel 
114ff718800SKamlakant Patel static void xlp_gpio_set_reg(void __iomem *addr, unsigned gpio, int state)
115ff718800SKamlakant Patel {
116ff718800SKamlakant Patel 	u32 value, pos, regset;
117ff718800SKamlakant Patel 
118ff718800SKamlakant Patel 	pos = gpio % XLP_GPIO_REGSZ;
119ff718800SKamlakant Patel 	regset = (gpio / XLP_GPIO_REGSZ) * 4;
120ff718800SKamlakant Patel 	value = readl(addr + regset);
121ff718800SKamlakant Patel 
122ff718800SKamlakant Patel 	if (state)
123ff718800SKamlakant Patel 		value |= BIT(pos);
124ff718800SKamlakant Patel 	else
125ff718800SKamlakant Patel 		value &= ~BIT(pos);
126ff718800SKamlakant Patel 
127ff718800SKamlakant Patel 	writel(value, addr + regset);
128ff718800SKamlakant Patel }
129ff718800SKamlakant Patel 
130ff718800SKamlakant Patel static void xlp_gpio_irq_disable(struct irq_data *d)
131ff718800SKamlakant Patel {
132ff718800SKamlakant Patel 	struct gpio_chip *gc  = irq_data_get_irq_chip_data(d);
133e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
134ff718800SKamlakant Patel 	unsigned long flags;
135ff718800SKamlakant Patel 
136ff718800SKamlakant Patel 	spin_lock_irqsave(&priv->lock, flags);
137ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
138ff718800SKamlakant Patel 	__clear_bit(d->hwirq, priv->gpio_enabled_mask);
139ff718800SKamlakant Patel 	spin_unlock_irqrestore(&priv->lock, flags);
140ff718800SKamlakant Patel }
141ff718800SKamlakant Patel 
142ff718800SKamlakant Patel static void xlp_gpio_irq_mask_ack(struct irq_data *d)
143ff718800SKamlakant Patel {
144ff718800SKamlakant Patel 	struct gpio_chip *gc  = irq_data_get_irq_chip_data(d);
145e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
146ff718800SKamlakant Patel 	unsigned long flags;
147ff718800SKamlakant Patel 
148ff718800SKamlakant Patel 	spin_lock_irqsave(&priv->lock, flags);
149ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
150ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_intr_stat, d->hwirq, 0x1);
151ff718800SKamlakant Patel 	__clear_bit(d->hwirq, priv->gpio_enabled_mask);
152ff718800SKamlakant Patel 	spin_unlock_irqrestore(&priv->lock, flags);
153ff718800SKamlakant Patel }
154ff718800SKamlakant Patel 
155ff718800SKamlakant Patel static void xlp_gpio_irq_unmask(struct irq_data *d)
156ff718800SKamlakant Patel {
157ff718800SKamlakant Patel 	struct gpio_chip *gc  = irq_data_get_irq_chip_data(d);
158e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
159ff718800SKamlakant Patel 	unsigned long flags;
160ff718800SKamlakant Patel 
161ff718800SKamlakant Patel 	spin_lock_irqsave(&priv->lock, flags);
162ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x1);
163ff718800SKamlakant Patel 	__set_bit(d->hwirq, priv->gpio_enabled_mask);
164ff718800SKamlakant Patel 	spin_unlock_irqrestore(&priv->lock, flags);
165ff718800SKamlakant Patel }
166ff718800SKamlakant Patel 
167ff718800SKamlakant Patel static int xlp_gpio_set_irq_type(struct irq_data *d, unsigned int type)
168ff718800SKamlakant Patel {
169ff718800SKamlakant Patel 	struct gpio_chip *gc  = irq_data_get_irq_chip_data(d);
170e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
171ff718800SKamlakant Patel 	int pol, irq_type;
172ff718800SKamlakant Patel 
173ff718800SKamlakant Patel 	switch (type) {
174ff718800SKamlakant Patel 	case IRQ_TYPE_EDGE_RISING:
175ff718800SKamlakant Patel 		irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
176ff718800SKamlakant Patel 		pol = XLP_GPIO_IRQ_POL_HIGH;
177ff718800SKamlakant Patel 		break;
178ff718800SKamlakant Patel 	case IRQ_TYPE_EDGE_FALLING:
179ff718800SKamlakant Patel 		irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
180ff718800SKamlakant Patel 		pol = XLP_GPIO_IRQ_POL_LOW;
181ff718800SKamlakant Patel 		break;
182ff718800SKamlakant Patel 	case IRQ_TYPE_LEVEL_HIGH:
183ff718800SKamlakant Patel 		irq_type = XLP_GPIO_IRQ_TYPE_LVL;
184ff718800SKamlakant Patel 		pol = XLP_GPIO_IRQ_POL_HIGH;
185ff718800SKamlakant Patel 		break;
186ff718800SKamlakant Patel 	case IRQ_TYPE_LEVEL_LOW:
187ff718800SKamlakant Patel 		irq_type = XLP_GPIO_IRQ_TYPE_LVL;
188ff718800SKamlakant Patel 		pol = XLP_GPIO_IRQ_POL_LOW;
189ff718800SKamlakant Patel 		break;
190ff718800SKamlakant Patel 	default:
191ff718800SKamlakant Patel 		return -EINVAL;
192ff718800SKamlakant Patel 	}
193ff718800SKamlakant Patel 
194ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_intr_type, d->hwirq, irq_type);
195ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_intr_pol, d->hwirq, pol);
196ff718800SKamlakant Patel 
197ff718800SKamlakant Patel 	return 0;
198ff718800SKamlakant Patel }
199ff718800SKamlakant Patel 
200ff718800SKamlakant Patel static struct irq_chip xlp_gpio_irq_chip = {
201ff718800SKamlakant Patel 	.name		= "XLP-GPIO",
202ff718800SKamlakant Patel 	.irq_mask_ack	= xlp_gpio_irq_mask_ack,
203ff718800SKamlakant Patel 	.irq_disable	= xlp_gpio_irq_disable,
204ff718800SKamlakant Patel 	.irq_set_type	= xlp_gpio_set_irq_type,
205ff718800SKamlakant Patel 	.irq_unmask	= xlp_gpio_irq_unmask,
206ff718800SKamlakant Patel 	.flags		= IRQCHIP_ONESHOT_SAFE,
207ff718800SKamlakant Patel };
208ff718800SKamlakant Patel 
20983ea24fdSKamlakant Patel static void xlp_gpio_generic_handler(struct irq_desc *desc)
210ff718800SKamlakant Patel {
21183ea24fdSKamlakant Patel 	struct xlp_gpio_priv *priv = irq_desc_get_handler_data(desc);
21283ea24fdSKamlakant Patel 	struct irq_chip *irqchip = irq_desc_get_chip(desc);
213ff718800SKamlakant Patel 	int gpio, regoff;
214ff718800SKamlakant Patel 	u32 gpio_stat;
215ff718800SKamlakant Patel 
216ff718800SKamlakant Patel 	regoff = -1;
217ff718800SKamlakant Patel 	gpio_stat = 0;
21883ea24fdSKamlakant Patel 
21983ea24fdSKamlakant Patel 	chained_irq_enter(irqchip, desc);
220ff718800SKamlakant Patel 	for_each_set_bit(gpio, priv->gpio_enabled_mask, XLP_MAX_NR_GPIO) {
221ff718800SKamlakant Patel 		if (regoff != gpio / XLP_GPIO_REGSZ) {
222ff718800SKamlakant Patel 			regoff = gpio / XLP_GPIO_REGSZ;
223ff718800SKamlakant Patel 			gpio_stat = readl(priv->gpio_intr_stat + regoff * 4);
224ff718800SKamlakant Patel 		}
22583ea24fdSKamlakant Patel 
226ff718800SKamlakant Patel 		if (gpio_stat & BIT(gpio % XLP_GPIO_REGSZ))
227ff718800SKamlakant Patel 			generic_handle_irq(irq_find_mapping(
228ff718800SKamlakant Patel 						priv->chip.irqdomain, gpio));
229ff718800SKamlakant Patel 	}
23083ea24fdSKamlakant Patel 	chained_irq_exit(irqchip, desc);
231ff718800SKamlakant Patel }
232ff718800SKamlakant Patel 
233ff718800SKamlakant Patel static int xlp_gpio_dir_output(struct gpio_chip *gc, unsigned gpio, int state)
234ff718800SKamlakant Patel {
235e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
236ff718800SKamlakant Patel 
237ff718800SKamlakant Patel 	BUG_ON(gpio >= gc->ngpio);
238ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x1);
239ff718800SKamlakant Patel 
240ff718800SKamlakant Patel 	return 0;
241ff718800SKamlakant Patel }
242ff718800SKamlakant Patel 
243ff718800SKamlakant Patel static int xlp_gpio_dir_input(struct gpio_chip *gc, unsigned gpio)
244ff718800SKamlakant Patel {
245e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
246ff718800SKamlakant Patel 
247ff718800SKamlakant Patel 	BUG_ON(gpio >= gc->ngpio);
248ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x0);
249ff718800SKamlakant Patel 
250ff718800SKamlakant Patel 	return 0;
251ff718800SKamlakant Patel }
252ff718800SKamlakant Patel 
253ff718800SKamlakant Patel static int xlp_gpio_get(struct gpio_chip *gc, unsigned gpio)
254ff718800SKamlakant Patel {
255e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
256ff718800SKamlakant Patel 
257ff718800SKamlakant Patel 	BUG_ON(gpio >= gc->ngpio);
258ff718800SKamlakant Patel 	return xlp_gpio_get_reg(priv->gpio_paddrv, gpio);
259ff718800SKamlakant Patel }
260ff718800SKamlakant Patel 
261ff718800SKamlakant Patel static void xlp_gpio_set(struct gpio_chip *gc, unsigned gpio, int state)
262ff718800SKamlakant Patel {
263e730a595SLinus Walleij 	struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
264ff718800SKamlakant Patel 
265ff718800SKamlakant Patel 	BUG_ON(gpio >= gc->ngpio);
266ff718800SKamlakant Patel 	xlp_gpio_set_reg(priv->gpio_paddrv, gpio, state);
267ff718800SKamlakant Patel }
268ff718800SKamlakant Patel 
269ff718800SKamlakant Patel static const struct of_device_id xlp_gpio_of_ids[] = {
270ff718800SKamlakant Patel 	{
271ff718800SKamlakant Patel 		.compatible = "netlogic,xlp832-gpio",
272ff718800SKamlakant Patel 		.data	    = (void *)XLP_GPIO_VARIANT_XLP832,
273ff718800SKamlakant Patel 	},
274ff718800SKamlakant Patel 	{
275ff718800SKamlakant Patel 		.compatible = "netlogic,xlp316-gpio",
276ff718800SKamlakant Patel 		.data	    = (void *)XLP_GPIO_VARIANT_XLP316,
277ff718800SKamlakant Patel 	},
278ff718800SKamlakant Patel 	{
279ff718800SKamlakant Patel 		.compatible = "netlogic,xlp208-gpio",
280ff718800SKamlakant Patel 		.data	    = (void *)XLP_GPIO_VARIANT_XLP208,
281ff718800SKamlakant Patel 	},
282ff718800SKamlakant Patel 	{
283ff718800SKamlakant Patel 		.compatible = "netlogic,xlp980-gpio",
284ff718800SKamlakant Patel 		.data	    = (void *)XLP_GPIO_VARIANT_XLP980,
285ff718800SKamlakant Patel 	},
286ff718800SKamlakant Patel 	{
287ff718800SKamlakant Patel 		.compatible = "netlogic,xlp532-gpio",
288ff718800SKamlakant Patel 		.data	    = (void *)XLP_GPIO_VARIANT_XLP532,
289ff718800SKamlakant Patel 	},
290dd98756dSKamlakant Patel 	{
291dd98756dSKamlakant Patel 		.compatible = "brcm,vulcan-gpio",
292dd98756dSKamlakant Patel 		.data	    = (void *)GPIO_VARIANT_VULCAN,
293dd98756dSKamlakant Patel 	},
294ff718800SKamlakant Patel 	{ /* sentinel */ },
295ff718800SKamlakant Patel };
296ff718800SKamlakant Patel MODULE_DEVICE_TABLE(of, xlp_gpio_of_ids);
297ff718800SKamlakant Patel 
298ff718800SKamlakant Patel static int xlp_gpio_probe(struct platform_device *pdev)
299ff718800SKamlakant Patel {
300ff718800SKamlakant Patel 	struct gpio_chip *gc;
301ff718800SKamlakant Patel 	struct resource *iores;
302ff718800SKamlakant Patel 	struct xlp_gpio_priv *priv;
303ff718800SKamlakant Patel 	void __iomem *gpio_base;
304ff718800SKamlakant Patel 	int irq_base, irq, err;
305ff718800SKamlakant Patel 	int ngpio;
306ff718800SKamlakant Patel 	u32 soc_type;
307ff718800SKamlakant Patel 
308ff718800SKamlakant Patel 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309ff718800SKamlakant Patel 	if (!iores)
310ff718800SKamlakant Patel 		return -ENODEV;
311ff718800SKamlakant Patel 
312ff718800SKamlakant Patel 	priv = devm_kzalloc(&pdev->dev,	sizeof(*priv), GFP_KERNEL);
313ff718800SKamlakant Patel 	if (!priv)
314ff718800SKamlakant Patel 		return -ENOMEM;
315ff718800SKamlakant Patel 
316ff718800SKamlakant Patel 	gpio_base = devm_ioremap_resource(&pdev->dev, iores);
317ff718800SKamlakant Patel 	if (IS_ERR(gpio_base))
318ff718800SKamlakant Patel 		return PTR_ERR(gpio_base);
319ff718800SKamlakant Patel 
320ff718800SKamlakant Patel 	irq = platform_get_irq(pdev, 0);
321ff718800SKamlakant Patel 	if (irq < 0)
322ff718800SKamlakant Patel 		return irq;
323ff718800SKamlakant Patel 
324baa1b920SKamlakant Patel 	if (pdev->dev.of_node) {
325baa1b920SKamlakant Patel 		const struct of_device_id *of_id;
326baa1b920SKamlakant Patel 
327ff718800SKamlakant Patel 		of_id = of_match_device(xlp_gpio_of_ids, &pdev->dev);
328ff718800SKamlakant Patel 		if (!of_id) {
329baa1b920SKamlakant Patel 			dev_err(&pdev->dev, "Unable to match OF ID\n");
330ff718800SKamlakant Patel 			return -ENODEV;
331ff718800SKamlakant Patel 		}
332ff718800SKamlakant Patel 		soc_type = (uintptr_t) of_id->data;
333baa1b920SKamlakant Patel 	} else {
334baa1b920SKamlakant Patel 		const struct acpi_device_id *acpi_id;
335baa1b920SKamlakant Patel 
336baa1b920SKamlakant Patel 		acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
337baa1b920SKamlakant Patel 						&pdev->dev);
338baa1b920SKamlakant Patel 		if (!acpi_id || !acpi_id->driver_data) {
339baa1b920SKamlakant Patel 			dev_err(&pdev->dev, "Unable to match ACPI ID\n");
340baa1b920SKamlakant Patel 			return -ENODEV;
341baa1b920SKamlakant Patel 		}
342baa1b920SKamlakant Patel 		soc_type = (uintptr_t) acpi_id->driver_data;
343baa1b920SKamlakant Patel 	}
344ff718800SKamlakant Patel 
345ff718800SKamlakant Patel 	switch (soc_type) {
346ff718800SKamlakant Patel 	case XLP_GPIO_VARIANT_XLP832:
347ff718800SKamlakant Patel 		priv->gpio_out_en = gpio_base + GPIO_OUTPUT_EN;
348ff718800SKamlakant Patel 		priv->gpio_paddrv = gpio_base + GPIO_PADDRV;
349ff718800SKamlakant Patel 		priv->gpio_intr_stat = gpio_base + GPIO_INT_STAT;
350ff718800SKamlakant Patel 		priv->gpio_intr_type = gpio_base + GPIO_INT_TYPE;
351ff718800SKamlakant Patel 		priv->gpio_intr_pol = gpio_base + GPIO_INT_POL;
352ff718800SKamlakant Patel 		priv->gpio_intr_en = gpio_base + GPIO_INT_EN00;
353ff718800SKamlakant Patel 		ngpio = 41;
354ff718800SKamlakant Patel 		break;
355ff718800SKamlakant Patel 	case XLP_GPIO_VARIANT_XLP208:
356ff718800SKamlakant Patel 	case XLP_GPIO_VARIANT_XLP316:
357ff718800SKamlakant Patel 		priv->gpio_out_en = gpio_base + GPIO_OUTPUT_EN;
358ff718800SKamlakant Patel 		priv->gpio_paddrv = gpio_base + GPIO_PADDRV;
359ff718800SKamlakant Patel 		priv->gpio_intr_stat = gpio_base + GPIO_3XX_INT_STAT;
360ff718800SKamlakant Patel 		priv->gpio_intr_type = gpio_base + GPIO_3XX_INT_TYPE;
361ff718800SKamlakant Patel 		priv->gpio_intr_pol = gpio_base + GPIO_3XX_INT_POL;
362ff718800SKamlakant Patel 		priv->gpio_intr_en = gpio_base + GPIO_3XX_INT_EN00;
363ff718800SKamlakant Patel 
364ff718800SKamlakant Patel 		ngpio = (soc_type == XLP_GPIO_VARIANT_XLP208) ? 42 : 57;
365ff718800SKamlakant Patel 		break;
366ff718800SKamlakant Patel 	case XLP_GPIO_VARIANT_XLP980:
367ff718800SKamlakant Patel 	case XLP_GPIO_VARIANT_XLP532:
368dd98756dSKamlakant Patel 	case GPIO_VARIANT_VULCAN:
369ff718800SKamlakant Patel 		priv->gpio_out_en = gpio_base + GPIO_9XX_OUTPUT_EN;
370ff718800SKamlakant Patel 		priv->gpio_paddrv = gpio_base + GPIO_9XX_PADDRV;
371ff718800SKamlakant Patel 		priv->gpio_intr_stat = gpio_base + GPIO_9XX_INT_STAT;
372ff718800SKamlakant Patel 		priv->gpio_intr_type = gpio_base + GPIO_9XX_INT_TYPE;
373ff718800SKamlakant Patel 		priv->gpio_intr_pol = gpio_base + GPIO_9XX_INT_POL;
374ff718800SKamlakant Patel 		priv->gpio_intr_en = gpio_base + GPIO_9XX_INT_EN00;
375ff718800SKamlakant Patel 
376dd98756dSKamlakant Patel 		if (soc_type == XLP_GPIO_VARIANT_XLP980)
377dd98756dSKamlakant Patel 			ngpio = 66;
378dd98756dSKamlakant Patel 		else if (soc_type == XLP_GPIO_VARIANT_XLP532)
379dd98756dSKamlakant Patel 			ngpio = 67;
380dd98756dSKamlakant Patel 		else
381dd98756dSKamlakant Patel 			ngpio = 70;
382ff718800SKamlakant Patel 		break;
383ff718800SKamlakant Patel 	default:
384ff718800SKamlakant Patel 		dev_err(&pdev->dev, "Unknown Processor type!\n");
385ff718800SKamlakant Patel 		return -ENODEV;
386ff718800SKamlakant Patel 	}
387ff718800SKamlakant Patel 
388ff718800SKamlakant Patel 	bitmap_zero(priv->gpio_enabled_mask, XLP_MAX_NR_GPIO);
389ff718800SKamlakant Patel 
390ff718800SKamlakant Patel 	gc = &priv->chip;
391ff718800SKamlakant Patel 
392b8a3f52eSAxel Lin 	gc->owner = THIS_MODULE;
393b8a3f52eSAxel Lin 	gc->label = dev_name(&pdev->dev);
394ff718800SKamlakant Patel 	gc->base = 0;
39558383c78SLinus Walleij 	gc->parent = &pdev->dev;
396ff718800SKamlakant Patel 	gc->ngpio = ngpio;
397ff718800SKamlakant Patel 	gc->of_node = pdev->dev.of_node;
398ff718800SKamlakant Patel 	gc->direction_output = xlp_gpio_dir_output;
399ff718800SKamlakant Patel 	gc->direction_input = xlp_gpio_dir_input;
400ff718800SKamlakant Patel 	gc->set = xlp_gpio_set;
401ff718800SKamlakant Patel 	gc->get = xlp_gpio_get;
402ff718800SKamlakant Patel 
403ff718800SKamlakant Patel 	spin_lock_init(&priv->lock);
4041630a062SKamlakant Patel 
4051630a062SKamlakant Patel 	/* XLP(MIPS) has fixed range for GPIO IRQs, Vulcan(ARM64) does not */
4061630a062SKamlakant Patel 	if (soc_type != GPIO_VARIANT_VULCAN) {
40731bd86d9SBartosz Golaszewski 		irq_base = devm_irq_alloc_descs(&pdev->dev, -1,
40831bd86d9SBartosz Golaszewski 						XLP_GPIO_IRQ_BASE,
40931bd86d9SBartosz Golaszewski 						gc->ngpio, 0);
410287980e4SArnd Bergmann 		if (irq_base < 0) {
411ff718800SKamlakant Patel 			dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
412dd98756dSKamlakant Patel 			return irq_base;
413ff718800SKamlakant Patel 		}
4141630a062SKamlakant Patel 	} else {
4151630a062SKamlakant Patel 		irq_base = 0;
4161630a062SKamlakant Patel 	}
417ff718800SKamlakant Patel 
418e730a595SLinus Walleij 	err = gpiochip_add_data(gc, priv);
419ff718800SKamlakant Patel 	if (err < 0)
42031bd86d9SBartosz Golaszewski 		return err;
421ff718800SKamlakant Patel 
422ff718800SKamlakant Patel 	err = gpiochip_irqchip_add(gc, &xlp_gpio_irq_chip, irq_base,
423ff718800SKamlakant Patel 				handle_level_irq, IRQ_TYPE_NONE);
424ff718800SKamlakant Patel 	if (err) {
425ff718800SKamlakant Patel 		dev_err(&pdev->dev, "Could not connect irqchip to gpiochip!\n");
426ff718800SKamlakant Patel 		goto out_gpio_remove;
427ff718800SKamlakant Patel 	}
428ff718800SKamlakant Patel 
42983ea24fdSKamlakant Patel 	gpiochip_set_chained_irqchip(gc, &xlp_gpio_irq_chip, irq,
43083ea24fdSKamlakant Patel 			xlp_gpio_generic_handler);
43183ea24fdSKamlakant Patel 
432ff718800SKamlakant Patel 	dev_info(&pdev->dev, "registered %d GPIOs\n", gc->ngpio);
433ff718800SKamlakant Patel 
434ff718800SKamlakant Patel 	return 0;
435ff718800SKamlakant Patel 
436ff718800SKamlakant Patel out_gpio_remove:
437ff718800SKamlakant Patel 	gpiochip_remove(gc);
438ff718800SKamlakant Patel 	return err;
439ff718800SKamlakant Patel }
440ff718800SKamlakant Patel 
441baa1b920SKamlakant Patel #ifdef CONFIG_ACPI
442baa1b920SKamlakant Patel static const struct acpi_device_id xlp_gpio_acpi_match[] = {
443baa1b920SKamlakant Patel 	{ "BRCM9006", GPIO_VARIANT_VULCAN },
444*529f75d8SJayachandran C 	{ "CAV9006",  GPIO_VARIANT_VULCAN },
445baa1b920SKamlakant Patel 	{},
446baa1b920SKamlakant Patel };
447baa1b920SKamlakant Patel MODULE_DEVICE_TABLE(acpi, xlp_gpio_acpi_match);
448baa1b920SKamlakant Patel #endif
449baa1b920SKamlakant Patel 
450ff718800SKamlakant Patel static struct platform_driver xlp_gpio_driver = {
451ff718800SKamlakant Patel 	.driver		= {
452ff718800SKamlakant Patel 		.name	= "xlp-gpio",
453ff718800SKamlakant Patel 		.of_match_table = xlp_gpio_of_ids,
454baa1b920SKamlakant Patel 		.acpi_match_table = ACPI_PTR(xlp_gpio_acpi_match),
455ff718800SKamlakant Patel 	},
456ff718800SKamlakant Patel 	.probe		= xlp_gpio_probe,
457ff718800SKamlakant Patel };
458ff718800SKamlakant Patel module_platform_driver(xlp_gpio_driver);
459ff718800SKamlakant Patel 
460ff718800SKamlakant Patel MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
461ff718800SKamlakant Patel MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@broadcom.com>");
462ff718800SKamlakant Patel MODULE_DESCRIPTION("Netlogic XLP GPIO Driver");
463ff718800SKamlakant Patel MODULE_LICENSE("GPL v2");
464