]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
dm: rpi: Convert GPIO driver to driver model
authorSimon Glass <sjg@chromium.org>
Mon, 22 Sep 2014 23:30:56 +0000 (17:30 -0600)
committerSimon Glass <sjg@chromium.org>
Wed, 22 Oct 2014 16:36:57 +0000 (10:36 -0600)
Convert the BCM2835 GPIO driver to use driver model, and switch over
Raspberry Pi to use this, since it is the only board.

Signed-off-by: Simon Glass <sjg@chromium.org>
Tested-by: Stephen Warren <swarren@wwwdotorg.org>
Acked-by: Stephen Warren <swarren@wwwdotorg.org>
arch/arm/include/asm/arch-bcm2835/gpio.h
board/raspberrypi/rpi_b/rpi_b.c
drivers/gpio/bcm2835_gpio.c
include/configs/rpi_b.h

index 9a49b6e05efa5af346953a183f88d415d98fe46d..db42896201b321fb623af79c78a22addddf9de34 100644 (file)
@@ -52,4 +52,13 @@ struct bcm2835_gpio_regs {
        u32 gppudclk[2];
 };
 
        u32 gppudclk[2];
 };
 
+/**
+ * struct bcm2835_gpio_platdata - GPIO platform description
+ *
+ * @base: Base address of GPIO controller
+ */
+struct bcm2835_gpio_platdata {
+       unsigned long base;
+};
+
 #endif /* _BCM2835_GPIO_H_ */
 #endif /* _BCM2835_GPIO_H_ */
index 220bb90dc1ae025ddf37e8fe9636201843c00650..447c940f63154aa560881046a1d742b6bc1e7d5e 100644 (file)
 
 #include <common.h>
 #include <config.h>
 
 #include <common.h>
 #include <config.h>
+#include <dm.h>
 #include <fdt_support.h>
 #include <lcd.h>
 #include <mmc.h>
 #include <fdt_support.h>
 #include <lcd.h>
 #include <mmc.h>
+#include <asm/gpio.h>
 #include <asm/arch/mbox.h>
 #include <asm/arch/sdhci.h>
 #include <asm/global_data.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
 #include <asm/arch/mbox.h>
 #include <asm/arch/sdhci.h>
 #include <asm/global_data.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+static const struct bcm2835_gpio_platdata gpio_platdata = {
+       .base = BCM2835_GPIO_BASE,
+};
+
+U_BOOT_DEVICE(bcm2835_gpios) = {
+       .name = "gpio_bcm2835",
+       .platdata = &gpio_platdata,
+};
+
 struct msg_get_arm_mem {
        struct bcm2835_mbox_hdr hdr;
        struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
 struct msg_get_arm_mem {
        struct bcm2835_mbox_hdr hdr;
        struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
index 97b51371145e81de9f268a33f001c72d6f63ff98..332cfc2b231835ca7c4ffd9f0ead44b2a8351730 100644 (file)
  */
 
 #include <common.h>
  */
 
 #include <common.h>
+#include <dm.h>
+#include <errno.h>
 #include <asm/gpio.h>
 #include <asm/io.h>
 
 #include <asm/gpio.h>
 #include <asm/io.h>
 
-inline int gpio_is_valid(unsigned gpio)
+#define GPIO_NAME_SIZE         20
+
+struct bcm2835_gpios {
+       char label[BCM2835_GPIO_COUNT][GPIO_NAME_SIZE];
+       struct bcm2835_gpio_regs *reg;
+};
+
+/**
+ * gpio_is_requested() - check if a GPIO has been requested
+ *
+ * @bank:      Bank to check
+ * @offset:    GPIO offset within bank to check
+ * @return true if marked as requested, false if not
+ */
+static inline bool gpio_is_requested(struct bcm2835_gpios *gpios, int offset)
 {
 {
-       return (gpio < BCM2835_GPIO_COUNT);
+       return *gpios->label[offset] != '\0';
 }
 
 }
 
-int gpio_request(unsigned gpio, const char *label)
+static int check_requested(struct udevice *dev, unsigned offset,
+                          const char *func)
 {
 {
-       return !gpio_is_valid(gpio);
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
+       struct gpio_dev_priv *uc_priv = dev->uclass_priv;
+
+       if (!gpio_is_requested(gpios, offset)) {
+               printf("omap_gpio: %s: error: gpio %s%d not requested\n",
+                      func, uc_priv->bank_name, offset);
+               return -EPERM;
+       }
+
+       return 0;
 }
 
 }
 
-int gpio_free(unsigned gpio)
+static int bcm2835_gpio_request(struct udevice *dev, unsigned offset,
+                               const char *label)
 {
 {
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
+
+       if (gpio_is_requested(gpios, offset))
+               return -EBUSY;
+
+       strncpy(gpios->label[offset], label, GPIO_NAME_SIZE);
+       gpios->label[offset][GPIO_NAME_SIZE - 1] = '\0';
+
        return 0;
 }
 
        return 0;
 }
 
-int gpio_direction_input(unsigned gpio)
+static int bcm2835_gpio_free(struct udevice *dev, unsigned offset)
 {
 {
-       struct bcm2835_gpio_regs *reg =
-               (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
+       int ret;
+
+       ret = check_requested(dev, offset, __func__);
+       if (ret)
+               return ret;
+       gpios->label[offset][0] = '\0';
+
+       return 0;
+}
+
+static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
+{
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
        unsigned val;
 
        unsigned val;
 
-       val = readl(&reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+       val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
        val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
        val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
        val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
        val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
-       writel(val, &reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+       writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
 
        return 0;
 }
 
 
        return 0;
 }
 
-int gpio_direction_output(unsigned gpio, int value)
+static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
+                                        int value)
 {
 {
-       struct bcm2835_gpio_regs *reg =
-               (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
        unsigned val;
 
        gpio_set_value(gpio, value);
 
        unsigned val;
 
        gpio_set_value(gpio, value);
 
-       val = readl(&reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+       val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
        val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
        val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
        val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
        val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
-       writel(val, &reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+       writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
 
        return 0;
 }
 
 
        return 0;
 }
 
-int gpio_get_value(unsigned gpio)
+static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio)
+{
+       u32 val;
+
+       val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+       val &= BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio);
+       return val ? true : false;
+}
+
+static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
 {
 {
-       struct bcm2835_gpio_regs *reg =
-               (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
        unsigned val;
 
        unsigned val;
 
-       val = readl(&reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
+       val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
 
        return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
 }
 
 
        return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
 }
 
-int gpio_set_value(unsigned gpio, int value)
+static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
 {
 {
-       struct bcm2835_gpio_regs *reg =
-               (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
-       u32 *output_reg = value ? reg->gpset : reg->gpclr;
+       const struct bcm2835_gpios *gpios = dev_get_priv(dev);
+
+       return bcm2835_get_value(gpios, gpio);
+}
+
+static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
+                                 int value)
+{
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
+       u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
 
        writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
                                &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
 
        return 0;
 }
 
        writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
                                &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
 
        return 0;
 }
+
+static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
+{
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
+
+       if (!gpio_is_requested(gpios, offset))
+               return GPIOF_UNUSED;
+
+       /* GPIOF_FUNC is not implemented yet */
+       if (bcm2835_gpio_is_output(gpios, offset))
+               return GPIOF_OUTPUT;
+       else
+               return GPIOF_INPUT;
+}
+
+static int bcm2835_gpio_get_state(struct udevice *dev, unsigned int offset,
+                                 char *buf, int bufsize)
+{
+       struct gpio_dev_priv *uc_priv = dev->uclass_priv;
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
+       const char *label;
+       bool requested;
+       bool is_output;
+       int size;
+
+       label = gpios->label[offset];
+       is_output = bcm2835_gpio_is_output(gpios, offset);
+       size = snprintf(buf, bufsize, "%s%d: ",
+                       uc_priv->bank_name ? uc_priv->bank_name : "", offset);
+       buf += size;
+       bufsize -= size;
+       requested = gpio_is_requested(gpios, offset);
+       snprintf(buf, bufsize, "%s: %d [%c]%s%s",
+                is_output ? "out" : " in",
+                bcm2835_get_value(gpios, offset),
+                requested ? 'x' : ' ',
+                requested ? " " : "",
+                label);
+
+       return 0;
+}
+
+static const struct dm_gpio_ops gpio_bcm2835_ops = {
+       .request                = bcm2835_gpio_request,
+       .free                   = bcm2835_gpio_free,
+       .direction_input        = bcm2835_gpio_direction_input,
+       .direction_output       = bcm2835_gpio_direction_output,
+       .get_value              = bcm2835_gpio_get_value,
+       .set_value              = bcm2835_gpio_set_value,
+       .get_function           = bcm2835_gpio_get_function,
+       .get_state              = bcm2835_gpio_get_state,
+};
+
+static int bcm2835_gpio_probe(struct udevice *dev)
+{
+       struct bcm2835_gpios *gpios = dev_get_priv(dev);
+       struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
+       struct gpio_dev_priv *uc_priv = dev->uclass_priv;
+
+       uc_priv->bank_name = "GPIO";
+       uc_priv->gpio_count = BCM2835_GPIO_COUNT;
+       gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
+
+       return 0;
+}
+
+U_BOOT_DRIVER(gpio_bcm2835) = {
+       .name   = "gpio_bcm2835",
+       .id     = UCLASS_GPIO,
+       .ops    = &gpio_bcm2835_ops,
+       .probe  = bcm2835_gpio_probe,
+       .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
+};
index 2d698094800f2c7bbc50d6f7b8ef63e27b5b7742..d9475e950b798188244e15269348c1a79ec5c687 100644 (file)
  */
 #define CONFIG_MACH_TYPE               MACH_TYPE_BCM2708
 
  */
 #define CONFIG_MACH_TYPE               MACH_TYPE_BCM2708
 
+/* Enable driver model */
+#define CONFIG_DM
+#define CONFIG_CMD_DM
+#define CONFIG_DM_GPIO
+
 /* Memory layout */
 #define CONFIG_NR_DRAM_BANKS           1
 #define CONFIG_SYS_SDRAM_BASE          0x00000000
 /* Memory layout */
 #define CONFIG_NR_DRAM_BANKS           1
 #define CONFIG_SYS_SDRAM_BASE          0x00000000