gpio: add sl28cpld driver

The gpio block is part of the sl28cpld sl28cpld management controller.
There are three different flavors: the usual input and output where the
direction is configurable, but also input only and output only variants.

Signed-off-by: Michael Walle <michael@walle.cc>
[Rebased]
Signed-off-by: Priyanka Jain <priyanka.jain@nxp.com>
This commit is contained in:
Michael Walle 2022-02-25 18:10:24 +05:30 committed by Priyanka Jain
parent f606c9a895
commit 07d6cb9378
4 changed files with 173 additions and 0 deletions

View file

@ -1164,6 +1164,7 @@ F: arch/sh/
SL28CLPD
M: Michael Walle <michael@walle.cc>
S: Maintained
F: drivers/gpio/sl28cpld-gpio.c
F: drivers/misc/sl28cpld.c
F: drivers/watchdog/sl28cpld-wdt.c

View file

@ -544,4 +544,10 @@ config ZYNQMP_GPIO_MODEPIN
are accessed using xilinx firmware. In modepin register, [3:0] bits
set direction, [7:4] bits read IO, [11:8] bits set/clear IO.
config SL28CPLD_GPIO
bool "Kontron sl28cpld GPIO driver"
depends on DM_GPIO && SL28CPLD
help
Support GPIO access on Kontron sl28cpld board management controllers.
endif

View file

@ -70,4 +70,5 @@ obj-$(CONFIG_NX_GPIO) += nx_gpio.o
obj-$(CONFIG_SIFIVE_GPIO) += sifive-gpio.o
obj-$(CONFIG_NOMADIK_GPIO) += nmk_gpio.o
obj-$(CONFIG_MAX7320_GPIO) += max7320_gpio.o
obj-$(CONFIG_SL28CPLD_GPIO) += sl28cpld-gpio.o
obj-$(CONFIG_ZYNQMP_GPIO_MODEPIN) += zynqmp_gpio_modepin.o

View file

@ -0,0 +1,165 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* GPIO driver for the sl28cpld
*
* Copyright (c) 2021 Michael Walle <michael@walle.cc>
*/
#include <common.h>
#include <dm.h>
#include <asm/gpio.h>
#include <sl28cpld.h>
/* GPIO flavor */
#define SL28CPLD_GPIO_DIR 0x00
#define SL28CPLD_GPIO_OUT 0x01
#define SL28CPLD_GPIO_IN 0x02
/* input-only flavor */
#define SL28CPLD_GPI_IN 0x00
/* output-only flavor */
#define SL28CPLD_GPO_OUT 0x00
enum {
SL28CPLD_GPIO,
SL28CPLD_GPI,
SL28CPLD_GPO,
};
static int sl28cpld_gpio_get_value(struct udevice *dev, unsigned int gpio)
{
ulong type = dev_get_driver_data(dev);
int val, reg;
switch (type) {
case SL28CPLD_GPIO:
reg = SL28CPLD_GPIO_IN;
break;
case SL28CPLD_GPI:
reg = SL28CPLD_GPI_IN;
break;
case SL28CPLD_GPO:
/* we are output only, thus just return the output value */
reg = SL28CPLD_GPO_OUT;
break;
default:
return -EINVAL;
}
val = sl28cpld_read(dev, reg);
return val < 0 ? val : !!(val & BIT(gpio));
}
static int sl28cpld_gpio_set_value(struct udevice *dev, unsigned int gpio,
int value)
{
ulong type = dev_get_driver_data(dev);
uint reg;
switch (type) {
case SL28CPLD_GPIO:
reg = SL28CPLD_GPIO_OUT;
break;
case SL28CPLD_GPO:
reg = SL28CPLD_GPO_OUT;
break;
case SL28CPLD_GPI:
default:
return -EINVAL;
}
if (value)
return sl28cpld_update(dev, reg, 0, BIT(gpio));
else
return sl28cpld_update(dev, reg, BIT(gpio), 0);
}
static int sl28cpld_gpio_direction_input(struct udevice *dev, unsigned int gpio)
{
ulong type = dev_get_driver_data(dev);
switch (type) {
case SL28CPLD_GPI:
return 0;
case SL28CPLD_GPIO:
return sl28cpld_update(dev, SL28CPLD_GPIO_DIR, BIT(gpio), 0);
case SL28CPLD_GPO:
default:
return -EINVAL;
}
}
static int sl28cpld_gpio_direction_output(struct udevice *dev,
unsigned int gpio, int value)
{
ulong type = dev_get_driver_data(dev);
int ret;
/* set_value() will report an error if we are input-only */
ret = sl28cpld_gpio_set_value(dev, gpio, value);
if (ret)
return ret;
if (type == SL28CPLD_GPIO)
return sl28cpld_update(dev, SL28CPLD_GPIO_DIR, 0, BIT(gpio));
return 0;
}
static int sl28cpld_gpio_get_function(struct udevice *dev, unsigned int gpio)
{
ulong type = dev_get_driver_data(dev);
int val;
switch (type) {
case SL28CPLD_GPIO:
val = sl28cpld_read(dev, SL28CPLD_GPIO_DIR);
if (val < 0)
return val;
if (val & BIT(gpio))
return GPIOF_OUTPUT;
else
return GPIOF_INPUT;
case SL28CPLD_GPI:
return GPIOF_INPUT;
case SL28CPLD_GPO:
return GPIOF_OUTPUT;
default:
return -EINVAL;
}
}
static const struct dm_gpio_ops sl28cpld_gpio_ops = {
.direction_input = sl28cpld_gpio_direction_input,
.direction_output = sl28cpld_gpio_direction_output,
.get_value = sl28cpld_gpio_get_value,
.set_value = sl28cpld_gpio_set_value,
.get_function = sl28cpld_gpio_get_function,
};
static int sl28cpld_gpio_probe(struct udevice *dev)
{
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
uc_priv->gpio_count = 8;
uc_priv->bank_name = dev_read_name(dev);
return 0;
}
static const struct udevice_id sl28cpld_gpio_ids[] = {
{ .compatible = "kontron,sl28cpld-gpio", .data = SL28CPLD_GPIO},
{ .compatible = "kontron,sl28cpld-gpo", .data = SL28CPLD_GPO},
{ .compatible = "kontron,sl28cpld-gpi", .data = SL28CPLD_GPI},
{ }
};
U_BOOT_DRIVER(sl28cpld_gpio) = {
.name = "sl28cpld_gpio",
.id = UCLASS_GPIO,
.of_match = sl28cpld_gpio_ids,
.probe = sl28cpld_gpio_probe,
.ops = &sl28cpld_gpio_ops,
};