2438 lines · 61.4 KB · C
RawDownload
// 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 #include <linux/array_size.h>#include <linux/cleanup.h>#include <linux/debugfs.h>#include <linux/device.h>#include <linux/err.h>#include <linux/export.h>#include <linux/init.h>#include <linux/kref.h>#include <linux/list.h>#include <linux/seq_file.h>#include <linux/slab.h> #include <linux/gpio/consumer.h>#include <linux/gpio/driver.h> #include <linux/pinctrl/consumer.h>#include <linux/pinctrl/devinfo.h>#include <linux/pinctrl/machine.h>#include <linux/pinctrl/pinconf.h>#include <linux/pinctrl/pinctrl.h> #include "core.h"#include "devicetree.h"#include "pinconf.h"#include "pinmux.h" static bool pinctrl_dummy_state; /* 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;}EXPORT_SYMBOL_GPL(pinctrl_provide_dummies); const char *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); const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev){	return dev_name(pctldev->dev);}EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname); void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev){	return pctldev->driver_data;}EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata); /** * 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(const char *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;		}	} 	mutex_unlock(&pinctrldev_list_mutex); 	return NULL;} struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np){	struct pinctrl_dev *pctldev; 	mutex_lock(&pinctrldev_list_mutex); 	list_for_each_entry(pctldev, &pinctrldev_list, node)		if (device_match_of_node(pctldev->dev, np)) {			mutex_unlock(&pinctrldev_list_mutex);			return pctldev;		} 	mutex_unlock(&pinctrldev_list_mutex); 	return NULL;} /** * 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, const char *name){	unsigned int 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 */const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned int pin){	const struct 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;	} 	return desc->name;}EXPORT_SYMBOL_GPL(pin_get_name); /* Deletes a range of pin descriptors */static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,				  const struct pinctrl_pin_desc *pins,				  unsigned int 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);	}} static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,				    const struct pinctrl_pin_desc *pin){	struct pin_desc *pindesc;	int error; 	pindesc = pin_desc_get(pctldev, pin->number);	if (pindesc) {		dev_err(pctldev->dev, "pin %d already registered\n",			pin->number);		return -EINVAL;	} 	pindesc = kzalloc_obj(*pindesc);	if (!pindesc)		return -ENOMEM; 	/* Set owner */	pindesc->pctldev = pctldev;#ifdef CONFIG_PINMUX	mutex_init(&pindesc->mux_lock);#endif 	/* Copy basic pin info */	if (pin->name) {		pindesc->name = pin->name;	} else {		pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);		if (!pindesc->name) {			error = -ENOMEM;			goto failed;		}		pindesc->dynamic_name = true;	} 	pindesc->drv_data = pin->drv_data; 	error = radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);	if (error)		goto failed; 	pr_debug("registered pin %d (%s) on %s\n",		 pin->number, pindesc->name, pctldev->desc->name);	return 0; failed:	kfree(pindesc);	return error;} static int pinctrl_register_pins(struct pinctrl_dev *pctldev,				 const struct pinctrl_pin_desc *pins,				 unsigned int num_descs){	unsigned int 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)! */static inline int gpio_to_pin(struct pinctrl_gpio_range *range,			      struct gpio_chip *gc, unsigned int offset){	unsigned int 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 */static struct pinctrl_gpio_range *pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, struct gpio_chip *gc,			 unsigned int 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_GPIOLIBstatic bool pinctrl_ready_for_gpio_range(struct gpio_chip *gc,					 unsigned int 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);			return true;		}		mutex_unlock(&pctldev->mutex);	} 	mutex_unlock(&pinctrldev_list_mutex); 	return false;}#elsestatic inline boolpinctrl_ready_for_gpio_range(struct gpio_chip *gc, unsigned int offset){	return true;}#endif /** * 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. */static int pinctrl_get_device_gpio_range(struct gpio_chip *gc,					 unsigned int 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); void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,			     struct pinctrl_gpio_range *ranges,			     unsigned int nranges){	int i; 	for (i = 0; i < nranges; i++)		pinctrl_add_gpio_range(pctldev, &ranges[i]);}EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges); struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,		struct pinctrl_gpio_range *range){	struct pinctrl_dev *pctldev; 	pctldev = get_pinctrl_dev_from_devname(devname); 	/*	 * 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); 	pinctrl_add_gpio_range(pctldev, range); 	return pctldev;}EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range); int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,			   const unsigned int **pins, unsigned int *num_pins){	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;	int gs; 	if (!pctlops->get_group_pins)		return -EINVAL; 	gs = pinctrl_get_group_selector(pctldev, pin_group);	if (gs < 0)		return gs; 	return pctlops->get_group_pins(pctldev, gs, pins, num_pins);}EXPORT_SYMBOL_GPL(pinctrl_get_group_pins); struct pinctrl_gpio_range *pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,					unsigned int pin){	struct pinctrl_gpio_range *range; 	/* 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;			}		} else if (pin >= range->pin_base &&

Lines 1–500 of 2438.