Commit d90a5a30 authored by Masahiro Yamada's avatar Masahiro Yamada Committed by Simon Glass

pinctrl: add pin control uclass support

This creates a new framework for handling of pin control devices,
i.e. devices that control different aspects of package pins.

This uclass handles pinmuxing and pin configuration; pinmuxing
controls switching among silicon blocks that share certain physical
pins, pin configuration handles electronic properties such as pin-
biasing, load capacitance etc.

This framework can support the same device tree bindings, but if you
do not need full interface support, you can disable some features to
reduce memory foot print.  Typically around 1.5KB is necessary to
include full-featured uclass support on ARM board (CONFIG_PINCTRL +
CONFIG_PINCTRL_FULL + CONFIG_PINCTRL_GENERIC + CONFIG_PINCTRL_PINMUX),
for example.

We are often limited on code size for SPL.  Besides, we still have
many boards that do not support device tree configuration.  The full
pinctrl, which requires OF_CONTROL, does not make sense for those
boards.  So, this framework also has a Do-It-Yourself (let's say
simple pinctrl) interface.  With CONFIG_PINCTRL_FULL disabled, the
uclass itself provides no systematic mechanism for identifying the
peripheral device, applying pinctrl settings, etc.  They must be
done in each low-level driver.  In return, you can save much memory
footprint and it might be useful especially for SPL.
Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: default avatarSimon Glass <sjg@chromium.org>
parent e6cabe4a
......@@ -40,6 +40,8 @@ source "drivers/pci/Kconfig"
source "drivers/pcmcia/Kconfig"
source "drivers/pinctrl/Kconfig"
source "drivers/power/Kconfig"
source "drivers/ram/Kconfig"
......
obj-$(CONFIG_$(SPL_)DM) += core/
obj-$(CONFIG_$(SPL_)CLK) += clk/
obj-$(CONFIG_$(SPL_)LED) += led/
obj-$(CONFIG_$(SPL_)PINCTRL) += pinctrl/
obj-$(CONFIG_$(SPL_)RAM) += ram/
ifdef CONFIG_SPL_BUILD
......
......@@ -15,6 +15,7 @@
#include <dm/device.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dm/pinctrl.h>
#include <dm/platdata.h>
#include <dm/uclass.h>
#include <dm/uclass-internal.h>
......@@ -286,6 +287,9 @@ int device_probe_child(struct udevice *dev, void *parent_priv)
dev->flags |= DM_FLAG_ACTIVATED;
/* continue regardless of the result of pinctrl */
pinctrl_select_state(dev, "default");
ret = uclass_pre_probe_device(dev);
if (ret)
goto fail;
......
#
# PINCTRL infrastructure and drivers
#
menu "Pin controllers"
config PINCTRL
bool "Support pin controllers"
depends on DM
help
This enables the basic support for pinctrl framework. You may want
to enable some more options depending on what you want to do.
config PINCTRL_FULL
bool "Support full pin controllers"
depends on PINCTRL && OF_CONTROL
default y
help
This provides Linux-compatible device tree interface for the pinctrl
subsystem. This feature depends on device tree configuration because
it parses a device tree to look for the pinctrl device which the
peripheral device is associated with.
If this option is disabled (it is the only possible choice for non-DT
boards), the pinctrl core provides no systematic mechanism for
identifying peripheral devices, applying needed pinctrl settings.
It is totally up to the implementation of each low-level driver.
You can save memory footprint in return for some limitations.
config PINCTRL_GENERIC
bool "Support generic pin controllers"
depends on PINCTRL_FULL
default y
help
Say Y here if you want to use the pinctrl subsystem through the
generic DT interface. If enabled, some functions become available
to parse common properties such as "pins", "groups", "functions" and
some pin configuration parameters. It would be easier if you only
need the generic DT interface for pin muxing and pin configuration.
If you need to handle vendor-specific DT properties, you can disable
this option and implement your own set_state callback in the pinctrl
operations.
config PINMUX
bool "Support pin multiplexing controllers"
depends on PINCTRL_GENERIC
default y
help
This option enables pin multiplexing through the generic pinctrl
framework.
config PINCONF
bool "Support pin configuration controllers"
depends on PINCTRL_GENERIC
help
This option enables pin configuration through the generic pinctrl
framework.
config SPL_PINCTRL
bool "Support pin controlloers in SPL"
depends on SPL && SPL_DM
help
This option is an SPL-variant of the PINCTRL option.
See the help of PINCTRL for details.
config SPL_PINCTRL_FULL
bool "Support full pin controllers in SPL"
depends on SPL_PINCTRL && SPL_OF_CONTROL
default y
help
This option is an SPL-variant of the PINCTRL_FULL option.
See the help of PINCTRL_FULL for details.
config SPL_PINCTRL_GENERIC
bool "Support generic pin controllers in SPL"
depends on SPL_PINCTRL_FULL
default y
help
This option is an SPL-variant of the PINCTRL_GENERIC option.
See the help of PINCTRL_GENERIC for details.
config SPL_PINMUX
bool "Support pin multiplexing controllers in SPL"
depends on SPL_PINCTRL_GENERIC
default y
help
This option is an SPL-variant of the PINMUX option.
See the help of PINMUX for details.
config SPL_PINCONF
bool "Support pin configuration controllers in SPL"
depends on SPL_PINCTRL_GENERIC
help
This option is an SPL-variant of the PINCONF option.
See the help of PINCONF for details.
if PINCTRL || SPL_PINCTRL
endif
endmenu
obj-y += pinctrl-uclass.o
obj-$(CONFIG_$(SPL_)PINCTRL_GENERIC) += pinctrl-generic.o
/*
* Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <linux/compat.h>
#include <dm/device.h>
#include <dm/pinctrl.h>
DECLARE_GLOBAL_DATA_PTR;
/**
* pinctrl_pin_name_to_selector() - return the pin selector for a pin
*
* @dev: pin controller device
* @pin: the pin name to look up
* @return: pin selector, or negative error code on failure
*/
static int pinctrl_pin_name_to_selector(struct udevice *dev, const char *pin)
{
const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
unsigned npins, selector;
if (!ops->get_pins_count || !ops->get_pin_name) {
dev_dbg(dev, "get_pins_count or get_pin_name missing\n");
return -ENOSYS;
}
npins = ops->get_pins_count(dev);
/* See if this pctldev has this pin */
for (selector = 0; selector < npins; selector++) {
const char *pname = ops->get_pin_name(dev, selector);
if (!strcmp(pin, pname))
return selector;
}
return -ENOSYS;
}
/**
* pinctrl_group_name_to_selector() - return the group selector for a group
*
* @dev: pin controller device
* @group: the pin group name to look up
* @return: pin group selector, or negative error code on failure
*/
static int pinctrl_group_name_to_selector(struct udevice *dev,
const char *group)
{
const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
unsigned ngroups, selector;
if (!ops->get_groups_count || !ops->get_group_name) {
dev_dbg(dev, "get_groups_count or get_group_name missing\n");
return -ENOSYS;
}
ngroups = ops->get_groups_count(dev);
/* See if this pctldev has this group */
for (selector = 0; selector < ngroups; selector++) {
const char *gname = ops->get_group_name(dev, selector);
if (!strcmp(group, gname))
return selector;
}
return -ENOSYS;
}
#if CONFIG_IS_ENABLED(PINMUX)
/**
* pinmux_func_name_to_selector() - return the function selector for a function
*
* @dev: pin controller device
* @function: the function name to look up
* @return: function selector, or negative error code on failure
*/
static int pinmux_func_name_to_selector(struct udevice *dev,
const char *function)
{
const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
unsigned nfuncs, selector = 0;
if (!ops->get_functions_count || !ops->get_function_name) {
dev_dbg(dev,
"get_functions_count or get_function_name missing\n");
return -ENOSYS;
}
nfuncs = ops->get_functions_count(dev);
/* See if this pctldev has this function */
for (selector = 0; selector < nfuncs; selector++) {
const char *fname = ops->get_function_name(dev, selector);
if (!strcmp(function, fname))
return selector;
}
return -ENOSYS;
}
/**
* pinmux_enable_setting() - enable pin-mux setting for a certain pin/group
*
* @dev: pin controller device
* @is_group: target of operation (true: pin group, false: pin)
* @selector: pin selector or group selector, depending on @is_group
* @func_selector: function selector
* @return: 0 on success, or negative error code on failure
*/
static int pinmux_enable_setting(struct udevice *dev, bool is_group,
unsigned selector, unsigned func_selector)
{
const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
if (is_group) {
if (!ops->pinmux_group_set) {
dev_dbg(dev, "pinmux_group_set op missing\n");
return -ENOSYS;
}
return ops->pinmux_group_set(dev, selector, func_selector);
} else {
if (!ops->pinmux_set) {
dev_dbg(dev, "pinmux_set op missing\n");
return -ENOSYS;
}
return ops->pinmux_set(dev, selector, func_selector);
}
}
#else
static int pinmux_func_name_to_selector(struct udevice *dev,
const char *function)
{
return 0;
}
static int pinmux_enable_setting(struct udevice *dev, bool is_group,
unsigned selector, unsigned func_selector)
{
return 0;
}
#endif
#if CONFIG_IS_ENABLED(PINCONF)
/**
* pinconf_prop_name_to_param() - return parameter ID for a property name
*
* @dev: pin controller device
* @property: property name in DTS, such as "bias-pull-up", "slew-rate", etc.
* @default_value: return default value in case no value is specified in DTS
* @return: return pamater ID, or negative error code on failure
*/
static int pinconf_prop_name_to_param(struct udevice *dev,
const char *property, u32 *default_value)
{
const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
const struct pinconf_param *p, *end;
if (!ops->pinconf_num_params || !ops->pinconf_params) {
dev_dbg(dev, "pinconf_num_params or pinconf_params missing\n");
return -ENOSYS;
}
p = ops->pinconf_params;
end = p + ops->pinconf_num_params;
/* See if this pctldev supports this parameter */
for (; p < end; p++) {
if (!strcmp(property, p->property)) {
*default_value = p->default_value;
return p->param;
}
}
return -ENOSYS;
}
/**
* pinconf_enable_setting() - apply pin configuration for a certain pin/group
*
* @dev: pin controller device
* @is_group: target of operation (true: pin group, false: pin)
* @selector: pin selector or group selector, depending on @is_group
* @param: configuration paramter
* @argument: argument taken by some configuration parameters
* @return: 0 on success, or negative error code on failure
*/
static int pinconf_enable_setting(struct udevice *dev, bool is_group,
unsigned selector, unsigned param,
u32 argument)
{
const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
if (is_group) {
if (!ops->pinconf_group_set) {
dev_dbg(dev, "pinconf_group_set op missing\n");
return -ENOSYS;
}
return ops->pinconf_group_set(dev, selector, param,
argument);
} else {
if (!ops->pinconf_set) {
dev_dbg(dev, "pinconf_set op missing\n");
return -ENOSYS;
}
return ops->pinconf_set(dev, selector, param, argument);
}
}
#else
static int pinconf_prop_name_to_param(struct udevice *dev,
const char *property, u32 *default_value)
{
return -ENOSYS;
}
static int pinconf_enable_setting(struct udevice *dev, bool is_group,
unsigned selector, unsigned param,
u32 argument)
{
return 0;
}
#endif
/**
* pinctrl_generic_set_state_one() - set state for a certain pin/group
* Apply all pin multiplexing and pin configurations specified by @config
* for a given pin or pin group.
*
* @dev: pin controller device
* @config: pseudo device pointing to config node
* @is_group: target of operation (true: pin group, false: pin)
* @selector: pin selector or group selector, depending on @is_group
* @return: 0 on success, or negative error code on failure
*/
static int pinctrl_generic_set_state_one(struct udevice *dev,
struct udevice *config,
bool is_group, unsigned selector)
{
const void *fdt = gd->fdt_blob;
int node_offset = config->of_offset;
const char *propname;
const void *value;
int prop_offset, len, func_selector, param, ret;
u32 arg, default_val;
for (prop_offset = fdt_first_property_offset(fdt, node_offset);
prop_offset > 0;
prop_offset = fdt_next_property_offset(fdt, prop_offset)) {
value = fdt_getprop_by_offset(fdt, prop_offset,
&propname, &len);
if (!value)
return -EINVAL;
if (!strcmp(propname, "function")) {
func_selector = pinmux_func_name_to_selector(dev,
value);
if (func_selector < 0)
return func_selector;
ret = pinmux_enable_setting(dev, is_group,
selector,
func_selector);
} else {
param = pinconf_prop_name_to_param(dev, propname,
&default_val);
if (param < 0)
continue; /* just skip unknown properties */
if (len >= sizeof(fdt32_t))
arg = fdt32_to_cpu(*(fdt32_t *)value);
else
arg = default_val;
ret = pinconf_enable_setting(dev, is_group,
selector, param, arg);
}
if (ret)
return ret;
}
return 0;
}
/**
* pinctrl_generic_set_state_subnode() - apply all settings in config node
*
* @dev: pin controller device
* @config: pseudo device pointing to config node
* @return: 0 on success, or negative error code on failure
*/
static int pinctrl_generic_set_state_subnode(struct udevice *dev,
struct udevice *config)
{
const void *fdt = gd->fdt_blob;
int node = config->of_offset;
const char *subnode_target_type = "pins";
bool is_group = false;
const char *name;
int strings_count, selector, i, ret;
strings_count = fdt_count_strings(fdt, node, subnode_target_type);
if (strings_count < 0) {
subnode_target_type = "groups";
is_group = true;
strings_count = fdt_count_strings(fdt, node,
subnode_target_type);
if (strings_count < 0)
return -EINVAL;
}
for (i = 0; i < strings_count; i++) {
ret = fdt_get_string_index(fdt, node, subnode_target_type,
i, &name);
if (ret < 0)
return -EINVAL;
if (is_group)
selector = pinctrl_group_name_to_selector(dev, name);
else
selector = pinctrl_pin_name_to_selector(dev, name);
if (selector < 0)
return selector;
ret = pinctrl_generic_set_state_one(dev, config,
is_group, selector);
if (ret)
return ret;
}
return 0;
}
int pinctrl_generic_set_state(struct udevice *dev, struct udevice *config)
{
struct udevice *child;
int ret;
ret = pinctrl_generic_set_state_subnode(dev, config);
if (ret)
return ret;
for (device_find_first_child(config, &child);
child;
device_find_next_child(&child)) {
ret = pinctrl_generic_set_state_subnode(dev, child);
if (ret)
return ret;
}
return 0;
}
/*
* Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <libfdt.h>
#include <linux/err.h>
#include <linux/list.h>
#include <dm/device.h>
#include <dm/lists.h>
#include <dm/pinctrl.h>
#include <dm/uclass.h>
DECLARE_GLOBAL_DATA_PTR;
#if CONFIG_IS_ENABLED(PINCTRL_FULL)
/**
* pinctrl_config_one() - apply pinctrl settings for a single node
*
* @config: pin configuration node
* @return: 0 on success, or negative error code on failure
*/
static int pinctrl_config_one(struct udevice *config)
{
struct udevice *pctldev;
const struct pinctrl_ops *ops;
pctldev = config;
for (;;) {
pctldev = dev_get_parent(pctldev);
if (!pctldev) {
dev_err(config, "could not find pctldev\n");
return -EINVAL;
}
if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
break;
}
ops = pinctrl_get_ops(pctldev);
return ops->set_state(pctldev, config);
}
/**
* pinctrl_select_state_full() - full implementation of pinctrl_select_state
*
* @dev: peripheral device
* @statename: state name, like "default"
* @return: 0 on success, or negative error code on failure
*/
static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
{
const void *fdt = gd->fdt_blob;
int node = dev->of_offset;
char propname[32]; /* long enough */
const fdt32_t *list;
uint32_t phandle;
int config_node;
struct udevice *config;
int state, size, i, ret;
state = fdt_find_string(fdt, node, "pinctrl-names", statename);
if (state < 0) {
char *end;
/*
* If statename is not found in "pinctrl-names",
* assume statename is just the integer state ID.
*/
state = simple_strtoul(statename, &end, 10);
if (*end)
return -EINVAL;
}
snprintf(propname, sizeof(propname), "pinctrl-%d", state);
list = fdt_getprop(fdt, node, propname, &size);
if (!list)
return -EINVAL;
size /= sizeof(*list);
for (i = 0; i < size; i++) {
phandle = fdt32_to_cpu(*list++);
config_node = fdt_node_offset_by_phandle(fdt, phandle);
if (config_node < 0) {
dev_err(dev, "prop %s index %d invalid phandle\n",
propname, i);
return -EINVAL;
}
ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG,
config_node, &config);
if (ret)
return ret;
ret = pinctrl_config_one(config);
if (ret)
return ret;
}
return 0;
}
/**
* pinconfig_post-bind() - post binding for PINCONFIG uclass
* Recursively bind its children as pinconfig devices.
*
* @dev: pinconfig device
* @return: 0 on success, or negative error code on failure
*/
static int pinconfig_post_bind(struct udevice *dev)
{
const void *fdt = gd->fdt_blob;
int offset = dev->of_offset;
const char *name;
int ret;
for (offset = fdt_first_subnode(fdt, offset);
offset > 0;
offset = fdt_next_subnode(fdt, offset)) {
/*
* If this node has "compatible" property, this is not
* a pin configuration node, but a normal device. skip.
*/
fdt_get_property(fdt, offset, "compatible", &ret);
if (ret >= 0)
continue;
if (ret != -FDT_ERR_NOTFOUND)
return ret;
name = fdt_get_name(fdt, offset, NULL);
if (!name)
return -EINVAL;
ret = device_bind_driver_to_node(dev, "pinconfig", name,
offset, NULL);
if (ret)
return ret;
}
return 0;
}
UCLASS_DRIVER(pinconfig) = {
.id = UCLASS_PINCONFIG,
.post_bind = pinconfig_post_bind,
.name = "pinconfig",
};
U_BOOT_DRIVER(pinconfig_generic) = {
.name = "pinconfig",
.id = UCLASS_PINCONFIG,
};
#else
static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
{
return -ENODEV;
}
static int pinconfig_post_bind(struct udevice *dev)
{
return 0;
}
#endif
/**
* pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
*
* @dev: peripheral device
* @return: 0 on success, or negative error code on failure
*/
static int pinctrl_select_state_simple(struct udevice *dev)
{
struct udevice *pctldev;
struct pinctrl_ops *ops;
int ret;
/*
* For simplicity, assume the first device of PINCTRL uclass
* is the correct one. This is most likely OK as there is
* usually only one pinctrl device on the system.
*/
ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
if (ret)
return ret;
ops = pinctrl_get_ops(pctldev);
if (!ops->set_state_simple) {
dev_dbg(dev, "set_state_simple op missing\n");
return -ENOSYS;
}
return ops->set_state_simple(pctldev, dev);
}
int pinctrl_select_state(struct udevice *dev, const char *statename)
{
/*
* Try full-implemented pinctrl first.
* If it fails or is not implemented, try simple one.
*/
if (pinctrl_select_state_full(dev, statename))
return pinctrl_select_state_simple(dev);
return 0;
}
/**
* pinconfig_post-bind() - post binding for PINCTRL uclass
* Recursively bind child nodes as pinconfig devices in case of full pinctrl.
*
* @dev: pinctrl device
* @return: 0 on success, or negative error code on failure
*/
static int pinctrl_post_bind(struct udevice *dev)
{
const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
if (!ops) {
dev_dbg(dev, "ops is not set. Do not bind.\n");
return -EINVAL;
}
/*
* If set_state callback is set, we assume this pinctrl driver is the
* full implementation. In this case, its child nodes should be bound
* so that peripheral devices can easily search in parent devices
* during later DT-parsing.
*/
if (ops->set_state)
return pinconfig_post_bind(dev);
return 0;
}
UCLASS_DRIVER(pinctrl) = {
.id = UCLASS_PINCTRL,
.post_bind = pinctrl_post_bind,
.name = "pinctrl",
};
This diff is collapsed.
......@@ -44,6 +44,8 @@ enum uclass_id {