// SPDX-License-Identifier: GPL-2.0-only /* * Core driver for 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) "pinctrl core: " fmt
/* Mutex taken to protect pinctrl_list */ static DEFINE_MUTEX(pinctrl_list_mutex);
/* Mutex taken to protect pinctrl_maps */
DEFINE_MUTEX(pinctrl_maps_mutex);
/* Mutex taken to protect pinctrldev_list */ static DEFINE_MUTEX(pinctrldev_list_mutex);
/* Global list of pin control devices (struct pinctrl_dev) */ static LIST_HEAD(pinctrldev_list);
/* List of pin controller handles (struct pinctrl) */ static LIST_HEAD(pinctrl_list);
/* List of pinctrl maps (struct pinctrl_maps) */
LIST_HEAD(pinctrl_maps);
/** * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support * * Usually this function is called by platforms without pinctrl driver support * but run with some shared drivers using pinctrl APIs. * After calling this function, the pinctrl core will return successfully * with creating a dummy state for the driver to keep going smoothly.
*/ void pinctrl_provide_dummies(void)
{
pinctrl_dummy_state = true;
}
constchar *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
{ /* We're not allowed to register devices without name */ return pctldev->desc->name;
}
EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
/** * get_pinctrl_dev_from_devname() - look up pin controller device * @devname: the name of a device instance, as returned by dev_name() * * Looks up a pin control device matching a certain device name or pure device * pointer, the pure device pointer will take precedence.
*/ struct pinctrl_dev *get_pinctrl_dev_from_devname(constchar *devname)
{ struct pinctrl_dev *pctldev;
if (!devname) return NULL;
mutex_lock(&pinctrldev_list_mutex);
list_for_each_entry(pctldev, &pinctrldev_list, node) { if (!strcmp(dev_name(pctldev->dev), devname)) { /* Matched on device name */
mutex_unlock(&pinctrldev_list_mutex); return pctldev;
}
}
/** * pin_get_from_name() - look up a pin number from a name * @pctldev: the pin control device to lookup the pin on * @name: the name of the pin to look up
*/ int pin_get_from_name(struct pinctrl_dev *pctldev, constchar *name)
{ unsignedint i, pin;
/* The pin number can be retrived from the pin controller descriptor */ for (i = 0; i < pctldev->desc->npins; i++) { struct pin_desc *desc;
pin = pctldev->desc->pins[i].number;
desc = pin_desc_get(pctldev, pin); /* Pin space may be sparse */ if (desc && !strcmp(name, desc->name)) return pin;
}
return -EINVAL;
}
/** * pin_get_name() - look up a pin name from a pin id * @pctldev: the pin control device to lookup the pin on * @pin: pin number/id to look up
*/ constchar *pin_get_name(struct pinctrl_dev *pctldev, constunsignedint pin)
{ conststruct pin_desc *desc;
desc = pin_desc_get(pctldev, pin); if (!desc) {
dev_err(pctldev->dev, "failed to get pin(%d) name\n",
pin); return NULL;
}
/* Deletes a range of pin descriptors */ staticvoid pinctrl_free_pindescs(struct pinctrl_dev *pctldev, conststruct pinctrl_pin_desc *pins, unsignedint num_pins)
{ int i;
for (i = 0; i < num_pins; i++) { struct pin_desc *pindesc;
pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
pins[i].number); if (pindesc) {
radix_tree_delete(&pctldev->pin_desc_tree,
pins[i].number); if (pindesc->dynamic_name)
kfree(pindesc->name);
}
kfree(pindesc);
}
}
staticint pinctrl_register_pins(struct pinctrl_dev *pctldev, conststruct pinctrl_pin_desc *pins, unsignedint num_descs)
{ unsignedint i; int ret = 0;
for (i = 0; i < num_descs; i++) {
ret = pinctrl_register_one_pin(pctldev, &pins[i]); if (ret) return ret;
}
return 0;
}
/** * gpio_to_pin() - GPIO range GPIO number to pin number translation * @range: GPIO range used for the translation * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * * Finds the pin number for a given GPIO using the specified GPIO range * as a base for translation. The distinction between linear GPIO ranges * and pin list based GPIO ranges is managed correctly by this function. * * This function assumes the gpio is part of the specified GPIO range, use * only after making sure this is the case (e.g. by calling it on the * result of successful pinctrl_get_device_gpio_range calls)!
*/ staticinlineint gpio_to_pin(struct pinctrl_gpio_range *range, struct gpio_chip *gc, unsignedint offset)
{ unsignedint pin = gc->base + offset - range->base; if (range->pins) return range->pins[pin]; else return range->pin_base + pin;
}
/** * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range * @pctldev: pin controller device to check * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * * Tries to match a GPIO pin number to the ranges handled by a certain pin * controller, return the range or NULL
*/ staticstruct pinctrl_gpio_range *
pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, struct gpio_chip *gc, unsignedint offset)
{ struct pinctrl_gpio_range *range;
mutex_lock(&pctldev->mutex); /* Loop over the ranges */
list_for_each_entry(range, &pctldev->gpio_ranges, node) { /* Check if we're in the valid range */ if ((gc->base + offset) >= range->base &&
(gc->base + offset) < range->base + range->npins) {
mutex_unlock(&pctldev->mutex); return range;
}
}
mutex_unlock(&pctldev->mutex); return NULL;
}
/** * pinctrl_ready_for_gpio_range() - check if other GPIO pins of * the same GPIO chip are in range * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * * This function is complement of pinctrl_match_gpio_range(). If the return * value of pinctrl_match_gpio_range() is NULL, this function could be used * to check whether pinctrl device is ready or not. Maybe some GPIO pins * of the same GPIO chip don't have back-end pinctrl interface. * If the return value is true, it means that pinctrl device is ready & the * certain GPIO pin doesn't have back-end pinctrl device. If the return value * is false, it means that pinctrl device may not be ready.
*/ #ifdef CONFIG_GPIOLIB staticbool pinctrl_ready_for_gpio_range(struct gpio_chip *gc, unsignedint offset)
{ struct pinctrl_dev *pctldev; struct pinctrl_gpio_range *range = NULL;
mutex_lock(&pinctrldev_list_mutex);
/* Loop over the pin controllers */
list_for_each_entry(pctldev, &pinctrldev_list, node) { /* Loop over the ranges */
mutex_lock(&pctldev->mutex);
list_for_each_entry(range, &pctldev->gpio_ranges, node) { /* Check if any gpio range overlapped with gpio chip */ if (range->base + range->npins - 1 < gc->base ||
range->base > gc->base + gc->ngpio - 1) continue;
mutex_unlock(&pctldev->mutex);
mutex_unlock(&pinctrldev_list_mutex); returntrue;
}
mutex_unlock(&pctldev->mutex);
}
/** * pinctrl_get_device_gpio_range() - find device for GPIO range * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * @outdev: the pin control device if found * @outrange: the GPIO range if found * * Find the pin controller handling a certain GPIO pin from the pinspace of * the GPIO subsystem, return the device and the matching GPIO range. Returns * -EPROBE_DEFER if the GPIO range could not be found in any device since it * may still have not been registered.
*/ staticint pinctrl_get_device_gpio_range(struct gpio_chip *gc, unsignedint offset, struct pinctrl_dev **outdev, struct pinctrl_gpio_range **outrange)
{ struct pinctrl_dev *pctldev;
mutex_lock(&pinctrldev_list_mutex);
/* Loop over the pin controllers */
list_for_each_entry(pctldev, &pinctrldev_list, node) { struct pinctrl_gpio_range *range;
range = pinctrl_match_gpio_range(pctldev, gc, offset); if (range) {
*outdev = pctldev;
*outrange = range;
mutex_unlock(&pinctrldev_list_mutex); return 0;
}
}
mutex_unlock(&pinctrldev_list_mutex);
return -EPROBE_DEFER;
}
/** * pinctrl_add_gpio_range() - register a GPIO range for a controller * @pctldev: pin controller device to add the range to * @range: the GPIO range to add * * DEPRECATED: Don't use this function in new code. See section 2 of * Documentation/devicetree/bindings/gpio/gpio.txt on how to bind pinctrl and * gpio drivers. * * This adds a range of GPIOs to be handled by a certain pin controller. Call * this to register handled ranges after registering your pin controller.
*/ void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range)
{
mutex_lock(&pctldev->mutex);
list_add_tail(&range->node, &pctldev->gpio_ranges);
mutex_unlock(&pctldev->mutex);
}
EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
/* * If we can't find this device, let's assume that is because * it has not probed yet, so the driver trying to register this * range need to defer probing.
*/ if (!pctldev) return ERR_PTR(-EPROBE_DEFER);
/* Loop over the ranges */
list_for_each_entry(range, &pctldev->gpio_ranges, node) { /* Check if we're in the valid range */ if (range->pins) { int a; for (a = 0; a < range->npins; a++) { if (range->pins[a] == pin) return range;
}
} elseif (pin >= range->pin_base &&
pin < range->pin_base + range->npins) return range;
}
/** * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin * @pctldev: the pin controller device to look in * @pin: a controller-local number to find the range for
*/ struct pinctrl_gpio_range *
pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev, unsignedint pin)
{ struct pinctrl_gpio_range *range;
mutex_lock(&pctldev->mutex);
range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
mutex_unlock(&pctldev->mutex);
/** * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller * @pctldev: pin controller device to remove the range from * @range: the GPIO range to remove
*/ void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range)
{
mutex_lock(&pctldev->mutex);
list_del(&range->node);
mutex_unlock(&pctldev->mutex);
}
EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
/** * pinctrl_generic_get_group_count() - returns the number of pin groups * @pctldev: pin controller device
*/ int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
{ return pctldev->num_groups;
}
EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
/** * pinctrl_generic_get_group_name() - returns the name of a pin group * @pctldev: pin controller device * @selector: group number
*/ constchar *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev, unsignedint selector)
{ struct group_desc *group;
group = radix_tree_lookup(&pctldev->pin_group_tree,
selector); if (!group) return NULL;
/** * pinctrl_generic_get_group_pins() - gets the pin group pins * @pctldev: pin controller device * @selector: group number * @pins: pins in the group * @num_pins: number of pins in the group
*/ int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev, unsignedint selector, constunsignedint **pins, unsignedint *num_pins)
{ struct group_desc *group;
group = radix_tree_lookup(&pctldev->pin_group_tree,
selector); if (!group) {
dev_err(pctldev->dev, "%s could not find pingroup%i\n",
__func__, selector); return -EINVAL;
}
/** * pinctrl_generic_get_group() - returns a pin group based on the number * @pctldev: pin controller device * @selector: group number
*/ struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev, unsignedint selector)
{ struct group_desc *group;
group = radix_tree_lookup(&pctldev->pin_group_tree,
selector); if (!group) return NULL;
staticint pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev, constchar *function)
{ conststruct pinctrl_ops *ops = pctldev->desc->pctlops; int ngroups = ops->get_groups_count(pctldev); int selector = 0;
/* See if this pctldev has this group */ while (selector < ngroups) { constchar *gname = ops->get_group_name(pctldev, selector);
if (gname && !strcmp(function, gname)) return selector;
selector++;
}
return -EINVAL;
}
/** * pinctrl_generic_add_group() - adds a new pin group * @pctldev: pin controller device * @name: name of the pin group * @pins: pins in the pin group * @num_pins: number of pins in the pin group * @data: pin controller driver specific data * * Note that the caller must take care of locking.
*/ int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, constchar *name, constunsignedint *pins, int num_pins, void *data)
{ struct group_desc *group; int selector, error;
if (!name) return -EINVAL;
selector = pinctrl_generic_group_name_to_selector(pctldev, name); if (selector >= 0) return selector;
selector = pctldev->num_groups;
group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL); if (!group) return -ENOMEM;
/** * pinctrl_generic_remove_group() - removes a numbered pin group * @pctldev: pin controller device * @selector: group number * * Note that the caller must take care of locking.
*/ int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev, unsignedint selector)
{ struct group_desc *group;
group = radix_tree_lookup(&pctldev->pin_group_tree,
selector); if (!group) return -ENOENT;
/** * pinctrl_generic_free_groups() - removes all pin groups * @pctldev: pin controller device * * Note that the caller must take care of locking. The pinctrl groups * are allocated with devm_kzalloc() so no need to free them here.
*/ staticvoid pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
{ struct radix_tree_iter iter; void __rcu **slot;
/** * pinctrl_get_group_selector() - returns the group selector for a group * @pctldev: the pin controller handling the group * @pin_group: the pin group to look up
*/ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, constchar *pin_group)
{ conststruct pinctrl_ops *pctlops = pctldev->desc->pctlops; unsignedint ngroups = pctlops->get_groups_count(pctldev); unsignedint group_selector = 0;
while (group_selector < ngroups) { constchar *gname = pctlops->get_group_name(pctldev,
group_selector); if (gname && !strcmp(gname, pin_group)) {
dev_dbg(pctldev->dev, "found group selector %u for %s\n",
group_selector,
pin_group); return group_selector;
}
group_selector++;
}
dev_err(pctldev->dev, "does not have pin group %s\n",
pin_group);
/* * Try to obtain GPIO range, if it fails * we're probably dealing with GPIO driver * without a backing pin controller - bail out.
*/ if (pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range)) returntrue;
mutex_lock(&pctldev->mutex);
/* Convert to the pin controllers number space */
pin = gpio_to_pin(range, gc, offset);
result = pinmux_can_be_used_for_gpio(pctldev, pin);
/** * pinctrl_gpio_request() - request a single pin to be used as GPIO * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * * This function should *ONLY* be used from gpiolib-based GPIO drivers, * as part of their gpio_request() semantics, platforms and individual drivers * shall *NOT* request GPIO pins to be muxed in.
*/ int pinctrl_gpio_request(struct gpio_chip *gc, unsignedint offset)
{ struct pinctrl_gpio_range *range; struct pinctrl_dev *pctldev; int ret, pin;
ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range); if (ret) { if (pinctrl_ready_for_gpio_range(gc, offset))
ret = 0; return ret;
}
mutex_lock(&pctldev->mutex);
/* Convert to the pin controllers number space */
pin = gpio_to_pin(range, gc, offset);
ret = pinmux_request_gpio(pctldev, range, pin, gc->base + offset);
/** * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * * This function should *ONLY* be used from gpiolib-based GPIO drivers, * as part of their gpio_request() semantics, platforms and individual drivers * shall *NOT* request GPIO pins to be muxed in.
*/ void pinctrl_gpio_free(struct gpio_chip *gc, unsignedint offset)
{ struct pinctrl_gpio_range *range; struct pinctrl_dev *pctldev; int ret, pin;
ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range); if (ret) return;
mutex_lock(&pctldev->mutex);
/* Convert to the pin controllers number space */
pin = gpio_to_pin(range, gc, offset);
staticint pinctrl_gpio_direction(struct gpio_chip *gc, unsignedint offset, bool input)
{ struct pinctrl_dev *pctldev; struct pinctrl_gpio_range *range; int ret; int pin;
ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range); if (ret) { return ret;
}
mutex_lock(&pctldev->mutex);
/* Convert to the pin controllers number space */
pin = gpio_to_pin(range, gc, offset);
ret = pinmux_gpio_direction(pctldev, range, pin, input);
mutex_unlock(&pctldev->mutex);
return ret;
}
/** * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * * This function should *ONLY* be used from gpiolib-based GPIO drivers, * as part of their gpio_direction_input() semantics, platforms and individual * drivers shall *NOT* touch pin control GPIO calls.
*/ int pinctrl_gpio_direction_input(struct gpio_chip *gc, unsignedint offset)
{ return pinctrl_gpio_direction(gc, offset, true);
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
/** * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * * This function should *ONLY* be used from gpiolib-based GPIO drivers, * as part of their gpio_direction_output() semantics, platforms and individual * drivers shall *NOT* touch pin control GPIO calls.
*/ int pinctrl_gpio_direction_output(struct gpio_chip *gc, unsignedint offset)
{ return pinctrl_gpio_direction(gc, offset, false);
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
/** * pinctrl_gpio_set_config() - Apply config to given GPIO pin * @gc: GPIO chip structure from the GPIO subsystem * @offset: hardware offset of the GPIO relative to the controller * @config: the configuration to apply to the GPIO * * This function should *ONLY* be used from gpiolib-based GPIO drivers, if * they need to call the underlying pin controller to change GPIO config * (for example set debounce time).
*/ int pinctrl_gpio_set_config(struct gpio_chip *gc, unsignedint offset, unsignedlong config)
{ unsignedlong configs[] = { config }; struct pinctrl_gpio_range *range; struct pinctrl_dev *pctldev; int ret, pin;
ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range); if (ret) return ret;
state = find_state(p, map->name); if (!state)
state = create_state(p, map->name); if (IS_ERR(state)) return PTR_ERR(state);
if (map->type == PIN_MAP_TYPE_DUMMY_STATE) return 0;
setting = kzalloc(sizeof(*setting), GFP_KERNEL); if (!setting) return -ENOMEM;
setting->type = map->type;
if (pctldev)
setting->pctldev = pctldev; else
setting->pctldev =
get_pinctrl_dev_from_devname(map->ctrl_dev_name); if (!setting->pctldev) {
kfree(setting); /* Do not defer probing of hogs (circular loop) */ if (!strcmp(map->ctrl_dev_name, map->dev_name)) return -ENODEV; /* * OK let us guess that the driver is not there yet, and * let's defer obtaining this pinctrl handle to later...
*/
dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
map->ctrl_dev_name); return -EPROBE_DEFER;
}
setting->dev_name = map->dev_name;
switch (map->type) { case PIN_MAP_TYPE_MUX_GROUP:
ret = pinmux_map_to_setting(map, setting); break; case PIN_MAP_TYPE_CONFIGS_PIN: case PIN_MAP_TYPE_CONFIGS_GROUP:
ret = pinconf_map_to_setting(map, setting); break; default:
ret = -EINVAL; break;
} if (ret < 0) {
kfree(setting); return ret;
}
/* * create the state cookie holder struct pinctrl for each * mapping, this is what consumers will get when requesting * a pin control handle with pinctrl_get()
*/
p = kzalloc(sizeof(*p), GFP_KERNEL); if (!p) return ERR_PTR(-ENOMEM);
p->dev = dev;
INIT_LIST_HEAD(&p->states);
INIT_LIST_HEAD(&p->dt_maps);
ret = pinctrl_dt_to_map(p, pctldev); if (ret < 0) {
kfree(p); return ERR_PTR(ret);
}
devname = dev_name(dev);
mutex_lock(&pinctrl_maps_mutex); /* Iterate over the pin control maps to locate the right ones */
for_each_pin_map(maps_node, map) { /* Map must be for this device */ if (strcmp(map->dev_name, devname)) continue; /* * If pctldev is not null, we are claiming hog for it, * that means, setting that is served by pctldev by itself. * * Thus we must skip map that is for this device but is served * by other device.
*/ if (pctldev &&
strcmp(dev_name(pctldev->dev), map->ctrl_dev_name)) continue;
ret = add_setting(p, pctldev, map); /* * At this point the adding of a setting may: * * - Defer, if the pinctrl device is not yet available * - Fail, if the pinctrl device is not yet available, * AND the setting is a hog. We cannot defer that, since * the hog will kick in immediately after the device * is registered. * * If the error returned was not -EPROBE_DEFER then we * accumulate the errors to see if we end up with * an -EPROBE_DEFER later, as that is the worst case.
*/ if (ret == -EPROBE_DEFER) {
mutex_unlock(&pinctrl_maps_mutex);
pinctrl_free(p, false); return ERR_PTR(ret);
}
}
mutex_unlock(&pinctrl_maps_mutex);
if (ret < 0) { /* If some other error than deferral occurred, return here */
pinctrl_free(p, false); return ERR_PTR(ret);
}
kref_init(&p->users);
/* Add the pinctrl handle to the global list */
mutex_lock(&pinctrl_list_mutex);
list_add_tail(&p->node, &pinctrl_list);
mutex_unlock(&pinctrl_list_mutex);
return p;
}
/** * pinctrl_get() - retrieves the pinctrl handle for a device * @dev: the device to obtain the handle for
*/ struct pinctrl *pinctrl_get(struct device *dev)
{ struct pinctrl *p;
if (WARN_ON(!dev)) return ERR_PTR(-EINVAL);
/* * See if somebody else (such as the device core) has already * obtained a handle to the pinctrl for this device. In that case, * return another pointer to it.
*/
p = find_pinctrl(dev); if (p) {
dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
kref_get(&p->users); return p;
}
if (inlist)
list_del(&p->node);
kfree(p);
mutex_unlock(&pinctrl_list_mutex);
}
/** * pinctrl_release() - release the pinctrl handle * @kref: the kref in the pinctrl being released
*/ staticvoid pinctrl_release(struct kref *kref)
{ struct pinctrl *p = container_of(kref, struct pinctrl, users);
pinctrl_free(p, true);
}
/** * pinctrl_put() - decrease use count on a previously claimed pinctrl handle * @p: the pinctrl handle to release
*/ void pinctrl_put(struct pinctrl *p)
{
kref_put(&p->users, pinctrl_release);
}
EXPORT_SYMBOL_GPL(pinctrl_put);
/** * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle * @p: the pinctrl handle to retrieve the state from * @name: the state name to retrieve
*/ struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, constchar *name)
{ struct pinctrl_state *state;
state = find_state(p, name); if (!state) { if (pinctrl_dummy_state) { /* create dummy state */
dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
name);
state = create_state(p, name);
} else
state = ERR_PTR(-ENODEV);
}
if (setting->type == PIN_MAP_TYPE_MUX_GROUP)
pinmux_disable_setting(setting);
}
}
/** * pinctrl_commit_state() - select/activate/program a pinctrl state to HW * @p: the pinctrl handle for the device that requests configuration * @state: the state handle to select/activate/program
*/ staticint pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
{ struct pinctrl_setting *setting; struct pinctrl_state *old_state = READ_ONCE(p->state); int ret;
if (old_state) { /* * For each pinmux setting in the old state, forget SW's record * of mux owner for that pingroup. Any pingroups which are * still owned by the new state will be re-acquired by the call * to pinmux_enable_setting() in the loop below.
*/
pinctrl_cond_disable_mux_setting(old_state, NULL);
}
p->state = NULL;
/* Apply all the settings for the new state - pinmux first */
list_for_each_entry(setting, &state->settings, node) { switch (setting->type) { case PIN_MAP_TYPE_MUX_GROUP:
ret = pinmux_enable_setting(setting); break; case PIN_MAP_TYPE_CONFIGS_PIN: case PIN_MAP_TYPE_CONFIGS_GROUP:
ret = 0; break; default:
ret = -EINVAL; break;
}
if (ret < 0) goto unapply_new_state;
/* Do not link hogs (circular dependency) */ if (p != setting->pctldev->p)
pinctrl_link_add(setting->pctldev, p->dev);
}
/* Apply all the settings for the new state - pinconf after */
list_for_each_entry(setting, &state->settings, node) { switch (setting->type) { case PIN_MAP_TYPE_MUX_GROUP:
ret = 0; break; case PIN_MAP_TYPE_CONFIGS_PIN: case PIN_MAP_TYPE_CONFIGS_GROUP:
ret = pinconf_apply_setting(setting); break; default:
ret = -EINVAL; break;
}
if (ret < 0) { goto unapply_mux_setting;
}
/* Do not link hogs (circular dependency) */ if (p != setting->pctldev->p)
pinctrl_link_add(setting->pctldev, p->dev);
}
unapply_new_state:
dev_err(p->dev, "Error applying setting, reverse things back\n");
/* * All we can do here is pinmux_disable_setting. * That means that some pins are muxed differently now * than they were before applying the setting (We can't * "unmux a pin"!), but it's not a big deal since the pins * are free to be muxed by another apply_setting.
*/
pinctrl_cond_disable_mux_setting(state, setting);
restore_old_state: /* There's no infinite recursive loop here because p->state is NULL */ if (old_state)
pinctrl_select_state(p, old_state);
return ret;
}
/** * pinctrl_select_state() - select/activate/program a pinctrl state to HW * @p: the pinctrl handle for the device that requests configuration * @state: the state handle to select/activate/program
*/ int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
{ if (p->state == state) return 0;
/** * devm_pinctrl_get() - Resource managed pinctrl_get() * @dev: the device to obtain the handle for * * If there is a need to explicitly destroy the returned struct pinctrl, * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
*/ struct pinctrl *devm_pinctrl_get(struct device *dev)
{ struct pinctrl **ptr, *p;
ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); if (!ptr) return ERR_PTR(-ENOMEM);
p = pinctrl_get(dev); if (!IS_ERR(p)) {
*ptr = p;
devres_add(dev, ptr);
} else {
devres_free(ptr);
}
/** * devm_pinctrl_put() - Resource managed pinctrl_put() * @p: the pinctrl handle to release * * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally * this function will not need to be called and the resource management * code will ensure that the resource is freed.
*/ void devm_pinctrl_put(struct pinctrl *p)
{
WARN_ON(devres_release(p->dev, devm_pinctrl_release,
devm_pinctrl_match, p));
}
EXPORT_SYMBOL_GPL(devm_pinctrl_put);
/** * pinctrl_register_mappings() - register a set of pin controller mappings * @maps: the pincontrol mappings table to register. Note the pinctrl-core * keeps a reference to the passed in maps, so they should _not_ be * marked with __initdata. * @num_maps: the number of maps in the mapping table
*/ int pinctrl_register_mappings(conststruct pinctrl_map *maps, unsignedint num_maps)
{ int i, ret; struct pinctrl_maps *maps_node;
pr_debug("add %u pinctrl maps\n", num_maps);
/* First sanity check the new mapping */ for (i = 0; i < num_maps; i++) { if (!maps[i].dev_name) {
pr_err("failed to register map %s (%d): no device given\n",
maps[i].name, i); return -EINVAL;
}
if (!maps[i].name) {
pr_err("failed to register map %d: no map name given\n",
i); return -EINVAL;
}
if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
!maps[i].ctrl_dev_name) {
pr_err("failed to register map %s (%d): no pin control device given\n",
maps[i].name, i); return -EINVAL;
}
switch (maps[i].type) { case PIN_MAP_TYPE_DUMMY_STATE: break; case PIN_MAP_TYPE_MUX_GROUP:
ret = pinmux_validate_map(&maps[i], i); if (ret < 0) return ret; break; case PIN_MAP_TYPE_CONFIGS_PIN: case PIN_MAP_TYPE_CONFIGS_GROUP:
ret = pinconf_validate_map(&maps[i], i); if (ret < 0) return ret; break; default:
pr_err("failed to register map %s (%d): invalid type given\n",
maps[i].name, i); return -EINVAL;
}
}
maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL); if (!maps_node) return -ENOMEM;
/** * pinctrl_unregister_mappings() - unregister a set of pin controller mappings * @map: the pincontrol mappings table passed to pinctrl_register_mappings() * when registering the mappings.
*/ void pinctrl_unregister_mappings(conststruct pinctrl_map *map)
{ struct pinctrl_maps *maps_node;
/** * devm_pinctrl_register_mappings() - Resource managed pinctrl_register_mappings() * @dev: device for which mappings are registered * @maps: the pincontrol mappings table to register. Note the pinctrl-core * keeps a reference to the passed in maps, so they should _not_ be * marked with __initdata. * @num_maps: the number of maps in the mapping table * * Returns: 0 on success, or negative errno on failure.
*/ int devm_pinctrl_register_mappings(struct device *dev, conststruct pinctrl_map *maps, unsignedint num_maps)
{ int ret;
ret = pinctrl_register_mappings(maps, num_maps); if (ret) return ret;
/** * pinctrl_force_sleep() - turn a given controller device into sleep state * @pctldev: pin controller device
*/ int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
{ if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep)) return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep); return 0;
}
EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
/** * pinctrl_force_default() - turn a given controller device into default state * @pctldev: pin controller device
*/ int pinctrl_force_default(struct pinctrl_dev *pctldev)
{ if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default)) return pinctrl_commit_state(pctldev->p, pctldev->hog_default); return 0;
}
EXPORT_SYMBOL_GPL(pinctrl_force_default);
/** * pinctrl_init_done() - tell pinctrl probe is done * * We'll use this time to switch the pins from "init" to "default" unless the * driver selected some other state. * * @dev: device to that's done probing
*/ int pinctrl_init_done(struct device *dev)
{ struct dev_pin_info *pins = dev->pins; int ret;
if (!pins) return 0;
if (IS_ERR(pins->init_state)) return 0; /* No such state */
if (pins->p->state != pins->init_state) return 0; /* Not at init anyway */
if (IS_ERR(pins->default_state)) return 0; /* No default state */
ret = pinctrl_select_state(pins->p, pins->default_state); if (ret)
dev_err(dev, "failed to activate default pinctrl state\n");
if (IS_ERR(state)) return 0; /* No such state */
ret = pinctrl_select_state(pins->p, state); if (ret)
dev_err(dev, "failed to activate pinctrl state %s\n",
state->name); return ret;
}
/** * pinctrl_select_default_state() - select default pinctrl state * @dev: device to select default state for
*/ int pinctrl_select_default_state(struct device *dev)
{ if (!dev->pins) return 0;
/** * pinctrl_pm_select_default_state() - select default pinctrl state for PM * @dev: device to select default state for
*/ int pinctrl_pm_select_default_state(struct device *dev)
{ return pinctrl_select_default_state(dev);
}
EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
/** * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM * @dev: device to select sleep state for
*/ int pinctrl_pm_select_sleep_state(struct device *dev)
{ if (!dev->pins) return 0;
/** * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM * @dev: device to select idle state for
*/ int pinctrl_pm_select_idle_state(struct device *dev)
{ if (!dev->pins) return 0;
/* The pin number can be retrived from the pin controller descriptor */ for (i = 0; i < pctldev->desc->npins; i++) { struct pin_desc *desc;
pin = pctldev->desc->pins[i].number;
desc = pin_desc_get(pctldev, pin); /* Pin space may be sparse */ if (!desc) continue;
seq_printf(s, "pin %d (%s) ", pin, desc->name);
#ifdef CONFIG_GPIOLIB
gdev = NULL;
gpio_num = -1;
list_for_each_entry(range, &pctldev->gpio_ranges, node) { if (range->pins != NULL) { for (int i = 0; i < range->npins; ++i) { if (range->pins[i] == pin) {
gpio_num = range->base + i; break;
}
}
} elseif ((pin >= range->pin_base) &&
(pin < (range->pin_base + range->npins))) {
gpio_num =
range->base + (pin - range->pin_base);
} if (gpio_num != -1) break;
} if (gpio_num >= 0) /* * FIXME: gpio_num comes from the global GPIO numberspace. * we need to get rid of the range->base eventually and * get the descriptor directly from the gpio_chip.
*/
gdev = gpiod_to_gpio_device(gpio_to_desc(gpio_num)); if (gdev)
seq_printf(s, "%u:%s ",
gpio_num - gpio_device_get_base(gdev),
gpio_device_get_label(gdev)); else
seq_puts(s, "0:? "); #endif
/* Driver-specific info per pin */ if (ops->pin_dbg_show)
ops->pin_dbg_show(pctldev, s, pin);
staticint pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
{
pctldev->p = create_pinctrl(pctldev->dev, pctldev); if (PTR_ERR(pctldev->p) == -ENODEV) {
dev_dbg(pctldev->dev, "no hogs found\n");
return 0;
}
if (IS_ERR(pctldev->p)) {
dev_err(pctldev->dev, "error claiming hogs: %li\n",
PTR_ERR(pctldev->p));
return PTR_ERR(pctldev->p);
}
pctldev->hog_default =
pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT); if (IS_ERR(pctldev->hog_default)) {
dev_dbg(pctldev->dev, "failed to lookup the default state\n");
} else { if (pinctrl_select_state(pctldev->p,
pctldev->hog_default))
dev_err(pctldev->dev, "failed to select default state\n");
}
pctldev->hog_sleep =
pinctrl_lookup_state(pctldev->p,
PINCTRL_STATE_SLEEP); if (IS_ERR(pctldev->hog_sleep))
dev_dbg(pctldev->dev, "failed to lookup the sleep state\n");
return 0;
}
int pinctrl_enable(struct pinctrl_dev *pctldev)
{ int error;
error = pinctrl_claim_hogs(pctldev); if (error) {
dev_err(pctldev->dev, "could not claim hogs: %i\n", error); return error;
}
/** * pinctrl_register() - register a pin controller device * @pctldesc: descriptor for this pin controller * @dev: parent device for this pin controller * @driver_data: private pin controller data for this pin controller * * Note that pinctrl_register() is known to have problems as the pin * controller driver functions are called before the driver has a * struct pinctrl_dev handle. To avoid issues later on, please use the * new pinctrl_register_and_init() below instead.
*/ struct pinctrl_dev *pinctrl_register(conststruct pinctrl_desc *pctldesc, struct device *dev, void *driver_data)
{ struct pinctrl_dev *pctldev; int error;
pctldev = pinctrl_init_controller(pctldesc, dev, driver_data); if (IS_ERR(pctldev)) return pctldev;
/** * pinctrl_register_and_init() - register and init pin controller device * @pctldesc: descriptor for this pin controller * @dev: parent device for this pin controller * @driver_data: private pin controller data for this pin controller * @pctldev: pin controller device * * Note that pinctrl_enable() still needs to be manually called after * this once the driver is ready.
*/ int pinctrl_register_and_init(conststruct pinctrl_desc *pctldesc, struct device *dev, void *driver_data, struct pinctrl_dev **pctldev)
{ struct pinctrl_dev *p;
p = pinctrl_init_controller(pctldesc, dev, driver_data); if (IS_ERR(p)) return PTR_ERR(p);
/* * We have pinctrl_start() call functions in the pin controller * driver with create_pinctrl() for at least dt_node_to_map(). So * let's make sure pctldev is properly initialized for the * pin controller driver before we do anything.
*/
*pctldev = p;
/** * devm_pinctrl_register() - Resource managed version of pinctrl_register(). * @dev: parent device for this pin controller * @pctldesc: descriptor for this pin controller * @driver_data: private pin controller data for this pin controller * * Returns an error pointer if pincontrol register failed. Otherwise * it returns valid pinctrl handle. * * The pinctrl device will be automatically released when the device is unbound.
*/ struct pinctrl_dev *devm_pinctrl_register(struct device *dev, conststruct pinctrl_desc *pctldesc, void *driver_data)
{ struct pinctrl_dev **ptr, *pctldev;
ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL); if (!ptr) return ERR_PTR(-ENOMEM);
/** * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init * @dev: parent device for this pin controller * @pctldesc: descriptor for this pin controller * @driver_data: private pin controller data for this pin controller * @pctldev: pin controller device * * Returns zero on success or an error number on failure. * * The pinctrl device will be automatically released when the device is unbound.
*/ int devm_pinctrl_register_and_init(struct device *dev, conststruct pinctrl_desc *pctldesc, void *driver_data, struct pinctrl_dev **pctldev)
{ struct pinctrl_dev **ptr; int error;
ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL); if (!ptr) return -ENOMEM;
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.