Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / arch / arm / plat-orion / gpio.c
blob595e9cb33c1d2df0d43dfe8fbbdfe307e41b62b9
1 /*
2 * arch/arm/plat-orion/gpio.c
4 * Marvell Orion SoC GPIO handling.
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
11 #define DEBUG
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/bitops.h>
20 #include <linux/io.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/leds.h>
24 #include <linux/of.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_address.h>
27 #include <plat/orion-gpio.h>
30 * GPIO unit register offsets.
32 #define GPIO_OUT_OFF 0x0000
33 #define GPIO_IO_CONF_OFF 0x0004
34 #define GPIO_BLINK_EN_OFF 0x0008
35 #define GPIO_IN_POL_OFF 0x000c
36 #define GPIO_DATA_IN_OFF 0x0010
37 #define GPIO_EDGE_CAUSE_OFF 0x0014
38 #define GPIO_EDGE_MASK_OFF 0x0018
39 #define GPIO_LEVEL_MASK_OFF 0x001c
41 struct orion_gpio_chip {
42 struct gpio_chip chip;
43 spinlock_t lock;
44 void __iomem *base;
45 unsigned long valid_input;
46 unsigned long valid_output;
47 int mask_offset;
48 int secondary_irq_base;
49 struct irq_domain *domain;
52 static void __iomem *GPIO_OUT(struct orion_gpio_chip *ochip)
54 return ochip->base + GPIO_OUT_OFF;
57 static void __iomem *GPIO_IO_CONF(struct orion_gpio_chip *ochip)
59 return ochip->base + GPIO_IO_CONF_OFF;
62 static void __iomem *GPIO_BLINK_EN(struct orion_gpio_chip *ochip)
64 return ochip->base + GPIO_BLINK_EN_OFF;
67 static void __iomem *GPIO_IN_POL(struct orion_gpio_chip *ochip)
69 return ochip->base + GPIO_IN_POL_OFF;
72 static void __iomem *GPIO_DATA_IN(struct orion_gpio_chip *ochip)
74 return ochip->base + GPIO_DATA_IN_OFF;
77 static void __iomem *GPIO_EDGE_CAUSE(struct orion_gpio_chip *ochip)
79 return ochip->base + GPIO_EDGE_CAUSE_OFF;
82 static void __iomem *GPIO_EDGE_MASK(struct orion_gpio_chip *ochip)
84 return ochip->base + ochip->mask_offset + GPIO_EDGE_MASK_OFF;
87 static void __iomem *GPIO_LEVEL_MASK(struct orion_gpio_chip *ochip)
89 return ochip->base + ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
93 static struct orion_gpio_chip orion_gpio_chips[2];
94 static int orion_gpio_chip_count;
96 static inline void
97 __set_direction(struct orion_gpio_chip *ochip, unsigned pin, int input)
99 u32 u;
101 u = readl(GPIO_IO_CONF(ochip));
102 if (input)
103 u |= 1 << pin;
104 else
105 u &= ~(1 << pin);
106 writel(u, GPIO_IO_CONF(ochip));
109 static void __set_level(struct orion_gpio_chip *ochip, unsigned pin, int high)
111 u32 u;
113 u = readl(GPIO_OUT(ochip));
114 if (high)
115 u |= 1 << pin;
116 else
117 u &= ~(1 << pin);
118 writel(u, GPIO_OUT(ochip));
121 static inline void
122 __set_blinking(struct orion_gpio_chip *ochip, unsigned pin, int blink)
124 u32 u;
126 u = readl(GPIO_BLINK_EN(ochip));
127 if (blink)
128 u |= 1 << pin;
129 else
130 u &= ~(1 << pin);
131 writel(u, GPIO_BLINK_EN(ochip));
134 static inline int
135 orion_gpio_is_valid(struct orion_gpio_chip *ochip, unsigned pin, int mode)
137 if (pin >= ochip->chip.ngpio)
138 goto err_out;
140 if ((mode & GPIO_INPUT_OK) && !test_bit(pin, &ochip->valid_input))
141 goto err_out;
143 if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, &ochip->valid_output))
144 goto err_out;
146 return 1;
148 err_out:
149 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
150 return false;
154 * GPIO primitives.
156 static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
158 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
160 if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
161 orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
162 return 0;
164 return -EINVAL;
167 static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
169 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
170 unsigned long flags;
172 if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
173 return -EINVAL;
175 spin_lock_irqsave(&ochip->lock, flags);
176 __set_direction(ochip, pin, 1);
177 spin_unlock_irqrestore(&ochip->lock, flags);
179 return 0;
182 static int orion_gpio_get(struct gpio_chip *chip, unsigned pin)
184 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
185 int val;
187 if (readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
188 val = readl(GPIO_DATA_IN(ochip)) ^ readl(GPIO_IN_POL(ochip));
189 } else {
190 val = readl(GPIO_OUT(ochip));
193 return (val >> pin) & 1;
196 static int
197 orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int value)
199 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
200 unsigned long flags;
202 if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
203 return -EINVAL;
205 spin_lock_irqsave(&ochip->lock, flags);
206 __set_blinking(ochip, pin, 0);
207 __set_level(ochip, pin, value);
208 __set_direction(ochip, pin, 0);
209 spin_unlock_irqrestore(&ochip->lock, flags);
211 return 0;
214 static void orion_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
216 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
217 unsigned long flags;
219 spin_lock_irqsave(&ochip->lock, flags);
220 __set_level(ochip, pin, value);
221 spin_unlock_irqrestore(&ochip->lock, flags);
224 static int orion_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
226 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
228 return irq_create_mapping(ochip->domain,
229 ochip->secondary_irq_base + pin);
233 * Orion-specific GPIO API extensions.
235 static struct orion_gpio_chip *orion_gpio_chip_find(int pin)
237 int i;
239 for (i = 0; i < orion_gpio_chip_count; i++) {
240 struct orion_gpio_chip *ochip = orion_gpio_chips + i;
241 struct gpio_chip *chip = &ochip->chip;
243 if (pin >= chip->base && pin < chip->base + chip->ngpio)
244 return ochip;
247 return NULL;
250 void __init orion_gpio_set_unused(unsigned pin)
252 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
254 if (ochip == NULL)
255 return;
257 pin -= ochip->chip.base;
259 /* Configure as output, drive low. */
260 __set_level(ochip, pin, 0);
261 __set_direction(ochip, pin, 0);
264 void __init orion_gpio_set_valid(unsigned pin, int mode)
266 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
268 if (ochip == NULL)
269 return;
271 pin -= ochip->chip.base;
273 if (mode == 1)
274 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
276 if (mode & GPIO_INPUT_OK)
277 __set_bit(pin, &ochip->valid_input);
278 else
279 __clear_bit(pin, &ochip->valid_input);
281 if (mode & GPIO_OUTPUT_OK)
282 __set_bit(pin, &ochip->valid_output);
283 else
284 __clear_bit(pin, &ochip->valid_output);
287 void orion_gpio_set_blink(unsigned pin, int blink)
289 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
290 unsigned long flags;
292 if (ochip == NULL)
293 return;
295 spin_lock_irqsave(&ochip->lock, flags);
296 __set_level(ochip, pin & 31, 0);
297 __set_blinking(ochip, pin & 31, blink);
298 spin_unlock_irqrestore(&ochip->lock, flags);
300 EXPORT_SYMBOL(orion_gpio_set_blink);
302 #define ORION_BLINK_HALF_PERIOD 100 /* ms */
304 int orion_gpio_led_blink_set(struct gpio_desc *desc, int state,
305 unsigned long *delay_on, unsigned long *delay_off)
307 unsigned gpio = desc_to_gpio(desc);
309 if (delay_on && delay_off && !*delay_on && !*delay_off)
310 *delay_on = *delay_off = ORION_BLINK_HALF_PERIOD;
312 switch (state) {
313 case GPIO_LED_NO_BLINK_LOW:
314 case GPIO_LED_NO_BLINK_HIGH:
315 orion_gpio_set_blink(gpio, 0);
316 gpiod_set_raw_value(desc, state);
317 break;
318 case GPIO_LED_BLINK:
319 orion_gpio_set_blink(gpio, 1);
321 return 0;
323 EXPORT_SYMBOL_GPL(orion_gpio_led_blink_set);
326 /*****************************************************************************
327 * Orion GPIO IRQ
329 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
330 * value of the line or the opposite value.
332 * Level IRQ handlers: DATA_IN is used directly as cause register.
333 * Interrupt are masked by LEVEL_MASK registers.
334 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
335 * Interrupt are masked by EDGE_MASK registers.
336 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
337 * the polarity to catch the next line transaction.
338 * This is a race condition that might not perfectly
339 * work on some use cases.
341 * Every eight GPIO lines are grouped (OR'ed) before going up to main
342 * cause register.
344 * EDGE cause mask
345 * data-in /--------| |-----| |----\
346 * -----| |----- ---- to main cause reg
347 * X \----------------| |----/
348 * polarity LEVEL mask
350 ****************************************************************************/
352 static int gpio_irq_set_type(struct irq_data *d, u32 type)
354 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
355 struct irq_chip_type *ct = irq_data_get_chip_type(d);
356 struct orion_gpio_chip *ochip = gc->private;
357 int pin;
358 u32 u;
360 pin = d->hwirq - ochip->secondary_irq_base;
362 u = readl(GPIO_IO_CONF(ochip)) & (1 << pin);
363 if (!u) {
364 return -EINVAL;
367 type &= IRQ_TYPE_SENSE_MASK;
368 if (type == IRQ_TYPE_NONE)
369 return -EINVAL;
371 /* Check if we need to change chip and handler */
372 if (!(ct->type & type))
373 if (irq_setup_alt_chip(d, type))
374 return -EINVAL;
377 * Configure interrupt polarity.
379 if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
380 u = readl(GPIO_IN_POL(ochip));
381 u &= ~(1 << pin);
382 writel(u, GPIO_IN_POL(ochip));
383 } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
384 u = readl(GPIO_IN_POL(ochip));
385 u |= 1 << pin;
386 writel(u, GPIO_IN_POL(ochip));
387 } else if (type == IRQ_TYPE_EDGE_BOTH) {
388 u32 v;
390 v = readl(GPIO_IN_POL(ochip)) ^ readl(GPIO_DATA_IN(ochip));
393 * set initial polarity based on current input level
395 u = readl(GPIO_IN_POL(ochip));
396 if (v & (1 << pin))
397 u |= 1 << pin; /* falling */
398 else
399 u &= ~(1 << pin); /* rising */
400 writel(u, GPIO_IN_POL(ochip));
402 return 0;
405 static void gpio_irq_handler(struct irq_desc *desc)
407 struct orion_gpio_chip *ochip = irq_desc_get_handler_data(desc);
408 u32 cause, type;
409 int i;
411 if (ochip == NULL)
412 return;
414 cause = readl(GPIO_DATA_IN(ochip)) & readl(GPIO_LEVEL_MASK(ochip));
415 cause |= readl(GPIO_EDGE_CAUSE(ochip)) & readl(GPIO_EDGE_MASK(ochip));
417 for (i = 0; i < ochip->chip.ngpio; i++) {
418 int irq;
420 irq = ochip->secondary_irq_base + i;
422 if (!(cause & (1 << i)))
423 continue;
425 type = irq_get_trigger_type(irq);
426 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
427 /* Swap polarity (race with GPIO line) */
428 u32 polarity;
430 polarity = readl(GPIO_IN_POL(ochip));
431 polarity ^= 1 << i;
432 writel(polarity, GPIO_IN_POL(ochip));
434 generic_handle_irq(irq);
438 #ifdef CONFIG_DEBUG_FS
439 #include <linux/seq_file.h>
441 static void orion_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
444 struct orion_gpio_chip *ochip = gpiochip_get_data(chip);
445 u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
446 const char *label;
447 int i;
449 out = readl_relaxed(GPIO_OUT(ochip));
450 io_conf = readl_relaxed(GPIO_IO_CONF(ochip));
451 blink = readl_relaxed(GPIO_BLINK_EN(ochip));
452 in_pol = readl_relaxed(GPIO_IN_POL(ochip));
453 data_in = readl_relaxed(GPIO_DATA_IN(ochip));
454 cause = readl_relaxed(GPIO_EDGE_CAUSE(ochip));
455 edg_msk = readl_relaxed(GPIO_EDGE_MASK(ochip));
456 lvl_msk = readl_relaxed(GPIO_LEVEL_MASK(ochip));
458 for_each_requested_gpio(chip, i, label) {
459 u32 msk;
460 bool is_out;
462 msk = 1 << i;
463 is_out = !(io_conf & msk);
465 seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
467 if (is_out) {
468 seq_printf(s, " out %s %s\n",
469 out & msk ? "hi" : "lo",
470 blink & msk ? "(blink )" : "");
471 continue;
474 seq_printf(s, " in %s (act %s) - IRQ",
475 (data_in ^ in_pol) & msk ? "hi" : "lo",
476 in_pol & msk ? "lo" : "hi");
477 if (!((edg_msk | lvl_msk) & msk)) {
478 seq_puts(s, " disabled\n");
479 continue;
481 if (edg_msk & msk)
482 seq_puts(s, " edge ");
483 if (lvl_msk & msk)
484 seq_puts(s, " level");
485 seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
488 #else
489 #define orion_gpio_dbg_show NULL
490 #endif
492 static void orion_gpio_unmask_irq(struct irq_data *d)
494 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
495 struct irq_chip_type *ct = irq_data_get_chip_type(d);
496 u32 reg_val;
497 u32 mask = d->mask;
499 irq_gc_lock(gc);
500 reg_val = irq_reg_readl(gc, ct->regs.mask);
501 reg_val |= mask;
502 irq_reg_writel(gc, reg_val, ct->regs.mask);
503 irq_gc_unlock(gc);
506 static void orion_gpio_mask_irq(struct irq_data *d)
508 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
509 struct irq_chip_type *ct = irq_data_get_chip_type(d);
510 u32 mask = d->mask;
511 u32 reg_val;
513 irq_gc_lock(gc);
514 reg_val = irq_reg_readl(gc, ct->regs.mask);
515 reg_val &= ~mask;
516 irq_reg_writel(gc, reg_val, ct->regs.mask);
517 irq_gc_unlock(gc);
520 void __init orion_gpio_init(int gpio_base, int ngpio,
521 void __iomem *base, int mask_offset,
522 int secondary_irq_base,
523 int irqs[4])
525 struct orion_gpio_chip *ochip;
526 struct irq_chip_generic *gc;
527 struct irq_chip_type *ct;
528 char gc_label[16];
529 int i;
531 if (orion_gpio_chip_count == ARRAY_SIZE(orion_gpio_chips))
532 return;
534 snprintf(gc_label, sizeof(gc_label), "orion_gpio%d",
535 orion_gpio_chip_count);
537 ochip = orion_gpio_chips + orion_gpio_chip_count;
538 ochip->chip.label = kstrdup(gc_label, GFP_KERNEL);
539 ochip->chip.request = orion_gpio_request;
540 ochip->chip.direction_input = orion_gpio_direction_input;
541 ochip->chip.get = orion_gpio_get;
542 ochip->chip.direction_output = orion_gpio_direction_output;
543 ochip->chip.set = orion_gpio_set;
544 ochip->chip.to_irq = orion_gpio_to_irq;
545 ochip->chip.base = gpio_base;
546 ochip->chip.ngpio = ngpio;
547 ochip->chip.can_sleep = 0;
548 ochip->chip.dbg_show = orion_gpio_dbg_show;
550 spin_lock_init(&ochip->lock);
551 ochip->base = (void __iomem *)base;
552 ochip->valid_input = 0;
553 ochip->valid_output = 0;
554 ochip->mask_offset = mask_offset;
555 ochip->secondary_irq_base = secondary_irq_base;
557 gpiochip_add_data(&ochip->chip, ochip);
560 * Mask and clear GPIO interrupts.
562 writel(0, GPIO_EDGE_CAUSE(ochip));
563 writel(0, GPIO_EDGE_MASK(ochip));
564 writel(0, GPIO_LEVEL_MASK(ochip));
566 /* Setup the interrupt handlers. Each chip can have up to 4
567 * interrupt handlers, with each handler dealing with 8 GPIO
568 * pins. */
570 for (i = 0; i < 4; i++) {
571 if (irqs[i]) {
572 irq_set_chained_handler_and_data(irqs[i],
573 gpio_irq_handler,
574 ochip);
578 gc = irq_alloc_generic_chip("orion_gpio_irq", 2,
579 secondary_irq_base,
580 ochip->base, handle_level_irq);
581 gc->private = ochip;
582 ct = gc->chip_types;
583 ct->regs.mask = ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
584 ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
585 ct->chip.irq_mask = orion_gpio_mask_irq;
586 ct->chip.irq_unmask = orion_gpio_unmask_irq;
587 ct->chip.irq_set_type = gpio_irq_set_type;
588 ct->chip.name = ochip->chip.label;
590 ct++;
591 ct->regs.mask = ochip->mask_offset + GPIO_EDGE_MASK_OFF;
592 ct->regs.ack = GPIO_EDGE_CAUSE_OFF;
593 ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
594 ct->chip.irq_ack = irq_gc_ack_clr_bit;
595 ct->chip.irq_mask = orion_gpio_mask_irq;
596 ct->chip.irq_unmask = orion_gpio_unmask_irq;
597 ct->chip.irq_set_type = gpio_irq_set_type;
598 ct->handler = handle_edge_irq;
599 ct->chip.name = ochip->chip.label;
601 irq_setup_generic_chip(gc, IRQ_MSK(ngpio), IRQ_GC_INIT_MASK_CACHE,
602 IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
604 /* Setup irq domain on top of the generic chip. */
605 ochip->domain = irq_domain_add_legacy(NULL,
606 ochip->chip.ngpio,
607 ochip->secondary_irq_base,
608 ochip->secondary_irq_base,
609 &irq_domain_simple_ops,
610 ochip);
611 if (!ochip->domain)
612 panic("%s: couldn't allocate irq domain (DT).\n",
613 ochip->chip.label);
615 orion_gpio_chip_count++;