gpio: add helper GPIOD_FLAGS_OUTPUT
[oweals/u-boot.git] / drivers / gpio / at91_gpio.c
index 98dbd8210eb86046e0ecdeab42ed89810d1ec532..3621cf240888a0e550a873b7b10377d7a27f4637 100644 (file)
@@ -1,17 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
  *
  * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
  *
  *  Copyright (C) 2005 HP Labs
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <config.h>
 #include <common.h>
 #include <clk.h>
 #include <dm.h>
+#include <malloc.h>
 #include <asm/io.h>
 #include <linux/sizes.h>
 #include <asm/gpio.h>
@@ -211,7 +211,7 @@ int at91_pio3_set_d_periph(unsigned port, unsigned pin, int use_pullup)
        return 0;
 }
 
-#ifdef CONFIG_DM_GPIO
+#if CONFIG_IS_ENABLED(DM_GPIO)
 static bool at91_get_port_output(struct at91_port *at91_port, int offset)
 {
        u32 mask, val;
@@ -458,7 +458,7 @@ int at91_get_pio_value(unsigned port, unsigned pin)
        return 0;
 }
 
-#ifndef CONFIG_DM_GPIO
+#if !CONFIG_IS_ENABLED(DM_GPIO)
 /* Common GPIO API */
 
 int gpio_request(unsigned gpio, const char *label)
@@ -500,7 +500,7 @@ int gpio_set_value(unsigned gpio, int value)
 }
 #endif
 
-#ifdef CONFIG_DM_GPIO
+#if CONFIG_IS_ENABLED(DM_GPIO)
 
 struct at91_port_priv {
        struct at91_port *regs;
@@ -557,6 +557,28 @@ static int at91_gpio_get_function(struct udevice *dev, unsigned offset)
                return GPIOF_INPUT;
 }
 
+static const char *at91_get_bank_name(uint32_t base_addr)
+{
+       switch (base_addr) {
+       case ATMEL_BASE_PIOA:
+               return "PIOA";
+       case ATMEL_BASE_PIOB:
+               return "PIOB";
+       case ATMEL_BASE_PIOC:
+               return "PIOC";
+#if (ATMEL_PIO_PORTS > 3)
+       case ATMEL_BASE_PIOD:
+               return "PIOD";
+#if (ATMEL_PIO_PORTS > 4)
+       case ATMEL_BASE_PIOE:
+               return "PIOE";
+#endif
+#endif
+       }
+
+       return "undefined";
+}
+
 static const struct dm_gpio_ops gpio_at91_ops = {
        .direction_input        = at91_gpio_direction_input,
        .direction_output       = at91_gpio_direction_output,
@@ -583,14 +605,15 @@ static int at91_gpio_probe(struct udevice *dev)
 
        clk_free(&clk);
 
-       uc_priv->bank_name = plat->bank_name;
-       uc_priv->gpio_count = GPIO_PER_BANK;
-
 #if CONFIG_IS_ENABLED(OF_CONTROL)
-       plat->base_addr = (uint32_t)dev_get_addr_ptr(dev);
+       plat->base_addr = (uint32_t)devfdt_get_addr_ptr(dev);
 #endif
+       plat->bank_name = at91_get_bank_name(plat->base_addr);
        port->regs = (struct at91_port *)plat->base_addr;
 
+       uc_priv->bank_name = plat->bank_name;
+       uc_priv->gpio_count = GPIO_PER_BANK;
+
        return 0;
 }