// 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 <[email protected]> * * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. */ #define pr_fmt(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.h> #include <linux/gpio/driver.h> #include <linux/pinctrl/consumer.h> #include <linux/pinctrl/devinfo.h> #include <linux/pinctrl/machine.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(…) …; /* 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_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) { … } const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); /** * 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 *get_pinctrl_dev_from_of_node(struct device_node *np) { … } /** * 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) { … } /** * 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) { … } EXPORT_SYMBOL_GPL(…); /* 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) { … } static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev, const struct pinctrl_pin_desc *pin) { … } static int pinctrl_register_pins(struct pinctrl_dev *pctldev, const struct pinctrl_pin_desc *pins, unsigned int num_descs) { … } /** * 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) { … } /** * 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) { … } /** * 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 static bool pinctrl_ready_for_gpio_range(struct gpio_chip *gc, unsigned int offset) { … } #else static inline bool pinctrl_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) { … } /** * 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) { … } EXPORT_SYMBOL_GPL(…); void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *ranges, unsigned int nranges) { … } EXPORT_SYMBOL_GPL(…); struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname, struct pinctrl_gpio_range *range) { … } EXPORT_SYMBOL_GPL(…); int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group, const unsigned int **pins, unsigned int *num_pins) { … } EXPORT_SYMBOL_GPL(…); struct pinctrl_gpio_range * pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev, unsigned int pin) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int pin) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } EXPORT_SYMBOL_GPL(…); #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) { … } EXPORT_SYMBOL_GPL(…); /** * pinctrl_generic_get_group_name() - returns the name of a pin group * @pctldev: pin controller device * @selector: group number */ const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev, unsigned int selector) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int selector, const unsigned int **pins, unsigned int *num_pins) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int selector) { … } EXPORT_SYMBOL_GPL(…); static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev, const char *function) { … } /** * 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, const char *name, const unsigned int *pins, int num_pins, void *data) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int selector) { … } EXPORT_SYMBOL_GPL(…); /** * 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. */ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) { … } #else static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) { } #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */ /** * 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, const char *pin_group) { … } bool pinctrl_gpio_can_use_line(struct gpio_chip *gc, unsigned int offset) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int offset) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int offset) { … } EXPORT_SYMBOL_GPL(…); static int pinctrl_gpio_direction(struct gpio_chip *gc, unsigned int offset, bool input) { … } /** * 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, unsigned int offset) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int offset) { … } EXPORT_SYMBOL_GPL(…); /** * 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, unsigned int offset, unsigned long config) { … } EXPORT_SYMBOL_GPL(…); static struct pinctrl_state *find_state(struct pinctrl *p, const char *name) { … } static struct pinctrl_state *create_state(struct pinctrl *p, const char *name) { … } static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev, const struct pinctrl_map *map) { … } static struct pinctrl *find_pinctrl(struct device *dev) { … } static void pinctrl_free(struct pinctrl *p, bool inlist); static struct pinctrl *create_pinctrl(struct device *dev, struct pinctrl_dev *pctldev) { … } /** * pinctrl_get() - retrieves the pinctrl handle for a device * @dev: the device to obtain the handle for */ struct pinctrl *pinctrl_get(struct device *dev) { … } EXPORT_SYMBOL_GPL(…); static void pinctrl_free_setting(bool disable_setting, struct pinctrl_setting *setting) { … } static void pinctrl_free(struct pinctrl *p, bool inlist) { … } /** * pinctrl_release() - release the pinctrl handle * @kref: the kref in the pinctrl being released */ static void pinctrl_release(struct kref *kref) { … } /** * pinctrl_put() - decrease use count on a previously claimed pinctrl handle * @p: the pinctrl handle to release */ void pinctrl_put(struct pinctrl *p) { … } EXPORT_SYMBOL_GPL(…); /** * 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, const char *name) { … } EXPORT_SYMBOL_GPL(…); static void pinctrl_link_add(struct pinctrl_dev *pctldev, struct device *consumer) { … } /** * 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 */ static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state) { … } /** * 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) { … } EXPORT_SYMBOL_GPL(…); static void devm_pinctrl_release(struct device *dev, void *res) { … } /** * 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) { … } EXPORT_SYMBOL_GPL(…); static int devm_pinctrl_match(struct device *dev, void *res, void *data) { … } /** * 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) { … } EXPORT_SYMBOL_GPL(…); /** * 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(const struct pinctrl_map *maps, unsigned int num_maps) { … } EXPORT_SYMBOL_GPL(…); /** * 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(const struct pinctrl_map *map) { … } EXPORT_SYMBOL_GPL(…); /** * pinctrl_force_sleep() - turn a given controller device into sleep state * @pctldev: pin controller device */ int pinctrl_force_sleep(struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); /** * pinctrl_force_default() - turn a given controller device into default state * @pctldev: pin controller device */ int pinctrl_force_default(struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } static int pinctrl_select_bound_state(struct device *dev, struct pinctrl_state *state) { … } /** * pinctrl_select_default_state() - select default pinctrl state * @dev: device to select default state for */ int pinctrl_select_default_state(struct device *dev) { … } EXPORT_SYMBOL_GPL(…); #ifdef CONFIG_PM /** * 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) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } EXPORT_SYMBOL_GPL(…); #endif #ifdef CONFIG_DEBUG_FS static int pinctrl_pins_show(struct seq_file *s, void *what) { … } DEFINE_SHOW_ATTRIBUTE(…); static int pinctrl_groups_show(struct seq_file *s, void *what) { … } DEFINE_SHOW_ATTRIBUTE(…); static int pinctrl_gpioranges_show(struct seq_file *s, void *what) { … } DEFINE_SHOW_ATTRIBUTE(…); static int pinctrl_devices_show(struct seq_file *s, void *what) { … } DEFINE_SHOW_ATTRIBUTE(…); static inline const char *map_type(enum pinctrl_map_type type) { … } static int pinctrl_maps_show(struct seq_file *s, void *what) { … } DEFINE_SHOW_ATTRIBUTE(…); static int pinctrl_show(struct seq_file *s, void *what) { … } DEFINE_SHOW_ATTRIBUTE(…); static struct dentry *debugfs_root; static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) { … } static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) { … } static void pinctrl_init_debugfs(void) { … } #else /* CONFIG_DEBUG_FS */ static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) { } static void pinctrl_init_debugfs(void) { } static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) { } #endif static int pinctrl_check_ops(struct pinctrl_dev *pctldev) { … } /** * pinctrl_init_controller() - init 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 */ static struct pinctrl_dev * pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev, void *driver_data) { … } static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev, struct pinctrl_desc *pctldesc) { … } static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev) { … } int pinctrl_enable(struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); /** * 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(struct pinctrl_desc *pctldesc, struct device *dev, void *driver_data) { … } EXPORT_SYMBOL_GPL(…); /** * 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(struct pinctrl_desc *pctldesc, struct device *dev, void *driver_data, struct pinctrl_dev **pctldev) { … } EXPORT_SYMBOL_GPL(…); /** * pinctrl_unregister() - unregister pinmux * @pctldev: pin controller to unregister * * Called by pinmux drivers to unregister a pinmux. */ void pinctrl_unregister(struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); static void devm_pinctrl_dev_release(struct device *dev, void *res) { … } static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data) { … } /** * 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, struct pinctrl_desc *pctldesc, void *driver_data) { … } EXPORT_SYMBOL_GPL(…); /** * 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, struct pinctrl_desc *pctldesc, void *driver_data, struct pinctrl_dev **pctldev) { … } EXPORT_SYMBOL_GPL(…); /** * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister(). * @dev: device for which resource was allocated * @pctldev: the pinctrl device to unregister. */ void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev) { … } EXPORT_SYMBOL_GPL(…); static int __init pinctrl_init(void) { … } /* init early since many drivers really need to initialized pinmux early */ core_initcall(pinctrl_init);