// SPDX-License-Identifier: GPL-2.0-only /* * Core driver for the pin muxing portions of the pin control subsystem * * Copyright (C) 2011-2012 ST-Ericsson SA * Written on behalf of Linaro for ST-Ericsson * Based on bits of regulator core, gpio core and clk core * * Author: Linus Walleij <linus.walleij@linaro.org> * * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
*/ #define pr_fmt(fmt) "pinmux core: " fmt
/* Check that we implement required operations */ if (!ops ||
!ops->get_functions_count ||
!ops->get_function_name ||
!ops->get_function_groups ||
!ops->set_mux) {
dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n"); return -EINVAL;
} /* Check that all functions registered have names */
nfuncs = ops->get_functions_count(pctldev); while (selector < nfuncs) { constchar *fname = ops->get_function_name(pctldev,
selector); if (!fname) {
dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
selector); return -EINVAL;
}
selector++;
}
return 0;
}
int pinmux_validate_map(conststruct pinctrl_map *map, int i)
{ if (!map->data.mux.function) {
pr_err("failed to register map %s (%d): no function given\n",
map->name, i); return -EINVAL;
}
return 0;
}
/** * pinmux_can_be_used_for_gpio() - check if a specific pin * is either muxed to a different function or used as gpio. * * @pctldev: the associated pin controller device * @pin: the pin number in the global pin space * * Controllers not defined as strict will always return true, * menaning that the gpio can be used.
*/ bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsignedint pin)
{ struct pin_desc *desc = pin_desc_get(pctldev, pin); conststruct pinmux_ops *ops = pctldev->desc->pmxops;
/* Can't inspect pin, assume it can be used */ if (!desc || !ops) returntrue;
guard(mutex)(&desc->mux_lock); if (ops->strict && desc->mux_usecount) returnfalse;
return !(ops->strict && !!desc->gpio_owner);
}
/** * pin_request() - request a single pin to be muxed in, typically for GPIO * @pctldev: the associated pin controller device * @pin: the pin number in the global pin space * @owner: a representation of the owner of this pin; typically the device * name that controls its mux function, or the requested GPIO name * @gpio_range: the range matching the GPIO pin if this is a request for a * single GPIO pin
*/ staticint pin_request(struct pinctrl_dev *pctldev, int pin, constchar *owner, struct pinctrl_gpio_range *gpio_range)
{ struct pin_desc *desc; conststruct pinmux_ops *ops = pctldev->desc->pmxops; int status = -EINVAL;
desc = pin_desc_get(pctldev, pin); if (desc == NULL) {
dev_err(pctldev->dev, "pin %d is not registered so it cannot be requested\n",
pin); goto out;
}
dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
pin, desc->name, owner);
scoped_guard(mutex, &desc->mux_lock) { if ((!gpio_range || ops->strict) &&
desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
dev_err(pctldev->dev, "pin %s already requested by %s; cannot claim for %s\n",
desc->name, desc->mux_owner, owner); goto out;
}
if ((gpio_range || ops->strict) && desc->gpio_owner) {
dev_err(pctldev->dev, "pin %s already requested by %s; cannot claim for %s\n",
desc->name, desc->gpio_owner, owner); goto out;
}
if (gpio_range) {
desc->gpio_owner = owner;
} else {
desc->mux_usecount++; if (desc->mux_usecount > 1) return 0;
desc->mux_owner = owner;
}
}
/* Let each pin increase references to this module */ if (!try_module_get(pctldev->owner)) {
dev_err(pctldev->dev, "could not increase module refcount for pin %d\n",
pin);
status = -EINVAL; goto out_free_pin;
}
/* * If there is no kind of request function for the pin we just assume * we got it by default and proceed.
*/ if (gpio_range && ops->gpio_request_enable) /* This requests and enables a single GPIO pin */
status = ops->gpio_request_enable(pctldev, gpio_range, pin); elseif (ops->request)
status = ops->request(pctldev, pin); else
status = 0;
if (status)
module_put(pctldev->owner);
out_free_pin: if (status) {
scoped_guard(mutex, &desc->mux_lock) { if (gpio_range) {
desc->gpio_owner = NULL;
} else {
desc->mux_usecount--; if (!desc->mux_usecount)
desc->mux_owner = NULL;
}
}
}
out: if (status)
dev_err_probe(pctldev->dev, status, "pin-%d (%s)\n",
pin, owner);
return status;
}
/** * pin_free() - release a single muxed in pin so something else can be muxed * @pctldev: pin controller device handling this pin * @pin: the pin to free * @gpio_range: the range matching the GPIO pin if this is a request for a * single GPIO pin * * This function returns a pointer to the previous owner. This is used * for callers that dynamically allocate an owner name so it can be freed * once the pin is free. This is done for GPIO request functions.
*/ staticconstchar *pin_free(struct pinctrl_dev *pctldev, int pin, struct pinctrl_gpio_range *gpio_range)
{ conststruct pinmux_ops *ops = pctldev->desc->pmxops; struct pin_desc *desc; constchar *owner;
desc = pin_desc_get(pctldev, pin); if (desc == NULL) {
dev_err(pctldev->dev, "pin is not registered so it cannot be freed\n"); return NULL;
}
scoped_guard(mutex, &desc->mux_lock) { if (!gpio_range) { /* * A pin should not be freed more times than allocated.
*/ if (WARN_ON(!desc->mux_usecount)) return NULL;
desc->mux_usecount--; if (desc->mux_usecount) return NULL;
}
/* * If there is no kind of request function for the pin we just assume * we got it by default and proceed.
*/ if (gpio_range && ops->gpio_disable_free)
ops->gpio_disable_free(pctldev, gpio_range, pin); elseif (ops->free)
ops->free(pctldev, pin);
module_put(pctldev->owner);
return owner;
}
/** * pinmux_request_gpio() - request pinmuxing for a GPIO pin * @pctldev: pin controller device affected * @pin: the pin to mux in for GPIO * @range: the applicable GPIO range * @gpio: number of requested GPIO
*/ int pinmux_request_gpio(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsignedint pin, unsignedint gpio)
{ constchar *owner; int ret;
/* Conjure some name stating what chip and pin this is taken by */
owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio); if (!owner) return -ENOMEM;
ret = pin_request(pctldev, pin, owner, range); if (ret < 0)
kfree(owner);
return ret;
}
/** * pinmux_free_gpio() - release a pin from GPIO muxing * @pctldev: the pin controller device for the pin * @pin: the affected currently GPIO-muxed in pin * @range: applicable GPIO range
*/ void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsignedint pin, struct pinctrl_gpio_range *range)
{ constchar *owner;
/** * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin * @pctldev: the pin controller handling this pin * @range: applicable GPIO range * @pin: the affected GPIO pin in this controller * @input: true if we set the pin as input, false for output
*/ int pinmux_gpio_direction(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsignedint pin, bool input)
{ conststruct pinmux_ops *ops; int ret;
ops = pctldev->desc->pmxops;
if (ops->gpio_set_direction)
ret = ops->gpio_set_direction(pctldev, range, pin, input); else
ret = 0;
if (!pmxops) {
dev_err(pctldev->dev, "does not support mux function\n"); return -EINVAL;
}
ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function); if (ret < 0) {
dev_err(pctldev->dev, "invalid function %s in map table\n",
map->data.mux.function); return ret;
}
setting->data.mux.func = ret;
ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
&groups, &num_groups); if (ret < 0) {
dev_err(pctldev->dev, "can't query groups for function %s\n",
map->data.mux.function); return ret;
} if (!num_groups) {
dev_err(pctldev->dev, "function %s can't be selected on any group\n",
map->data.mux.function); return -EINVAL;
} if (map->data.mux.group) {
group = map->data.mux.group;
ret = match_string(groups, num_groups, group); if (ret < 0) {
dev_err(pctldev->dev, "invalid group \"%s\" for function \"%s\"\n",
group, map->data.mux.function); return ret;
}
} else {
group = groups[0];
}
ret = pinctrl_get_group_selector(pctldev, group); if (ret < 0) {
dev_err(pctldev->dev, "invalid group %s in map table\n",
map->data.mux.group); return ret;
}
setting->data.mux.group = ret;
return 0;
}
void pinmux_free_setting(conststruct pinctrl_setting *setting)
{ /* This function is currently unused */
}
int pinmux_enable_setting(conststruct pinctrl_setting *setting)
{ struct pinctrl_dev *pctldev = setting->pctldev; conststruct pinctrl_ops *pctlops = pctldev->desc->pctlops; conststruct pinmux_ops *ops = pctldev->desc->pmxops; int ret = 0; constunsignedint *pins = NULL; unsignedint num_pins = 0; int i; struct pin_desc *desc;
if (pctlops->get_group_pins)
ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
&pins, &num_pins);
if (ret) { constchar *gname;
/* errors only affect debug data, so just warn */
gname = pctlops->get_group_name(pctldev,
setting->data.mux.group);
dev_warn(pctldev->dev, "could not get pins for group %s\n",
gname);
num_pins = 0;
}
/* Try to allocate all pins in this group, one by one */ for (i = 0; i < num_pins; i++) {
ret = pin_request(pctldev, pins[i], setting->dev_name, NULL); if (ret) { constchar *gname; constchar *pname;
desc = pin_desc_get(pctldev, pins[i]);
pname = desc ? desc->name : "non-existing";
gname = pctlops->get_group_name(pctldev,
setting->data.mux.group);
dev_err_probe(pctldev->dev, ret, "could not request pin %d (%s) from group %s on device %s\n",
pins[i], pname, gname,
pinctrl_dev_get_name(pctldev)); goto err_pin_request;
}
}
/* Now that we have acquired the pins, encode the mux setting */ for (i = 0; i < num_pins; i++) {
desc = pin_desc_get(pctldev, pins[i]); if (desc == NULL) {
dev_warn(pctldev->dev, "could not get pin desc for pin %d\n",
pins[i]); continue;
}
scoped_guard(mutex, &desc->mux_lock)
desc->mux_setting = &(setting->data.mux);
}
ret = ops->set_mux(pctldev, setting->data.mux.func,
setting->data.mux.group);
if (ret) goto err_set_mux;
return 0;
err_set_mux: for (i = 0; i < num_pins; i++) {
desc = pin_desc_get(pctldev, pins[i]); if (desc) {
scoped_guard(mutex, &desc->mux_lock)
desc->mux_setting = NULL;
}
}
err_pin_request: /* On error release all taken pins */ while (--i >= 0)
pin_free(pctldev, pins[i], NULL);
if (pctlops->get_group_pins)
ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
&pins, &num_pins); if (ret) { constchar *gname;
/* errors only affect debug data, so just warn */
gname = pctlops->get_group_name(pctldev,
setting->data.mux.group);
dev_warn(pctldev->dev, "could not get pins for group %s\n",
gname);
num_pins = 0;
}
/* Flag the descs that no setting is active */ for (i = 0; i < num_pins; i++) {
desc = pin_desc_get(pctldev, pins[i]); if (desc == NULL) {
dev_warn(pctldev->dev, "could not get pin desc for pin %d\n",
pins[i]); continue;
}
scoped_guard(mutex, &desc->mux_lock)
is_equal = (desc->mux_setting == &(setting->data.mux));
gname = pctlops->get_group_name(pctldev,
setting->data.mux.group);
dev_warn(pctldev->dev, "not freeing pin %d (%s) as part of deactivating group %s - it is already used for some other setting",
pins[i], desc->name, gname);
}
}
}
mutex_lock(&pctldev->mutex);
nfuncs = pmxops->get_functions_count(pctldev); while (func_selector < nfuncs) { constchar *func = pmxops->get_function_name(pctldev,
func_selector); constchar * const *groups; unsignedint num_groups; int ret; int i;
ret = pmxops->get_function_groups(pctldev, func_selector,
&groups, &num_groups); if (ret) {
seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
func);
func_selector++; continue;
}
seq_printf(s, "function %d: %s, groups = [ ", func_selector, func); for (i = 0; i < num_groups; i++)
seq_printf(s, "%s ", groups[i]);
seq_puts(s, "]\n");
/* The pin number can be retrived from the pin controller descriptor */ for (i = 0; i < pctldev->desc->npins; i++) { struct pin_desc *desc; bool is_hog = false;
pin = pctldev->desc->pins[i].number;
desc = pin_desc_get(pctldev, pin); /* Skip if we cannot search the pin */ if (desc == NULL) continue;
buf = memdup_user_nul(user_buf, len); if (IS_ERR(buf)) return PTR_ERR(buf);
/* remove leading and trailing spaces of input buffer */
gname = strstrip(buf); if (*gname == '\0') {
ret = -EINVAL; goto exit_free_buf;
}
/* find a separator which is a spacelike character */ for (fname = gname; !isspace(*fname); fname++) { if (*fname == '\0') {
ret = -EINVAL; goto exit_free_buf;
}
}
*fname = '\0';
/* drop extra spaces between function and group names */
fname = skip_spaces(fname + 1); if (*fname == '\0') {
ret = -EINVAL; goto exit_free_buf;
}
ret = pinmux_func_name_to_selector(pctldev, fname); if (ret < 0) {
dev_err(pctldev->dev, "invalid function %s in map table\n", fname); goto exit_free_buf;
}
fsel = ret;
ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups); if (ret) {
dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname); goto exit_free_buf;
}
ret = match_string(groups, num_groups, gname); if (ret < 0) {
dev_err(pctldev->dev, "invalid group %s", gname); goto exit_free_buf;
}
ret = pinctrl_get_group_selector(pctldev, gname); if (ret < 0) goto exit_free_buf;
gsel = ret;
ret = pmxops->set_mux(pctldev, fsel, gsel); if (ret) {
dev_err(pctldev->dev, "set_mux() failed: %d", ret); goto exit_free_buf;
}
ret = len;
/** * pinmux_generic_get_function_groups() - gets the function groups * @pctldev: pin controller device * @selector: function number * @groups: array of pin groups * @ngroups: number of pin groups
*/ int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev, unsignedint selector, constchar * const **groups, unsignedint * const ngroups)
{ struct function_desc *function;
function = radix_tree_lookup(&pctldev->pin_function_tree,
selector); if (!function) {
dev_err(pctldev->dev, "%s could not find function%i\n",
__func__, selector); return -EINVAL;
}
*groups = function->func.groups;
*ngroups = function->func.ngroups;
/** * pinmux_generic_get_function() - returns a function based on the number * @pctldev: pin controller device * @selector: function number
*/ struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev, unsignedint selector)
{ struct function_desc *function;
function = radix_tree_lookup(&pctldev->pin_function_tree,
selector); if (!function) return NULL;
/** * pinmux_generic_add_function() - adds a function group * @pctldev: pin controller device * @name: name of the function * @groups: array of pin groups * @ngroups: number of pin groups * @data: pin controller driver specific data
*/ int pinmux_generic_add_function(struct pinctrl_dev *pctldev, constchar *name, constchar * const *groups, constunsignedint ngroups, void *data)
{ struct pinfunction func = PINCTRL_PINFUNCTION(name, groups, ngroups);
/** * pinmux_generic_add_pinfunction() - adds a function group * @pctldev: pin controller device * @func: pinfunction structure describing the function group * @data: pin controller driver specific data
*/ int pinmux_generic_add_pinfunction(struct pinctrl_dev *pctldev, conststruct pinfunction *func, void *data)
{ struct function_desc *function; int selector, error;
selector = pinmux_func_name_to_selector(pctldev, func->name); if (selector >= 0) return selector;
selector = pctldev->num_functions;
function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL); if (!function) return -ENOMEM;
function->func = *func;
function->data = data;
error = radix_tree_insert(&pctldev->pin_function_tree, selector, function); if (error) return error;
/** * pinmux_generic_remove_function() - removes a numbered function * @pctldev: pin controller device * @selector: function number * * Note that the caller must take care of locking.
*/ int pinmux_generic_remove_function(struct pinctrl_dev *pctldev, unsignedint selector)
{ struct function_desc *function;
function = radix_tree_lookup(&pctldev->pin_function_tree,
selector); if (!function) return -ENOENT;
/** * pinmux_generic_free_functions() - removes all functions * @pctldev: pin controller device * * Note that the caller must take care of locking. The pinctrl * functions are allocated with devm_kzalloc() so no need to free * them here.
*/ void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
{ struct radix_tree_iter iter; void __rcu **slot;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.