13 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/bitops.h>
31 #define GPIO_OUT_OFF 0x0000
32 #define GPIO_IO_CONF_OFF 0x0004
33 #define GPIO_BLINK_EN_OFF 0x0008
34 #define GPIO_IN_POL_OFF 0x000c
35 #define GPIO_DATA_IN_OFF 0x0010
36 #define GPIO_EDGE_CAUSE_OFF 0x0014
37 #define GPIO_EDGE_MASK_OFF 0x0018
38 #define GPIO_LEVEL_MASK_OFF 0x001c
93 static int orion_gpio_chip_count;
100 u =
readl(GPIO_IO_CONF(ochip));
105 writel(u, GPIO_IO_CONF(ochip));
125 u =
readl(GPIO_BLINK_EN(ochip));
130 writel(u, GPIO_BLINK_EN(ochip));
136 if (pin >= ochip->
chip.ngpio)
148 pr_debug(
"%s: invalid GPIO %d\n", __func__, pin);
155 static int orion_gpio_request(
struct gpio_chip *
chip,
unsigned pin)
160 if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
161 orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
167 static int orion_gpio_direction_input(
struct gpio_chip *chip,
unsigned pin)
173 if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
177 __set_direction(ochip, pin, 1);
178 spin_unlock_irqrestore(&ochip->
lock, flags);
183 static int orion_gpio_get(
struct gpio_chip *chip,
unsigned pin)
189 if (
readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
195 return (val >> pin) & 1;
199 orion_gpio_direction_output(
struct gpio_chip *chip,
unsigned pin,
int value)
205 if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
209 __set_blinking(ochip, pin, 0);
210 __set_level(ochip, pin, value);
211 __set_direction(ochip, pin, 0);
212 spin_unlock_irqrestore(&ochip->
lock, flags);
217 static void orion_gpio_set(
struct gpio_chip *chip,
unsigned pin,
int value)
224 __set_level(ochip, pin, value);
225 spin_unlock_irqrestore(&ochip->
lock, flags);
228 static int orion_gpio_to_irq(
struct gpio_chip *chip,
unsigned pin)
244 for (i = 0; i < orion_gpio_chip_count; i++) {
246 struct gpio_chip *chip = &ochip->
chip;
248 if (pin >= chip->base && pin < chip->base + chip->ngpio)
262 pin -= ochip->
chip.base;
265 __set_level(ochip, pin, 0);
266 __set_direction(ochip, pin, 0);
276 pin -= ochip->
chip.base;
281 if (mode & GPIO_INPUT_OK)
286 if (mode & GPIO_OUTPUT_OK)
301 __set_level(ochip, pin & 31, 0);
302 __set_blinking(ochip, pin & 31, blink);
303 spin_unlock_irqrestore(&ochip->
lock, flags);
307 #define ORION_BLINK_HALF_PERIOD 100
310 unsigned long *delay_on,
unsigned long *delay_off)
313 if (delay_on && delay_off && !*delay_on && !*delay_off)
358 struct irq_chip_generic *
gc = irq_data_get_irq_chip_data(d);
359 struct irq_chip_type *
ct = irq_data_get_chip_type(d);
366 u =
readl(GPIO_IO_CONF(ochip)) & (1 <<
pin);
376 if (!(ct->type & type))
384 u =
readl(GPIO_IN_POL(ochip));
386 writel(u, GPIO_IN_POL(ochip));
388 u =
readl(GPIO_IN_POL(ochip));
390 writel(u, GPIO_IN_POL(ochip));
399 u =
readl(GPIO_IN_POL(ochip));
404 writel(u, GPIO_IN_POL(ochip));
409 static void gpio_irq_handler(
unsigned irq,
struct irq_desc *
desc)
419 cause |=
readl(GPIO_EDGE_CAUSE(ochip)) &
readl(GPIO_EDGE_MASK(ochip));
421 for (i = 0; i < ochip->
chip.ngpio; i++) {
426 if (!(cause & (1 << i)))
434 polarity =
readl(GPIO_IN_POL(ochip));
436 writel(polarity, GPIO_IN_POL(ochip));
449 struct irq_chip_generic *gc;
450 struct irq_chip_type *
ct;
454 if (orion_gpio_chip_count ==
ARRAY_SIZE(orion_gpio_chips))
457 snprintf(gc_label,
sizeof(gc_label),
"orion_gpio%d",
458 orion_gpio_chip_count);
460 ochip = orion_gpio_chips + orion_gpio_chip_count;
462 ochip->
chip.request = orion_gpio_request;
463 ochip->
chip.direction_input = orion_gpio_direction_input;
464 ochip->
chip.get = orion_gpio_get;
465 ochip->
chip.direction_output = orion_gpio_direction_output;
466 ochip->
chip.set = orion_gpio_set;
467 ochip->
chip.to_irq = orion_gpio_to_irq;
469 ochip->
chip.ngpio = ngpio;
470 ochip->
chip.can_sleep = 0;
472 ochip->
chip.of_node = np;
487 writel(0, GPIO_EDGE_CAUSE(ochip));
488 writel(0, GPIO_EDGE_MASK(ochip));
489 writel(0, GPIO_LEVEL_MASK(ochip));
495 for (i = 0; i < 4; i++) {
498 irq_set_chained_handler(irqs[i], gpio_irq_handler);
511 ct->chip.irq_set_type = gpio_irq_set_type;
512 ct->chip.name = ochip->
chip.label;
521 ct->chip.irq_set_type = gpio_irq_set_type;
523 ct->chip.name = ochip->
chip.label;
536 panic(
"%s: couldn't allocate irq domain (DT).\n",
539 orion_gpio_chip_count++;
550 int secondary_irq_base;
552 ret = of_property_read_u32(np,
"ngpio", &ngpio);
555 ret = of_property_read_u32(np,
"mask-offset", &mask_offset);
564 secondary_irq_base = irq_gpio_base + (32 * orion_gpio_chip_count);
565 gpio_base = 32 * orion_gpio_chip_count;
571 for (i = 0; i < 4; i++)
575 secondary_irq_base, irqs);
578 pr_err(
"%s: %s: missing mandatory property\n", __func__, np->
name);
585 for_each_compatible_node(np,
NULL,
"marvell,orion-gpio")
586 orion_gpio_of_init_one(np, irq_gpio_base);