xref: /linux/drivers/pinctrl/core.h (revision c033a718f615b6b3ddc83ce3e0a217c30bd09cb5)
12744e8afSLinus Walleij /*
22744e8afSLinus Walleij  * Core private header for the pin control subsystem
32744e8afSLinus Walleij  *
42744e8afSLinus Walleij  * Copyright (C) 2011 ST-Ericsson SA
52744e8afSLinus Walleij  * Written on behalf of Linaro for ST-Ericsson
62744e8afSLinus Walleij  *
72744e8afSLinus Walleij  * Author: Linus Walleij <linus.walleij@linaro.org>
82744e8afSLinus Walleij  *
92744e8afSLinus Walleij  * License terms: GNU General Public License (GPL) version 2
102744e8afSLinus Walleij  */
112744e8afSLinus Walleij 
12ab78029eSLinus Walleij #include <linux/kref.h>
1357b676f9SStephen Warren #include <linux/mutex.h>
1457b676f9SStephen Warren #include <linux/radix-tree.h>
15ae6b4d85SLinus Walleij #include <linux/pinctrl/pinconf.h>
16872acc32SLinus Walleij #include <linux/pinctrl/machine.h>
17ae6b4d85SLinus Walleij 
18ae6b4d85SLinus Walleij struct pinctrl_gpio_range;
19ae6b4d85SLinus Walleij 
202744e8afSLinus Walleij /**
212744e8afSLinus Walleij  * struct pinctrl_dev - pin control class device
222744e8afSLinus Walleij  * @node: node to include this pin controller in the global pin controller list
232744e8afSLinus Walleij  * @desc: the pin controller descriptor supplied when initializing this pin
242744e8afSLinus Walleij  *	controller
252744e8afSLinus Walleij  * @pin_desc_tree: each pin descriptor for this pin controller is stored in
262744e8afSLinus Walleij  *	this radix tree
27c7059c5aSTony Lindgren  * @pin_group_tree: optionally each pin group can be stored in this radix tree
28c7059c5aSTony Lindgren  * @num_groups: optionally number of groups can be kept here
292744e8afSLinus Walleij  * @gpio_ranges: a list of GPIO ranges that is handled by this pin controller,
302744e8afSLinus Walleij  *	ranges are added to this list at runtime
312744e8afSLinus Walleij  * @dev: the device entry for this pin controller
322744e8afSLinus Walleij  * @owner: module providing the pin controller, used for refcounting
332744e8afSLinus Walleij  * @driver_data: driver data for drivers registering to the pin controller
342744e8afSLinus Walleij  *	subsystem
3546919ae6SStephen Warren  * @p: result of pinctrl_get() for this device
36840a47baSJulien Delacou  * @hog_default: default state for pins hogged by this device
37840a47baSJulien Delacou  * @hog_sleep: sleep state for pins hogged by this device
3899e4f675STony Lindgren  * @late_init: delayed work for pin controller to finish registration
3942fed7baSPatrice Chotard  * @mutex: mutex taken on each pin controller specific action
40befe5bdfSLinus Walleij  * @device_root: debugfs root for this device
412744e8afSLinus Walleij  */
422744e8afSLinus Walleij struct pinctrl_dev {
432744e8afSLinus Walleij 	struct list_head node;
442744e8afSLinus Walleij 	struct pinctrl_desc *desc;
452744e8afSLinus Walleij 	struct radix_tree_root pin_desc_tree;
46*c033a718SLinus Walleij #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
47c7059c5aSTony Lindgren 	struct radix_tree_root pin_group_tree;
48c7059c5aSTony Lindgren 	unsigned int num_groups;
49*c033a718SLinus Walleij #endif
502744e8afSLinus Walleij 	struct list_head gpio_ranges;
5151cd24eeSStephen Warren 	struct device *dev;
522744e8afSLinus Walleij 	struct module *owner;
532744e8afSLinus Walleij 	void *driver_data;
5446919ae6SStephen Warren 	struct pinctrl *p;
55840a47baSJulien Delacou 	struct pinctrl_state *hog_default;
56840a47baSJulien Delacou 	struct pinctrl_state *hog_sleep;
5799e4f675STony Lindgren 	struct delayed_work late_init;
5842fed7baSPatrice Chotard 	struct mutex mutex;
5902157160STony Lindgren #ifdef CONFIG_DEBUG_FS
6002157160STony Lindgren 	struct dentry *device_root;
6102157160STony Lindgren #endif
62befe5bdfSLinus Walleij };
63befe5bdfSLinus Walleij 
64befe5bdfSLinus Walleij /**
65befe5bdfSLinus Walleij  * struct pinctrl - per-device pin control state holder
66befe5bdfSLinus Walleij  * @node: global list node
67befe5bdfSLinus Walleij  * @dev: the device using this pin control handle
686e5e959dSStephen Warren  * @states: a list of states for this device
696e5e959dSStephen Warren  * @state: the current state
7057291ce2SStephen Warren  * @dt_maps: the mapping table chunks dynamically parsed from device tree for
7157291ce2SStephen Warren  *	this device, if any
72ab78029eSLinus Walleij  * @users: reference count
73befe5bdfSLinus Walleij  */
74befe5bdfSLinus Walleij struct pinctrl {
75befe5bdfSLinus Walleij 	struct list_head node;
76befe5bdfSLinus Walleij 	struct device *dev;
776e5e959dSStephen Warren 	struct list_head states;
786e5e959dSStephen Warren 	struct pinctrl_state *state;
7957291ce2SStephen Warren 	struct list_head dt_maps;
80ab78029eSLinus Walleij 	struct kref users;
816e5e959dSStephen Warren };
826e5e959dSStephen Warren 
836e5e959dSStephen Warren /**
846e5e959dSStephen Warren  * struct pinctrl_state - a pinctrl state for a device
852c9abf80SRichard Genoud  * @node: list node for struct pinctrl's @states field
866e5e959dSStephen Warren  * @name: the name of this state
876e5e959dSStephen Warren  * @settings: a list of settings for this state
886e5e959dSStephen Warren  */
896e5e959dSStephen Warren struct pinctrl_state {
906e5e959dSStephen Warren 	struct list_head node;
916e5e959dSStephen Warren 	const char *name;
927ecdb16fSStephen Warren 	struct list_head settings;
937ecdb16fSStephen Warren };
947ecdb16fSStephen Warren 
957ecdb16fSStephen Warren /**
961e2082b5SStephen Warren  * struct pinctrl_setting_mux - setting data for MAP_TYPE_MUX_GROUP
971e2082b5SStephen Warren  * @group: the group selector to program
981e2082b5SStephen Warren  * @func: the function selector to program
991e2082b5SStephen Warren  */
1001e2082b5SStephen Warren struct pinctrl_setting_mux {
1011e2082b5SStephen Warren 	unsigned group;
1021e2082b5SStephen Warren 	unsigned func;
1031e2082b5SStephen Warren };
1041e2082b5SStephen Warren 
1051e2082b5SStephen Warren /**
1061e2082b5SStephen Warren  * struct pinctrl_setting_configs - setting data for MAP_TYPE_CONFIGS_*
1071e2082b5SStephen Warren  * @group_or_pin: the group selector or pin ID to program
1081e2082b5SStephen Warren  * @configs: a pointer to an array of config parameters/values to program into
1091e2082b5SStephen Warren  *	hardware. Each individual pin controller defines the format and meaning
1101e2082b5SStephen Warren  *	of config parameters.
1111e2082b5SStephen Warren  * @num_configs: the number of entries in array @configs
1121e2082b5SStephen Warren  */
1131e2082b5SStephen Warren struct pinctrl_setting_configs {
1141e2082b5SStephen Warren 	unsigned group_or_pin;
1151e2082b5SStephen Warren 	unsigned long *configs;
1161e2082b5SStephen Warren 	unsigned num_configs;
1171e2082b5SStephen Warren };
1181e2082b5SStephen Warren 
1191e2082b5SStephen Warren /**
120872acc32SLinus Walleij  * struct pinctrl_setting - an individual mux or config setting
1216e5e959dSStephen Warren  * @node: list node for struct pinctrl_settings's @settings field
1221e2082b5SStephen Warren  * @type: the type of setting
12357291ce2SStephen Warren  * @pctldev: pin control device handling to be programmed. Not used for
12457291ce2SStephen Warren  *   PIN_MAP_TYPE_DUMMY_STATE.
1251a78958dSLinus Walleij  * @dev_name: the name of the device using this state
1261e2082b5SStephen Warren  * @data: Data specific to the setting type
1277ecdb16fSStephen Warren  */
1287ecdb16fSStephen Warren struct pinctrl_setting {
1297ecdb16fSStephen Warren 	struct list_head node;
1301e2082b5SStephen Warren 	enum pinctrl_map_type type;
131befe5bdfSLinus Walleij 	struct pinctrl_dev *pctldev;
1321a78958dSLinus Walleij 	const char *dev_name;
1331e2082b5SStephen Warren 	union {
1341e2082b5SStephen Warren 		struct pinctrl_setting_mux mux;
1351e2082b5SStephen Warren 		struct pinctrl_setting_configs configs;
1361e2082b5SStephen Warren 	} data;
1372744e8afSLinus Walleij };
1382744e8afSLinus Walleij 
1392744e8afSLinus Walleij /**
1402744e8afSLinus Walleij  * struct pin_desc - pin descriptor for each physical pin in the arch
1412744e8afSLinus Walleij  * @pctldev: corresponding pin control device
1422744e8afSLinus Walleij  * @name: a name for the pin, e.g. the name of the pin/pad/finger on a
1432744e8afSLinus Walleij  *	datasheet or such
144ca53c5f1SLinus Walleij  * @dynamic_name: if the name of this pin was dynamically allocated
145cd8f61f1SMasahiro Yamada  * @drv_data: driver-defined per-pin data. pinctrl core does not touch this
146652162d4SStephen Warren  * @mux_usecount: If zero, the pin is not claimed, and @owner should be NULL.
1470e3db173SStephen Warren  *	If non-zero, this pin is claimed by @owner. This field is an integer
1480e3db173SStephen Warren  *	rather than a boolean, since pinctrl_get() might process multiple
1490e3db173SStephen Warren  *	mapping table entries that refer to, and hence claim, the same group
1500e3db173SStephen Warren  *	or pin, and each of these will increment the @usecount.
151652162d4SStephen Warren  * @mux_owner: The name of device that called pinctrl_get().
152ba110d90SStephen Warren  * @mux_setting: The most recent selected mux setting for this pin, if any.
153652162d4SStephen Warren  * @gpio_owner: If pinctrl_request_gpio() was called for this pin, this is
154652162d4SStephen Warren  *	the name of the GPIO that "owns" this pin.
1552744e8afSLinus Walleij  */
1562744e8afSLinus Walleij struct pin_desc {
1572744e8afSLinus Walleij 	struct pinctrl_dev *pctldev;
1589af1e44fSStephen Warren 	const char *name;
159ca53c5f1SLinus Walleij 	bool dynamic_name;
160cd8f61f1SMasahiro Yamada 	void *drv_data;
1612744e8afSLinus Walleij 	/* These fields only added when supporting pinmux drivers */
1622744e8afSLinus Walleij #ifdef CONFIG_PINMUX
163652162d4SStephen Warren 	unsigned mux_usecount;
164652162d4SStephen Warren 	const char *mux_owner;
165ba110d90SStephen Warren 	const struct pinctrl_setting_mux *mux_setting;
166652162d4SStephen Warren 	const char *gpio_owner;
1672744e8afSLinus Walleij #endif
1682744e8afSLinus Walleij };
1692744e8afSLinus Walleij 
1706f9e41f4SLaurent Meunier /**
171*c033a718SLinus Walleij  * struct pinctrl_maps - a list item containing part of the mapping table
172*c033a718SLinus Walleij  * @node: mapping table list node
173*c033a718SLinus Walleij  * @maps: array of mapping table entries
174*c033a718SLinus Walleij  * @num_maps: the number of entries in @maps
175*c033a718SLinus Walleij  */
176*c033a718SLinus Walleij struct pinctrl_maps {
177*c033a718SLinus Walleij 	struct list_head node;
178*c033a718SLinus Walleij 	struct pinctrl_map const *maps;
179*c033a718SLinus Walleij 	unsigned num_maps;
180*c033a718SLinus Walleij };
181*c033a718SLinus Walleij 
182*c033a718SLinus Walleij #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
183*c033a718SLinus Walleij 
184*c033a718SLinus Walleij /**
185c7059c5aSTony Lindgren  * struct group_desc - generic pin group descriptor
186c7059c5aSTony Lindgren  * @name: name of the pin group
187c7059c5aSTony Lindgren  * @pins: array of pins that belong to the group
188c7059c5aSTony Lindgren  * @num_pins: number of pins in the group
189c7059c5aSTony Lindgren  * @data: pin controller driver specific data
190c7059c5aSTony Lindgren  */
191c7059c5aSTony Lindgren struct group_desc {
192c7059c5aSTony Lindgren 	const char *name;
193c7059c5aSTony Lindgren 	int *pins;
194c7059c5aSTony Lindgren 	int num_pins;
195c7059c5aSTony Lindgren 	void *data;
196c7059c5aSTony Lindgren };
197c7059c5aSTony Lindgren 
198c7059c5aSTony Lindgren int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev);
199c7059c5aSTony Lindgren 
200c7059c5aSTony Lindgren const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
201c7059c5aSTony Lindgren 					   unsigned int group_selector);
202c7059c5aSTony Lindgren 
203c7059c5aSTony Lindgren int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
204c7059c5aSTony Lindgren 				   unsigned int group_selector,
205c7059c5aSTony Lindgren 				   const unsigned int **pins,
206c7059c5aSTony Lindgren 				   unsigned int *npins);
207c7059c5aSTony Lindgren 
208c7059c5aSTony Lindgren struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
209c7059c5aSTony Lindgren 					     unsigned int group_selector);
210c7059c5aSTony Lindgren 
211c7059c5aSTony Lindgren int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
212c7059c5aSTony Lindgren 			      int *gpins, int ngpins, void *data);
213c7059c5aSTony Lindgren 
214c7059c5aSTony Lindgren int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
215c7059c5aSTony Lindgren 				 unsigned int group_selector);
216c7059c5aSTony Lindgren 
217c7059c5aSTony Lindgren static inline int
218c7059c5aSTony Lindgren pinctrl_generic_remove_last_group(struct pinctrl_dev *pctldev)
219c7059c5aSTony Lindgren {
220c7059c5aSTony Lindgren 	return pinctrl_generic_remove_group(pctldev, pctldev->num_groups - 1);
221c7059c5aSTony Lindgren }
222c7059c5aSTony Lindgren 
223*c033a718SLinus Walleij #endif	/* CONFIG_GENERIC_PINCTRL_GROUPS */
224c7059c5aSTony Lindgren 
2259dfac4fdSLinus Walleij struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
22642fed7baSPatrice Chotard struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np);
227ae6b4d85SLinus Walleij int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name);
228dcb5dbc3SDong Aisheng const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin);
2297afde8baSLinus Walleij int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
2307afde8baSLinus Walleij 			       const char *pin_group);
2312304b473SStephen Warren 
2322304b473SStephen Warren static inline struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev,
2332304b473SStephen Warren 					    unsigned int pin)
2342304b473SStephen Warren {
2352304b473SStephen Warren 	return radix_tree_lookup(&pctldev->pin_desc_tree, pin);
2362304b473SStephen Warren }
23757b676f9SStephen Warren 
238b18537cdSJoachim Eastwood extern struct pinctrl_gpio_range *
239b18537cdSJoachim Eastwood pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
240b18537cdSJoachim Eastwood 					unsigned int pin);
241b18537cdSJoachim Eastwood 
24257291ce2SStephen Warren int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
243c5272a28SDoug Anderson 			 bool dup);
24457291ce2SStephen Warren void pinctrl_unregister_map(struct pinctrl_map const *map);
24557291ce2SStephen Warren 
246840a47baSJulien Delacou extern int pinctrl_force_sleep(struct pinctrl_dev *pctldev);
247840a47baSJulien Delacou extern int pinctrl_force_default(struct pinctrl_dev *pctldev);
248840a47baSJulien Delacou 
24942fed7baSPatrice Chotard extern struct mutex pinctrl_maps_mutex;
2506f9e41f4SLaurent Meunier extern struct list_head pinctrl_maps;
2516f9e41f4SLaurent Meunier 
2526f9e41f4SLaurent Meunier #define for_each_maps(_maps_node_, _i_, _map_) \
2536f9e41f4SLaurent Meunier 	list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
2546f9e41f4SLaurent Meunier 		for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
2556f9e41f4SLaurent Meunier 			_i_ < _maps_node_->num_maps; \
2566f9e41f4SLaurent Meunier 			_i_++, _map_ = &_maps_node_->maps[_i_])
257