xref: /OK3568_Linux_fs/u-boot/include/asm-generic/gpio.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2011 The Chromium OS Authors.
3*4882a593Smuzhiyun  * Copyright (c) 2011, NVIDIA Corp. All rights reserved.
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef _ASM_GENERIC_GPIO_H_
8*4882a593Smuzhiyun #define _ASM_GENERIC_GPIO_H_
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <dm/ofnode.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun struct ofnode_phandle_args;
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  * Generic GPIO API for U-Boot
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * --
18*4882a593Smuzhiyun  * NB: This is deprecated. Please use the driver model functions instead:
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *    - gpio_request_by_name()
21*4882a593Smuzhiyun  *    - dm_gpio_get_value() etc.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * For now we need a dm_ prefix on some functions to avoid name collision.
24*4882a593Smuzhiyun  * --
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined
27*4882a593Smuzhiyun  * by the SOC/architecture.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * Each GPIO can be an input or output. If an input then its value can
30*4882a593Smuzhiyun  * be read as 0 or 1. If an output then its value can be set to 0 or 1.
31*4882a593Smuzhiyun  * If you try to write an input then the value is undefined. If you try
32*4882a593Smuzhiyun  * to read an output, barring something very unusual,  you will get
33*4882a593Smuzhiyun  * back the value of the output that you previously set.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * In some cases the operation may fail, for example if the GPIO number
36*4882a593Smuzhiyun  * is out of range, or the GPIO is not available because its pin is
37*4882a593Smuzhiyun  * being used by another function. In that case, functions may return
38*4882a593Smuzhiyun  * an error value of -1.
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun  * @deprecated	Please use driver model instead
43*4882a593Smuzhiyun  * Request a GPIO. This should be called before any of the other functions
44*4882a593Smuzhiyun  * are used on this GPIO.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * Note: With driver model, the label is allocated so there is no need for
47*4882a593Smuzhiyun  * the caller to preserve it.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * @param gpio	GPIO number
50*4882a593Smuzhiyun  * @param label	User label for this GPIO
51*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun int gpio_request(unsigned gpio, const char *label);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun  * @deprecated	Please use driver model instead
57*4882a593Smuzhiyun  * Stop using the GPIO.  This function should not alter pin configuration.
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * @param gpio	GPIO number
60*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
61*4882a593Smuzhiyun  */
62*4882a593Smuzhiyun int gpio_free(unsigned gpio);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /**
65*4882a593Smuzhiyun  * @deprecated	Please use driver model instead
66*4882a593Smuzhiyun  * Make a GPIO an input.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * @param gpio	GPIO number
69*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
70*4882a593Smuzhiyun  */
71*4882a593Smuzhiyun int gpio_direction_input(unsigned gpio);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /**
74*4882a593Smuzhiyun  * @deprecated	Please use driver model instead
75*4882a593Smuzhiyun  * Make a GPIO an output, and set its value.
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * @param gpio	GPIO number
78*4882a593Smuzhiyun  * @param value	GPIO value (0 for low or 1 for high)
79*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun int gpio_direction_output(unsigned gpio, int value);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * @deprecated	Please use driver model instead
85*4882a593Smuzhiyun  * Get a GPIO's value. This will work whether the GPIO is an input
86*4882a593Smuzhiyun  * or an output.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * @param gpio	GPIO number
89*4882a593Smuzhiyun  * @return 0 if low, 1 if high, -1 on error
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun int gpio_get_value(unsigned gpio);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /**
94*4882a593Smuzhiyun  * @deprecated	Please use driver model instead
95*4882a593Smuzhiyun  * Set an output GPIO's value. The GPIO must already be an output or
96*4882a593Smuzhiyun  * this function may have no effect.
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * @param gpio	GPIO number
99*4882a593Smuzhiyun  * @param value	GPIO value (0 for low or 1 for high)
100*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
101*4882a593Smuzhiyun  */
102*4882a593Smuzhiyun int gpio_set_value(unsigned gpio, int value);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /* State of a GPIO, as reported by get_function() */
105*4882a593Smuzhiyun enum gpio_func_t {
106*4882a593Smuzhiyun 	GPIOF_INPUT = 0,
107*4882a593Smuzhiyun 	GPIOF_OUTPUT,
108*4882a593Smuzhiyun 	GPIOF_UNUSED,		/* Not claimed */
109*4882a593Smuzhiyun 	GPIOF_UNKNOWN,		/* Not known */
110*4882a593Smuzhiyun 	GPIOF_FUNC,		/* Not used as a GPIO */
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	GPIOF_COUNT,
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun struct udevice;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun struct gpio_desc {
118*4882a593Smuzhiyun 	struct udevice *dev;	/* Device, NULL for invalid GPIO */
119*4882a593Smuzhiyun 	unsigned long flags;
120*4882a593Smuzhiyun #define GPIOD_REQUESTED		(1 << 0)	/* Requested/claimed */
121*4882a593Smuzhiyun #define GPIOD_IS_OUT		(1 << 1)	/* GPIO is an output */
122*4882a593Smuzhiyun #define GPIOD_IS_IN		(1 << 2)	/* GPIO is an input */
123*4882a593Smuzhiyun #define GPIOD_ACTIVE_LOW	(1 << 3)	/* value has active low */
124*4882a593Smuzhiyun #define GPIOD_IS_OUT_ACTIVE	(1 << 4)	/* set output active */
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	uint offset;		/* GPIO offset within the device */
127*4882a593Smuzhiyun 	/*
128*4882a593Smuzhiyun 	 * We could consider adding the GPIO label in here. Possibly we could
129*4882a593Smuzhiyun 	 * use this structure for internal GPIO information.
130*4882a593Smuzhiyun 	 */
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /**
134*4882a593Smuzhiyun  * dm_gpio_is_valid() - Check if a GPIO is valid
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  * @desc:	GPIO description containing device, offset and flags,
137*4882a593Smuzhiyun  *		previously returned by gpio_request_by_name()
138*4882a593Smuzhiyun  * @return true if valid, false if not
139*4882a593Smuzhiyun  */
dm_gpio_is_valid(const struct gpio_desc * desc)140*4882a593Smuzhiyun static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	return desc->dev != NULL;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun  * gpio_get_status() - get the current GPIO status as a string
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * Obtain the current GPIO status as a string which can be presented to the
149*4882a593Smuzhiyun  * user. A typical string is:
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  * "b4:  in: 1 [x] sdmmc_cd"
152*4882a593Smuzhiyun  *
153*4882a593Smuzhiyun  * which means this is GPIO bank b, offset 4, currently set to input, current
154*4882a593Smuzhiyun  * value 1, [x] means that it is requested and the owner is 'sdmmc_cd'
155*4882a593Smuzhiyun  *
156*4882a593Smuzhiyun  * TODO(sjg@chromium.org): This should use struct gpio_desc
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * @dev:	Device to check
159*4882a593Smuzhiyun  * @offset:	Offset of device GPIO to check
160*4882a593Smuzhiyun  * @buf:	Place to put string
161*4882a593Smuzhiyun  * @buffsize:	Size of string including \0
162*4882a593Smuzhiyun  */
163*4882a593Smuzhiyun int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /**
166*4882a593Smuzhiyun  * gpio_get_function() - get the current function for a GPIO pin
167*4882a593Smuzhiyun  *
168*4882a593Smuzhiyun  * Note this returns GPIOF_UNUSED if the GPIO is not requested.
169*4882a593Smuzhiyun  *
170*4882a593Smuzhiyun  * TODO(sjg@chromium.org): This should use struct gpio_desc
171*4882a593Smuzhiyun  *
172*4882a593Smuzhiyun  * @dev:	Device to check
173*4882a593Smuzhiyun  * @offset:	Offset of device GPIO to check
174*4882a593Smuzhiyun  * @namep:	If non-NULL, this is set to the name given when the GPIO
175*4882a593Smuzhiyun  *		was requested, or -1 if it has not been requested
176*4882a593Smuzhiyun  * @return  -ENODATA if the driver returned an unknown function,
177*4882a593Smuzhiyun  * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
178*4882a593Smuzhiyun  * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
179*4882a593Smuzhiyun  * function from enum gpio_func_t.
180*4882a593Smuzhiyun  */
181*4882a593Smuzhiyun int gpio_get_function(struct udevice *dev, int offset, const char **namep);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /**
184*4882a593Smuzhiyun  * gpio_get_raw_function() - get the current raw function for a GPIO pin
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  * Note this does not return GPIOF_UNUSED - it will always return the GPIO
187*4882a593Smuzhiyun  * driver's view of a pin function, even if it is not correctly set up.
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * TODO(sjg@chromium.org): This should use struct gpio_desc
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * @dev:	Device to check
192*4882a593Smuzhiyun  * @offset:	Offset of device GPIO to check
193*4882a593Smuzhiyun  * @namep:	If non-NULL, this is set to the name given when the GPIO
194*4882a593Smuzhiyun  *		was requested, or -1 if it has not been requested
195*4882a593Smuzhiyun  * @return  -ENODATA if the driver returned an unknown function,
196*4882a593Smuzhiyun  * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
197*4882a593Smuzhiyun  * Otherwise returns the function from enum gpio_func_t.
198*4882a593Smuzhiyun  */
199*4882a593Smuzhiyun int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /**
202*4882a593Smuzhiyun  * gpio_requestf() - request a GPIO using a format string for the owner
203*4882a593Smuzhiyun  *
204*4882a593Smuzhiyun  * This is a helper function for gpio_request(). It allows you to provide
205*4882a593Smuzhiyun  * a printf()-format string for the GPIO owner. It calls gpio_request() with
206*4882a593Smuzhiyun  * the string that is created
207*4882a593Smuzhiyun  */
208*4882a593Smuzhiyun int gpio_requestf(unsigned gpio, const char *fmt, ...)
209*4882a593Smuzhiyun 		__attribute__ ((format (__printf__, 2, 3)));
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun struct fdtdec_phandle_args;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun /**
214*4882a593Smuzhiyun  * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * This routine sets the offset field to args[0] and the flags field to
217*4882a593Smuzhiyun  * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1].
218*4882a593Smuzhiyun  */
219*4882a593Smuzhiyun int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
220*4882a593Smuzhiyun 			  struct ofnode_phandle_args *args);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /**
223*4882a593Smuzhiyun  * struct struct dm_gpio_ops - Driver model GPIO operations
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Refer to functions above for description. These function largely copy
226*4882a593Smuzhiyun  * the old API.
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
229*4882a593Smuzhiyun  * new DM GPIO API, this should be really easy to flip over to the Linux
230*4882a593Smuzhiyun  * GPIO API-alike interface.
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * Also it would be useful to standardise additional functions like
233*4882a593Smuzhiyun  * pullup, slew rate and drive strength.
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * gpio_request() and gpio_free() are optional - if NULL then they will
236*4882a593Smuzhiyun  * not be called.
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * Note that @offset is the offset from the base GPIO of the device. So
239*4882a593Smuzhiyun  * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
240*4882a593Smuzhiyun  * where o is the number of GPIO lines controlled by the device. A device
241*4882a593Smuzhiyun  * is typically used to control a single bank of GPIOs. Within complex
242*4882a593Smuzhiyun  * SoCs there may be many banks and therefore many devices all referring
243*4882a593Smuzhiyun  * to the different IO addresses within the SoC.
244*4882a593Smuzhiyun  *
245*4882a593Smuzhiyun  * The uclass combines all GPIO devices together to provide a consistent
246*4882a593Smuzhiyun  * numbering from 0 to n-1, where n is the number of GPIOs in total across
247*4882a593Smuzhiyun  * all devices. Be careful not to confuse offset with gpio in the parameters.
248*4882a593Smuzhiyun  */
249*4882a593Smuzhiyun struct dm_gpio_ops {
250*4882a593Smuzhiyun 	int (*request)(struct udevice *dev, unsigned offset, const char *label);
251*4882a593Smuzhiyun 	int (*free)(struct udevice *dev, unsigned offset);
252*4882a593Smuzhiyun 	int (*direction_input)(struct udevice *dev, unsigned offset);
253*4882a593Smuzhiyun 	int (*direction_output)(struct udevice *dev, unsigned offset,
254*4882a593Smuzhiyun 				int value);
255*4882a593Smuzhiyun 	int (*get_value)(struct udevice *dev, unsigned offset);
256*4882a593Smuzhiyun 	int (*set_value)(struct udevice *dev, unsigned offset, int value);
257*4882a593Smuzhiyun 	int (*get_open_drain)(struct udevice *dev, unsigned offset);
258*4882a593Smuzhiyun 	int (*set_open_drain)(struct udevice *dev, unsigned offset, int value);
259*4882a593Smuzhiyun 	/**
260*4882a593Smuzhiyun 	 * get_function() Get the GPIO function
261*4882a593Smuzhiyun 	 *
262*4882a593Smuzhiyun 	 * @dev:     Device to check
263*4882a593Smuzhiyun 	 * @offset:  GPIO offset within that device
264*4882a593Smuzhiyun 	 * @return current function - GPIOF_...
265*4882a593Smuzhiyun 	 */
266*4882a593Smuzhiyun 	int (*get_function)(struct udevice *dev, unsigned offset);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/**
269*4882a593Smuzhiyun 	 * xlate() - Translate phandle arguments into a GPIO description
270*4882a593Smuzhiyun 	 *
271*4882a593Smuzhiyun 	 * This function should set up the fields in desc according to the
272*4882a593Smuzhiyun 	 * information in the arguments. The uclass will have set up:
273*4882a593Smuzhiyun 	 *
274*4882a593Smuzhiyun 	 *   @desc->dev to @dev
275*4882a593Smuzhiyun 	 *   @desc->flags to 0
276*4882a593Smuzhiyun 	 *   @desc->offset to 0
277*4882a593Smuzhiyun 	 *
278*4882a593Smuzhiyun 	 * This method is optional and defaults to gpio_xlate_offs_flags,
279*4882a593Smuzhiyun 	 * which will parse offset and the GPIO_ACTIVE_LOW flag in the first
280*4882a593Smuzhiyun 	 * two arguments.
281*4882a593Smuzhiyun 	 *
282*4882a593Smuzhiyun 	 * Note that @dev is passed in as a parameter to follow driver model
283*4882a593Smuzhiyun 	 * uclass conventions, even though it is already available as
284*4882a593Smuzhiyun 	 * desc->dev.
285*4882a593Smuzhiyun 	 *
286*4882a593Smuzhiyun 	 * @dev:	GPIO device
287*4882a593Smuzhiyun 	 * @desc:	Place to put GPIO description
288*4882a593Smuzhiyun 	 * @args:	Arguments provided in description
289*4882a593Smuzhiyun 	 * @return 0 if OK, -ve on error
290*4882a593Smuzhiyun 	 */
291*4882a593Smuzhiyun 	int (*xlate)(struct udevice *dev, struct gpio_desc *desc,
292*4882a593Smuzhiyun 		     struct ofnode_phandle_args *args);
293*4882a593Smuzhiyun };
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun  * struct gpio_dev_priv - information about a device used by the uclass
297*4882a593Smuzhiyun  *
298*4882a593Smuzhiyun  * The uclass combines all active GPIO devices into a unified numbering
299*4882a593Smuzhiyun  * scheme. To do this it maintains some private information about each
300*4882a593Smuzhiyun  * device.
301*4882a593Smuzhiyun  *
302*4882a593Smuzhiyun  * To implement driver model support in your GPIO driver, add a probe
303*4882a593Smuzhiyun  * handler, and set @gpio_count and @bank_name correctly in that handler.
304*4882a593Smuzhiyun  * This tells the uclass the name of the GPIO bank and the number of GPIOs
305*4882a593Smuzhiyun  * it contains.
306*4882a593Smuzhiyun  *
307*4882a593Smuzhiyun  * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
308*4882a593Smuzhiyun  * 'A0', 'A1', etc.
309*4882a593Smuzhiyun  * @gpio_count: Number of GPIOs in this device
310*4882a593Smuzhiyun  * @gpio_base: Base GPIO number for this device. For the first active device
311*4882a593Smuzhiyun  * this will be 0; the numbering for others will follow sequentially so that
312*4882a593Smuzhiyun  * @gpio_base for device 1 will equal the number of GPIOs in device 0.
313*4882a593Smuzhiyun  * @name: Array of pointers to the name for each GPIO in this bank. The
314*4882a593Smuzhiyun  * value of the pointer will be NULL if the GPIO has not been claimed.
315*4882a593Smuzhiyun  */
316*4882a593Smuzhiyun struct gpio_dev_priv {
317*4882a593Smuzhiyun 	const char *bank_name;
318*4882a593Smuzhiyun 	unsigned gpio_count;
319*4882a593Smuzhiyun 	unsigned gpio_base;
320*4882a593Smuzhiyun 	char **name;
321*4882a593Smuzhiyun };
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun /* Access the GPIO operations for a device */
324*4882a593Smuzhiyun #define gpio_get_ops(dev)	((struct dm_gpio_ops *)(dev)->driver->ops)
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun /**
327*4882a593Smuzhiyun  * gpio_get_bank_info - Return information about a GPIO bank/device
328*4882a593Smuzhiyun  *
329*4882a593Smuzhiyun  * This looks up a device and returns both its GPIO base name and the number
330*4882a593Smuzhiyun  * of GPIOs it controls.
331*4882a593Smuzhiyun  *
332*4882a593Smuzhiyun  * @dev: Device to look up
333*4882a593Smuzhiyun  * @offset_count: Returns number of GPIOs within this bank
334*4882a593Smuzhiyun  * @return bank name of this device
335*4882a593Smuzhiyun  */
336*4882a593Smuzhiyun const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun  * dm_gpio_lookup_name() - Look up a named GPIO and return its description
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * The name of a GPIO is typically its bank name followed by a number from 0.
342*4882a593Smuzhiyun  * For example A0 is the first GPIO in bank A. Each bank is a separate driver
343*4882a593Smuzhiyun  * model device.
344*4882a593Smuzhiyun  *
345*4882a593Smuzhiyun  * @name:	Name to look up
346*4882a593Smuzhiyun  * @desc:	Returns description, on success
347*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
348*4882a593Smuzhiyun  */
349*4882a593Smuzhiyun int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun  * gpio_hog_lookup_name() - Look up a named GPIO and return the gpio descr.
353*4882a593Smuzhiyun  *
354*4882a593Smuzhiyun  * @name:	Name to look up
355*4882a593Smuzhiyun  * @desc:	Returns GPIO description, on success, else NULL
356*4882a593Smuzhiyun  * @return:	Returns 0 if OK, else -ENODEV
357*4882a593Smuzhiyun  */
358*4882a593Smuzhiyun int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun /**
361*4882a593Smuzhiyun  * gpio_hog_probe_all() - probe all gpio devices with
362*4882a593Smuzhiyun  * gpio-hog subnodes.
363*4882a593Smuzhiyun  *
364*4882a593Smuzhiyun  * @return:	Returns return value from device_probe()
365*4882a593Smuzhiyun  */
366*4882a593Smuzhiyun int gpio_hog_probe_all(void);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun /**
369*4882a593Smuzhiyun  * gpio_lookup_name - Look up a GPIO name and return its details
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * This is used to convert a named GPIO into a device, offset and GPIO
372*4882a593Smuzhiyun  * number.
373*4882a593Smuzhiyun  *
374*4882a593Smuzhiyun  * @name: GPIO name to look up
375*4882a593Smuzhiyun  * @devp: Returns pointer to device which contains this GPIO
376*4882a593Smuzhiyun  * @offsetp: Returns the offset number within this device
377*4882a593Smuzhiyun  * @gpiop: Returns the absolute GPIO number, numbered from 0
378*4882a593Smuzhiyun  */
379*4882a593Smuzhiyun int gpio_lookup_name(const char *name, struct udevice **devp,
380*4882a593Smuzhiyun 		     unsigned int *offsetp, unsigned int *gpiop);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /**
383*4882a593Smuzhiyun  * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * This puts the value of the first GPIO into bit 0, the second into bit 1,
386*4882a593Smuzhiyun  * etc. then returns the resulting integer.
387*4882a593Smuzhiyun  *
388*4882a593Smuzhiyun  * @gpio_list: List of GPIOs to collect
389*4882a593Smuzhiyun  * @return resulting integer value, or -ve on error
390*4882a593Smuzhiyun  */
391*4882a593Smuzhiyun int gpio_get_values_as_int(const int *gpio_list);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /**
394*4882a593Smuzhiyun  * dm_gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
395*4882a593Smuzhiyun  *
396*4882a593Smuzhiyun  * This puts the value of the first GPIO into bit 0, the second into bit 1,
397*4882a593Smuzhiyun  * etc. then returns the resulting integer.
398*4882a593Smuzhiyun  *
399*4882a593Smuzhiyun  * @desc_list: List of GPIOs to collect
400*4882a593Smuzhiyun  * @count: Number of GPIOs
401*4882a593Smuzhiyun  * @return resulting integer value, or -ve on error
402*4882a593Smuzhiyun  */
403*4882a593Smuzhiyun int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count);
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /**
406*4882a593Smuzhiyun  * gpio_claim_vector() - claim a number of GPIOs for input
407*4882a593Smuzhiyun  *
408*4882a593Smuzhiyun  * @gpio_num_array:	array of gpios to claim, terminated by -1
409*4882a593Smuzhiyun  * @fmt:		format string for GPIO names, e.g. "board_id%d"
410*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
411*4882a593Smuzhiyun  */
412*4882a593Smuzhiyun int gpio_claim_vector(const int *gpio_num_array, const char *fmt);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun /**
415*4882a593Smuzhiyun  * gpio_request_by_name() - Locate and request a GPIO by name
416*4882a593Smuzhiyun  *
417*4882a593Smuzhiyun  * This operates by looking up the given list name in the device (device
418*4882a593Smuzhiyun  * tree property) and requesting the GPIO for use. The property must exist
419*4882a593Smuzhiyun  * in @dev's node.
420*4882a593Smuzhiyun  *
421*4882a593Smuzhiyun  * Use @flags to specify whether the GPIO should be an input or output. In
422*4882a593Smuzhiyun  * principle this can also come from the device tree binding but most
423*4882a593Smuzhiyun  * bindings don't provide this information. Specifically, when the GPIO uclass
424*4882a593Smuzhiyun  * calls the xlate() method, it can return default flags, which are then
425*4882a593Smuzhiyun  * ORed with this @flags.
426*4882a593Smuzhiyun  *
427*4882a593Smuzhiyun  * If we find that requesting the GPIO is not always needed we could add a
428*4882a593Smuzhiyun  * new function or a new GPIOD_NO_REQUEST flag.
429*4882a593Smuzhiyun  *
430*4882a593Smuzhiyun  * At present driver model has no reference counting so if one device
431*4882a593Smuzhiyun  * requests a GPIO which subsequently is unbound, the @desc->dev pointer
432*4882a593Smuzhiyun  * will be invalid. However this will only happen if the GPIO device is
433*4882a593Smuzhiyun  * unbound, not if it is removed, so this seems like a reasonable limitation
434*4882a593Smuzhiyun  * for now. There is no real use case for unbinding drivers in normal
435*4882a593Smuzhiyun  * operation.
436*4882a593Smuzhiyun  *
437*4882a593Smuzhiyun  * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in
438*4882a593Smuzhiyun  * generate terms and each specific device may add additional details in
439*4882a593Smuzhiyun  * a binding file in the same directory.
440*4882a593Smuzhiyun  *
441*4882a593Smuzhiyun  * @dev:	Device requesting the GPIO
442*4882a593Smuzhiyun  * @list_name:	Name of GPIO list (e.g. "board-id-gpios")
443*4882a593Smuzhiyun  * @index:	Index number of the GPIO in that list use request (0=first)
444*4882a593Smuzhiyun  * @desc:	Returns GPIO description information. If there is no such
445*4882a593Smuzhiyun  *		GPIO, dev->dev will be NULL.
446*4882a593Smuzhiyun  * @flags:	Indicates the GPIO input/output settings (GPIOD_...)
447*4882a593Smuzhiyun  * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is
448*4882a593Smuzhiyun  * something wrong with the list, or other -ve for another error (e.g.
449*4882a593Smuzhiyun  * -EBUSY if a GPIO was already requested)
450*4882a593Smuzhiyun  */
451*4882a593Smuzhiyun int gpio_request_by_name(struct udevice *dev, const char *list_name,
452*4882a593Smuzhiyun 			 int index, struct gpio_desc *desc, int flags);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun /**
455*4882a593Smuzhiyun  * gpio_request_list_by_name() - Request a list of GPIOs
456*4882a593Smuzhiyun  *
457*4882a593Smuzhiyun  * Reads all the GPIOs from a list and requests them. See
458*4882a593Smuzhiyun  * gpio_request_by_name() for additional details. Lists should not be
459*4882a593Smuzhiyun  * misused to hold unrelated or optional GPIOs. They should only be used
460*4882a593Smuzhiyun  * for things like parallel data lines. A zero phandle terminates the list
461*4882a593Smuzhiyun  * the list.
462*4882a593Smuzhiyun  *
463*4882a593Smuzhiyun  * This function will either succeed, and request all GPIOs in the list, or
464*4882a593Smuzhiyun  * fail and request none (it will free already-requested GPIOs in case of
465*4882a593Smuzhiyun  * an error part-way through).
466*4882a593Smuzhiyun  *
467*4882a593Smuzhiyun  * @dev:	Device requesting the GPIO
468*4882a593Smuzhiyun  * @list_name:	Name of GPIO list (e.g. "board-id-gpios")
469*4882a593Smuzhiyun  * @desc_list:	Returns a list of GPIO description information
470*4882a593Smuzhiyun  * @max_count:	Maximum number of GPIOs to return (@desc_list must be at least
471*4882a593Smuzhiyun  *		this big)
472*4882a593Smuzhiyun  * @flags:	Indicates the GPIO input/output settings (GPIOD_...)
473*4882a593Smuzhiyun  * @return number of GPIOs requested, or -ve on error
474*4882a593Smuzhiyun  */
475*4882a593Smuzhiyun int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
476*4882a593Smuzhiyun 			      struct gpio_desc *desc_list, int max_count,
477*4882a593Smuzhiyun 			      int flags);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun /**
480*4882a593Smuzhiyun  * dm_gpio_request() - manually request a GPIO
481*4882a593Smuzhiyun  *
482*4882a593Smuzhiyun  * Note: This function should only be used for testing / debugging. Instead.
483*4882a593Smuzhiyun  * use gpio_request_by_name() to pull GPIOs from the device tree.
484*4882a593Smuzhiyun  *
485*4882a593Smuzhiyun  * @desc:	GPIO description of GPIO to request (see dm_gpio_lookup_name())
486*4882a593Smuzhiyun  * @label:	Label to attach to the GPIO while claimed
487*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
488*4882a593Smuzhiyun  */
489*4882a593Smuzhiyun int dm_gpio_request(struct gpio_desc *desc, const char *label);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun /**
492*4882a593Smuzhiyun  * gpio_get_list_count() - Returns the number of GPIOs in a list
493*4882a593Smuzhiyun  *
494*4882a593Smuzhiyun  * Counts the GPIOs in a list. See gpio_request_by_name() for additional
495*4882a593Smuzhiyun  * details.
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  * @dev:	Device requesting the GPIO
498*4882a593Smuzhiyun  * @list_name:	Name of GPIO list (e.g. "board-id-gpios")
499*4882a593Smuzhiyun  * @return number of GPIOs (0 for an empty property) or -ENOENT if the list
500*4882a593Smuzhiyun  * does not exist
501*4882a593Smuzhiyun  */
502*4882a593Smuzhiyun int gpio_get_list_count(struct udevice *dev, const char *list_name);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun /**
505*4882a593Smuzhiyun  * gpio_request_by_name_nodev() - request GPIOs without a device
506*4882a593Smuzhiyun  *
507*4882a593Smuzhiyun  * This is a version of gpio_request_list_by_name() that does not use a
508*4882a593Smuzhiyun  * device. Avoid it unless the caller is not yet using driver model
509*4882a593Smuzhiyun  */
510*4882a593Smuzhiyun int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
511*4882a593Smuzhiyun 			       struct gpio_desc *desc, int flags);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun /**
514*4882a593Smuzhiyun  * gpio_request_list_by_name_nodev() - request GPIOs without a device
515*4882a593Smuzhiyun  *
516*4882a593Smuzhiyun  * This is a version of gpio_request_list_by_name() that does not use a
517*4882a593Smuzhiyun  * device. Avoid it unless the caller is not yet using driver model
518*4882a593Smuzhiyun  */
519*4882a593Smuzhiyun int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
520*4882a593Smuzhiyun 				    struct gpio_desc *desc_list, int max_count,
521*4882a593Smuzhiyun 				    int flags);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun /**
524*4882a593Smuzhiyun  * gpio_dev_request_index() - request single GPIO from gpio device
525*4882a593Smuzhiyun  *
526*4882a593Smuzhiyun  * @dev:	GPIO device
527*4882a593Smuzhiyun  * @nodename:	Name of node for which gpio gets requested, used
528*4882a593Smuzhiyun  *		for the gpio label name
529*4882a593Smuzhiyun  * @list_name:	Name of GPIO list (e.g. "board-id-gpios")
530*4882a593Smuzhiyun  * @index:	Index number of the GPIO in that list use request (0=first)
531*4882a593Smuzhiyun  * @flags:	GPIOD_* flags
532*4882a593Smuzhiyun  * @dtflags:	GPIO flags read from DT defined see GPIOD_*
533*4882a593Smuzhiyun  * @desc:	returns GPIO descriptor filled from this function
534*4882a593Smuzhiyun  * @return:	return value from gpio_request_tail()
535*4882a593Smuzhiyun  */
536*4882a593Smuzhiyun int gpio_dev_request_index(struct udevice *dev, const char *nodename,
537*4882a593Smuzhiyun 			   char *list_name, int index, int flags,
538*4882a593Smuzhiyun 			   int dtflags, struct gpio_desc *desc);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun  * dm_gpio_free() - Free a single GPIO
542*4882a593Smuzhiyun  *
543*4882a593Smuzhiyun  * This frees a single GPIOs previously returned from gpio_request_by_name().
544*4882a593Smuzhiyun  *
545*4882a593Smuzhiyun  * @dev:	Device which requested the GPIO
546*4882a593Smuzhiyun  * @desc:	GPIO to free
547*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
548*4882a593Smuzhiyun  */
549*4882a593Smuzhiyun int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun /**
552*4882a593Smuzhiyun  * gpio_free_list() - Free a list of GPIOs
553*4882a593Smuzhiyun  *
554*4882a593Smuzhiyun  * This frees a list of GPIOs previously returned from
555*4882a593Smuzhiyun  * gpio_request_list_by_name().
556*4882a593Smuzhiyun  *
557*4882a593Smuzhiyun  * @dev:	Device which requested the GPIOs
558*4882a593Smuzhiyun  * @desc:	List of GPIOs to free
559*4882a593Smuzhiyun  * @count:	Number of GPIOs in the list
560*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
561*4882a593Smuzhiyun  */
562*4882a593Smuzhiyun int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count);
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun /**
565*4882a593Smuzhiyun  * gpio_free_list_nodev() - free GPIOs without a device
566*4882a593Smuzhiyun  *
567*4882a593Smuzhiyun  * This is a version of gpio_free_list() that does not use a
568*4882a593Smuzhiyun  * device. Avoid it unless the caller is not yet using driver model
569*4882a593Smuzhiyun  */
570*4882a593Smuzhiyun int gpio_free_list_nodev(struct gpio_desc *desc, int count);
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /**
573*4882a593Smuzhiyun  * dm_gpio_get_value() - Get the value of a GPIO
574*4882a593Smuzhiyun  *
575*4882a593Smuzhiyun  * This is the driver model version of the existing gpio_get_value() function
576*4882a593Smuzhiyun  * and should be used instead of that.
577*4882a593Smuzhiyun  *
578*4882a593Smuzhiyun  * For now, these functions have a dm_ prefix since they conflict with
579*4882a593Smuzhiyun  * existing names.
580*4882a593Smuzhiyun  *
581*4882a593Smuzhiyun  * @desc:	GPIO description containing device, offset and flags,
582*4882a593Smuzhiyun  *		previously returned by gpio_request_by_name()
583*4882a593Smuzhiyun  * @return GPIO value (0 for inactive, 1 for active) or -ve on error
584*4882a593Smuzhiyun  */
585*4882a593Smuzhiyun int dm_gpio_get_value(const struct gpio_desc *desc);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun int dm_gpio_set_value(const struct gpio_desc *desc, int value);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun /**
590*4882a593Smuzhiyun  * dm_gpio_get_open_drain() - Check if open-drain-mode of a GPIO is active
591*4882a593Smuzhiyun  *
592*4882a593Smuzhiyun  * This checks if open-drain-mode for a GPIO is enabled or not. This method is
593*4882a593Smuzhiyun  * optional.
594*4882a593Smuzhiyun  *
595*4882a593Smuzhiyun  * @desc:	GPIO description containing device, offset and flags,
596*4882a593Smuzhiyun  *		previously returned by gpio_request_by_name()
597*4882a593Smuzhiyun  * @return Value of open drain mode for GPIO (0 for inactive, 1 for active) or
598*4882a593Smuzhiyun  *	   -ve on error
599*4882a593Smuzhiyun  */
600*4882a593Smuzhiyun int dm_gpio_get_open_drain(struct gpio_desc *desc);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun /**
603*4882a593Smuzhiyun  * dm_gpio_set_open_drain() - Switch open-drain-mode of a GPIO on or off
604*4882a593Smuzhiyun  *
605*4882a593Smuzhiyun  * This enables or disables open-drain mode for a GPIO. This method is
606*4882a593Smuzhiyun  * optional; if the driver does not support it, nothing happens when the method
607*4882a593Smuzhiyun  * is called.
608*4882a593Smuzhiyun  *
609*4882a593Smuzhiyun  * In open-drain mode, instead of actively driving the output (Push-pull
610*4882a593Smuzhiyun  * output), the GPIO's pin is connected to the collector (for a NPN transistor)
611*4882a593Smuzhiyun  * or the drain (for a MOSFET) of a transistor, respectively. The pin then
612*4882a593Smuzhiyun  * either forms an open circuit or a connection to ground, depending on the
613*4882a593Smuzhiyun  * state of the transistor.
614*4882a593Smuzhiyun  *
615*4882a593Smuzhiyun  * @desc:	GPIO description containing device, offset and flags,
616*4882a593Smuzhiyun  *		previously returned by gpio_request_by_name()
617*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
618*4882a593Smuzhiyun  */
619*4882a593Smuzhiyun int dm_gpio_set_open_drain(struct gpio_desc *desc, int value);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun /**
622*4882a593Smuzhiyun  * dm_gpio_set_dir() - Set the direction for a GPIO
623*4882a593Smuzhiyun  *
624*4882a593Smuzhiyun  * This sets up the direction according tot the provided flags. It will do
625*4882a593Smuzhiyun  * nothing unless the direction is actually specified.
626*4882a593Smuzhiyun  *
627*4882a593Smuzhiyun  * @desc:	GPIO description containing device, offset and flags,
628*4882a593Smuzhiyun  *		previously returned by gpio_request_by_name()
629*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
630*4882a593Smuzhiyun  */
631*4882a593Smuzhiyun int dm_gpio_set_dir(struct gpio_desc *desc);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun /**
634*4882a593Smuzhiyun  * dm_gpio_set_dir_flags() - Set direction using specific flags
635*4882a593Smuzhiyun  *
636*4882a593Smuzhiyun  * This is like dm_gpio_set_dir() except that the flags value is provided
637*4882a593Smuzhiyun  * instead of being used from desc->flags. This is needed because in many
638*4882a593Smuzhiyun  * cases the GPIO description does not include direction information.
639*4882a593Smuzhiyun  * Note that desc->flags is updated by this function.
640*4882a593Smuzhiyun  *
641*4882a593Smuzhiyun  * @desc:	GPIO description containing device, offset and flags,
642*4882a593Smuzhiyun  *		previously returned by gpio_request_by_name()
643*4882a593Smuzhiyun  * @flags:	New flags to use
644*4882a593Smuzhiyun  * @return 0 if OK, -ve on error, in which case desc->flags is not updated
645*4882a593Smuzhiyun  */
646*4882a593Smuzhiyun int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun /**
649*4882a593Smuzhiyun  * gpio_get_number() - Get the global GPIO number of a GPIO
650*4882a593Smuzhiyun  *
651*4882a593Smuzhiyun  * This should only be used for debugging or interest. It returns the number
652*4882a593Smuzhiyun  * that should be used for gpio_get_value() etc. to access this GPIO.
653*4882a593Smuzhiyun  *
654*4882a593Smuzhiyun  * @desc:	GPIO description containing device, offset and flags,
655*4882a593Smuzhiyun  *		previously returned by gpio_request_by_name()
656*4882a593Smuzhiyun  * @return GPIO number, or -ve if not found
657*4882a593Smuzhiyun  */
658*4882a593Smuzhiyun int gpio_get_number(const struct gpio_desc *desc);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun #endif	/* _ASM_GENERIC_GPIO_H_ */
661