xref: /linux/drivers/pinctrl/core.h (revision c5272a28566b00cce79127ad382406e0a8650690)
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
272744e8afSLinus Walleij  * @gpio_ranges: a list of GPIO ranges that is handled by this pin controller,
282744e8afSLinus Walleij  *	ranges are added to this list at runtime
292744e8afSLinus Walleij  * @dev: the device entry for this pin controller
302744e8afSLinus Walleij  * @owner: module providing the pin controller, used for refcounting
312744e8afSLinus Walleij  * @driver_data: driver data for drivers registering to the pin controller
322744e8afSLinus Walleij  *	subsystem
3346919ae6SStephen Warren  * @p: result of pinctrl_get() for this device
34840a47baSJulien Delacou  * @hog_default: default state for pins hogged by this device
35840a47baSJulien Delacou  * @hog_sleep: sleep state for pins hogged by this device
3642fed7baSPatrice Chotard  * @mutex: mutex taken on each pin controller specific action
37befe5bdfSLinus Walleij  * @device_root: debugfs root for this device
382744e8afSLinus Walleij  */
392744e8afSLinus Walleij struct pinctrl_dev {
402744e8afSLinus Walleij 	struct list_head node;
412744e8afSLinus Walleij 	struct pinctrl_desc *desc;
422744e8afSLinus Walleij 	struct radix_tree_root pin_desc_tree;
432744e8afSLinus Walleij 	struct list_head gpio_ranges;
4451cd24eeSStephen Warren 	struct device *dev;
452744e8afSLinus Walleij 	struct module *owner;
462744e8afSLinus Walleij 	void *driver_data;
4746919ae6SStephen Warren 	struct pinctrl *p;
48840a47baSJulien Delacou 	struct pinctrl_state *hog_default;
49840a47baSJulien Delacou 	struct pinctrl_state *hog_sleep;
5042fed7baSPatrice Chotard 	struct mutex mutex;
5102157160STony Lindgren #ifdef CONFIG_DEBUG_FS
5202157160STony Lindgren 	struct dentry *device_root;
5302157160STony Lindgren #endif
54befe5bdfSLinus Walleij };
55befe5bdfSLinus Walleij 
56befe5bdfSLinus Walleij /**
57befe5bdfSLinus Walleij  * struct pinctrl - per-device pin control state holder
58befe5bdfSLinus Walleij  * @node: global list node
59befe5bdfSLinus Walleij  * @dev: the device using this pin control handle
606e5e959dSStephen Warren  * @states: a list of states for this device
616e5e959dSStephen Warren  * @state: the current state
6257291ce2SStephen Warren  * @dt_maps: the mapping table chunks dynamically parsed from device tree for
6357291ce2SStephen Warren  *	this device, if any
64ab78029eSLinus Walleij  * @users: reference count
65befe5bdfSLinus Walleij  */
66befe5bdfSLinus Walleij struct pinctrl {
67befe5bdfSLinus Walleij 	struct list_head node;
68befe5bdfSLinus Walleij 	struct device *dev;
696e5e959dSStephen Warren 	struct list_head states;
706e5e959dSStephen Warren 	struct pinctrl_state *state;
7157291ce2SStephen Warren 	struct list_head dt_maps;
72ab78029eSLinus Walleij 	struct kref users;
736e5e959dSStephen Warren };
746e5e959dSStephen Warren 
756e5e959dSStephen Warren /**
766e5e959dSStephen Warren  * struct pinctrl_state - a pinctrl state for a device
772c9abf80SRichard Genoud  * @node: list node for struct pinctrl's @states field
786e5e959dSStephen Warren  * @name: the name of this state
796e5e959dSStephen Warren  * @settings: a list of settings for this state
806e5e959dSStephen Warren  */
816e5e959dSStephen Warren struct pinctrl_state {
826e5e959dSStephen Warren 	struct list_head node;
836e5e959dSStephen Warren 	const char *name;
847ecdb16fSStephen Warren 	struct list_head settings;
857ecdb16fSStephen Warren };
867ecdb16fSStephen Warren 
877ecdb16fSStephen Warren /**
881e2082b5SStephen Warren  * struct pinctrl_setting_mux - setting data for MAP_TYPE_MUX_GROUP
891e2082b5SStephen Warren  * @group: the group selector to program
901e2082b5SStephen Warren  * @func: the function selector to program
911e2082b5SStephen Warren  */
921e2082b5SStephen Warren struct pinctrl_setting_mux {
931e2082b5SStephen Warren 	unsigned group;
941e2082b5SStephen Warren 	unsigned func;
951e2082b5SStephen Warren };
961e2082b5SStephen Warren 
971e2082b5SStephen Warren /**
981e2082b5SStephen Warren  * struct pinctrl_setting_configs - setting data for MAP_TYPE_CONFIGS_*
991e2082b5SStephen Warren  * @group_or_pin: the group selector or pin ID to program
1001e2082b5SStephen Warren  * @configs: a pointer to an array of config parameters/values to program into
1011e2082b5SStephen Warren  *	hardware. Each individual pin controller defines the format and meaning
1021e2082b5SStephen Warren  *	of config parameters.
1031e2082b5SStephen Warren  * @num_configs: the number of entries in array @configs
1041e2082b5SStephen Warren  */
1051e2082b5SStephen Warren struct pinctrl_setting_configs {
1061e2082b5SStephen Warren 	unsigned group_or_pin;
1071e2082b5SStephen Warren 	unsigned long *configs;
1081e2082b5SStephen Warren 	unsigned num_configs;
1091e2082b5SStephen Warren };
1101e2082b5SStephen Warren 
1111e2082b5SStephen Warren /**
112872acc32SLinus Walleij  * struct pinctrl_setting - an individual mux or config setting
1136e5e959dSStephen Warren  * @node: list node for struct pinctrl_settings's @settings field
1141e2082b5SStephen Warren  * @type: the type of setting
11557291ce2SStephen Warren  * @pctldev: pin control device handling to be programmed. Not used for
11657291ce2SStephen Warren  *   PIN_MAP_TYPE_DUMMY_STATE.
1171a78958dSLinus Walleij  * @dev_name: the name of the device using this state
1181e2082b5SStephen Warren  * @data: Data specific to the setting type
1197ecdb16fSStephen Warren  */
1207ecdb16fSStephen Warren struct pinctrl_setting {
1217ecdb16fSStephen Warren 	struct list_head node;
1221e2082b5SStephen Warren 	enum pinctrl_map_type type;
123befe5bdfSLinus Walleij 	struct pinctrl_dev *pctldev;
1241a78958dSLinus Walleij 	const char *dev_name;
1251e2082b5SStephen Warren 	union {
1261e2082b5SStephen Warren 		struct pinctrl_setting_mux mux;
1271e2082b5SStephen Warren 		struct pinctrl_setting_configs configs;
1281e2082b5SStephen Warren 	} data;
1292744e8afSLinus Walleij };
1302744e8afSLinus Walleij 
1312744e8afSLinus Walleij /**
1322744e8afSLinus Walleij  * struct pin_desc - pin descriptor for each physical pin in the arch
1332744e8afSLinus Walleij  * @pctldev: corresponding pin control device
1342744e8afSLinus Walleij  * @name: a name for the pin, e.g. the name of the pin/pad/finger on a
1352744e8afSLinus Walleij  *	datasheet or such
136ca53c5f1SLinus Walleij  * @dynamic_name: if the name of this pin was dynamically allocated
137652162d4SStephen Warren  * @mux_usecount: If zero, the pin is not claimed, and @owner should be NULL.
1380e3db173SStephen Warren  *	If non-zero, this pin is claimed by @owner. This field is an integer
1390e3db173SStephen Warren  *	rather than a boolean, since pinctrl_get() might process multiple
1400e3db173SStephen Warren  *	mapping table entries that refer to, and hence claim, the same group
1410e3db173SStephen Warren  *	or pin, and each of these will increment the @usecount.
142652162d4SStephen Warren  * @mux_owner: The name of device that called pinctrl_get().
143ba110d90SStephen Warren  * @mux_setting: The most recent selected mux setting for this pin, if any.
144652162d4SStephen Warren  * @gpio_owner: If pinctrl_request_gpio() was called for this pin, this is
145652162d4SStephen Warren  *	the name of the GPIO that "owns" this pin.
1462744e8afSLinus Walleij  */
1472744e8afSLinus Walleij struct pin_desc {
1482744e8afSLinus Walleij 	struct pinctrl_dev *pctldev;
1499af1e44fSStephen Warren 	const char *name;
150ca53c5f1SLinus Walleij 	bool dynamic_name;
1512744e8afSLinus Walleij 	/* These fields only added when supporting pinmux drivers */
1522744e8afSLinus Walleij #ifdef CONFIG_PINMUX
153652162d4SStephen Warren 	unsigned mux_usecount;
154652162d4SStephen Warren 	const char *mux_owner;
155ba110d90SStephen Warren 	const struct pinctrl_setting_mux *mux_setting;
156652162d4SStephen Warren 	const char *gpio_owner;
1572744e8afSLinus Walleij #endif
1582744e8afSLinus Walleij };
1592744e8afSLinus Walleij 
1606f9e41f4SLaurent Meunier /**
1616f9e41f4SLaurent Meunier  * struct pinctrl_maps - a list item containing part of the mapping table
1626f9e41f4SLaurent Meunier  * @node: mapping table list node
1636f9e41f4SLaurent Meunier  * @maps: array of mapping table entries
1646f9e41f4SLaurent Meunier  * @num_maps: the number of entries in @maps
1656f9e41f4SLaurent Meunier  */
1666f9e41f4SLaurent Meunier struct pinctrl_maps {
1676f9e41f4SLaurent Meunier 	struct list_head node;
1686f9e41f4SLaurent Meunier 	struct pinctrl_map const *maps;
1696f9e41f4SLaurent Meunier 	unsigned num_maps;
1706f9e41f4SLaurent Meunier };
1716f9e41f4SLaurent Meunier 
1729dfac4fdSLinus Walleij struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
17342fed7baSPatrice Chotard struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np);
174ae6b4d85SLinus Walleij int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name);
175dcb5dbc3SDong Aisheng const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin);
1767afde8baSLinus Walleij int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
1777afde8baSLinus Walleij 			       const char *pin_group);
1782304b473SStephen Warren 
1792304b473SStephen Warren static inline struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev,
1802304b473SStephen Warren 					    unsigned int pin)
1812304b473SStephen Warren {
1822304b473SStephen Warren 	return radix_tree_lookup(&pctldev->pin_desc_tree, pin);
1832304b473SStephen Warren }
18457b676f9SStephen Warren 
18557291ce2SStephen Warren int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
186*c5272a28SDoug Anderson 			 bool dup);
18757291ce2SStephen Warren void pinctrl_unregister_map(struct pinctrl_map const *map);
18857291ce2SStephen Warren 
189840a47baSJulien Delacou extern int pinctrl_force_sleep(struct pinctrl_dev *pctldev);
190840a47baSJulien Delacou extern int pinctrl_force_default(struct pinctrl_dev *pctldev);
191840a47baSJulien Delacou 
19242fed7baSPatrice Chotard extern struct mutex pinctrl_maps_mutex;
1936f9e41f4SLaurent Meunier extern struct list_head pinctrl_maps;
1946f9e41f4SLaurent Meunier 
1956f9e41f4SLaurent Meunier #define for_each_maps(_maps_node_, _i_, _map_) \
1966f9e41f4SLaurent Meunier 	list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
1976f9e41f4SLaurent Meunier 		for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
1986f9e41f4SLaurent Meunier 			_i_ < _maps_node_->num_maps; \
1996f9e41f4SLaurent Meunier 			_i_++, _map_ = &_maps_node_->maps[_i_])
200