2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-08-27 03:44:30 +00:00
|
|
|
/*
|
2020-09-14 15:01:57 +00:00
|
|
|
* Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
|
|
|
|
* Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
|
2015-08-27 03:44:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2015-08-27 03:44:30 +00:00
|
|
|
#include <dm/pinctrl.h>
|
2020-09-14 15:01:57 +00:00
|
|
|
#include <dt-bindings/pinctrl/sandbox-pinmux.h>
|
|
|
|
#include <log.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2015-08-27 03:44:30 +00:00
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
/*
|
|
|
|
* This driver emulates a pin controller with the following rules:
|
|
|
|
* - The pinctrl config for each pin must be set individually
|
|
|
|
* - The first three pins (P0-P2) must be muxed as a group
|
|
|
|
* - The next two pins (P3-P4) must be muxed as a group
|
|
|
|
* - The last four pins (P5-P8) must be muxed individually
|
|
|
|
*/
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
static const char * const sandbox_pins[] = {
|
2020-09-14 15:01:57 +00:00
|
|
|
#define PIN(x) \
|
|
|
|
[x] = "P" #x
|
|
|
|
PIN(0),
|
|
|
|
PIN(1),
|
|
|
|
PIN(2),
|
|
|
|
PIN(3),
|
|
|
|
PIN(4),
|
|
|
|
PIN(5),
|
|
|
|
PIN(6),
|
|
|
|
PIN(7),
|
|
|
|
PIN(8),
|
|
|
|
#undef PIN
|
2015-08-27 03:44:30 +00:00
|
|
|
};
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
static const char * const sandbox_pins_muxing[][2] = {
|
|
|
|
{ "UART TX", "I2C SCL" },
|
|
|
|
{ "UART RX", "I2C SDA" },
|
|
|
|
{ "SPI SCLK", "I2S SCK" },
|
|
|
|
{ "SPI MOSI", "I2S SD" },
|
|
|
|
{ "SPI MISO", "I2S WS" },
|
|
|
|
{ "GPIO0", "SPI CS0" },
|
|
|
|
{ "GPIO1", "SPI CS1" },
|
|
|
|
{ "GPIO2", "PWM0" },
|
|
|
|
{ "GPIO3", "PWM1" },
|
2018-10-24 12:10:22 +00:00
|
|
|
};
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
#define SANDBOX_GROUP_I2C_UART 0
|
|
|
|
#define SANDBOX_GROUP_SPI_I2S 1
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
static const char * const sandbox_groups[] = {
|
2020-09-14 15:01:57 +00:00
|
|
|
[SANDBOX_GROUP_I2C_UART] = "I2C_UART",
|
|
|
|
[SANDBOX_GROUP_SPI_I2S] = "SPI_I2S",
|
2015-08-27 03:44:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const sandbox_functions[] = {
|
2020-09-14 15:01:57 +00:00
|
|
|
#define FUNC(id) \
|
|
|
|
[SANDBOX_PINMUX_##id] = #id
|
|
|
|
FUNC(UART),
|
|
|
|
FUNC(I2C),
|
|
|
|
FUNC(SPI),
|
|
|
|
FUNC(I2S),
|
|
|
|
FUNC(GPIO),
|
|
|
|
FUNC(CS),
|
|
|
|
FUNC(PWM),
|
|
|
|
#undef FUNC
|
2015-08-27 03:44:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct pinconf_param sandbox_conf_params[] = {
|
|
|
|
{ "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
|
|
|
|
{ "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
|
|
|
|
{ "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
|
|
|
|
{ "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
|
|
|
|
{ "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
|
|
|
|
{ "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
|
|
|
|
{ "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
|
|
|
|
{ "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
|
|
|
|
{ "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
|
|
|
|
{ "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
|
|
|
|
{ "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
|
|
|
|
};
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
/* Bitfield used to save param and value of each pin/selector */
|
|
|
|
struct sandbox_pinctrl_priv {
|
|
|
|
unsigned int mux;
|
|
|
|
unsigned int pins_param[ARRAY_SIZE(sandbox_pins)];
|
|
|
|
unsigned int pins_value[ARRAY_SIZE(sandbox_pins)];
|
|
|
|
};
|
2020-01-13 10:35:11 +00:00
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
static int sandbox_get_pins_count(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return ARRAY_SIZE(sandbox_pins);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
|
|
|
|
{
|
|
|
|
return sandbox_pins[selector];
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:10:22 +00:00
|
|
|
static int sandbox_get_pin_muxing(struct udevice *dev,
|
|
|
|
unsigned int selector,
|
|
|
|
char *buf, int size)
|
|
|
|
{
|
2020-01-13 10:35:11 +00:00
|
|
|
const struct pinconf_param *p;
|
2020-09-14 15:01:57 +00:00
|
|
|
struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
|
2020-01-13 10:35:11 +00:00
|
|
|
int i;
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
snprintf(buf, size, "%s",
|
|
|
|
sandbox_pins_muxing[selector][!!(priv->mux & BIT(selector))]);
|
2018-10-24 12:10:22 +00:00
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
if (priv->pins_param[selector]) {
|
2020-01-13 10:35:11 +00:00
|
|
|
for (i = 0, p = sandbox_conf_params;
|
|
|
|
i < ARRAY_SIZE(sandbox_conf_params);
|
|
|
|
i++, p++) {
|
2020-09-14 15:01:57 +00:00
|
|
|
if ((priv->pins_param[selector] & BIT(p->param)) &&
|
|
|
|
(!!(priv->pins_value[selector] & BIT(p->param)) ==
|
2020-01-13 10:35:11 +00:00
|
|
|
p->default_value)) {
|
|
|
|
strncat(buf, " ", size);
|
|
|
|
strncat(buf, p->property, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strncat(buf, ".", size);
|
|
|
|
|
2018-10-24 12:10:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
static int sandbox_get_groups_count(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return ARRAY_SIZE(sandbox_groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *sandbox_get_group_name(struct udevice *dev,
|
|
|
|
unsigned selector)
|
|
|
|
{
|
|
|
|
return sandbox_groups[selector];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_get_functions_count(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return ARRAY_SIZE(sandbox_functions);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *sandbox_get_function_name(struct udevice *dev,
|
|
|
|
unsigned selector)
|
|
|
|
{
|
|
|
|
return sandbox_functions[selector];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
|
|
|
|
unsigned func_selector)
|
|
|
|
{
|
2020-09-14 15:01:57 +00:00
|
|
|
int mux;
|
|
|
|
struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
|
|
|
|
pin_selector, sandbox_get_pin_name(dev, pin_selector),
|
|
|
|
func_selector, sandbox_get_function_name(dev, func_selector));
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
if (pin_selector < 5)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (func_selector) {
|
|
|
|
case SANDBOX_PINMUX_GPIO:
|
|
|
|
mux = 0;
|
|
|
|
break;
|
|
|
|
case SANDBOX_PINMUX_CS:
|
|
|
|
case SANDBOX_PINMUX_PWM:
|
|
|
|
mux = BIT(pin_selector);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->mux &= ~BIT(pin_selector);
|
|
|
|
priv->mux |= mux;
|
|
|
|
priv->pins_param[pin_selector] = 0;
|
|
|
|
priv->pins_value[pin_selector] = 0;
|
2020-01-13 10:35:11 +00:00
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_pinmux_group_set(struct udevice *dev,
|
|
|
|
unsigned group_selector,
|
|
|
|
unsigned func_selector)
|
|
|
|
{
|
2020-09-14 15:01:57 +00:00
|
|
|
bool mux;
|
|
|
|
int i, group_start, group_end;
|
|
|
|
struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
|
|
|
|
unsigned int mask;
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
|
|
|
|
group_selector, sandbox_get_group_name(dev, group_selector),
|
|
|
|
func_selector, sandbox_get_function_name(dev, func_selector));
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
if (group_selector == SANDBOX_GROUP_I2C_UART) {
|
|
|
|
group_start = 0;
|
|
|
|
group_end = 1;
|
|
|
|
|
|
|
|
if (func_selector == SANDBOX_PINMUX_UART)
|
|
|
|
mux = false;
|
|
|
|
else if (func_selector == SANDBOX_PINMUX_I2C)
|
|
|
|
mux = true;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
} else if (group_selector == SANDBOX_GROUP_SPI_I2S) {
|
|
|
|
group_start = 2;
|
|
|
|
group_end = 4;
|
|
|
|
|
|
|
|
if (func_selector == SANDBOX_PINMUX_SPI)
|
|
|
|
mux = false;
|
|
|
|
else if (func_selector == SANDBOX_PINMUX_I2S)
|
|
|
|
mux = true;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = GENMASK(group_end, group_start);
|
|
|
|
priv->mux &= ~mask;
|
|
|
|
priv->mux |= mux ? mask : 0;
|
|
|
|
|
|
|
|
for (i = group_start; i < group_end; i++) {
|
|
|
|
priv->pins_param[i] = 0;
|
|
|
|
priv->pins_value[i] = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
static int sandbox_pinmux_property_set(struct udevice *dev, u32 pinmux_group)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned pin_selector = pinmux_group & 0xFFFF;
|
|
|
|
unsigned func_selector = pinmux_group >> 16;
|
|
|
|
|
|
|
|
ret = sandbox_pinmux_set(dev, pin_selector, func_selector);
|
|
|
|
return ret ? ret : pin_selector;
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
|
|
|
|
unsigned param, unsigned argument)
|
|
|
|
{
|
2020-09-14 15:01:57 +00:00
|
|
|
struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
|
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
|
|
|
|
pin_selector, sandbox_get_pin_name(dev, pin_selector),
|
|
|
|
param, argument);
|
|
|
|
|
2020-09-14 15:01:57 +00:00
|
|
|
priv->pins_param[pin_selector] |= BIT(param);
|
2020-01-13 10:35:11 +00:00
|
|
|
if (argument)
|
2020-09-14 15:01:57 +00:00
|
|
|
priv->pins_value[pin_selector] |= BIT(param);
|
2020-01-13 10:35:11 +00:00
|
|
|
else
|
2020-09-14 15:01:57 +00:00
|
|
|
priv->pins_value[pin_selector] &= ~BIT(param);
|
2020-01-13 10:35:11 +00:00
|
|
|
|
2015-08-27 03:44:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_pinconf_group_set(struct udevice *dev,
|
|
|
|
unsigned group_selector,
|
|
|
|
unsigned param, unsigned argument)
|
|
|
|
{
|
|
|
|
debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
|
|
|
|
group_selector, sandbox_get_group_name(dev, group_selector),
|
|
|
|
param, argument);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct pinctrl_ops sandbox_pinctrl_ops = {
|
|
|
|
.get_pins_count = sandbox_get_pins_count,
|
|
|
|
.get_pin_name = sandbox_get_pin_name,
|
2018-10-24 12:10:22 +00:00
|
|
|
.get_pin_muxing = sandbox_get_pin_muxing,
|
2015-08-27 03:44:30 +00:00
|
|
|
.get_groups_count = sandbox_get_groups_count,
|
|
|
|
.get_group_name = sandbox_get_group_name,
|
|
|
|
.get_functions_count = sandbox_get_functions_count,
|
|
|
|
.get_function_name = sandbox_get_function_name,
|
|
|
|
.pinmux_set = sandbox_pinmux_set,
|
|
|
|
.pinmux_group_set = sandbox_pinmux_group_set,
|
2020-09-14 15:01:57 +00:00
|
|
|
.pinmux_property_set = sandbox_pinmux_property_set,
|
2015-08-27 03:44:30 +00:00
|
|
|
.pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
|
|
|
|
.pinconf_params = sandbox_conf_params,
|
|
|
|
.pinconf_set = sandbox_pinconf_set,
|
|
|
|
.pinconf_group_set = sandbox_pinconf_group_set,
|
|
|
|
.set_state = pinctrl_generic_set_state,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_pinctrl_match[] = {
|
|
|
|
{ .compatible = "sandbox,pinctrl" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(sandbox_pinctrl) = {
|
|
|
|
.name = "sandbox_pinctrl",
|
|
|
|
.id = UCLASS_PINCTRL,
|
|
|
|
.of_match = sandbox_pinctrl_match,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct sandbox_pinctrl_priv),
|
2015-08-27 03:44:30 +00:00
|
|
|
.ops = &sandbox_pinctrl_ops,
|
|
|
|
};
|