xref: /linux-6.15/include/linux/pinctrl/pinctrl.h (revision 7cc4e6b0)
1af873fceSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
22744e8afSLinus Walleij /*
32744e8afSLinus Walleij  * Interface the pinctrl subsystem
42744e8afSLinus Walleij  *
52744e8afSLinus Walleij  * Copyright (C) 2011 ST-Ericsson SA
62744e8afSLinus Walleij  * Written on behalf of Linaro for ST-Ericsson
72744e8afSLinus Walleij  * This interface is used in the core to keep track of pins.
82744e8afSLinus Walleij  *
92744e8afSLinus Walleij  * Author: Linus Walleij <[email protected]>
102744e8afSLinus Walleij  */
112744e8afSLinus Walleij #ifndef __LINUX_PINCTRL_PINCTRL_H
122744e8afSLinus Walleij #define __LINUX_PINCTRL_PINCTRL_H
132744e8afSLinus Walleij 
14e5530adcSAndy Shevchenko #include <linux/types.h>
1546919ae6SStephen Warren 
160acfb076SStephen Warren struct device;
17e5530adcSAndy Shevchenko struct device_node;
18e5530adcSAndy Shevchenko struct gpio_chip;
19e5530adcSAndy Shevchenko struct module;
20e5530adcSAndy Shevchenko struct seq_file;
21e5530adcSAndy Shevchenko 
22e5530adcSAndy Shevchenko struct pin_config_item;
23e5530adcSAndy Shevchenko struct pinconf_generic_params;
24e5530adcSAndy Shevchenko struct pinconf_ops;
252744e8afSLinus Walleij struct pinctrl_dev;
2657291ce2SStephen Warren struct pinctrl_map;
272744e8afSLinus Walleij struct pinmux_ops;
282744e8afSLinus Walleij 
292744e8afSLinus Walleij /**
30003cbe04SBasavaraj Natikar  * struct pingroup - provides information on pingroup
31003cbe04SBasavaraj Natikar  * @name: a name for pingroup
32003cbe04SBasavaraj Natikar  * @pins: an array of pins in the pingroup
33003cbe04SBasavaraj Natikar  * @npins: number of pins in the pingroup
34003cbe04SBasavaraj Natikar  */
35003cbe04SBasavaraj Natikar struct pingroup {
36003cbe04SBasavaraj Natikar 	const char *name;
37003cbe04SBasavaraj Natikar 	const unsigned int *pins;
38003cbe04SBasavaraj Natikar 	size_t npins;
39003cbe04SBasavaraj Natikar };
40003cbe04SBasavaraj Natikar 
41003cbe04SBasavaraj Natikar /* Convenience macro to define a single named or anonymous pingroup */
42003cbe04SBasavaraj Natikar #define PINCTRL_PINGROUP(_name, _pins, _npins)	\
43003cbe04SBasavaraj Natikar (struct pingroup) {				\
44003cbe04SBasavaraj Natikar 	.name = _name,				\
45003cbe04SBasavaraj Natikar 	.pins = _pins,				\
46003cbe04SBasavaraj Natikar 	.npins = _npins,			\
47003cbe04SBasavaraj Natikar }
48003cbe04SBasavaraj Natikar 
49003cbe04SBasavaraj Natikar /**
502744e8afSLinus Walleij  * struct pinctrl_pin_desc - boards/machines provide information on their
512744e8afSLinus Walleij  * pins, pads or other muxable units in this struct
522744e8afSLinus Walleij  * @number: unique pin number from the global pin number space
532744e8afSLinus Walleij  * @name: a name for this pin
54a30d5421SSherman Yin  * @drv_data: driver-defined per-pin data. pinctrl core does not touch this
552744e8afSLinus Walleij  */
562744e8afSLinus Walleij struct pinctrl_pin_desc {
57*7cc4e6b0SAndy Shevchenko 	unsigned int number;
582744e8afSLinus Walleij 	const char *name;
59a30d5421SSherman Yin 	void *drv_data;
602744e8afSLinus Walleij };
612744e8afSLinus Walleij 
622744e8afSLinus Walleij /* Convenience macro to define a single named or anonymous pin descriptor */
632744e8afSLinus Walleij #define PINCTRL_PIN(a, b) { .number = a, .name = b }
642744e8afSLinus Walleij #define PINCTRL_PIN_ANON(a) { .number = a }
652744e8afSLinus Walleij 
662744e8afSLinus Walleij /**
672744e8afSLinus Walleij  * struct pinctrl_gpio_range - each pin controller can provide subranges of
682744e8afSLinus Walleij  * the GPIO number space to be handled by the controller
692744e8afSLinus Walleij  * @node: list node for internal use
702744e8afSLinus Walleij  * @name: a name for the chip in this range
712744e8afSLinus Walleij  * @id: an ID number for the chip in this range
722744e8afSLinus Walleij  * @base: base offset of the GPIO range
7356a59911SChristian Ruppert  * @pin_base: base pin number of the GPIO range if pins == NULL
742744e8afSLinus Walleij  * @npins: number of pins in the GPIO range, including the base number
75a4da45ddSGeert Uytterhoeven  * @pins: enumeration of pins in GPIO range or NULL
762744e8afSLinus Walleij  * @gc: an optional pointer to a gpio_chip
772744e8afSLinus Walleij  */
782744e8afSLinus Walleij struct pinctrl_gpio_range {
792744e8afSLinus Walleij 	struct list_head node;
802744e8afSLinus Walleij 	const char *name;
812744e8afSLinus Walleij 	unsigned int id;
822744e8afSLinus Walleij 	unsigned int base;
833c739ad0SChanho Park 	unsigned int pin_base;
842744e8afSLinus Walleij 	unsigned int npins;
85*7cc4e6b0SAndy Shevchenko 	unsigned int const *pins;
862744e8afSLinus Walleij 	struct gpio_chip *gc;
872744e8afSLinus Walleij };
882744e8afSLinus Walleij 
892744e8afSLinus Walleij /**
902744e8afSLinus Walleij  * struct pinctrl_ops - global pin control operations, to be implemented by
912744e8afSLinus Walleij  * pin controller drivers.
92d1e90e9eSViresh Kumar  * @get_groups_count: Returns the count of total number of groups registered.
932744e8afSLinus Walleij  * @get_group_name: return the group name of the pin group
942744e8afSLinus Walleij  * @get_group_pins: return an array of pins corresponding to a certain
952744e8afSLinus Walleij  *	group selector @pins, and the size of the array in @num_pins
962744e8afSLinus Walleij  * @pin_dbg_show: optional debugfs display hook that will provide per-device
972744e8afSLinus Walleij  *	info for a certain pin in debugfs
9802ae6da2SStephen Warren  * @dt_node_to_map: parse a device tree "pin configuration node", and create
9902ae6da2SStephen Warren  *	mapping table entries for it. These are returned through the @map and
10002ae6da2SStephen Warren  *	@num_maps output parameters. This function is optional, and may be
10102ae6da2SStephen Warren  *	omitted for pinctrl drivers that do not support device tree.
10202ae6da2SStephen Warren  * @dt_free_map: free mapping table entries created via @dt_node_to_map. The
10302ae6da2SStephen Warren  *	top-level @map pointer must be freed, along with any dynamically
10402ae6da2SStephen Warren  *	allocated members of the mapping table entries themselves. This
10502ae6da2SStephen Warren  *	function is optional, and may be omitted for pinctrl drivers that do
10602ae6da2SStephen Warren  *	not support device tree.
1072744e8afSLinus Walleij  */
1082744e8afSLinus Walleij struct pinctrl_ops {
109d1e90e9eSViresh Kumar 	int (*get_groups_count) (struct pinctrl_dev *pctldev);
1102744e8afSLinus Walleij 	const char *(*get_group_name) (struct pinctrl_dev *pctldev,
111*7cc4e6b0SAndy Shevchenko 				       unsigned int selector);
1122744e8afSLinus Walleij 	int (*get_group_pins) (struct pinctrl_dev *pctldev,
113*7cc4e6b0SAndy Shevchenko 			       unsigned int selector,
114*7cc4e6b0SAndy Shevchenko 			       const unsigned int **pins,
115*7cc4e6b0SAndy Shevchenko 			       unsigned int *num_pins);
1162744e8afSLinus Walleij 	void (*pin_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,
117*7cc4e6b0SAndy Shevchenko 			      unsigned int offset);
11857291ce2SStephen Warren 	int (*dt_node_to_map) (struct pinctrl_dev *pctldev,
11957291ce2SStephen Warren 			       struct device_node *np_config,
120*7cc4e6b0SAndy Shevchenko 			       struct pinctrl_map **map, unsigned int *num_maps);
12157291ce2SStephen Warren 	void (*dt_free_map) (struct pinctrl_dev *pctldev,
122*7cc4e6b0SAndy Shevchenko 			     struct pinctrl_map *map, unsigned int num_maps);
1232744e8afSLinus Walleij };
1242744e8afSLinus Walleij 
1252744e8afSLinus Walleij /**
1262744e8afSLinus Walleij  * struct pinctrl_desc - pin controller descriptor, register this to pin
1272744e8afSLinus Walleij  * control subsystem
1282744e8afSLinus Walleij  * @name: name for the pin controller
1292744e8afSLinus Walleij  * @pins: an array of pin descriptors describing all the pins handled by
1302744e8afSLinus Walleij  *	this pin controller
1312744e8afSLinus Walleij  * @npins: number of descriptors in the array, usually just ARRAY_SIZE()
1322744e8afSLinus Walleij  *	of the pins field above
1332744e8afSLinus Walleij  * @pctlops: pin control operation vtable, to support global concepts like
1342744e8afSLinus Walleij  *	grouping of pins, this is optional.
135ae6b4d85SLinus Walleij  * @pmxops: pinmux operations vtable, if you support pinmuxing in your driver
136ae6b4d85SLinus Walleij  * @confops: pin config operations vtable, if you support pin configuration in
137ae6b4d85SLinus Walleij  *	your driver
1382744e8afSLinus Walleij  * @owner: module providing the pin controller, used for refcounting
139f684e4acSLinus Walleij  * @num_custom_params: Number of driver-specific custom parameters to be parsed
140f684e4acSLinus Walleij  *	from the hardware description
141f684e4acSLinus Walleij  * @custom_params: List of driver_specific custom parameters to be parsed from
142f684e4acSLinus Walleij  *	the hardware description
143f684e4acSLinus Walleij  * @custom_conf_items: Information how to print @params in debugfs, must be
144f684e4acSLinus Walleij  *	the same size as the @custom_params, i.e. @num_custom_params
145036f394dSBenjamin Gaignard  * @link_consumers: If true create a device link between pinctrl and its
146036f394dSBenjamin Gaignard  *	consumers (i.e. the devices requesting pin control states). This is
147036f394dSBenjamin Gaignard  *	sometimes necessary to ascertain the right suspend/resume order for
148036f394dSBenjamin Gaignard  *	example.
1492744e8afSLinus Walleij  */
1502744e8afSLinus Walleij struct pinctrl_desc {
1512744e8afSLinus Walleij 	const char *name;
152b3da97eeSMasahiro Yamada 	const struct pinctrl_pin_desc *pins;
1532744e8afSLinus Walleij 	unsigned int npins;
154022ab148SLaurent Pinchart 	const struct pinctrl_ops *pctlops;
155022ab148SLaurent Pinchart 	const struct pinmux_ops *pmxops;
156022ab148SLaurent Pinchart 	const struct pinconf_ops *confops;
1572744e8afSLinus Walleij 	struct module *owner;
158f684e4acSLinus Walleij #ifdef CONFIG_GENERIC_PINCONF
159f684e4acSLinus Walleij 	unsigned int num_custom_params;
160f684e4acSLinus Walleij 	const struct pinconf_generic_params *custom_params;
161f684e4acSLinus Walleij 	const struct pin_config_item *custom_conf_items;
162dd4d01f7SSoren Brinkmann #endif
163036f394dSBenjamin Gaignard 	bool link_consumers;
1642744e8afSLinus Walleij };
1652744e8afSLinus Walleij 
1662744e8afSLinus Walleij /* External interface to pin controller */
167950b0d91STony Lindgren 
168950b0d91STony Lindgren extern int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
169950b0d91STony Lindgren 				     struct device *dev, void *driver_data,
170950b0d91STony Lindgren 				     struct pinctrl_dev **pctldev);
17161187142STony Lindgren extern int pinctrl_enable(struct pinctrl_dev *pctldev);
172950b0d91STony Lindgren 
17361187142STony Lindgren /* Please use pinctrl_register_and_init() and pinctrl_enable() instead */
1742744e8afSLinus Walleij extern struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1752744e8afSLinus Walleij 				struct device *dev, void *driver_data);
176950b0d91STony Lindgren 
1772744e8afSLinus Walleij extern void pinctrl_unregister(struct pinctrl_dev *pctldev);
178950b0d91STony Lindgren 
179950b0d91STony Lindgren extern int devm_pinctrl_register_and_init(struct device *dev,
180950b0d91STony Lindgren 				struct pinctrl_desc *pctldesc,
181950b0d91STony Lindgren 				void *driver_data,
182950b0d91STony Lindgren 				struct pinctrl_dev **pctldev);
183950b0d91STony Lindgren 
184950b0d91STony Lindgren /* Please use devm_pinctrl_register_and_init() instead */
18580e0f8d9SLaxman Dewangan extern struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
18680e0f8d9SLaxman Dewangan 				struct pinctrl_desc *pctldesc,
18780e0f8d9SLaxman Dewangan 				void *driver_data);
188950b0d91STony Lindgren 
18980e0f8d9SLaxman Dewangan extern void devm_pinctrl_unregister(struct device *dev,
19080e0f8d9SLaxman Dewangan 				struct pinctrl_dev *pctldev);
19180e0f8d9SLaxman Dewangan 
1922744e8afSLinus Walleij extern void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
1932744e8afSLinus Walleij 				struct pinctrl_gpio_range *range);
1943e5e00b6SDong Aisheng extern void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
1953e5e00b6SDong Aisheng 				struct pinctrl_gpio_range *ranges,
196*7cc4e6b0SAndy Shevchenko 				unsigned int nranges);
1977e10ee68SViresh Kumar extern void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
1987e10ee68SViresh Kumar 				struct pinctrl_gpio_range *range);
199f23f1516SShiraz Hashim 
200192c369cSLinus Walleij extern struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
201f23f1516SShiraz Hashim 		struct pinctrl_gpio_range *range);
2029afbefb2SLinus Walleij extern struct pinctrl_gpio_range *
2039afbefb2SLinus Walleij pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
2049afbefb2SLinus Walleij 				 unsigned int pin);
205586a87e6SChristian Ruppert extern int pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
206*7cc4e6b0SAndy Shevchenko 				  const char *pin_group, const unsigned int **pins,
207*7cc4e6b0SAndy Shevchenko 				  unsigned int *num_pins);
208f23f1516SShiraz Hashim 
209443a0a0fSAndy Shevchenko /**
210443a0a0fSAndy Shevchenko  * struct pinfunction - Description about a function
211443a0a0fSAndy Shevchenko  * @name: Name of the function
212443a0a0fSAndy Shevchenko  * @groups: An array of groups for this function
213443a0a0fSAndy Shevchenko  * @ngroups: Number of groups in @groups
214443a0a0fSAndy Shevchenko  */
215443a0a0fSAndy Shevchenko struct pinfunction {
216443a0a0fSAndy Shevchenko 	const char *name;
217443a0a0fSAndy Shevchenko 	const char * const *groups;
218443a0a0fSAndy Shevchenko 	size_t ngroups;
219443a0a0fSAndy Shevchenko };
220443a0a0fSAndy Shevchenko 
221443a0a0fSAndy Shevchenko /* Convenience macro to define a single named pinfunction */
222443a0a0fSAndy Shevchenko #define PINCTRL_PINFUNCTION(_name, _groups, _ngroups)	\
223443a0a0fSAndy Shevchenko (struct pinfunction) {					\
224443a0a0fSAndy Shevchenko 		.name = (_name),			\
225443a0a0fSAndy Shevchenko 		.groups = (_groups),			\
226443a0a0fSAndy Shevchenko 		.ngroups = (_ngroups),			\
227443a0a0fSAndy Shevchenko 	}
228443a0a0fSAndy Shevchenko 
229e45ee71aSThierry Reding #if IS_ENABLED(CONFIG_OF) && IS_ENABLED(CONFIG_PINCTRL)
2301e63d7b9SLinus Walleij extern struct pinctrl_dev *of_pinctrl_get(struct device_node *np);
231f23f1516SShiraz Hashim #else
232f23f1516SShiraz Hashim static inline
of_pinctrl_get(struct device_node * np)2331e63d7b9SLinus Walleij struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
234f23f1516SShiraz Hashim {
235f23f1516SShiraz Hashim 	return NULL;
236f23f1516SShiraz Hashim }
237f23f1516SShiraz Hashim #endif /* CONFIG_OF */
238f23f1516SShiraz Hashim 
2392744e8afSLinus Walleij extern const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev);
240d6e99abbSHaojian Zhuang extern const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev);
2412744e8afSLinus Walleij extern void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev);
2422744e8afSLinus Walleij 
2432744e8afSLinus Walleij #endif /* __LINUX_PINCTRL_PINCTRL_H */
244