xref: /openbmc/linux/drivers/pinctrl/core.h (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
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 {
1141e2082b5SStephen Warren 	unsigned group;
1151e2082b5SStephen Warren 	unsigned 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 {
1271e2082b5SStephen Warren 	unsigned group_or_pin;
1281e2082b5SStephen Warren 	unsigned long *configs;
1291e2082b5SStephen Warren 	unsigned 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
176652162d4SStephen Warren 	unsigned mux_usecount;
177652162d4SStephen Warren 	const char *mux_owner;
178ba110d90SStephen Warren 	const struct pinctrl_setting_mux *mux_setting;
179652162d4SStephen Warren 	const char *gpio_owner;
180*2da32aedSMukesh Ojha 	struct mutex mux_lock;
1812744e8afSLinus Walleij #endif
1822744e8afSLinus Walleij };
1832744e8afSLinus Walleij 
1846f9e41f4SLaurent Meunier /**
185c033a718SLinus Walleij  * struct pinctrl_maps - a list item containing part of the mapping table
186c033a718SLinus Walleij  * @node: mapping table list node
187c033a718SLinus Walleij  * @maps: array of mapping table entries
188c033a718SLinus Walleij  * @num_maps: the number of entries in @maps
189c033a718SLinus Walleij  */
190c033a718SLinus Walleij struct pinctrl_maps {
191c033a718SLinus Walleij 	struct list_head node;
1923f713b7cSMasahiro Yamada 	const struct pinctrl_map *maps;
193c033a718SLinus Walleij 	unsigned num_maps;
194c033a718SLinus Walleij };
195c033a718SLinus Walleij 
196c033a718SLinus Walleij #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
197c033a718SLinus Walleij 
198c033a718SLinus Walleij /**
199c7059c5aSTony Lindgren  * struct group_desc - generic pin group descriptor
200c7059c5aSTony Lindgren  * @name: name of the pin group
201c7059c5aSTony Lindgren  * @pins: array of pins that belong to the group
202c7059c5aSTony Lindgren  * @num_pins: number of pins in the group
203c7059c5aSTony Lindgren  * @data: pin controller driver specific data
204c7059c5aSTony Lindgren  */
205c7059c5aSTony Lindgren struct group_desc {
206c7059c5aSTony Lindgren 	const char *name;
207c7059c5aSTony Lindgren 	int *pins;
208c7059c5aSTony Lindgren 	int num_pins;
209c7059c5aSTony Lindgren 	void *data;
210c7059c5aSTony Lindgren };
211c7059c5aSTony Lindgren 
212c7059c5aSTony Lindgren int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev);
213c7059c5aSTony Lindgren 
214c7059c5aSTony Lindgren const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
215c7059c5aSTony Lindgren 					   unsigned int group_selector);
216c7059c5aSTony Lindgren 
217c7059c5aSTony Lindgren int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
218c7059c5aSTony Lindgren 				   unsigned int group_selector,
219c7059c5aSTony Lindgren 				   const unsigned int **pins,
220c7059c5aSTony Lindgren 				   unsigned int *npins);
221c7059c5aSTony Lindgren 
222c7059c5aSTony Lindgren struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
223c7059c5aSTony Lindgren 					     unsigned int group_selector);
224c7059c5aSTony Lindgren 
225c7059c5aSTony Lindgren int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
226c7059c5aSTony Lindgren 			      int *gpins, int ngpins, void *data);
227c7059c5aSTony Lindgren 
228c7059c5aSTony Lindgren int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
229c7059c5aSTony Lindgren 				 unsigned int group_selector);
230c7059c5aSTony Lindgren 
231c033a718SLinus Walleij #endif	/* CONFIG_GENERIC_PINCTRL_GROUPS */
232c7059c5aSTony Lindgren 
2339dfac4fdSLinus Walleij struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
23442fed7baSPatrice Chotard struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np);
235ae6b4d85SLinus Walleij int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name);
236dcb5dbc3SDong Aisheng const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin);
2377afde8baSLinus Walleij int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
2387afde8baSLinus Walleij 			       const char *pin_group);
2392304b473SStephen Warren 
pin_desc_get(struct pinctrl_dev * pctldev,unsigned int pin)2402304b473SStephen Warren static inline struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev,
2412304b473SStephen Warren 					    unsigned int pin)
2422304b473SStephen Warren {
2432304b473SStephen Warren 	return radix_tree_lookup(&pctldev->pin_desc_tree, pin);
2442304b473SStephen Warren }
24557b676f9SStephen Warren 
246b18537cdSJoachim Eastwood extern struct pinctrl_gpio_range *
247b18537cdSJoachim Eastwood pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
248b18537cdSJoachim Eastwood 					unsigned int pin);
249b18537cdSJoachim Eastwood 
250840a47baSJulien Delacou extern int pinctrl_force_sleep(struct pinctrl_dev *pctldev);
251840a47baSJulien Delacou extern int pinctrl_force_default(struct pinctrl_dev *pctldev);
252840a47baSJulien Delacou 
25342fed7baSPatrice Chotard extern struct mutex pinctrl_maps_mutex;
2546f9e41f4SLaurent Meunier extern struct list_head pinctrl_maps;
2556f9e41f4SLaurent Meunier 
25606de5193SAndy Shevchenko #define for_each_pin_map(_maps_node_, _map_)						\
2576f9e41f4SLaurent Meunier 	list_for_each_entry(_maps_node_, &pinctrl_maps, node)				\
25806de5193SAndy Shevchenko 		for (unsigned int __i = 0;						\
25906de5193SAndy Shevchenko 		     __i < _maps_node_->num_maps && (_map_ = &_maps_node_->maps[__i]);	\
26006de5193SAndy Shevchenko 		     __i++)
261