xref: /linux/drivers/gpio/gpio-merrifield.c (revision d00d2109c3679bf87d412b1667bcb6d42c1ac12f)
193374b76SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0
2ccf6fd6dSAndy Shevchenko /*
3ccf6fd6dSAndy Shevchenko  * Intel Merrifield SoC GPIO driver
4ccf6fd6dSAndy Shevchenko  *
5ccf6fd6dSAndy Shevchenko  * Copyright (c) 2016 Intel Corporation.
6ccf6fd6dSAndy Shevchenko  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
7ccf6fd6dSAndy Shevchenko  */
8ccf6fd6dSAndy Shevchenko 
9dd1dbf94SAndy Shevchenko #include <linux/acpi.h>
10ccf6fd6dSAndy Shevchenko #include <linux/bitops.h>
11ccf6fd6dSAndy Shevchenko #include <linux/gpio/driver.h>
12ccf6fd6dSAndy Shevchenko #include <linux/interrupt.h>
13ccf6fd6dSAndy Shevchenko #include <linux/io.h>
14ccf6fd6dSAndy Shevchenko #include <linux/module.h>
15ccf6fd6dSAndy Shevchenko #include <linux/pci.h>
16ccf6fd6dSAndy Shevchenko #include <linux/pinctrl/consumer.h>
17ccf6fd6dSAndy Shevchenko 
18ccf6fd6dSAndy Shevchenko #define GCCR		0x000	/* controller configuration */
19ccf6fd6dSAndy Shevchenko #define GPLR		0x004	/* pin level r/o */
20ccf6fd6dSAndy Shevchenko #define GPDR		0x01c	/* pin direction */
21ccf6fd6dSAndy Shevchenko #define GPSR		0x034	/* pin set w/o */
22ccf6fd6dSAndy Shevchenko #define GPCR		0x04c	/* pin clear w/o */
23ccf6fd6dSAndy Shevchenko #define GRER		0x064	/* rising edge detect */
24ccf6fd6dSAndy Shevchenko #define GFER		0x07c	/* falling edge detect */
25ccf6fd6dSAndy Shevchenko #define GFBR		0x094	/* glitch filter bypass */
26ccf6fd6dSAndy Shevchenko #define GIMR		0x0ac	/* interrupt mask */
27ccf6fd6dSAndy Shevchenko #define GISR		0x0c4	/* interrupt source */
28ccf6fd6dSAndy Shevchenko #define GITR		0x300	/* input type */
29ccf6fd6dSAndy Shevchenko #define GLPR		0x318	/* level input polarity */
30ccf6fd6dSAndy Shevchenko #define GWMR		0x400	/* wake mask */
31ccf6fd6dSAndy Shevchenko #define GWSR		0x418	/* wake source */
32ccf6fd6dSAndy Shevchenko #define GSIR		0xc00	/* secure input */
33ccf6fd6dSAndy Shevchenko 
34ccf6fd6dSAndy Shevchenko /* Intel Merrifield has 192 GPIO pins */
35ccf6fd6dSAndy Shevchenko #define MRFLD_NGPIO	192
36ccf6fd6dSAndy Shevchenko 
37ccf6fd6dSAndy Shevchenko struct mrfld_gpio_pinrange {
38ccf6fd6dSAndy Shevchenko 	unsigned int gpio_base;
39ccf6fd6dSAndy Shevchenko 	unsigned int pin_base;
40ccf6fd6dSAndy Shevchenko 	unsigned int npins;
41ccf6fd6dSAndy Shevchenko };
42ccf6fd6dSAndy Shevchenko 
43ccf6fd6dSAndy Shevchenko #define GPIO_PINRANGE(gstart, gend, pstart)		\
44ccf6fd6dSAndy Shevchenko 	{						\
45ccf6fd6dSAndy Shevchenko 		.gpio_base = (gstart),			\
46ccf6fd6dSAndy Shevchenko 		.pin_base = (pstart),			\
47ccf6fd6dSAndy Shevchenko 		.npins = (gend) - (gstart) + 1,		\
48ccf6fd6dSAndy Shevchenko 	}
49ccf6fd6dSAndy Shevchenko 
50ccf6fd6dSAndy Shevchenko struct mrfld_gpio {
51ccf6fd6dSAndy Shevchenko 	struct gpio_chip	chip;
52ccf6fd6dSAndy Shevchenko 	void __iomem		*reg_base;
53ccf6fd6dSAndy Shevchenko 	raw_spinlock_t		lock;
54ccf6fd6dSAndy Shevchenko 	struct device		*dev;
55ccf6fd6dSAndy Shevchenko };
56ccf6fd6dSAndy Shevchenko 
57ccf6fd6dSAndy Shevchenko static const struct mrfld_gpio_pinrange mrfld_gpio_ranges[] = {
58ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(0, 11, 146),
59ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(12, 13, 144),
60ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(14, 15, 35),
61ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(16, 16, 164),
62ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(17, 18, 105),
63ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(19, 22, 101),
64ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(23, 30, 107),
65ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(32, 43, 67),
66ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(44, 63, 195),
67ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(64, 67, 140),
68ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(68, 69, 165),
69ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(70, 71, 65),
70ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(72, 76, 228),
71ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(77, 86, 37),
72ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(87, 87, 48),
73ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(88, 88, 47),
74ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(89, 96, 49),
75ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(97, 97, 34),
76ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(102, 119, 83),
77ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(120, 123, 79),
78ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(124, 135, 115),
79ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(137, 142, 158),
80ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(154, 163, 24),
81ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(164, 176, 215),
82ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(177, 189, 127),
83ccf6fd6dSAndy Shevchenko 	GPIO_PINRANGE(190, 191, 178),
84ccf6fd6dSAndy Shevchenko };
85ccf6fd6dSAndy Shevchenko 
86ccf6fd6dSAndy Shevchenko static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset,
87ccf6fd6dSAndy Shevchenko 			      unsigned int reg_type_offset)
88ccf6fd6dSAndy Shevchenko {
89ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(chip);
90ccf6fd6dSAndy Shevchenko 	u8 reg = offset / 32;
91ccf6fd6dSAndy Shevchenko 
92ccf6fd6dSAndy Shevchenko 	return priv->reg_base + reg_type_offset + reg * 4;
93ccf6fd6dSAndy Shevchenko }
94ccf6fd6dSAndy Shevchenko 
95ccf6fd6dSAndy Shevchenko static int mrfld_gpio_get(struct gpio_chip *chip, unsigned int offset)
96ccf6fd6dSAndy Shevchenko {
97ccf6fd6dSAndy Shevchenko 	void __iomem *gplr = gpio_reg(chip, offset, GPLR);
98ccf6fd6dSAndy Shevchenko 
99ccf6fd6dSAndy Shevchenko 	return !!(readl(gplr) & BIT(offset % 32));
100ccf6fd6dSAndy Shevchenko }
101ccf6fd6dSAndy Shevchenko 
102ccf6fd6dSAndy Shevchenko static void mrfld_gpio_set(struct gpio_chip *chip, unsigned int offset,
103ccf6fd6dSAndy Shevchenko 			   int value)
104ccf6fd6dSAndy Shevchenko {
105fcce9f14SAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(chip);
106ccf6fd6dSAndy Shevchenko 	void __iomem *gpsr, *gpcr;
107fcce9f14SAndy Shevchenko 	unsigned long flags;
108fcce9f14SAndy Shevchenko 
109fcce9f14SAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
110ccf6fd6dSAndy Shevchenko 
111ccf6fd6dSAndy Shevchenko 	if (value) {
112ccf6fd6dSAndy Shevchenko 		gpsr = gpio_reg(chip, offset, GPSR);
113ccf6fd6dSAndy Shevchenko 		writel(BIT(offset % 32), gpsr);
114ccf6fd6dSAndy Shevchenko 	} else {
115ccf6fd6dSAndy Shevchenko 		gpcr = gpio_reg(chip, offset, GPCR);
116ccf6fd6dSAndy Shevchenko 		writel(BIT(offset % 32), gpcr);
117ccf6fd6dSAndy Shevchenko 	}
118fcce9f14SAndy Shevchenko 
119fcce9f14SAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
120ccf6fd6dSAndy Shevchenko }
121ccf6fd6dSAndy Shevchenko 
122ccf6fd6dSAndy Shevchenko static int mrfld_gpio_direction_input(struct gpio_chip *chip,
123ccf6fd6dSAndy Shevchenko 				      unsigned int offset)
124ccf6fd6dSAndy Shevchenko {
125ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(chip);
126ccf6fd6dSAndy Shevchenko 	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
127ccf6fd6dSAndy Shevchenko 	unsigned long flags;
128ccf6fd6dSAndy Shevchenko 	u32 value;
129ccf6fd6dSAndy Shevchenko 
130ccf6fd6dSAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
131ccf6fd6dSAndy Shevchenko 
132ccf6fd6dSAndy Shevchenko 	value = readl(gpdr);
133ccf6fd6dSAndy Shevchenko 	value &= ~BIT(offset % 32);
134ccf6fd6dSAndy Shevchenko 	writel(value, gpdr);
135ccf6fd6dSAndy Shevchenko 
136ccf6fd6dSAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
137ccf6fd6dSAndy Shevchenko 
138ccf6fd6dSAndy Shevchenko 	return 0;
139ccf6fd6dSAndy Shevchenko }
140ccf6fd6dSAndy Shevchenko 
141ccf6fd6dSAndy Shevchenko static int mrfld_gpio_direction_output(struct gpio_chip *chip,
142ccf6fd6dSAndy Shevchenko 				       unsigned int offset, int value)
143ccf6fd6dSAndy Shevchenko {
144ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(chip);
145ccf6fd6dSAndy Shevchenko 	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
146ccf6fd6dSAndy Shevchenko 	unsigned long flags;
147ccf6fd6dSAndy Shevchenko 
148ccf6fd6dSAndy Shevchenko 	mrfld_gpio_set(chip, offset, value);
149ccf6fd6dSAndy Shevchenko 
150ccf6fd6dSAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
151ccf6fd6dSAndy Shevchenko 
152ccf6fd6dSAndy Shevchenko 	value = readl(gpdr);
153ccf6fd6dSAndy Shevchenko 	value |= BIT(offset % 32);
154ccf6fd6dSAndy Shevchenko 	writel(value, gpdr);
155ccf6fd6dSAndy Shevchenko 
156ccf6fd6dSAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
157ccf6fd6dSAndy Shevchenko 
158ccf6fd6dSAndy Shevchenko 	return 0;
159ccf6fd6dSAndy Shevchenko }
160ccf6fd6dSAndy Shevchenko 
16146a5c112SAndy Shevchenko static int mrfld_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
16246a5c112SAndy Shevchenko {
16346a5c112SAndy Shevchenko 	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
16446a5c112SAndy Shevchenko 
1657477e137SAndy Shevchenko 	return !(readl(gpdr) & BIT(offset % 32));
16646a5c112SAndy Shevchenko }
16746a5c112SAndy Shevchenko 
168e7a718f9SAndy Shevchenko static int mrfld_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
169e7a718f9SAndy Shevchenko 				   unsigned int debounce)
170e7a718f9SAndy Shevchenko {
171e7a718f9SAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(chip);
172e7a718f9SAndy Shevchenko 	void __iomem *gfbr = gpio_reg(chip, offset, GFBR);
173e7a718f9SAndy Shevchenko 	unsigned long flags;
174e7a718f9SAndy Shevchenko 	u32 value;
175e7a718f9SAndy Shevchenko 
176e7a718f9SAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
177e7a718f9SAndy Shevchenko 
178e7a718f9SAndy Shevchenko 	if (debounce)
179e7a718f9SAndy Shevchenko 		value = readl(gfbr) & ~BIT(offset % 32);
180e7a718f9SAndy Shevchenko 	else
181e7a718f9SAndy Shevchenko 		value = readl(gfbr) | BIT(offset % 32);
182e7a718f9SAndy Shevchenko 	writel(value, gfbr);
183e7a718f9SAndy Shevchenko 
184e7a718f9SAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
185e7a718f9SAndy Shevchenko 
186e7a718f9SAndy Shevchenko 	return 0;
187e7a718f9SAndy Shevchenko }
188e7a718f9SAndy Shevchenko 
1892956b5d9SMika Westerberg static int mrfld_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
1902956b5d9SMika Westerberg 				 unsigned long config)
1912956b5d9SMika Westerberg {
1922956b5d9SMika Westerberg 	u32 debounce;
1932956b5d9SMika Westerberg 
1942956b5d9SMika Westerberg 	if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
1952956b5d9SMika Westerberg 		return -ENOTSUPP;
1962956b5d9SMika Westerberg 
1972956b5d9SMika Westerberg 	debounce = pinconf_to_config_argument(config);
1982956b5d9SMika Westerberg 	return mrfld_gpio_set_debounce(chip, offset, debounce);
1992956b5d9SMika Westerberg }
2002956b5d9SMika Westerberg 
201ccf6fd6dSAndy Shevchenko static void mrfld_irq_ack(struct irq_data *d)
202ccf6fd6dSAndy Shevchenko {
203ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
204ccf6fd6dSAndy Shevchenko 	u32 gpio = irqd_to_hwirq(d);
205ccf6fd6dSAndy Shevchenko 	void __iomem *gisr = gpio_reg(&priv->chip, gpio, GISR);
206fcce9f14SAndy Shevchenko 	unsigned long flags;
207fcce9f14SAndy Shevchenko 
208fcce9f14SAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
209ccf6fd6dSAndy Shevchenko 
210ccf6fd6dSAndy Shevchenko 	writel(BIT(gpio % 32), gisr);
211fcce9f14SAndy Shevchenko 
212fcce9f14SAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
213ccf6fd6dSAndy Shevchenko }
214ccf6fd6dSAndy Shevchenko 
215ccf6fd6dSAndy Shevchenko static void mrfld_irq_unmask_mask(struct irq_data *d, bool unmask)
216ccf6fd6dSAndy Shevchenko {
217ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
218ccf6fd6dSAndy Shevchenko 	u32 gpio = irqd_to_hwirq(d);
219ccf6fd6dSAndy Shevchenko 	void __iomem *gimr = gpio_reg(&priv->chip, gpio, GIMR);
220ccf6fd6dSAndy Shevchenko 	unsigned long flags;
221ccf6fd6dSAndy Shevchenko 	u32 value;
222ccf6fd6dSAndy Shevchenko 
223ccf6fd6dSAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
224ccf6fd6dSAndy Shevchenko 
225ccf6fd6dSAndy Shevchenko 	if (unmask)
226ccf6fd6dSAndy Shevchenko 		value = readl(gimr) | BIT(gpio % 32);
227ccf6fd6dSAndy Shevchenko 	else
228ccf6fd6dSAndy Shevchenko 		value = readl(gimr) & ~BIT(gpio % 32);
229ccf6fd6dSAndy Shevchenko 	writel(value, gimr);
230ccf6fd6dSAndy Shevchenko 
231ccf6fd6dSAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
232ccf6fd6dSAndy Shevchenko }
233ccf6fd6dSAndy Shevchenko 
234ccf6fd6dSAndy Shevchenko static void mrfld_irq_mask(struct irq_data *d)
235ccf6fd6dSAndy Shevchenko {
236ccf6fd6dSAndy Shevchenko 	mrfld_irq_unmask_mask(d, false);
237ccf6fd6dSAndy Shevchenko }
238ccf6fd6dSAndy Shevchenko 
239ccf6fd6dSAndy Shevchenko static void mrfld_irq_unmask(struct irq_data *d)
240ccf6fd6dSAndy Shevchenko {
241ccf6fd6dSAndy Shevchenko 	mrfld_irq_unmask_mask(d, true);
242ccf6fd6dSAndy Shevchenko }
243ccf6fd6dSAndy Shevchenko 
244ccf6fd6dSAndy Shevchenko static int mrfld_irq_set_type(struct irq_data *d, unsigned int type)
245ccf6fd6dSAndy Shevchenko {
246ccf6fd6dSAndy Shevchenko 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
247ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(gc);
248ccf6fd6dSAndy Shevchenko 	u32 gpio = irqd_to_hwirq(d);
249ccf6fd6dSAndy Shevchenko 	void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
250ccf6fd6dSAndy Shevchenko 	void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
251ccf6fd6dSAndy Shevchenko 	void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR);
252ccf6fd6dSAndy Shevchenko 	void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR);
253ccf6fd6dSAndy Shevchenko 	unsigned long flags;
254ccf6fd6dSAndy Shevchenko 	u32 value;
255ccf6fd6dSAndy Shevchenko 
256ccf6fd6dSAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
257ccf6fd6dSAndy Shevchenko 
258ccf6fd6dSAndy Shevchenko 	if (type & IRQ_TYPE_EDGE_RISING)
259ccf6fd6dSAndy Shevchenko 		value = readl(grer) | BIT(gpio % 32);
260ccf6fd6dSAndy Shevchenko 	else
261ccf6fd6dSAndy Shevchenko 		value = readl(grer) & ~BIT(gpio % 32);
262ccf6fd6dSAndy Shevchenko 	writel(value, grer);
263ccf6fd6dSAndy Shevchenko 
264ccf6fd6dSAndy Shevchenko 	if (type & IRQ_TYPE_EDGE_FALLING)
265ccf6fd6dSAndy Shevchenko 		value = readl(gfer) | BIT(gpio % 32);
266ccf6fd6dSAndy Shevchenko 	else
267ccf6fd6dSAndy Shevchenko 		value = readl(gfer) & ~BIT(gpio % 32);
268ccf6fd6dSAndy Shevchenko 	writel(value, gfer);
269ccf6fd6dSAndy Shevchenko 
270ccf6fd6dSAndy Shevchenko 	/*
271ccf6fd6dSAndy Shevchenko 	 * To prevent glitches from triggering an unintended level interrupt,
272ccf6fd6dSAndy Shevchenko 	 * configure GLPR register first and then configure GITR.
273ccf6fd6dSAndy Shevchenko 	 */
274ccf6fd6dSAndy Shevchenko 	if (type & IRQ_TYPE_LEVEL_LOW)
275ccf6fd6dSAndy Shevchenko 		value = readl(glpr) | BIT(gpio % 32);
276ccf6fd6dSAndy Shevchenko 	else
277ccf6fd6dSAndy Shevchenko 		value = readl(glpr) & ~BIT(gpio % 32);
278ccf6fd6dSAndy Shevchenko 	writel(value, glpr);
279ccf6fd6dSAndy Shevchenko 
280ccf6fd6dSAndy Shevchenko 	if (type & IRQ_TYPE_LEVEL_MASK) {
281ccf6fd6dSAndy Shevchenko 		value = readl(gitr) | BIT(gpio % 32);
282ccf6fd6dSAndy Shevchenko 		writel(value, gitr);
283ccf6fd6dSAndy Shevchenko 
284ccf6fd6dSAndy Shevchenko 		irq_set_handler_locked(d, handle_level_irq);
285ccf6fd6dSAndy Shevchenko 	} else if (type & IRQ_TYPE_EDGE_BOTH) {
286ccf6fd6dSAndy Shevchenko 		value = readl(gitr) & ~BIT(gpio % 32);
287ccf6fd6dSAndy Shevchenko 		writel(value, gitr);
288ccf6fd6dSAndy Shevchenko 
289ccf6fd6dSAndy Shevchenko 		irq_set_handler_locked(d, handle_edge_irq);
290ccf6fd6dSAndy Shevchenko 	}
291ccf6fd6dSAndy Shevchenko 
292ccf6fd6dSAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
293ccf6fd6dSAndy Shevchenko 
294ccf6fd6dSAndy Shevchenko 	return 0;
295ccf6fd6dSAndy Shevchenko }
296ccf6fd6dSAndy Shevchenko 
297ccf6fd6dSAndy Shevchenko static int mrfld_irq_set_wake(struct irq_data *d, unsigned int on)
298ccf6fd6dSAndy Shevchenko {
299ccf6fd6dSAndy Shevchenko 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
300ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(gc);
301ccf6fd6dSAndy Shevchenko 	u32 gpio = irqd_to_hwirq(d);
302ccf6fd6dSAndy Shevchenko 	void __iomem *gwmr = gpio_reg(&priv->chip, gpio, GWMR);
303ccf6fd6dSAndy Shevchenko 	void __iomem *gwsr = gpio_reg(&priv->chip, gpio, GWSR);
304ccf6fd6dSAndy Shevchenko 	unsigned long flags;
305ccf6fd6dSAndy Shevchenko 	u32 value;
306ccf6fd6dSAndy Shevchenko 
307ccf6fd6dSAndy Shevchenko 	raw_spin_lock_irqsave(&priv->lock, flags);
308ccf6fd6dSAndy Shevchenko 
309ccf6fd6dSAndy Shevchenko 	/* Clear the existing wake status */
310ccf6fd6dSAndy Shevchenko 	writel(BIT(gpio % 32), gwsr);
311ccf6fd6dSAndy Shevchenko 
312ccf6fd6dSAndy Shevchenko 	if (on)
313ccf6fd6dSAndy Shevchenko 		value = readl(gwmr) | BIT(gpio % 32);
314ccf6fd6dSAndy Shevchenko 	else
315ccf6fd6dSAndy Shevchenko 		value = readl(gwmr) & ~BIT(gpio % 32);
316ccf6fd6dSAndy Shevchenko 	writel(value, gwmr);
317ccf6fd6dSAndy Shevchenko 
318ccf6fd6dSAndy Shevchenko 	raw_spin_unlock_irqrestore(&priv->lock, flags);
319ccf6fd6dSAndy Shevchenko 
320ccf6fd6dSAndy Shevchenko 	dev_dbg(priv->dev, "%sable wake for gpio %u\n", on ? "en" : "dis", gpio);
321ccf6fd6dSAndy Shevchenko 	return 0;
322ccf6fd6dSAndy Shevchenko }
323ccf6fd6dSAndy Shevchenko 
324ccf6fd6dSAndy Shevchenko static struct irq_chip mrfld_irqchip = {
325ccf6fd6dSAndy Shevchenko 	.name		= "gpio-merrifield",
326ccf6fd6dSAndy Shevchenko 	.irq_ack	= mrfld_irq_ack,
327ccf6fd6dSAndy Shevchenko 	.irq_mask	= mrfld_irq_mask,
328ccf6fd6dSAndy Shevchenko 	.irq_unmask	= mrfld_irq_unmask,
329ccf6fd6dSAndy Shevchenko 	.irq_set_type	= mrfld_irq_set_type,
330ccf6fd6dSAndy Shevchenko 	.irq_set_wake	= mrfld_irq_set_wake,
331ccf6fd6dSAndy Shevchenko };
332ccf6fd6dSAndy Shevchenko 
333ccf6fd6dSAndy Shevchenko static void mrfld_irq_handler(struct irq_desc *desc)
334ccf6fd6dSAndy Shevchenko {
335ccf6fd6dSAndy Shevchenko 	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
336ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv = gpiochip_get_data(gc);
337ccf6fd6dSAndy Shevchenko 	struct irq_chip *irqchip = irq_desc_get_chip(desc);
338ccf6fd6dSAndy Shevchenko 	unsigned long base, gpio;
339ccf6fd6dSAndy Shevchenko 
340ccf6fd6dSAndy Shevchenko 	chained_irq_enter(irqchip, desc);
341ccf6fd6dSAndy Shevchenko 
342ccf6fd6dSAndy Shevchenko 	/* Check GPIO controller to check which pin triggered the interrupt */
343ccf6fd6dSAndy Shevchenko 	for (base = 0; base < priv->chip.ngpio; base += 32) {
344ccf6fd6dSAndy Shevchenko 		void __iomem *gisr = gpio_reg(&priv->chip, base, GISR);
345ccf6fd6dSAndy Shevchenko 		void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR);
346ccf6fd6dSAndy Shevchenko 		unsigned long pending, enabled;
347ccf6fd6dSAndy Shevchenko 
348ccf6fd6dSAndy Shevchenko 		pending = readl(gisr);
349ccf6fd6dSAndy Shevchenko 		enabled = readl(gimr);
350ccf6fd6dSAndy Shevchenko 
351ccf6fd6dSAndy Shevchenko 		/* Only interrupts that are enabled */
352ccf6fd6dSAndy Shevchenko 		pending &= enabled;
353ccf6fd6dSAndy Shevchenko 
354ccf6fd6dSAndy Shevchenko 		for_each_set_bit(gpio, &pending, 32) {
355ccf6fd6dSAndy Shevchenko 			unsigned int irq;
356ccf6fd6dSAndy Shevchenko 
357f0fbe7bcSThierry Reding 			irq = irq_find_mapping(gc->irq.domain, base + gpio);
358ccf6fd6dSAndy Shevchenko 			generic_handle_irq(irq);
359ccf6fd6dSAndy Shevchenko 		}
360ccf6fd6dSAndy Shevchenko 	}
361ccf6fd6dSAndy Shevchenko 
362ccf6fd6dSAndy Shevchenko 	chained_irq_exit(irqchip, desc);
363ccf6fd6dSAndy Shevchenko }
364ccf6fd6dSAndy Shevchenko 
365ccf6fd6dSAndy Shevchenko static void mrfld_irq_init_hw(struct mrfld_gpio *priv)
366ccf6fd6dSAndy Shevchenko {
367ccf6fd6dSAndy Shevchenko 	void __iomem *reg;
368ccf6fd6dSAndy Shevchenko 	unsigned int base;
369ccf6fd6dSAndy Shevchenko 
370ccf6fd6dSAndy Shevchenko 	for (base = 0; base < priv->chip.ngpio; base += 32) {
371ccf6fd6dSAndy Shevchenko 		/* Clear the rising-edge detect register */
372ccf6fd6dSAndy Shevchenko 		reg = gpio_reg(&priv->chip, base, GRER);
373ccf6fd6dSAndy Shevchenko 		writel(0, reg);
374ccf6fd6dSAndy Shevchenko 		/* Clear the falling-edge detect register */
375ccf6fd6dSAndy Shevchenko 		reg = gpio_reg(&priv->chip, base, GFER);
376ccf6fd6dSAndy Shevchenko 		writel(0, reg);
377ccf6fd6dSAndy Shevchenko 	}
378ccf6fd6dSAndy Shevchenko }
379ccf6fd6dSAndy Shevchenko 
380*d00d2109SAndy Shevchenko static const char *mrfld_gpio_get_pinctrl_dev_name(struct mrfld_gpio *priv)
381dd1dbf94SAndy Shevchenko {
382*d00d2109SAndy Shevchenko 	struct acpi_device *adev;
383*d00d2109SAndy Shevchenko 	const char *name;
384*d00d2109SAndy Shevchenko 
385*d00d2109SAndy Shevchenko 	adev = acpi_dev_get_first_match_dev("INTC1002", NULL, -1);
386*d00d2109SAndy Shevchenko 	if (adev) {
387*d00d2109SAndy Shevchenko 		name = devm_kstrdup(priv->dev, acpi_dev_name(adev), GFP_KERNEL);
388*d00d2109SAndy Shevchenko 		put_device(&adev->dev);
389*d00d2109SAndy Shevchenko 	} else {
390*d00d2109SAndy Shevchenko 		name = "pinctrl-merrifield";
391*d00d2109SAndy Shevchenko 	}
392*d00d2109SAndy Shevchenko 
393*d00d2109SAndy Shevchenko 	return name;
394dd1dbf94SAndy Shevchenko }
395dd1dbf94SAndy Shevchenko 
396ccf6fd6dSAndy Shevchenko static int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id)
397ccf6fd6dSAndy Shevchenko {
398ccf6fd6dSAndy Shevchenko 	const struct mrfld_gpio_pinrange *range;
399dd1dbf94SAndy Shevchenko 	const char *pinctrl_dev_name;
400ccf6fd6dSAndy Shevchenko 	struct mrfld_gpio *priv;
401ccf6fd6dSAndy Shevchenko 	u32 gpio_base, irq_base;
402ccf6fd6dSAndy Shevchenko 	void __iomem *base;
403ccf6fd6dSAndy Shevchenko 	unsigned int i;
404ccf6fd6dSAndy Shevchenko 	int retval;
405ccf6fd6dSAndy Shevchenko 
406ccf6fd6dSAndy Shevchenko 	retval = pcim_enable_device(pdev);
407ccf6fd6dSAndy Shevchenko 	if (retval)
408ccf6fd6dSAndy Shevchenko 		return retval;
409ccf6fd6dSAndy Shevchenko 
410ccf6fd6dSAndy Shevchenko 	retval = pcim_iomap_regions(pdev, BIT(1) | BIT(0), pci_name(pdev));
411ccf6fd6dSAndy Shevchenko 	if (retval) {
412ccf6fd6dSAndy Shevchenko 		dev_err(&pdev->dev, "I/O memory mapping error\n");
413ccf6fd6dSAndy Shevchenko 		return retval;
414ccf6fd6dSAndy Shevchenko 	}
415ccf6fd6dSAndy Shevchenko 
416ccf6fd6dSAndy Shevchenko 	base = pcim_iomap_table(pdev)[1];
417ccf6fd6dSAndy Shevchenko 
418ccf6fd6dSAndy Shevchenko 	irq_base = readl(base);
419ccf6fd6dSAndy Shevchenko 	gpio_base = readl(sizeof(u32) + base);
420ccf6fd6dSAndy Shevchenko 
421ccf6fd6dSAndy Shevchenko 	/* Release the IO mapping, since we already get the info from BAR1 */
422ccf6fd6dSAndy Shevchenko 	pcim_iounmap_regions(pdev, BIT(1));
423ccf6fd6dSAndy Shevchenko 
424ccf6fd6dSAndy Shevchenko 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
4254b7edaefSMarkus Elfring 	if (!priv)
426ccf6fd6dSAndy Shevchenko 		return -ENOMEM;
427ccf6fd6dSAndy Shevchenko 
428ccf6fd6dSAndy Shevchenko 	priv->dev = &pdev->dev;
429ccf6fd6dSAndy Shevchenko 	priv->reg_base = pcim_iomap_table(pdev)[0];
430ccf6fd6dSAndy Shevchenko 
431ccf6fd6dSAndy Shevchenko 	priv->chip.label = dev_name(&pdev->dev);
432ccf6fd6dSAndy Shevchenko 	priv->chip.parent = &pdev->dev;
433ccf6fd6dSAndy Shevchenko 	priv->chip.request = gpiochip_generic_request;
434ccf6fd6dSAndy Shevchenko 	priv->chip.free = gpiochip_generic_free;
435ccf6fd6dSAndy Shevchenko 	priv->chip.direction_input = mrfld_gpio_direction_input;
436ccf6fd6dSAndy Shevchenko 	priv->chip.direction_output = mrfld_gpio_direction_output;
437ccf6fd6dSAndy Shevchenko 	priv->chip.get = mrfld_gpio_get;
438ccf6fd6dSAndy Shevchenko 	priv->chip.set = mrfld_gpio_set;
43946a5c112SAndy Shevchenko 	priv->chip.get_direction = mrfld_gpio_get_direction;
4402956b5d9SMika Westerberg 	priv->chip.set_config = mrfld_gpio_set_config;
441ccf6fd6dSAndy Shevchenko 	priv->chip.base = gpio_base;
442ccf6fd6dSAndy Shevchenko 	priv->chip.ngpio = MRFLD_NGPIO;
443ccf6fd6dSAndy Shevchenko 	priv->chip.can_sleep = false;
444ccf6fd6dSAndy Shevchenko 
445ccf6fd6dSAndy Shevchenko 	raw_spin_lock_init(&priv->lock);
446ccf6fd6dSAndy Shevchenko 
447ccf6fd6dSAndy Shevchenko 	pci_set_drvdata(pdev, priv);
448ccf6fd6dSAndy Shevchenko 	retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
449ccf6fd6dSAndy Shevchenko 	if (retval) {
450ccf6fd6dSAndy Shevchenko 		dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
451ccf6fd6dSAndy Shevchenko 		return retval;
452ccf6fd6dSAndy Shevchenko 	}
453ccf6fd6dSAndy Shevchenko 
454*d00d2109SAndy Shevchenko 	pinctrl_dev_name = mrfld_gpio_get_pinctrl_dev_name(priv);
455ccf6fd6dSAndy Shevchenko 	for (i = 0; i < ARRAY_SIZE(mrfld_gpio_ranges); i++) {
456ccf6fd6dSAndy Shevchenko 		range = &mrfld_gpio_ranges[i];
457ccf6fd6dSAndy Shevchenko 		retval = gpiochip_add_pin_range(&priv->chip,
458dd1dbf94SAndy Shevchenko 						pinctrl_dev_name,
459ccf6fd6dSAndy Shevchenko 						range->gpio_base,
460ccf6fd6dSAndy Shevchenko 						range->pin_base,
461ccf6fd6dSAndy Shevchenko 						range->npins);
462ccf6fd6dSAndy Shevchenko 		if (retval) {
463ccf6fd6dSAndy Shevchenko 			dev_err(&pdev->dev, "failed to add GPIO pin range\n");
464ccf6fd6dSAndy Shevchenko 			return retval;
465ccf6fd6dSAndy Shevchenko 		}
466ccf6fd6dSAndy Shevchenko 	}
467ccf6fd6dSAndy Shevchenko 
468ccf6fd6dSAndy Shevchenko 	retval = gpiochip_irqchip_add(&priv->chip, &mrfld_irqchip, irq_base,
469e78ade0aSAndy Shevchenko 				      handle_bad_irq, IRQ_TYPE_NONE);
470ccf6fd6dSAndy Shevchenko 	if (retval) {
471ccf6fd6dSAndy Shevchenko 		dev_err(&pdev->dev, "could not connect irqchip to gpiochip\n");
472ccf6fd6dSAndy Shevchenko 		return retval;
473ccf6fd6dSAndy Shevchenko 	}
474ccf6fd6dSAndy Shevchenko 
475ccf6fd6dSAndy Shevchenko 	mrfld_irq_init_hw(priv);
476ccf6fd6dSAndy Shevchenko 
477ccf6fd6dSAndy Shevchenko 	gpiochip_set_chained_irqchip(&priv->chip, &mrfld_irqchip, pdev->irq,
478ccf6fd6dSAndy Shevchenko 				     mrfld_irq_handler);
479ccf6fd6dSAndy Shevchenko 
480ccf6fd6dSAndy Shevchenko 	return 0;
481ccf6fd6dSAndy Shevchenko }
482ccf6fd6dSAndy Shevchenko 
483ccf6fd6dSAndy Shevchenko static const struct pci_device_id mrfld_gpio_ids[] = {
484ccf6fd6dSAndy Shevchenko 	{ PCI_VDEVICE(INTEL, 0x1199) },
485ccf6fd6dSAndy Shevchenko 	{ }
486ccf6fd6dSAndy Shevchenko };
487ccf6fd6dSAndy Shevchenko MODULE_DEVICE_TABLE(pci, mrfld_gpio_ids);
488ccf6fd6dSAndy Shevchenko 
489ccf6fd6dSAndy Shevchenko static struct pci_driver mrfld_gpio_driver = {
490ccf6fd6dSAndy Shevchenko 	.name		= "gpio-merrifield",
491ccf6fd6dSAndy Shevchenko 	.id_table	= mrfld_gpio_ids,
492ccf6fd6dSAndy Shevchenko 	.probe		= mrfld_gpio_probe,
493ccf6fd6dSAndy Shevchenko };
494ccf6fd6dSAndy Shevchenko 
495ccf6fd6dSAndy Shevchenko module_pci_driver(mrfld_gpio_driver);
496ccf6fd6dSAndy Shevchenko 
497ccf6fd6dSAndy Shevchenko MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
498ccf6fd6dSAndy Shevchenko MODULE_DESCRIPTION("Intel Merrifield SoC GPIO driver");
499ccf6fd6dSAndy Shevchenko MODULE_LICENSE("GPL v2");
500