Skip to content

Commit

Permalink
pinctrl: sunxi: move pinctrl code
Browse files Browse the repository at this point in the history
Move the existing sunxi-specific low level pinctrl routines from
arch/arm/mach-sunxi into the existing GPIO code under drivers/gpio, so
that the common code can be shared outside of arch/arm.

This also takes the opportunity to move some definitions from our
header file into the driver C file, as they are private to the driver
and are not needed elsewhere.

Signed-off-by: Andre Przywara <[email protected]>
Reviewed-by: Samuel Holland <[email protected]>
Tested-by: Samuel Holland <[email protected]>
  • Loading branch information
Andre-ARM committed Oct 22, 2023
1 parent 5ad98c5 commit 20b78c5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 96 deletions.
20 changes: 4 additions & 16 deletions arch/arm/include/asm/arch-sunxi/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* (C) Copyright 2007-2012
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
* Tom Cubie <[email protected]>
*
* Definitions that are shared between the Allwinner pinctrl and GPIO drivers,
* also used by some non-DM SPL code directly.
*/

#ifndef _SUNXI_GPIO_H
Expand Down Expand Up @@ -76,22 +79,6 @@ struct sunxi_gpio_reg {
#define SUN50I_H6_GPIO_POW_MOD_SEL 0x340
#define SUN50I_H6_GPIO_POW_MOD_VAL 0x348

#define BANK_TO_GPIO(bank) (((bank) < SUNXI_GPIO_L) ? \
&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank] : \
&((struct sunxi_gpio_reg *)SUNXI_R_PIO_BASE)->gpio_bank[(bank) - SUNXI_GPIO_L])

#define GPIO_BANK(pin) ((pin) >> 5)
#define GPIO_NUM(pin) ((pin) & 0x1f)

#define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
#define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)

#define GPIO_DRV_INDEX(pin) (((pin) & 0x1f) >> 4)
#define GPIO_DRV_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)

#define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
#define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)

/* GPIO bank sizes */
#define SUNXI_GPIOS_PER_BANK 32

Expand Down Expand Up @@ -217,6 +204,7 @@ struct sunxi_gpio_plat {
char bank_name[3];
};

/* prototypes for the non-DM GPIO/pinctrl functions, used in the SPL */
void sunxi_gpio_set_cfgbank(struct sunxi_gpio *pio, int bank_offset, u32 val);
void sunxi_gpio_set_cfgpin(u32 pin, u32 val);
int sunxi_gpio_get_cfgbank(struct sunxi_gpio *pio, int bank_offset);
Expand Down
1 change: 0 additions & 1 deletion arch/arm/mach-sunxi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ obj-y += board.o
obj-y += clock.o
obj-y += cpu_info.o
obj-y += dram_helpers.o
obj-y += pinmux.o
obj-$(CONFIG_SUN6I_PRCM) += prcm.o
obj-$(CONFIG_AXP_PMIC_BUS) += pmic_bus.o
obj-$(CONFIG_MACH_SUNIV) += clock_sun6i.o
Expand Down
78 changes: 0 additions & 78 deletions arch/arm/mach-sunxi/pinmux.c

This file was deleted.

102 changes: 101 additions & 1 deletion drivers/gpio/sunxi_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,104 @@
#include <asm/gpio.h>
#include <dt-bindings/gpio/gpio.h>

/*
* =======================================================================
* Low level GPIO/pin controller access functions, to be shared by non-DM
* SPL code and the DM pinctrl/GPIO drivers.
* The functions ending in "bank" take a base pointer to a GPIO bank, and
* the pin offset is relative to that bank.
* The functions without "bank" in their name take a linear GPIO number,
* covering all ports, and starting at 0 for PortA.
* =======================================================================
*/

#define BANK_TO_GPIO(bank) (((bank) < SUNXI_GPIO_L) ? \
&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank] : \
&((struct sunxi_gpio_reg *)SUNXI_R_PIO_BASE)->gpio_bank[(bank) - SUNXI_GPIO_L])

#define GPIO_BANK(pin) ((pin) >> 5)
#define GPIO_NUM(pin) ((pin) & 0x1f)

#define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
#define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)

#define GPIO_DRV_INDEX(pin) (((pin) & 0x1f) >> 4)
#define GPIO_DRV_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)

#define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
#define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)

void sunxi_gpio_set_cfgbank(struct sunxi_gpio *pio, int bank_offset, u32 val)
{
u32 index = GPIO_CFG_INDEX(bank_offset);
u32 offset = GPIO_CFG_OFFSET(bank_offset);

clrsetbits_le32(&pio->cfg[index], 0xf << offset, val << offset);
}

void sunxi_gpio_set_cfgpin(u32 pin, u32 val)
{
u32 bank = GPIO_BANK(pin);
struct sunxi_gpio *pio = BANK_TO_GPIO(bank);

sunxi_gpio_set_cfgbank(pio, pin, val);
}

int sunxi_gpio_get_cfgbank(struct sunxi_gpio *pio, int bank_offset)
{
u32 index = GPIO_CFG_INDEX(bank_offset);
u32 offset = GPIO_CFG_OFFSET(bank_offset);
u32 cfg;

cfg = readl(&pio->cfg[index]);
cfg >>= offset;

return cfg & 0xf;
}

int sunxi_gpio_get_cfgpin(u32 pin)
{
u32 bank = GPIO_BANK(pin);
struct sunxi_gpio *pio = BANK_TO_GPIO(bank);

return sunxi_gpio_get_cfgbank(pio, pin);
}

void sunxi_gpio_set_drv(u32 pin, u32 val)
{
u32 bank = GPIO_BANK(pin);
struct sunxi_gpio *pio = BANK_TO_GPIO(bank);

sunxi_gpio_set_drv_bank(pio, pin, val);
}

void sunxi_gpio_set_drv_bank(struct sunxi_gpio *pio, u32 bank_offset, u32 val)
{
u32 index = GPIO_DRV_INDEX(bank_offset);
u32 offset = GPIO_DRV_OFFSET(bank_offset);

clrsetbits_le32(&pio->drv[index], 0x3 << offset, val << offset);
}

void sunxi_gpio_set_pull(u32 pin, u32 val)
{
u32 bank = GPIO_BANK(pin);
struct sunxi_gpio *pio = BANK_TO_GPIO(bank);

sunxi_gpio_set_pull_bank(pio, pin, val);
}

void sunxi_gpio_set_pull_bank(struct sunxi_gpio *pio, int bank_offset, u32 val)
{
u32 index = GPIO_PULL_INDEX(bank_offset);
u32 offset = GPIO_PULL_OFFSET(bank_offset);

clrsetbits_le32(&pio->pull[index], 0x3 << offset, val << offset);
}


/* =========== Non-DM code, used by the SPL. ============ */

#if !CONFIG_IS_ENABLED(DM_GPIO)
static int sunxi_gpio_output(u32 pin, u32 val)
{
Expand Down Expand Up @@ -106,7 +204,9 @@ int sunxi_name_to_gpio(const char *name)
return -1;
return group * 32 + pin;
}
#endif /* DM_GPIO */
#endif /* !DM_GPIO */

/* =========== DM code, used by U-Boot proper. ============ */

#if CONFIG_IS_ENABLED(DM_GPIO)
/* TODO([email protected]): Remove this function and use device tree */
Expand Down

0 comments on commit 20b78c5

Please sign in to comment.