xref: /linux/drivers/gpio/gpio-sa1100.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/arm/mach-sa1100/gpio.c
4  *
5  * Generic SA-1100 GPIO handling
6  */
7 #include <linux/gpio/driver.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/io.h>
11 #include <linux/printk.h>
12 #include <linux/syscore_ops.h>
13 #include <soc/sa1100/pwer.h>
14 #include <mach/hardware.h>
15 #include <mach/irqs.h>
16 #include <mach/generic.h>
17 #include <linux/property.h>
18 
19 const struct software_node sa1100_gpiochip_node = {
20 	.name = "sa1100-gpio",
21 };
22 
23 struct sa1100_gpio_chip {
24 	struct gpio_chip chip;
25 	void __iomem *membase;
26 	int irqbase;
27 	u32 irqmask;
28 	u32 irqrising;
29 	u32 irqfalling;
30 	u32 irqwake;
31 };
32 
33 #define sa1100_gpio_chip(x) container_of(x, struct sa1100_gpio_chip, chip)
34 
35 enum {
36 	R_GPLR = 0x00,
37 	R_GPDR = 0x04,
38 	R_GPSR = 0x08,
39 	R_GPCR = 0x0c,
40 	R_GRER = 0x10,
41 	R_GFER = 0x14,
42 	R_GEDR = 0x18,
43 	R_GAFR = 0x1c,
44 };
45 
46 static int sa1100_gpio_get(struct gpio_chip *chip, unsigned offset)
47 {
48 	return readl_relaxed(sa1100_gpio_chip(chip)->membase + R_GPLR) &
49 		BIT(offset);
50 }
51 
52 static int sa1100_gpio_set(struct gpio_chip *chip, unsigned int offset,
53 			   int value)
54 {
55 	int reg = value ? R_GPSR : R_GPCR;
56 
57 	writel_relaxed(BIT(offset), sa1100_gpio_chip(chip)->membase + reg);
58 
59 	return 0;
60 }
61 
62 static int sa1100_get_direction(struct gpio_chip *chip, unsigned offset)
63 {
64 	void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
65 
66 	if (readl_relaxed(gpdr) & BIT(offset))
67 		return GPIO_LINE_DIRECTION_OUT;
68 
69 	return GPIO_LINE_DIRECTION_IN;
70 }
71 
72 static int sa1100_direction_input(struct gpio_chip *chip, unsigned offset)
73 {
74 	void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
75 	unsigned long flags;
76 
77 	local_irq_save(flags);
78 	writel_relaxed(readl_relaxed(gpdr) & ~BIT(offset), gpdr);
79 	local_irq_restore(flags);
80 
81 	return 0;
82 }
83 
84 static int sa1100_direction_output(struct gpio_chip *chip, unsigned offset, int value)
85 {
86 	void __iomem *gpdr = sa1100_gpio_chip(chip)->membase + R_GPDR;
87 	unsigned long flags;
88 
89 	local_irq_save(flags);
90 	sa1100_gpio_set(chip, offset, value);
91 	writel_relaxed(readl_relaxed(gpdr) | BIT(offset), gpdr);
92 	local_irq_restore(flags);
93 
94 	return 0;
95 }
96 
97 static int sa1100_to_irq(struct gpio_chip *chip, unsigned offset)
98 {
99 	return sa1100_gpio_chip(chip)->irqbase + offset;
100 }
101 
102 static struct sa1100_gpio_chip sa1100_gpio_chip = {
103 	.chip = {
104 		.label			= "gpio",
105 		.get_direction		= sa1100_get_direction,
106 		.direction_input	= sa1100_direction_input,
107 		.direction_output	= sa1100_direction_output,
108 		.set			= sa1100_gpio_set,
109 		.get			= sa1100_gpio_get,
110 		.to_irq			= sa1100_to_irq,
111 		.base			= 0,
112 		.ngpio			= GPIO_MAX + 1,
113 	},
114 	.membase = (void *)&GPLR,
115 	.irqbase = IRQ_GPIO0,
116 };
117 
118 /*
119  * SA1100 GPIO edge detection for IRQs:
120  * IRQs are generated on Falling-Edge, Rising-Edge, or both.
121  * Use this instead of directly setting GRER/GFER.
122  */
123 static void sa1100_update_edge_regs(struct sa1100_gpio_chip *sgc)
124 {
125 	void *base = sgc->membase;
126 	u32 grer, gfer;
127 
128 	grer = sgc->irqrising & sgc->irqmask;
129 	gfer = sgc->irqfalling & sgc->irqmask;
130 
131 	writel_relaxed(grer, base + R_GRER);
132 	writel_relaxed(gfer, base + R_GFER);
133 }
134 
135 static int sa1100_gpio_type(struct irq_data *d, unsigned int type)
136 {
137 	struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
138 	unsigned int mask = BIT(d->hwirq);
139 
140 	if (type == IRQ_TYPE_PROBE) {
141 		if ((sgc->irqrising | sgc->irqfalling) & mask)
142 			return 0;
143 		type = IRQ_TYPE_EDGE_BOTH;
144 	}
145 
146 	if (type & IRQ_TYPE_EDGE_RISING)
147 		sgc->irqrising |= mask;
148 	else
149 		sgc->irqrising &= ~mask;
150 	if (type & IRQ_TYPE_EDGE_FALLING)
151 		sgc->irqfalling |= mask;
152 	else
153 		sgc->irqfalling &= ~mask;
154 
155 	sa1100_update_edge_regs(sgc);
156 
157 	return 0;
158 }
159 
160 /*
161  * GPIO IRQs must be acknowledged.
162  */
163 static void sa1100_gpio_ack(struct irq_data *d)
164 {
165 	struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
166 
167 	writel_relaxed(BIT(d->hwirq), sgc->membase + R_GEDR);
168 }
169 
170 static void sa1100_gpio_mask(struct irq_data *d)
171 {
172 	struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
173 	unsigned int mask = BIT(d->hwirq);
174 
175 	sgc->irqmask &= ~mask;
176 
177 	sa1100_update_edge_regs(sgc);
178 }
179 
180 static void sa1100_gpio_unmask(struct irq_data *d)
181 {
182 	struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
183 	unsigned int mask = BIT(d->hwirq);
184 
185 	sgc->irqmask |= mask;
186 
187 	sa1100_update_edge_regs(sgc);
188 }
189 
190 static int sa1100_gpio_wake(struct irq_data *d, unsigned int on)
191 {
192 	struct sa1100_gpio_chip *sgc = irq_data_get_irq_chip_data(d);
193 	int ret = sa11x0_gpio_set_wake(d->hwirq, on);
194 	if (!ret) {
195 		if (on)
196 			sgc->irqwake |= BIT(d->hwirq);
197 		else
198 			sgc->irqwake &= ~BIT(d->hwirq);
199 	}
200 	return ret;
201 }
202 
203 /*
204  * This is for GPIO IRQs
205  */
206 static struct irq_chip sa1100_gpio_irq_chip = {
207 	.name		= "GPIO",
208 	.irq_ack	= sa1100_gpio_ack,
209 	.irq_mask	= sa1100_gpio_mask,
210 	.irq_unmask	= sa1100_gpio_unmask,
211 	.irq_set_type	= sa1100_gpio_type,
212 	.irq_set_wake	= sa1100_gpio_wake,
213 };
214 
215 static int sa1100_gpio_irqdomain_map(struct irq_domain *d,
216 		unsigned int irq, irq_hw_number_t hwirq)
217 {
218 	struct sa1100_gpio_chip *sgc = d->host_data;
219 
220 	irq_set_chip_data(irq, sgc);
221 	irq_set_chip_and_handler(irq, &sa1100_gpio_irq_chip, handle_edge_irq);
222 	irq_set_probe(irq);
223 
224 	return 0;
225 }
226 
227 static const struct irq_domain_ops sa1100_gpio_irqdomain_ops = {
228 	.map = sa1100_gpio_irqdomain_map,
229 	.xlate = irq_domain_xlate_onetwocell,
230 };
231 
232 static struct irq_domain *sa1100_gpio_irqdomain;
233 
234 /*
235  * IRQ 0-11 (GPIO) handler.  We enter here with the
236  * irq_controller_lock held, and IRQs disabled.  Decode the IRQ
237  * and call the handler.
238  */
239 static void sa1100_gpio_handler(struct irq_desc *desc)
240 {
241 	struct sa1100_gpio_chip *sgc = irq_desc_get_handler_data(desc);
242 	unsigned int irq, mask;
243 	void __iomem *gedr = sgc->membase + R_GEDR;
244 
245 	mask = readl_relaxed(gedr);
246 	do {
247 		/*
248 		 * clear down all currently active IRQ sources.
249 		 * We will be processing them all.
250 		 */
251 		writel_relaxed(mask, gedr);
252 
253 		irq = sgc->irqbase;
254 		do {
255 			if (mask & 1)
256 				generic_handle_irq(irq);
257 			mask >>= 1;
258 			irq++;
259 		} while (mask);
260 
261 		mask = readl_relaxed(gedr);
262 	} while (mask);
263 }
264 
265 static int sa1100_gpio_suspend(void *data)
266 {
267 	struct sa1100_gpio_chip *sgc = &sa1100_gpio_chip;
268 
269 	/*
270 	 * Set the appropriate edges for wakeup.
271 	 */
272 	writel_relaxed(sgc->irqwake & sgc->irqrising, sgc->membase + R_GRER);
273 	writel_relaxed(sgc->irqwake & sgc->irqfalling, sgc->membase + R_GFER);
274 
275 	/*
276 	 * Clear any pending GPIO interrupts.
277 	 */
278 	writel_relaxed(readl_relaxed(sgc->membase + R_GEDR),
279 		       sgc->membase + R_GEDR);
280 
281 	return 0;
282 }
283 
284 static void sa1100_gpio_resume(void *data)
285 {
286 	sa1100_update_edge_regs(&sa1100_gpio_chip);
287 }
288 
289 static const struct syscore_ops sa1100_gpio_syscore_ops = {
290 	.suspend	= sa1100_gpio_suspend,
291 	.resume		= sa1100_gpio_resume,
292 };
293 
294 static struct syscore sa1100_gpio_syscore = {
295 	.ops = &sa1100_gpio_syscore_ops,
296 };
297 
298 static int __init sa1100_gpio_init_devicefs(void)
299 {
300 	register_syscore(&sa1100_gpio_syscore);
301 	return 0;
302 }
303 
304 device_initcall(sa1100_gpio_init_devicefs);
305 
306 static const int sa1100_gpio_irqs[] __initconst = {
307 	/* Install handlers for GPIO 0-10 edge detect interrupts */
308 	IRQ_GPIO0_SC,
309 	IRQ_GPIO1_SC,
310 	IRQ_GPIO2_SC,
311 	IRQ_GPIO3_SC,
312 	IRQ_GPIO4_SC,
313 	IRQ_GPIO5_SC,
314 	IRQ_GPIO6_SC,
315 	IRQ_GPIO7_SC,
316 	IRQ_GPIO8_SC,
317 	IRQ_GPIO9_SC,
318 	IRQ_GPIO10_SC,
319 	/* Install handler for GPIO 11-27 edge detect interrupts */
320 	IRQ_GPIO11_27,
321 };
322 
323 void __init sa1100_init_gpio(void)
324 {
325 	struct sa1100_gpio_chip *sgc = &sa1100_gpio_chip;
326 	int i;
327 
328 	/* clear all GPIO edge detects */
329 	writel_relaxed(0, sgc->membase + R_GFER);
330 	writel_relaxed(0, sgc->membase + R_GRER);
331 	writel_relaxed(-1, sgc->membase + R_GEDR);
332 
333 	sa1100_gpio_irqdomain = irq_domain_create_simple(NULL,
334 			28, IRQ_GPIO0,
335 			&sa1100_gpio_irqdomain_ops, sgc);
336 
337 	for (i = 0; i < ARRAY_SIZE(sa1100_gpio_irqs); i++)
338 		irq_set_chained_handler_and_data(sa1100_gpio_irqs[i],
339 						 sa1100_gpio_handler, sgc);
340 }
341 
342 static int __init sa1100_gpio_init_chip(void)
343 {
344 	struct gpio_chip *gc = &sa1100_gpio_chip.chip;
345 	int error;
346 
347 	error = software_node_register(&sa1100_gpiochip_node);
348 	if (error) {
349 		pr_err("failed to register sa1100 gpiochip software node: %d\n",
350 		       error);
351 		return error;
352 	}
353 
354 	gc->fwnode = software_node_fwnode(&sa1100_gpiochip_node);
355 
356 	error = gpiochip_add_data(gc, NULL);
357 	if (error) {
358 		pr_err("failed to add sa1100 gpiochip: %d\n", error);
359 		gc->fwnode = NULL;
360 		software_node_unregister(&sa1100_gpiochip_node);
361 		return error;
362 	}
363 
364 	return 0;
365 }
366 postcore_initcall(sa1100_gpio_init_chip);
367