xref: /rk3399_rockchip-uboot/include/asm-generic/gpio.h (revision 3a57123e598ea46fc88a8e29e6932eba02d4ac5f)
19d2cb8e8SSimon Glass /*
29d2cb8e8SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
35f533aebSJoe Hershberger  * Copyright (c) 2011, NVIDIA Corp. All rights reserved.
41a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
59d2cb8e8SSimon Glass  */
69d2cb8e8SSimon Glass 
75f533aebSJoe Hershberger #ifndef _ASM_GENERIC_GPIO_H_
85f533aebSJoe Hershberger #define _ASM_GENERIC_GPIO_H_
95f533aebSJoe Hershberger 
10*3a57123eSSimon Glass struct ofnode_phandle_args;
11*3a57123eSSimon Glass 
129d2cb8e8SSimon Glass /*
139d2cb8e8SSimon Glass  * Generic GPIO API for U-Boot
149d2cb8e8SSimon Glass  *
155d1c17e9SSimon Glass  * --
165d1c17e9SSimon Glass  * NB: This is deprecated. Please use the driver model functions instead:
175d1c17e9SSimon Glass  *
185d1c17e9SSimon Glass  *    - gpio_request_by_name()
195d1c17e9SSimon Glass  *    - dm_gpio_get_value() etc.
205d1c17e9SSimon Glass  *
215d1c17e9SSimon Glass  * For now we need a dm_ prefix on some functions to avoid name collision.
225d1c17e9SSimon Glass  * --
235d1c17e9SSimon Glass  *
249d2cb8e8SSimon Glass  * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined
259d2cb8e8SSimon Glass  * by the SOC/architecture.
269d2cb8e8SSimon Glass  *
279d2cb8e8SSimon Glass  * Each GPIO can be an input or output. If an input then its value can
289d2cb8e8SSimon Glass  * be read as 0 or 1. If an output then its value can be set to 0 or 1.
299d2cb8e8SSimon Glass  * If you try to write an input then the value is undefined. If you try
309d2cb8e8SSimon Glass  * to read an output, barring something very unusual,  you will get
319d2cb8e8SSimon Glass  * back the value of the output that you previously set.
329d2cb8e8SSimon Glass  *
339d2cb8e8SSimon Glass  * In some cases the operation may fail, for example if the GPIO number
349d2cb8e8SSimon Glass  * is out of range, or the GPIO is not available because its pin is
359d2cb8e8SSimon Glass  * being used by another function. In that case, functions may return
369d2cb8e8SSimon Glass  * an error value of -1.
379d2cb8e8SSimon Glass  */
389d2cb8e8SSimon Glass 
399d2cb8e8SSimon Glass /**
405d1c17e9SSimon Glass  * @deprecated	Please use driver model instead
4125ca385dSMarcel Ziswiler  * Request a GPIO. This should be called before any of the other functions
4225ca385dSMarcel Ziswiler  * are used on this GPIO.
439d2cb8e8SSimon Glass  *
44b892d127SSimon Glass  * Note: With driver model, the label is allocated so there is no need for
45b892d127SSimon Glass  * the caller to preserve it.
46b892d127SSimon Glass  *
47d9a607f2SMasahiro Yamada  * @param gpio	GPIO number
4894740e47SNikita Kiryanov  * @param label	User label for this GPIO
499d2cb8e8SSimon Glass  * @return 0 if ok, -1 on error
509d2cb8e8SSimon Glass  */
515f533aebSJoe Hershberger int gpio_request(unsigned gpio, const char *label);
525f533aebSJoe Hershberger 
535f533aebSJoe Hershberger /**
545d1c17e9SSimon Glass  * @deprecated	Please use driver model instead
555f533aebSJoe Hershberger  * Stop using the GPIO.  This function should not alter pin configuration.
565f533aebSJoe Hershberger  *
575f533aebSJoe Hershberger  * @param gpio	GPIO number
585f533aebSJoe Hershberger  * @return 0 if ok, -1 on error
595f533aebSJoe Hershberger  */
605f533aebSJoe Hershberger int gpio_free(unsigned gpio);
615f533aebSJoe Hershberger 
625f533aebSJoe Hershberger /**
635d1c17e9SSimon Glass  * @deprecated	Please use driver model instead
645f533aebSJoe Hershberger  * Make a GPIO an input.
655f533aebSJoe Hershberger  *
665f533aebSJoe Hershberger  * @param gpio	GPIO number
675f533aebSJoe Hershberger  * @return 0 if ok, -1 on error
685f533aebSJoe Hershberger  */
695f533aebSJoe Hershberger int gpio_direction_input(unsigned gpio);
709d2cb8e8SSimon Glass 
719d2cb8e8SSimon Glass /**
725d1c17e9SSimon Glass  * @deprecated	Please use driver model instead
739d2cb8e8SSimon Glass  * Make a GPIO an output, and set its value.
749d2cb8e8SSimon Glass  *
755f533aebSJoe Hershberger  * @param gpio	GPIO number
769d2cb8e8SSimon Glass  * @param value	GPIO value (0 for low or 1 for high)
779d2cb8e8SSimon Glass  * @return 0 if ok, -1 on error
789d2cb8e8SSimon Glass  */
795f533aebSJoe Hershberger int gpio_direction_output(unsigned gpio, int value);
809d2cb8e8SSimon Glass 
819d2cb8e8SSimon Glass /**
825d1c17e9SSimon Glass  * @deprecated	Please use driver model instead
839d2cb8e8SSimon Glass  * Get a GPIO's value. This will work whether the GPIO is an input
849d2cb8e8SSimon Glass  * or an output.
859d2cb8e8SSimon Glass  *
865f533aebSJoe Hershberger  * @param gpio	GPIO number
879d2cb8e8SSimon Glass  * @return 0 if low, 1 if high, -1 on error
889d2cb8e8SSimon Glass  */
895f533aebSJoe Hershberger int gpio_get_value(unsigned gpio);
909d2cb8e8SSimon Glass 
919d2cb8e8SSimon Glass /**
925d1c17e9SSimon Glass  * @deprecated	Please use driver model instead
935f533aebSJoe Hershberger  * Set an output GPIO's value. The GPIO must already be an output or
949d2cb8e8SSimon Glass  * this function may have no effect.
959d2cb8e8SSimon Glass  *
965f533aebSJoe Hershberger  * @param gpio	GPIO number
979d2cb8e8SSimon Glass  * @param value	GPIO value (0 for low or 1 for high)
989d2cb8e8SSimon Glass  * @return 0 if ok, -1 on error
999d2cb8e8SSimon Glass  */
1005f533aebSJoe Hershberger int gpio_set_value(unsigned gpio, int value);
10196495d90SSimon Glass 
10289e64054SSimon Glass /* State of a GPIO, as reported by get_function() */
1036449a506SSimon Glass enum gpio_func_t {
10496495d90SSimon Glass 	GPIOF_INPUT = 0,
10596495d90SSimon Glass 	GPIOF_OUTPUT,
10689e64054SSimon Glass 	GPIOF_UNUSED,		/* Not claimed */
10789e64054SSimon Glass 	GPIOF_UNKNOWN,		/* Not known */
10889e64054SSimon Glass 	GPIOF_FUNC,		/* Not used as a GPIO */
10989e64054SSimon Glass 
11089e64054SSimon Glass 	GPIOF_COUNT,
11196495d90SSimon Glass };
11296495d90SSimon Glass 
11354c5d08aSHeiko Schocher struct udevice;
11496495d90SSimon Glass 
115ae7123f8SSimon Glass struct gpio_desc {
116ae7123f8SSimon Glass 	struct udevice *dev;	/* Device, NULL for invalid GPIO */
117ae7123f8SSimon Glass 	unsigned long flags;
118ae7123f8SSimon Glass #define GPIOD_REQUESTED		(1 << 0)	/* Requested/claimed */
119ae7123f8SSimon Glass #define GPIOD_IS_OUT		(1 << 1)	/* GPIO is an output */
120f9523961SSimon Glass #define GPIOD_IS_IN		(1 << 2)	/* GPIO is an input */
121ae7123f8SSimon Glass #define GPIOD_ACTIVE_LOW	(1 << 3)	/* value has active low */
122ae7123f8SSimon Glass #define GPIOD_IS_OUT_ACTIVE	(1 << 4)	/* set output active */
123ae7123f8SSimon Glass 
124ae7123f8SSimon Glass 	uint offset;		/* GPIO offset within the device */
125ae7123f8SSimon Glass 	/*
126ae7123f8SSimon Glass 	 * We could consider adding the GPIO label in here. Possibly we could
127ae7123f8SSimon Glass 	 * use this structure for internal GPIO information.
128ae7123f8SSimon Glass 	 */
129ae7123f8SSimon Glass };
130ae7123f8SSimon Glass 
13196495d90SSimon Glass /**
132d9a607f2SMasahiro Yamada  * dm_gpio_is_valid() - Check if a GPIO is valid
1333669e0e7SSimon Glass  *
1343669e0e7SSimon Glass  * @desc:	GPIO description containing device, offset and flags,
1353669e0e7SSimon Glass  *		previously returned by gpio_request_by_name()
1363669e0e7SSimon Glass  * @return true if valid, false if not
1373669e0e7SSimon Glass  */
13817c43f1aSSimon Glass static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
1393669e0e7SSimon Glass {
1403669e0e7SSimon Glass 	return desc->dev != NULL;
1413669e0e7SSimon Glass }
1423669e0e7SSimon Glass 
1433669e0e7SSimon Glass /**
1440757535aSSimon Glass  * gpio_get_status() - get the current GPIO status as a string
1450757535aSSimon Glass  *
1460757535aSSimon Glass  * Obtain the current GPIO status as a string which can be presented to the
1470757535aSSimon Glass  * user. A typical string is:
1480757535aSSimon Glass  *
1490757535aSSimon Glass  * "b4:  in: 1 [x] sdmmc_cd"
1500757535aSSimon Glass  *
1510757535aSSimon Glass  * which means this is GPIO bank b, offset 4, currently set to input, current
1520757535aSSimon Glass  * value 1, [x] means that it is requested and the owner is 'sdmmc_cd'
1530757535aSSimon Glass  *
1545d1c17e9SSimon Glass  * TODO(sjg@chromium.org): This should use struct gpio_desc
1555d1c17e9SSimon Glass  *
1560757535aSSimon Glass  * @dev:	Device to check
1570757535aSSimon Glass  * @offset:	Offset of device GPIO to check
1580757535aSSimon Glass  * @buf:	Place to put string
1590757535aSSimon Glass  * @buffsize:	Size of string including \0
1600757535aSSimon Glass  */
1610757535aSSimon Glass int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize);
1620757535aSSimon Glass 
1630757535aSSimon Glass /**
1646449a506SSimon Glass  * gpio_get_function() - get the current function for a GPIO pin
1656449a506SSimon Glass  *
1666449a506SSimon Glass  * Note this returns GPIOF_UNUSED if the GPIO is not requested.
1676449a506SSimon Glass  *
1685d1c17e9SSimon Glass  * TODO(sjg@chromium.org): This should use struct gpio_desc
1695d1c17e9SSimon Glass  *
1706449a506SSimon Glass  * @dev:	Device to check
1716449a506SSimon Glass  * @offset:	Offset of device GPIO to check
172d9a607f2SMasahiro Yamada  * @namep:	If non-NULL, this is set to the name given when the GPIO
1736449a506SSimon Glass  *		was requested, or -1 if it has not been requested
1746449a506SSimon Glass  * @return  -ENODATA if the driver returned an unknown function,
1756449a506SSimon Glass  * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
1766449a506SSimon Glass  * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
1776449a506SSimon Glass  * function from enum gpio_func_t.
1786449a506SSimon Glass  */
1796449a506SSimon Glass int gpio_get_function(struct udevice *dev, int offset, const char **namep);
1806449a506SSimon Glass 
1816449a506SSimon Glass /**
1826449a506SSimon Glass  * gpio_get_raw_function() - get the current raw function for a GPIO pin
1836449a506SSimon Glass  *
1846449a506SSimon Glass  * Note this does not return GPIOF_UNUSED - it will always return the GPIO
1856449a506SSimon Glass  * driver's view of a pin function, even if it is not correctly set up.
1866449a506SSimon Glass  *
1875d1c17e9SSimon Glass  * TODO(sjg@chromium.org): This should use struct gpio_desc
1885d1c17e9SSimon Glass  *
1896449a506SSimon Glass  * @dev:	Device to check
1906449a506SSimon Glass  * @offset:	Offset of device GPIO to check
191d9a607f2SMasahiro Yamada  * @namep:	If non-NULL, this is set to the name given when the GPIO
1926449a506SSimon Glass  *		was requested, or -1 if it has not been requested
1936449a506SSimon Glass  * @return  -ENODATA if the driver returned an unknown function,
1946449a506SSimon Glass  * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
1956449a506SSimon Glass  * Otherwise returns the function from enum gpio_func_t.
1966449a506SSimon Glass  */
1976449a506SSimon Glass int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
1986449a506SSimon Glass 
1996449a506SSimon Glass /**
200d44f597bSSimon Glass  * gpio_requestf() - request a GPIO using a format string for the owner
201d44f597bSSimon Glass  *
202d44f597bSSimon Glass  * This is a helper function for gpio_request(). It allows you to provide
203d44f597bSSimon Glass  * a printf()-format string for the GPIO owner. It calls gpio_request() with
204d44f597bSSimon Glass  * the string that is created
205d44f597bSSimon Glass  */
206d44f597bSSimon Glass int gpio_requestf(unsigned gpio, const char *fmt, ...)
207d44f597bSSimon Glass 		__attribute__ ((format (__printf__, 2, 3)));
208d44f597bSSimon Glass 
2090dac4d51SSimon Glass struct fdtdec_phandle_args;
2100dac4d51SSimon Glass 
211d44f597bSSimon Glass /**
2126c880b77SEric Nelson  * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate
2136c880b77SEric Nelson  *
2146c880b77SEric Nelson  * This routine sets the offset field to args[0] and the flags field to
2156c880b77SEric Nelson  * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1].
2166c880b77SEric Nelson  *
2176c880b77SEric Nelson  */
2186c880b77SEric Nelson int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
219*3a57123eSSimon Glass 			  struct ofnode_phandle_args *args);
2206c880b77SEric Nelson 
2216c880b77SEric Nelson /**
22296495d90SSimon Glass  * struct struct dm_gpio_ops - Driver model GPIO operations
22396495d90SSimon Glass  *
22496495d90SSimon Glass  * Refer to functions above for description. These function largely copy
22596495d90SSimon Glass  * the old API.
22696495d90SSimon Glass  *
22796495d90SSimon Glass  * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
22896495d90SSimon Glass  * new DM GPIO API, this should be really easy to flip over to the Linux
22996495d90SSimon Glass  * GPIO API-alike interface.
23096495d90SSimon Glass  *
23125ca385dSMarcel Ziswiler  * Also it would be useful to standardise additional functions like
23296495d90SSimon Glass  * pullup, slew rate and drive strength.
23396495d90SSimon Glass  *
234d9a607f2SMasahiro Yamada  * gpio_request() and gpio_free() are optional - if NULL then they will
23596495d90SSimon Glass  * not be called.
23696495d90SSimon Glass  *
23796495d90SSimon Glass  * Note that @offset is the offset from the base GPIO of the device. So
23896495d90SSimon Glass  * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
23996495d90SSimon Glass  * where o is the number of GPIO lines controlled by the device. A device
24096495d90SSimon Glass  * is typically used to control a single bank of GPIOs. Within complex
24196495d90SSimon Glass  * SoCs there may be many banks and therefore many devices all referring
24296495d90SSimon Glass  * to the different IO addresses within the SoC.
24396495d90SSimon Glass  *
24425ca385dSMarcel Ziswiler  * The uclass combines all GPIO devices together to provide a consistent
24596495d90SSimon Glass  * numbering from 0 to n-1, where n is the number of GPIOs in total across
24696495d90SSimon Glass  * all devices. Be careful not to confuse offset with gpio in the parameters.
24796495d90SSimon Glass  */
24896495d90SSimon Glass struct dm_gpio_ops {
24954c5d08aSHeiko Schocher 	int (*request)(struct udevice *dev, unsigned offset, const char *label);
25054c5d08aSHeiko Schocher 	int (*free)(struct udevice *dev, unsigned offset);
25154c5d08aSHeiko Schocher 	int (*direction_input)(struct udevice *dev, unsigned offset);
25254c5d08aSHeiko Schocher 	int (*direction_output)(struct udevice *dev, unsigned offset,
25396495d90SSimon Glass 				int value);
25454c5d08aSHeiko Schocher 	int (*get_value)(struct udevice *dev, unsigned offset);
25554c5d08aSHeiko Schocher 	int (*set_value)(struct udevice *dev, unsigned offset, int value);
25653ecdfb9Smario.six@gdsys.cc 	int (*get_open_drain)(struct udevice *dev, unsigned offset);
25753ecdfb9Smario.six@gdsys.cc 	int (*set_open_drain)(struct udevice *dev, unsigned offset, int value);
25889e64054SSimon Glass 	/**
25989e64054SSimon Glass 	 * get_function() Get the GPIO function
26089e64054SSimon Glass 	 *
26189e64054SSimon Glass 	 * @dev:     Device to check
26289e64054SSimon Glass 	 * @offset:  GPIO offset within that device
26389e64054SSimon Glass 	 * @return current function - GPIOF_...
26489e64054SSimon Glass 	 */
26554c5d08aSHeiko Schocher 	int (*get_function)(struct udevice *dev, unsigned offset);
2660dac4d51SSimon Glass 
2670dac4d51SSimon Glass 	/**
2680dac4d51SSimon Glass 	 * xlate() - Translate phandle arguments into a GPIO description
2690dac4d51SSimon Glass 	 *
2700dac4d51SSimon Glass 	 * This function should set up the fields in desc according to the
2710dac4d51SSimon Glass 	 * information in the arguments. The uclass will have set up:
2720dac4d51SSimon Glass 	 *
2730dac4d51SSimon Glass 	 *   @desc->dev to @dev
2740dac4d51SSimon Glass 	 *   @desc->flags to 0
2756c880b77SEric Nelson 	 *   @desc->offset to 0
2760dac4d51SSimon Glass 	 *
2776c880b77SEric Nelson 	 * This method is optional and defaults to gpio_xlate_offs_flags,
2786c880b77SEric Nelson 	 * which will parse offset and the GPIO_ACTIVE_LOW flag in the first
2796c880b77SEric Nelson 	 * two arguments.
2800dac4d51SSimon Glass 	 *
2810dac4d51SSimon Glass 	 * Note that @dev is passed in as a parameter to follow driver model
2820dac4d51SSimon Glass 	 * uclass conventions, even though it is already available as
2830dac4d51SSimon Glass 	 * desc->dev.
2840dac4d51SSimon Glass 	 *
2850dac4d51SSimon Glass 	 * @dev:	GPIO device
2860dac4d51SSimon Glass 	 * @desc:	Place to put GPIO description
287d9a607f2SMasahiro Yamada 	 * @args:	Arguments provided in description
2880dac4d51SSimon Glass 	 * @return 0 if OK, -ve on error
2890dac4d51SSimon Glass 	 */
2900dac4d51SSimon Glass 	int (*xlate)(struct udevice *dev, struct gpio_desc *desc,
291*3a57123eSSimon Glass 		     struct ofnode_phandle_args *args);
29296495d90SSimon Glass };
29396495d90SSimon Glass 
29496495d90SSimon Glass /**
29596495d90SSimon Glass  * struct gpio_dev_priv - information about a device used by the uclass
29696495d90SSimon Glass  *
29796495d90SSimon Glass  * The uclass combines all active GPIO devices into a unified numbering
29825ca385dSMarcel Ziswiler  * scheme. To do this it maintains some private information about each
29996495d90SSimon Glass  * device.
30096495d90SSimon Glass  *
30196495d90SSimon Glass  * To implement driver model support in your GPIO driver, add a probe
30296495d90SSimon Glass  * handler, and set @gpio_count and @bank_name correctly in that handler.
30396495d90SSimon Glass  * This tells the uclass the name of the GPIO bank and the number of GPIOs
30496495d90SSimon Glass  * it contains.
30596495d90SSimon Glass  *
30696495d90SSimon Glass  * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
30796495d90SSimon Glass  * 'A0', 'A1', etc.
30896495d90SSimon Glass  * @gpio_count: Number of GPIOs in this device
30996495d90SSimon Glass  * @gpio_base: Base GPIO number for this device. For the first active device
31096495d90SSimon Glass  * this will be 0; the numbering for others will follow sequentially so that
31196495d90SSimon Glass  * @gpio_base for device 1 will equal the number of GPIOs in device 0.
312b892d127SSimon Glass  * @name: Array of pointers to the name for each GPIO in this bank. The
313b892d127SSimon Glass  * value of the pointer will be NULL if the GPIO has not been claimed.
31496495d90SSimon Glass  */
31596495d90SSimon Glass struct gpio_dev_priv {
31696495d90SSimon Glass 	const char *bank_name;
31796495d90SSimon Glass 	unsigned gpio_count;
31896495d90SSimon Glass 	unsigned gpio_base;
319b892d127SSimon Glass 	char **name;
32096495d90SSimon Glass };
32196495d90SSimon Glass 
32296495d90SSimon Glass /* Access the GPIO operations for a device */
32396495d90SSimon Glass #define gpio_get_ops(dev)	((struct dm_gpio_ops *)(dev)->driver->ops)
32496495d90SSimon Glass 
32596495d90SSimon Glass /**
32696495d90SSimon Glass  * gpio_get_bank_info - Return information about a GPIO bank/device
32796495d90SSimon Glass  *
32896495d90SSimon Glass  * This looks up a device and returns both its GPIO base name and the number
32996495d90SSimon Glass  * of GPIOs it controls.
33096495d90SSimon Glass  *
33196495d90SSimon Glass  * @dev: Device to look up
33296495d90SSimon Glass  * @offset_count: Returns number of GPIOs within this bank
33396495d90SSimon Glass  * @return bank name of this device
33496495d90SSimon Glass  */
33554c5d08aSHeiko Schocher const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
33696495d90SSimon Glass 
33796495d90SSimon Glass /**
33832ec1598SSimon Glass  * dm_gpio_lookup_name() - Look up a named GPIO and return its description
33932ec1598SSimon Glass  *
34032ec1598SSimon Glass  * The name of a GPIO is typically its bank name followed by a number from 0.
34132ec1598SSimon Glass  * For example A0 is the first GPIO in bank A. Each bank is a separate driver
34232ec1598SSimon Glass  * model device.
34332ec1598SSimon Glass  *
34432ec1598SSimon Glass  * @name:	Name to look up
34532ec1598SSimon Glass  * @desc:	Returns description, on success
34632ec1598SSimon Glass  * @return 0 if OK, -ve on error
34732ec1598SSimon Glass  */
34832ec1598SSimon Glass int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc);
34932ec1598SSimon Glass 
35032ec1598SSimon Glass /**
35196495d90SSimon Glass  * gpio_lookup_name - Look up a GPIO name and return its details
35296495d90SSimon Glass  *
35396495d90SSimon Glass  * This is used to convert a named GPIO into a device, offset and GPIO
35496495d90SSimon Glass  * number.
35596495d90SSimon Glass  *
35696495d90SSimon Glass  * @name: GPIO name to look up
35796495d90SSimon Glass  * @devp: Returns pointer to device which contains this GPIO
35896495d90SSimon Glass  * @offsetp: Returns the offset number within this device
35996495d90SSimon Glass  * @gpiop: Returns the absolute GPIO number, numbered from 0
36096495d90SSimon Glass  */
36154c5d08aSHeiko Schocher int gpio_lookup_name(const char *name, struct udevice **devp,
36296495d90SSimon Glass 		     unsigned int *offsetp, unsigned int *gpiop);
36396495d90SSimon Glass 
364e5901c94SSimon Glass /**
365962f5cafSSimon Glass  * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
366e5901c94SSimon Glass  *
367e5901c94SSimon Glass  * This puts the value of the first GPIO into bit 0, the second into bit 1,
368e5901c94SSimon Glass  * etc. then returns the resulting integer.
369e5901c94SSimon Glass  *
370e5901c94SSimon Glass  * @gpio_list: List of GPIOs to collect
371962f5cafSSimon Glass  * @return resulting integer value, or -ve on error
372e5901c94SSimon Glass  */
373962f5cafSSimon Glass int gpio_get_values_as_int(const int *gpio_list);
374962f5cafSSimon Glass 
375962f5cafSSimon Glass /**
376bbf24780SSimon Glass  * dm_gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
377bbf24780SSimon Glass  *
378bbf24780SSimon Glass  * This puts the value of the first GPIO into bit 0, the second into bit 1,
379bbf24780SSimon Glass  * etc. then returns the resulting integer.
380bbf24780SSimon Glass  *
381bbf24780SSimon Glass  * @desc_list: List of GPIOs to collect
382bbf24780SSimon Glass  * @count: Number of GPIOs
383bbf24780SSimon Glass  * @return resulting integer value, or -ve on error
384bbf24780SSimon Glass  */
38517c43f1aSSimon Glass int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count);
386bbf24780SSimon Glass 
387bbf24780SSimon Glass /**
388962f5cafSSimon Glass  * gpio_claim_vector() - claim a number of GPIOs for input
389962f5cafSSimon Glass  *
390962f5cafSSimon Glass  * @gpio_num_array:	array of gpios to claim, terminated by -1
391962f5cafSSimon Glass  * @fmt:		format string for GPIO names, e.g. "board_id%d"
392962f5cafSSimon Glass  * @return 0 if OK, -ve on error
393962f5cafSSimon Glass  */
394962f5cafSSimon Glass int gpio_claim_vector(const int *gpio_num_array, const char *fmt);
3955b5ac645SJeroen Hofstee 
3963669e0e7SSimon Glass /**
3973669e0e7SSimon Glass  * gpio_request_by_name() - Locate and request a GPIO by name
3983669e0e7SSimon Glass  *
3993669e0e7SSimon Glass  * This operates by looking up the given list name in the device (device
4003669e0e7SSimon Glass  * tree property) and requesting the GPIO for use. The property must exist
4013669e0e7SSimon Glass  * in @dev's node.
4023669e0e7SSimon Glass  *
4033669e0e7SSimon Glass  * Use @flags to specify whether the GPIO should be an input or output. In
4043669e0e7SSimon Glass  * principle this can also come from the device tree binding but most
4053669e0e7SSimon Glass  * bindings don't provide this information. Specifically, when the GPIO uclass
4063669e0e7SSimon Glass  * calls the xlate() method, it can return default flags, which are then
4073669e0e7SSimon Glass  * ORed with this @flags.
4083669e0e7SSimon Glass  *
4093669e0e7SSimon Glass  * If we find that requesting the GPIO is not always needed we could add a
4103669e0e7SSimon Glass  * new function or a new GPIOD_NO_REQUEST flag.
4113669e0e7SSimon Glass  *
4123669e0e7SSimon Glass  * At present driver model has no reference counting so if one device
4133669e0e7SSimon Glass  * requests a GPIO which subsequently is unbound, the @desc->dev pointer
4143669e0e7SSimon Glass  * will be invalid. However this will only happen if the GPIO device is
4153669e0e7SSimon Glass  * unbound, not if it is removed, so this seems like a reasonable limitation
4163669e0e7SSimon Glass  * for now. There is no real use case for unbinding drivers in normal
4173669e0e7SSimon Glass  * operation.
4183669e0e7SSimon Glass  *
4193669e0e7SSimon Glass  * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in
4203669e0e7SSimon Glass  * generate terms and each specific device may add additional details in
4213669e0e7SSimon Glass  * a binding file in the same directory.
4223669e0e7SSimon Glass  *
4233669e0e7SSimon Glass  * @dev:	Device requesting the GPIO
4243669e0e7SSimon Glass  * @list_name:	Name of GPIO list (e.g. "board-id-gpios")
4253669e0e7SSimon Glass  * @index:	Index number of the GPIO in that list use request (0=first)
4263669e0e7SSimon Glass  * @desc:	Returns GPIO description information. If there is no such
4273669e0e7SSimon Glass  *		GPIO, dev->dev will be NULL.
4283669e0e7SSimon Glass  * @flags:	Indicates the GPIO input/output settings (GPIOD_...)
4293669e0e7SSimon Glass  * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is
4303669e0e7SSimon Glass  * something wrong with the list, or other -ve for another error (e.g.
4313669e0e7SSimon Glass  * -EBUSY if a GPIO was already requested)
4323669e0e7SSimon Glass  */
4333669e0e7SSimon Glass int gpio_request_by_name(struct udevice *dev, const char *list_name,
4343669e0e7SSimon Glass 			 int index, struct gpio_desc *desc, int flags);
4353669e0e7SSimon Glass 
4363669e0e7SSimon Glass /**
4373669e0e7SSimon Glass  * gpio_request_list_by_name() - Request a list of GPIOs
4383669e0e7SSimon Glass  *
439d9a607f2SMasahiro Yamada  * Reads all the GPIOs from a list and requests them. See
4403669e0e7SSimon Glass  * gpio_request_by_name() for additional details. Lists should not be
4413669e0e7SSimon Glass  * misused to hold unrelated or optional GPIOs. They should only be used
4423669e0e7SSimon Glass  * for things like parallel data lines. A zero phandle terminates the list
4433669e0e7SSimon Glass  * the list.
4443669e0e7SSimon Glass  *
4453669e0e7SSimon Glass  * This function will either succeed, and request all GPIOs in the list, or
4463669e0e7SSimon Glass  * fail and request none (it will free already-requested GPIOs in case of
4473669e0e7SSimon Glass  * an error part-way through).
4483669e0e7SSimon Glass  *
4493669e0e7SSimon Glass  * @dev:	Device requesting the GPIO
4503669e0e7SSimon Glass  * @list_name:	Name of GPIO list (e.g. "board-id-gpios")
4513669e0e7SSimon Glass  * @desc_list:	Returns a list of GPIO description information
4523669e0e7SSimon Glass  * @max_count:	Maximum number of GPIOs to return (@desc_list must be at least
4533669e0e7SSimon Glass  *		this big)
4543669e0e7SSimon Glass  * @flags:	Indicates the GPIO input/output settings (GPIOD_...)
4553669e0e7SSimon Glass  * @return number of GPIOs requested, or -ve on error
4563669e0e7SSimon Glass  */
4573669e0e7SSimon Glass int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
4583669e0e7SSimon Glass 			      struct gpio_desc *desc_list, int max_count,
4593669e0e7SSimon Glass 			      int flags);
4603669e0e7SSimon Glass 
4613669e0e7SSimon Glass /**
462efa677fbSSimon Glass  * dm_gpio_request() - manually request a GPIO
463efa677fbSSimon Glass  *
464efa677fbSSimon Glass  * Note: This function should only be used for testing / debugging. Instead.
465efa677fbSSimon Glass  * use gpio_request_by_name() to pull GPIOs from the device tree.
466efa677fbSSimon Glass  *
467efa677fbSSimon Glass  * @desc:	GPIO description of GPIO to request (see dm_gpio_lookup_name())
468efa677fbSSimon Glass  * @label:	Label to attach to the GPIO while claimed
469efa677fbSSimon Glass  * @return 0 if OK, -ve on error
470efa677fbSSimon Glass  */
471efa677fbSSimon Glass int dm_gpio_request(struct gpio_desc *desc, const char *label);
472efa677fbSSimon Glass 
473efa677fbSSimon Glass /**
4743669e0e7SSimon Glass  * gpio_get_list_count() - Returns the number of GPIOs in a list
4753669e0e7SSimon Glass  *
4763669e0e7SSimon Glass  * Counts the GPIOs in a list. See gpio_request_by_name() for additional
4773669e0e7SSimon Glass  * details.
4783669e0e7SSimon Glass  *
4793669e0e7SSimon Glass  * @dev:	Device requesting the GPIO
4803669e0e7SSimon Glass  * @list_name:	Name of GPIO list (e.g. "board-id-gpios")
4813669e0e7SSimon Glass  * @return number of GPIOs (0 for an empty property) or -ENOENT if the list
4823669e0e7SSimon Glass  * does not exist
4833669e0e7SSimon Glass  */
4843669e0e7SSimon Glass int gpio_get_list_count(struct udevice *dev, const char *list_name);
4853669e0e7SSimon Glass 
4863669e0e7SSimon Glass /**
4873669e0e7SSimon Glass  * gpio_request_by_name_nodev() - request GPIOs without a device
4883669e0e7SSimon Glass  *
4893669e0e7SSimon Glass  * This is a version of gpio_request_list_by_name() that does not use a
4903669e0e7SSimon Glass  * device. Avoid it unless the caller is not yet using driver model
4913669e0e7SSimon Glass  */
4923669e0e7SSimon Glass int gpio_request_by_name_nodev(const void *blob, int node,
4933669e0e7SSimon Glass 			       const char *list_name,
4943669e0e7SSimon Glass 			       int index, struct gpio_desc *desc, int flags);
4953669e0e7SSimon Glass 
4963669e0e7SSimon Glass /**
4973669e0e7SSimon Glass  * gpio_request_list_by_name_nodev() - request GPIOs without a device
4983669e0e7SSimon Glass  *
4993669e0e7SSimon Glass  * This is a version of gpio_request_list_by_name() that does not use a
5003669e0e7SSimon Glass  * device. Avoid it unless the caller is not yet using driver model
5013669e0e7SSimon Glass  */
5023669e0e7SSimon Glass int gpio_request_list_by_name_nodev(const void *blob, int node,
5033669e0e7SSimon Glass 				    const char *list_name,
5043669e0e7SSimon Glass 				    struct gpio_desc *desc_list, int max_count,
5053669e0e7SSimon Glass 				    int flags);
5063669e0e7SSimon Glass 
5073669e0e7SSimon Glass /**
5083669e0e7SSimon Glass  * dm_gpio_free() - Free a single GPIO
5093669e0e7SSimon Glass  *
5103669e0e7SSimon Glass  * This frees a single GPIOs previously returned from gpio_request_by_name().
5113669e0e7SSimon Glass  *
5123669e0e7SSimon Glass  * @dev:	Device which requested the GPIO
5133669e0e7SSimon Glass  * @desc:	GPIO to free
5143669e0e7SSimon Glass  * @return 0 if OK, -ve on error
5153669e0e7SSimon Glass  */
5163669e0e7SSimon Glass int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc);
5173669e0e7SSimon Glass 
5183669e0e7SSimon Glass /**
5193669e0e7SSimon Glass  * gpio_free_list() - Free a list of GPIOs
5203669e0e7SSimon Glass  *
5213669e0e7SSimon Glass  * This frees a list of GPIOs previously returned from
5223669e0e7SSimon Glass  * gpio_request_list_by_name().
5233669e0e7SSimon Glass  *
5243669e0e7SSimon Glass  * @dev:	Device which requested the GPIOs
5253669e0e7SSimon Glass  * @desc:	List of GPIOs to free
5263669e0e7SSimon Glass  * @count:	Number of GPIOs in the list
5273669e0e7SSimon Glass  * @return 0 if OK, -ve on error
5283669e0e7SSimon Glass  */
5293669e0e7SSimon Glass int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count);
5303669e0e7SSimon Glass 
5313669e0e7SSimon Glass /**
5323669e0e7SSimon Glass  * gpio_free_list_nodev() - free GPIOs without a device
5333669e0e7SSimon Glass  *
5343669e0e7SSimon Glass  * This is a version of gpio_free_list() that does not use a
5353669e0e7SSimon Glass  * device. Avoid it unless the caller is not yet using driver model
5363669e0e7SSimon Glass  */
5373669e0e7SSimon Glass int gpio_free_list_nodev(struct gpio_desc *desc, int count);
5383669e0e7SSimon Glass 
5393669e0e7SSimon Glass /**
5403669e0e7SSimon Glass  * dm_gpio_get_value() - Get the value of a GPIO
5413669e0e7SSimon Glass  *
5423669e0e7SSimon Glass  * This is the driver model version of the existing gpio_get_value() function
5433669e0e7SSimon Glass  * and should be used instead of that.
5443669e0e7SSimon Glass  *
5453669e0e7SSimon Glass  * For now, these functions have a dm_ prefix since they conflict with
5463669e0e7SSimon Glass  * existing names.
5473669e0e7SSimon Glass  *
5483669e0e7SSimon Glass  * @desc:	GPIO description containing device, offset and flags,
5493669e0e7SSimon Glass  *		previously returned by gpio_request_by_name()
5503669e0e7SSimon Glass  * @return GPIO value (0 for inactive, 1 for active) or -ve on error
5513669e0e7SSimon Glass  */
55217c43f1aSSimon Glass int dm_gpio_get_value(const struct gpio_desc *desc);
5533669e0e7SSimon Glass 
55417c43f1aSSimon Glass int dm_gpio_set_value(const struct gpio_desc *desc, int value);
5553669e0e7SSimon Glass 
5563669e0e7SSimon Glass /**
55753ecdfb9Smario.six@gdsys.cc  * dm_gpio_get_open_drain() - Check if open-drain-mode of a GPIO is active
55853ecdfb9Smario.six@gdsys.cc  *
55953ecdfb9Smario.six@gdsys.cc  * This checks if open-drain-mode for a GPIO is enabled or not. This method is
56053ecdfb9Smario.six@gdsys.cc  * optional.
56153ecdfb9Smario.six@gdsys.cc  *
56253ecdfb9Smario.six@gdsys.cc  * @desc:	GPIO description containing device, offset and flags,
56353ecdfb9Smario.six@gdsys.cc  *		previously returned by gpio_request_by_name()
56453ecdfb9Smario.six@gdsys.cc  * @return Value of open drain mode for GPIO (0 for inactive, 1 for active) or
56553ecdfb9Smario.six@gdsys.cc  *	   -ve on error
56653ecdfb9Smario.six@gdsys.cc  */
56753ecdfb9Smario.six@gdsys.cc int dm_gpio_get_open_drain(struct gpio_desc *desc);
56853ecdfb9Smario.six@gdsys.cc 
56953ecdfb9Smario.six@gdsys.cc /**
57053ecdfb9Smario.six@gdsys.cc  * dm_gpio_set_open_drain() - Switch open-drain-mode of a GPIO on or off
57153ecdfb9Smario.six@gdsys.cc  *
57253ecdfb9Smario.six@gdsys.cc  * This enables or disables open-drain mode for a GPIO. This method is
57353ecdfb9Smario.six@gdsys.cc  * optional; if the driver does not support it, nothing happens when the method
57453ecdfb9Smario.six@gdsys.cc  * is called.
57553ecdfb9Smario.six@gdsys.cc  *
57653ecdfb9Smario.six@gdsys.cc  * In open-drain mode, instead of actively driving the output (Push-pull
57753ecdfb9Smario.six@gdsys.cc  * output), the GPIO's pin is connected to the collector (for a NPN transistor)
57853ecdfb9Smario.six@gdsys.cc  * or the drain (for a MOSFET) of a transistor, respectively. The pin then
57953ecdfb9Smario.six@gdsys.cc  * either forms an open circuit or a connection to ground, depending on the
58053ecdfb9Smario.six@gdsys.cc  * state of the transistor.
58153ecdfb9Smario.six@gdsys.cc  *
58253ecdfb9Smario.six@gdsys.cc  * @desc:	GPIO description containing device, offset and flags,
58353ecdfb9Smario.six@gdsys.cc  *		previously returned by gpio_request_by_name()
58453ecdfb9Smario.six@gdsys.cc  * @return 0 if OK, -ve on error
58553ecdfb9Smario.six@gdsys.cc  */
58653ecdfb9Smario.six@gdsys.cc int dm_gpio_set_open_drain(struct gpio_desc *desc, int value);
58753ecdfb9Smario.six@gdsys.cc 
58853ecdfb9Smario.six@gdsys.cc /**
5893669e0e7SSimon Glass  * dm_gpio_set_dir() - Set the direction for a GPIO
5903669e0e7SSimon Glass  *
5913669e0e7SSimon Glass  * This sets up the direction according tot the provided flags. It will do
5923669e0e7SSimon Glass  * nothing unless the direction is actually specified.
5933669e0e7SSimon Glass  *
5943669e0e7SSimon Glass  * @desc:	GPIO description containing device, offset and flags,
5953669e0e7SSimon Glass  *		previously returned by gpio_request_by_name()
5963669e0e7SSimon Glass  * @return 0 if OK, -ve on error
5973669e0e7SSimon Glass  */
5983669e0e7SSimon Glass int dm_gpio_set_dir(struct gpio_desc *desc);
5993669e0e7SSimon Glass 
6003669e0e7SSimon Glass /**
6013669e0e7SSimon Glass  * dm_gpio_set_dir_flags() - Set direction using specific flags
6023669e0e7SSimon Glass  *
6033669e0e7SSimon Glass  * This is like dm_gpio_set_dir() except that the flags value is provided
6043669e0e7SSimon Glass  * instead of being used from desc->flags. This is needed because in many
6053669e0e7SSimon Glass  * cases the GPIO description does not include direction information.
6063669e0e7SSimon Glass  * Note that desc->flags is updated by this function.
6073669e0e7SSimon Glass  *
6083669e0e7SSimon Glass  * @desc:	GPIO description containing device, offset and flags,
6093669e0e7SSimon Glass  *		previously returned by gpio_request_by_name()
6103669e0e7SSimon Glass  * @flags:	New flags to use
6113669e0e7SSimon Glass  * @return 0 if OK, -ve on error, in which case desc->flags is not updated
6123669e0e7SSimon Glass  */
6133669e0e7SSimon Glass int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags);
6143669e0e7SSimon Glass 
6153669e0e7SSimon Glass /**
6163669e0e7SSimon Glass  * gpio_get_number() - Get the global GPIO number of a GPIO
6173669e0e7SSimon Glass  *
618d9a607f2SMasahiro Yamada  * This should only be used for debugging or interest. It returns the number
6193669e0e7SSimon Glass  * that should be used for gpio_get_value() etc. to access this GPIO.
6203669e0e7SSimon Glass  *
6213669e0e7SSimon Glass  * @desc:	GPIO description containing device, offset and flags,
6223669e0e7SSimon Glass  *		previously returned by gpio_request_by_name()
6233669e0e7SSimon Glass  * @return GPIO number, or -ve if not found
6243669e0e7SSimon Glass  */
62517c43f1aSSimon Glass int gpio_get_number(const struct gpio_desc *desc);
6263669e0e7SSimon Glass 
6275f533aebSJoe Hershberger #endif	/* _ASM_GENERIC_GPIO_H_ */
628