xref: /linux/drivers/pinctrl/core.h (revision 383da0c7f25428de5ad09dc2cfed7cd43c4fb6ba)
1af873fceSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
22744e8afSLinus Walleij /*
32744e8afSLinus Walleij  * Core private header for the pin control subsystem
42744e8afSLinus Walleij  *
52744e8afSLinus Walleij  * Copyright (C) 2011 ST-Ericsson SA
62744e8afSLinus Walleij  * Written on behalf of Linaro for ST-Ericsson
72744e8afSLinus Walleij  *
82744e8afSLinus Walleij  * Author: Linus Walleij <linus.walleij@linaro.org>
92744e8afSLinus Walleij  */
102744e8afSLinus Walleij 
11ab78029eSLinus Walleij #include <linux/kref.h>
12e5530adcSAndy Shevchenko #include <linux/list.h>
1357b676f9SStephen Warren #include <linux/mutex.h>
1457b676f9SStephen Warren #include <linux/radix-tree.h>
15e5530adcSAndy Shevchenko #include <linux/types.h>
16e5530adcSAndy Shevchenko 
17872acc32SLinus Walleij #include <linux/pinctrl/machine.h>
18ae6b4d85SLinus Walleij 
19e5530adcSAndy Shevchenko struct dentry;
20e5530adcSAndy Shevchenko struct device;
21e5530adcSAndy Shevchenko struct device_node;
22e5530adcSAndy Shevchenko struct module;
23e5530adcSAndy Shevchenko 
24e5530adcSAndy Shevchenko struct pinctrl;
25e5530adcSAndy Shevchenko struct pinctrl_desc;
26ae6b4d85SLinus Walleij struct pinctrl_gpio_range;
27e5530adcSAndy Shevchenko struct pinctrl_state;
28ae6b4d85SLinus Walleij 
292744e8afSLinus Walleij /**
302744e8afSLinus Walleij  * struct pinctrl_dev - pin control class device
312744e8afSLinus Walleij  * @node: node to include this pin controller in the global pin controller list
322744e8afSLinus Walleij  * @desc: the pin controller descriptor supplied when initializing this pin
332744e8afSLinus Walleij  *	controller
342744e8afSLinus Walleij  * @pin_desc_tree: each pin descriptor for this pin controller is stored in
352744e8afSLinus Walleij  *	this radix tree
36c7059c5aSTony Lindgren  * @pin_group_tree: optionally each pin group can be stored in this radix tree
37c7059c5aSTony Lindgren  * @num_groups: optionally number of groups can be kept here
38a76edc89STony Lindgren  * @pin_function_tree: optionally each function can be stored in this radix tree
39a76edc89STony Lindgren  * @num_functions: optionally number of functions can be kept here
402744e8afSLinus Walleij  * @gpio_ranges: a list of GPIO ranges that is handled by this pin controller,
412744e8afSLinus Walleij  *	ranges are added to this list at runtime
422744e8afSLinus Walleij  * @dev: the device entry for this pin controller
432744e8afSLinus Walleij  * @owner: module providing the pin controller, used for refcounting
442744e8afSLinus Walleij  * @driver_data: driver data for drivers registering to the pin controller
452744e8afSLinus Walleij  *	subsystem
4646919ae6SStephen Warren  * @p: result of pinctrl_get() for this device
47840a47baSJulien Delacou  * @hog_default: default state for pins hogged by this device
48840a47baSJulien Delacou  * @hog_sleep: sleep state for pins hogged by this device
4942fed7baSPatrice Chotard  * @mutex: mutex taken on each pin controller specific action
50befe5bdfSLinus Walleij  * @device_root: debugfs root for this device
512744e8afSLinus Walleij  */
522744e8afSLinus Walleij struct pinctrl_dev {
532744e8afSLinus Walleij 	struct list_head node;
542744e8afSLinus Walleij 	struct pinctrl_desc *desc;
552744e8afSLinus Walleij 	struct radix_tree_root pin_desc_tree;
56c033a718SLinus Walleij #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
57c7059c5aSTony Lindgren 	struct radix_tree_root pin_group_tree;
58c7059c5aSTony Lindgren 	unsigned int num_groups;
59c033a718SLinus Walleij #endif
60a76edc89STony Lindgren #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
61a76edc89STony Lindgren 	struct radix_tree_root pin_function_tree;
62a76edc89STony Lindgren 	unsigned int num_functions;
63a76edc89STony Lindgren #endif
642744e8afSLinus Walleij 	struct list_head gpio_ranges;
6551cd24eeSStephen Warren 	struct device *dev;
662744e8afSLinus Walleij 	struct module *owner;
672744e8afSLinus Walleij 	void *driver_data;
6846919ae6SStephen Warren 	struct pinctrl *p;
69840a47baSJulien Delacou 	struct pinctrl_state *hog_default;
70840a47baSJulien Delacou 	struct pinctrl_state *hog_sleep;
7142fed7baSPatrice Chotard 	struct mutex mutex;
7202157160STony Lindgren #ifdef CONFIG_DEBUG_FS
7302157160STony Lindgren 	struct dentry *device_root;
7402157160STony Lindgren #endif
75befe5bdfSLinus Walleij };
76befe5bdfSLinus Walleij 
77befe5bdfSLinus Walleij /**
78befe5bdfSLinus Walleij  * struct pinctrl - per-device pin control state holder
79befe5bdfSLinus Walleij  * @node: global list node
80befe5bdfSLinus Walleij  * @dev: the device using this pin control handle
816e5e959dSStephen Warren  * @states: a list of states for this device
826e5e959dSStephen Warren  * @state: the current state
8357291ce2SStephen Warren  * @dt_maps: the mapping table chunks dynamically parsed from device tree for
8457291ce2SStephen Warren  *	this device, if any
85ab78029eSLinus Walleij  * @users: reference count
86befe5bdfSLinus Walleij  */
87befe5bdfSLinus Walleij struct pinctrl {
88befe5bdfSLinus Walleij 	struct list_head node;
89befe5bdfSLinus Walleij 	struct device *dev;
906e5e959dSStephen Warren 	struct list_head states;
916e5e959dSStephen Warren 	struct pinctrl_state *state;
9257291ce2SStephen Warren 	struct list_head dt_maps;
93ab78029eSLinus Walleij 	struct kref users;
946e5e959dSStephen Warren };
956e5e959dSStephen Warren 
966e5e959dSStephen Warren /**
976e5e959dSStephen Warren  * struct pinctrl_state - a pinctrl state for a device
982c9abf80SRichard Genoud  * @node: list node for struct pinctrl's @states field
996e5e959dSStephen Warren  * @name: the name of this state
1006e5e959dSStephen Warren  * @settings: a list of settings for this state
1016e5e959dSStephen Warren  */
1026e5e959dSStephen Warren struct pinctrl_state {
1036e5e959dSStephen Warren 	struct list_head node;
1046e5e959dSStephen Warren 	const char *name;
1057ecdb16fSStephen Warren 	struct list_head settings;
1067ecdb16fSStephen Warren };
1077ecdb16fSStephen Warren 
1087ecdb16fSStephen Warren /**
1091e2082b5SStephen Warren  * struct pinctrl_setting_mux - setting data for MAP_TYPE_MUX_GROUP
1101e2082b5SStephen Warren  * @group: the group selector to program
1111e2082b5SStephen Warren  * @func: the function selector to program
1121e2082b5SStephen Warren  */
1131e2082b5SStephen Warren struct pinctrl_setting_mux {
1147cc4e6b0SAndy Shevchenko 	unsigned int group;
1157cc4e6b0SAndy Shevchenko 	unsigned int func;
1161e2082b5SStephen Warren };
1171e2082b5SStephen Warren 
1181e2082b5SStephen Warren /**
1191e2082b5SStephen Warren  * struct pinctrl_setting_configs - setting data for MAP_TYPE_CONFIGS_*
1201e2082b5SStephen Warren  * @group_or_pin: the group selector or pin ID to program
1211e2082b5SStephen Warren  * @configs: a pointer to an array of config parameters/values to program into
1221e2082b5SStephen Warren  *	hardware. Each individual pin controller defines the format and meaning
1231e2082b5SStephen Warren  *	of config parameters.
1241e2082b5SStephen Warren  * @num_configs: the number of entries in array @configs
1251e2082b5SStephen Warren  */
1261e2082b5SStephen Warren struct pinctrl_setting_configs {
1277cc4e6b0SAndy Shevchenko 	unsigned int group_or_pin;
1281e2082b5SStephen Warren 	unsigned long *configs;
1297cc4e6b0SAndy Shevchenko 	unsigned int num_configs;
1301e2082b5SStephen Warren };
1311e2082b5SStephen Warren 
1321e2082b5SStephen Warren /**
133872acc32SLinus Walleij  * struct pinctrl_setting - an individual mux or config setting
1346e5e959dSStephen Warren  * @node: list node for struct pinctrl_settings's @settings field
1351e2082b5SStephen Warren  * @type: the type of setting
13657291ce2SStephen Warren  * @pctldev: pin control device handling to be programmed. Not used for
13757291ce2SStephen Warren  *   PIN_MAP_TYPE_DUMMY_STATE.
1381a78958dSLinus Walleij  * @dev_name: the name of the device using this state
1391e2082b5SStephen Warren  * @data: Data specific to the setting type
1407ecdb16fSStephen Warren  */
1417ecdb16fSStephen Warren struct pinctrl_setting {
1427ecdb16fSStephen Warren 	struct list_head node;
1431e2082b5SStephen Warren 	enum pinctrl_map_type type;
144befe5bdfSLinus Walleij 	struct pinctrl_dev *pctldev;
1451a78958dSLinus Walleij 	const char *dev_name;
1461e2082b5SStephen Warren 	union {
1471e2082b5SStephen Warren 		struct pinctrl_setting_mux mux;
1481e2082b5SStephen Warren 		struct pinctrl_setting_configs configs;
1491e2082b5SStephen Warren 	} data;
1502744e8afSLinus Walleij };
1512744e8afSLinus Walleij 
1522744e8afSLinus Walleij /**
1532744e8afSLinus Walleij  * struct pin_desc - pin descriptor for each physical pin in the arch
1542744e8afSLinus Walleij  * @pctldev: corresponding pin control device
1552744e8afSLinus Walleij  * @name: a name for the pin, e.g. the name of the pin/pad/finger on a
1562744e8afSLinus Walleij  *	datasheet or such
157ca53c5f1SLinus Walleij  * @dynamic_name: if the name of this pin was dynamically allocated
158cd8f61f1SMasahiro Yamada  * @drv_data: driver-defined per-pin data. pinctrl core does not touch this
159652162d4SStephen Warren  * @mux_usecount: If zero, the pin is not claimed, and @owner should be NULL.
1600e3db173SStephen Warren  *	If non-zero, this pin is claimed by @owner. This field is an integer
1610e3db173SStephen Warren  *	rather than a boolean, since pinctrl_get() might process multiple
1620e3db173SStephen Warren  *	mapping table entries that refer to, and hence claim, the same group
1630e3db173SStephen Warren  *	or pin, and each of these will increment the @usecount.
164652162d4SStephen Warren  * @mux_owner: The name of device that called pinctrl_get().
165ba110d90SStephen Warren  * @mux_setting: The most recent selected mux setting for this pin, if any.
166a9a1d2a7SLinus Walleij  * @gpio_owner: If pinctrl_gpio_request() was called for this pin, this is
167652162d4SStephen Warren  *	the name of the GPIO that "owns" this pin.
1682744e8afSLinus Walleij  */
1692744e8afSLinus Walleij struct pin_desc {
1702744e8afSLinus Walleij 	struct pinctrl_dev *pctldev;
1719af1e44fSStephen Warren 	const char *name;
172ca53c5f1SLinus Walleij 	bool dynamic_name;
173cd8f61f1SMasahiro Yamada 	void *drv_data;
1742744e8afSLinus Walleij 	/* These fields only added when supporting pinmux drivers */
1752744e8afSLinus Walleij #ifdef CONFIG_PINMUX
1767cc4e6b0SAndy Shevchenko 	unsigned int mux_usecount;
177652162d4SStephen Warren 	const char *mux_owner;
178ba110d90SStephen Warren 	const struct pinctrl_setting_mux *mux_setting;
179652162d4SStephen Warren 	const char *gpio_owner;
1802744e8afSLinus Walleij #endif
1812744e8afSLinus Walleij };
1822744e8afSLinus Walleij 
1836f9e41f4SLaurent Meunier /**
184c033a718SLinus Walleij  * struct pinctrl_maps - a list item containing part of the mapping table
185c033a718SLinus Walleij  * @node: mapping table list node
186c033a718SLinus Walleij  * @maps: array of mapping table entries
187c033a718SLinus Walleij  * @num_maps: the number of entries in @maps
188c033a718SLinus Walleij  */
189c033a718SLinus Walleij struct pinctrl_maps {
190c033a718SLinus Walleij 	struct list_head node;
1913f713b7cSMasahiro Yamada 	const struct pinctrl_map *maps;
1927cc4e6b0SAndy Shevchenko 	unsigned int num_maps;
193c033a718SLinus Walleij };
194c033a718SLinus Walleij 
195c033a718SLinus Walleij #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
196c033a718SLinus Walleij 
197c033a718SLinus Walleij /**
198c7059c5aSTony Lindgren  * struct group_desc - generic pin group descriptor
199c7059c5aSTony Lindgren  * @name: name of the pin group
200c7059c5aSTony Lindgren  * @pins: array of pins that belong to the group
201c7059c5aSTony Lindgren  * @num_pins: number of pins in the group
202c7059c5aSTony Lindgren  * @data: pin controller driver specific data
203c7059c5aSTony Lindgren  */
204c7059c5aSTony Lindgren struct group_desc {
205c7059c5aSTony Lindgren 	const char *name;
206d98d7385SAndy Shevchenko 	const unsigned int *pins;
207c7059c5aSTony Lindgren 	int num_pins;
208c7059c5aSTony Lindgren 	void *data;
209c7059c5aSTony Lindgren };
210c7059c5aSTony Lindgren 
211*383da0c7SAndy Shevchenko /* Convenience macro to define a generic pin group descriptor */
212*383da0c7SAndy Shevchenko #define PINCTRL_GROUP_DESC(_name, _pins, _num_pins, _data)	\
213*383da0c7SAndy Shevchenko (struct group_desc) {						\
214*383da0c7SAndy Shevchenko 	.name = _name,						\
215*383da0c7SAndy Shevchenko 	.pins = _pins,						\
216*383da0c7SAndy Shevchenko 	.num_pins = _num_pins,					\
217*383da0c7SAndy Shevchenko 	.data = _data,						\
218*383da0c7SAndy Shevchenko }
219*383da0c7SAndy Shevchenko 
220c7059c5aSTony Lindgren int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev);
221c7059c5aSTony Lindgren 
222c7059c5aSTony Lindgren const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
223c7059c5aSTony Lindgren 					   unsigned int group_selector);
224c7059c5aSTony Lindgren 
225c7059c5aSTony Lindgren int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
226c7059c5aSTony Lindgren 				   unsigned int group_selector,
227c7059c5aSTony Lindgren 				   const unsigned int **pins,
228c7059c5aSTony Lindgren 				   unsigned int *npins);
229c7059c5aSTony Lindgren 
230c7059c5aSTony Lindgren struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
231c7059c5aSTony Lindgren 					     unsigned int group_selector);
232c7059c5aSTony Lindgren 
233c7059c5aSTony Lindgren int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
234d98d7385SAndy Shevchenko 			      const unsigned int *pins, int num_pins, void *data);
235c7059c5aSTony Lindgren 
236c7059c5aSTony Lindgren int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
237c7059c5aSTony Lindgren 				 unsigned int group_selector);
238c7059c5aSTony Lindgren 
239c033a718SLinus Walleij #endif	/* CONFIG_GENERIC_PINCTRL_GROUPS */
240c7059c5aSTony Lindgren 
2419dfac4fdSLinus Walleij struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
24242fed7baSPatrice Chotard struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np);
243ae6b4d85SLinus Walleij int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name);
2447cc4e6b0SAndy Shevchenko const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned int pin);
2457afde8baSLinus Walleij int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
2467afde8baSLinus Walleij 			       const char *pin_group);
2472304b473SStephen Warren 
2482304b473SStephen Warren static inline struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev,
2492304b473SStephen Warren 					    unsigned int pin)
2502304b473SStephen Warren {
2512304b473SStephen Warren 	return radix_tree_lookup(&pctldev->pin_desc_tree, pin);
2522304b473SStephen Warren }
25357b676f9SStephen Warren 
254b18537cdSJoachim Eastwood extern struct pinctrl_gpio_range *
255b18537cdSJoachim Eastwood pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
256b18537cdSJoachim Eastwood 					unsigned int pin);
257b18537cdSJoachim Eastwood 
258840a47baSJulien Delacou extern int pinctrl_force_sleep(struct pinctrl_dev *pctldev);
259840a47baSJulien Delacou extern int pinctrl_force_default(struct pinctrl_dev *pctldev);
260840a47baSJulien Delacou 
26142fed7baSPatrice Chotard extern struct mutex pinctrl_maps_mutex;
2626f9e41f4SLaurent Meunier extern struct list_head pinctrl_maps;
2636f9e41f4SLaurent Meunier 
26406de5193SAndy Shevchenko #define for_each_pin_map(_maps_node_, _map_)						\
2656f9e41f4SLaurent Meunier 	list_for_each_entry(_maps_node_, &pinctrl_maps, node)				\
26606de5193SAndy Shevchenko 		for (unsigned int __i = 0;						\
26706de5193SAndy Shevchenko 		     __i < _maps_node_->num_maps && (_map_ = &_maps_node_->maps[__i]);	\
26806de5193SAndy Shevchenko 		     __i++)
269