^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Core driver for the pin muxing portions of the pin control subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2011-2012 ST-Ericsson SA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Written on behalf of Linaro for ST-Ericsson
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on bits of regulator core, gpio core and clk core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: Linus Walleij <linus.walleij@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define pr_fmt(fmt) "pinmux core: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/radix-tree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/pinctrl/machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/pinctrl/pinmux.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "pinmux.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int pinmux_check_ops(struct pinctrl_dev *pctldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const struct pinmux_ops *ops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned nfuncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned selector = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Check that we implement required operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!ops ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) !ops->get_functions_count ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) !ops->get_function_name ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) !ops->get_function_groups ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) !ops->set_mux) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Check that all functions registered have names */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) nfuncs = ops->get_functions_count(pctldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) while (selector < nfuncs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const char *fname = ops->get_function_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!fname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) selector++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int pinmux_validate_map(const struct pinctrl_map *map, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!map->data.mux.function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_err("failed to register map %s (%d): no function given\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) map->name, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * pinmux_can_be_used_for_gpio() - check if a specific pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * is either muxed to a different function or used as gpio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * @pctldev: the associated pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @pin: the pin number in the global pin space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * Controllers not defined as strict will always return true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * menaning that the gpio can be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct pin_desc *desc = pin_desc_get(pctldev, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) const struct pinmux_ops *ops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* Can't inspect pin, assume it can be used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!desc || !ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (ops->strict && desc->mux_usecount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return !(ops->strict && !!desc->gpio_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * pin_request() - request a single pin to be muxed in, typically for GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * @pctldev: the associated pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * @pin: the pin number in the global pin space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * @owner: a representation of the owner of this pin; typically the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * name that controls its mux function, or the requested GPIO name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * @gpio_range: the range matching the GPIO pin if this is a request for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * single GPIO pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int pin_request(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int pin, const char *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct pinctrl_gpio_range *gpio_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct pin_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) const struct pinmux_ops *ops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) desc = pin_desc_get(pctldev, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (desc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) "pin %d is not registered so it cannot be requested\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pin, desc->name, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if ((!gpio_range || ops->strict) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) "pin %s already requested by %s; cannot claim for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) desc->name, desc->mux_owner, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if ((gpio_range || ops->strict) && desc->gpio_owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) "pin %s already requested by %s; cannot claim for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) desc->name, desc->gpio_owner, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (gpio_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) desc->gpio_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) desc->mux_usecount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (desc->mux_usecount > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) desc->mux_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Let each pin increase references to this module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!try_module_get(pctldev->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) "could not increase module refcount for pin %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto out_free_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * If there is no kind of request function for the pin we just assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * we got it by default and proceed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (gpio_range && ops->gpio_request_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* This requests and enables a single GPIO pin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) status = ops->gpio_request_enable(pctldev, gpio_range, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) else if (ops->request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) status = ops->request(pctldev, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) module_put(pctldev->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) out_free_pin:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (gpio_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) desc->gpio_owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) desc->mux_usecount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!desc->mux_usecount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) desc->mux_owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pin, owner, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * pin_free() - release a single muxed in pin so something else can be muxed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @pctldev: pin controller device handling this pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @pin: the pin to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * @gpio_range: the range matching the GPIO pin if this is a request for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * single GPIO pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * This function returns a pointer to the previous owner. This is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * for callers that dynamically allocate an owner name so it can be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * once the pin is free. This is done for GPIO request functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct pinctrl_gpio_range *gpio_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) const struct pinmux_ops *ops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct pin_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) const char *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) desc = pin_desc_get(pctldev, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (desc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) "pin is not registered so it cannot be freed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!gpio_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * A pin should not be freed more times than allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (WARN_ON(!desc->mux_usecount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) desc->mux_usecount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (desc->mux_usecount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * If there is no kind of request function for the pin we just assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * we got it by default and proceed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (gpio_range && ops->gpio_disable_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ops->gpio_disable_free(pctldev, gpio_range, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) else if (ops->free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ops->free(pctldev, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (gpio_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) owner = desc->gpio_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) desc->gpio_owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) owner = desc->mux_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) desc->mux_owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) desc->mux_setting = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) module_put(pctldev->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * pinmux_request_gpio() - request pinmuxing for a GPIO pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * @pctldev: pin controller device affected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * @pin: the pin to mux in for GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * @range: the applicable GPIO range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * @gpio: number of requested GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int pinmux_request_gpio(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct pinctrl_gpio_range *range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) unsigned pin, unsigned gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) const char *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* Conjure some name stating what chip and pin this is taken by */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = pin_request(pctldev, pin, owner, range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) kfree(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * pinmux_free_gpio() - release a pin from GPIO muxing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * @pctldev: the pin controller device for the pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * @pin: the affected currently GPIO-muxed in pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * @range: applicable GPIO range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct pinctrl_gpio_range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) const char *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) owner = pin_free(pctldev, pin, range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) kfree(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * @pctldev: the pin controller handling this pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * @range: applicable GPIO range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * @pin: the affected GPIO pin in this controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * @input: true if we set the pin as input, false for output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct pinctrl_gpio_range *range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) unsigned pin, bool input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) const struct pinmux_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (ops->gpio_set_direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ret = ops->gpio_set_direction(pctldev, range, pin, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) const char *function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) const struct pinmux_ops *ops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unsigned nfuncs = ops->get_functions_count(pctldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) unsigned selector = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* See if this pctldev has this function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) while (selector < nfuncs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) const char *fname = ops->get_function_name(pctldev, selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (!strcmp(function, fname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) selector++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) int pinmux_map_to_setting(const struct pinctrl_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct pinctrl_setting *setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct pinctrl_dev *pctldev = setting->pctldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) char const * const *groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) unsigned num_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) const char *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (!pmxops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) dev_err(pctldev->dev, "does not support mux function\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) dev_err(pctldev->dev, "invalid function %s in map table\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) map->data.mux.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) setting->data.mux.func = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) &groups, &num_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) dev_err(pctldev->dev, "can't query groups for function %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) map->data.mux.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (!num_groups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) "function %s can't be selected on any group\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) map->data.mux.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (map->data.mux.group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) group = map->data.mux.group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ret = match_string(groups, num_groups, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) "invalid group \"%s\" for function \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) group, map->data.mux.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) group = groups[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ret = pinctrl_get_group_selector(pctldev, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) dev_err(pctldev->dev, "invalid group %s in map table\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) map->data.mux.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) setting->data.mux.group = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) void pinmux_free_setting(const struct pinctrl_setting *setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /* This function is currently unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) int pinmux_enable_setting(const struct pinctrl_setting *setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct pinctrl_dev *pctldev = setting->pctldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) const struct pinmux_ops *ops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) const unsigned *pins = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) unsigned num_pins = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct pin_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (pctlops->get_group_pins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) &pins, &num_pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) const char *gname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /* errors only affect debug data, so just warn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) gname = pctlops->get_group_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) setting->data.mux.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) dev_warn(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) "could not get pins for group %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) gname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) num_pins = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* Try to allocate all pins in this group, one by one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) for (i = 0; i < num_pins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) const char *gname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) const char *pname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) desc = pin_desc_get(pctldev, pins[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) pname = desc ? desc->name : "non-existing";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) gname = pctlops->get_group_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) setting->data.mux.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) dev_err(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) "could not request pin %d (%s) from group %s "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) " on device %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) pins[i], pname, gname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) pinctrl_dev_get_name(pctldev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) goto err_pin_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /* Now that we have acquired the pins, encode the mux setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) for (i = 0; i < num_pins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) desc = pin_desc_get(pctldev, pins[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (desc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) dev_warn(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) "could not get pin desc for pin %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) pins[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) desc->mux_setting = &(setting->data.mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ret = ops->set_mux(pctldev, setting->data.mux.func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) setting->data.mux.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) goto err_set_mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) err_set_mux:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) for (i = 0; i < num_pins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) desc = pin_desc_get(pctldev, pins[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) desc->mux_setting = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) err_pin_request:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* On error release all taken pins */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) pin_free(pctldev, pins[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) void pinmux_disable_setting(const struct pinctrl_setting *setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct pinctrl_dev *pctldev = setting->pctldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) const unsigned *pins = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) unsigned num_pins = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct pin_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (pctlops->get_group_pins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) &pins, &num_pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) const char *gname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) /* errors only affect debug data, so just warn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) gname = pctlops->get_group_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) setting->data.mux.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) dev_warn(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) "could not get pins for group %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) gname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) num_pins = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /* Flag the descs that no setting is active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) for (i = 0; i < num_pins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) desc = pin_desc_get(pctldev, pins[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (desc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) dev_warn(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) "could not get pin desc for pin %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) pins[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (desc->mux_setting == &(setting->data.mux)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) pin_free(pctldev, pins[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) const char *gname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) gname = pctlops->get_group_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) setting->data.mux.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) dev_warn(pctldev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) "not freeing pin %d (%s) as part of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) "deactivating group %s - it is already "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) "used for some other setting",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) pins[i], desc->name, gname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /* Called from pincontrol core */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static int pinmux_functions_show(struct seq_file *s, void *what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) struct pinctrl_dev *pctldev = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) unsigned nfuncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) unsigned func_selector = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (!pmxops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) mutex_lock(&pctldev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) nfuncs = pmxops->get_functions_count(pctldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) while (func_selector < nfuncs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) const char *func = pmxops->get_function_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) func_selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) const char * const *groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) unsigned num_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) ret = pmxops->get_function_groups(pctldev, func_selector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) &groups, &num_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) func_selector++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) seq_printf(s, "function: %s, groups = [ ", func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) for (i = 0; i < num_groups; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) seq_printf(s, "%s ", groups[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) seq_puts(s, "]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) func_selector++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) mutex_unlock(&pctldev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) static int pinmux_pins_show(struct seq_file *s, void *what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct pinctrl_dev *pctldev = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) unsigned i, pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!pmxops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) seq_puts(s, "Pinmux settings per pin\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (pmxops->strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) seq_puts(s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) seq_puts(s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) "Format: pin (name): mux_owner gpio_owner hog?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) mutex_lock(&pctldev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /* The pin number can be retrived from the pin controller descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) for (i = 0; i < pctldev->desc->npins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) struct pin_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) bool is_hog = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) pin = pctldev->desc->pins[i].number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) desc = pin_desc_get(pctldev, pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /* Skip if we cannot search the pin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (desc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (desc->mux_owner &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) is_hog = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (pmxops->strict) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (desc->mux_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) seq_printf(s, "pin %d (%s): device %s%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) pin, desc->name, desc->mux_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) is_hog ? " (HOG)" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) else if (desc->gpio_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) seq_printf(s, "pin %d (%s): GPIO %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) pin, desc->name, desc->gpio_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) seq_printf(s, "pin %d (%s): UNCLAIMED",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) pin, desc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) /* For non-strict controllers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) desc->mux_owner ? desc->mux_owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) : "(MUX UNCLAIMED)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) desc->gpio_owner ? desc->gpio_owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) : "(GPIO UNCLAIMED)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) is_hog ? " (HOG)" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) /* If mux: print function+group claiming the pin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (desc->mux_setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) seq_printf(s, " function %s group %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) pmxops->get_function_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) desc->mux_setting->func),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) pctlops->get_group_name(pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) desc->mux_setting->group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) seq_putc(s, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) mutex_unlock(&pctldev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) seq_printf(s, "group %s\nfunction %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) map->data.mux.group ? map->data.mux.group : "(default)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) map->data.mux.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) void pinmux_show_setting(struct seq_file *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) const struct pinctrl_setting *setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) struct pinctrl_dev *pctldev = setting->pctldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) seq_printf(s, "group: %s (%u) function: %s (%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) pctlops->get_group_name(pctldev, setting->data.mux.group),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) setting->data.mux.group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) pmxops->get_function_name(pctldev, setting->data.mux.func),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) setting->data.mux.func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) void pinmux_init_device_debugfs(struct dentry *devroot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) struct pinctrl_dev *pctldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) devroot, pctldev, &pinmux_functions_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) devroot, pctldev, &pinmux_pins_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) #endif /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * pinmux_generic_get_function_count() - returns number of functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * @pctldev: pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) return pctldev->num_functions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * pinmux_generic_get_function_name() - returns the function name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * @pctldev: pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * @selector: function number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) unsigned int selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) struct function_desc *function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) function = radix_tree_lookup(&pctldev->pin_function_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (!function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return function->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * pinmux_generic_get_function_groups() - gets the function groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * @pctldev: pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * @selector: function number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * @groups: array of pin groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * @num_groups: number of pin groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) unsigned int selector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) const char * const **groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) unsigned * const num_groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct function_desc *function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) function = radix_tree_lookup(&pctldev->pin_function_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) if (!function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) dev_err(pctldev->dev, "%s could not find function%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) __func__, selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) *groups = function->group_names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) *num_groups = function->num_group_names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) * pinmux_generic_get_function() - returns a function based on the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) * @pctldev: pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * @selector: function number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) unsigned int selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) struct function_desc *function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) function = radix_tree_lookup(&pctldev->pin_function_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (!function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * pinmux_generic_add_function() - adds a function group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * @pctldev: pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * @name: name of the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * @groups: array of pin groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * @num_groups: number of pin groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * @data: pin controller driver specific data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) const char **groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) const unsigned int num_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) struct function_desc *function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) int selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) selector = pinmux_func_name_to_selector(pctldev, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (selector >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) return selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) selector = pctldev->num_functions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (!function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) function->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) function->group_names = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) function->num_group_names = num_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) function->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) radix_tree_insert(&pctldev->pin_function_tree, selector, function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) pctldev->num_functions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) return selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * pinmux_generic_remove_function() - removes a numbered function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * @pctldev: pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * @selector: function number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * Note that the caller must take care of locking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) unsigned int selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) struct function_desc *function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) function = radix_tree_lookup(&pctldev->pin_function_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (!function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) radix_tree_delete(&pctldev->pin_function_tree, selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) devm_kfree(pctldev->dev, function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) pctldev->num_functions--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * pinmux_generic_free_functions() - removes all functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * @pctldev: pin controller device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) * Note that the caller must take care of locking. The pinctrl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * functions are allocated with devm_kzalloc() so no need to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) * them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct radix_tree_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) void __rcu **slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) radix_tree_delete(&pctldev->pin_function_tree, iter.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) pctldev->num_functions = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */