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
10150c5afeSSimon Glass #include <dm/ofnode.h>
11150c5afeSSimon Glass
123a57123eSSimon Glass struct ofnode_phandle_args;
133a57123eSSimon Glass
149d2cb8e8SSimon Glass /*
159d2cb8e8SSimon Glass * Generic GPIO API for U-Boot
169d2cb8e8SSimon Glass *
175d1c17e9SSimon Glass * --
185d1c17e9SSimon Glass * NB: This is deprecated. Please use the driver model functions instead:
195d1c17e9SSimon Glass *
205d1c17e9SSimon Glass * - gpio_request_by_name()
215d1c17e9SSimon Glass * - dm_gpio_get_value() etc.
225d1c17e9SSimon Glass *
235d1c17e9SSimon Glass * For now we need a dm_ prefix on some functions to avoid name collision.
245d1c17e9SSimon Glass * --
255d1c17e9SSimon Glass *
269d2cb8e8SSimon Glass * GPIOs are numbered from 0 to GPIO_COUNT-1 which value is defined
279d2cb8e8SSimon Glass * by the SOC/architecture.
289d2cb8e8SSimon Glass *
299d2cb8e8SSimon Glass * Each GPIO can be an input or output. If an input then its value can
309d2cb8e8SSimon Glass * be read as 0 or 1. If an output then its value can be set to 0 or 1.
319d2cb8e8SSimon Glass * If you try to write an input then the value is undefined. If you try
329d2cb8e8SSimon Glass * to read an output, barring something very unusual, you will get
339d2cb8e8SSimon Glass * back the value of the output that you previously set.
349d2cb8e8SSimon Glass *
359d2cb8e8SSimon Glass * In some cases the operation may fail, for example if the GPIO number
369d2cb8e8SSimon Glass * is out of range, or the GPIO is not available because its pin is
379d2cb8e8SSimon Glass * being used by another function. In that case, functions may return
389d2cb8e8SSimon Glass * an error value of -1.
399d2cb8e8SSimon Glass */
409d2cb8e8SSimon Glass
419d2cb8e8SSimon Glass /**
425d1c17e9SSimon Glass * @deprecated Please use driver model instead
4325ca385dSMarcel Ziswiler * Request a GPIO. This should be called before any of the other functions
4425ca385dSMarcel Ziswiler * are used on this GPIO.
459d2cb8e8SSimon Glass *
46b892d127SSimon Glass * Note: With driver model, the label is allocated so there is no need for
47b892d127SSimon Glass * the caller to preserve it.
48b892d127SSimon Glass *
49d9a607f2SMasahiro Yamada * @param gpio GPIO number
5094740e47SNikita Kiryanov * @param label User label for this GPIO
519d2cb8e8SSimon Glass * @return 0 if ok, -1 on error
529d2cb8e8SSimon Glass */
535f533aebSJoe Hershberger int gpio_request(unsigned gpio, const char *label);
545f533aebSJoe Hershberger
555f533aebSJoe Hershberger /**
565d1c17e9SSimon Glass * @deprecated Please use driver model instead
575f533aebSJoe Hershberger * Stop using the GPIO. This function should not alter pin configuration.
585f533aebSJoe Hershberger *
595f533aebSJoe Hershberger * @param gpio GPIO number
605f533aebSJoe Hershberger * @return 0 if ok, -1 on error
615f533aebSJoe Hershberger */
625f533aebSJoe Hershberger int gpio_free(unsigned gpio);
635f533aebSJoe Hershberger
645f533aebSJoe Hershberger /**
655d1c17e9SSimon Glass * @deprecated Please use driver model instead
665f533aebSJoe Hershberger * Make a GPIO an input.
675f533aebSJoe Hershberger *
685f533aebSJoe Hershberger * @param gpio GPIO number
695f533aebSJoe Hershberger * @return 0 if ok, -1 on error
705f533aebSJoe Hershberger */
715f533aebSJoe Hershberger int gpio_direction_input(unsigned gpio);
729d2cb8e8SSimon Glass
739d2cb8e8SSimon Glass /**
745d1c17e9SSimon Glass * @deprecated Please use driver model instead
759d2cb8e8SSimon Glass * Make a GPIO an output, and set its value.
769d2cb8e8SSimon Glass *
775f533aebSJoe Hershberger * @param gpio GPIO number
789d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high)
799d2cb8e8SSimon Glass * @return 0 if ok, -1 on error
809d2cb8e8SSimon Glass */
815f533aebSJoe Hershberger int gpio_direction_output(unsigned gpio, int value);
829d2cb8e8SSimon Glass
839d2cb8e8SSimon Glass /**
845d1c17e9SSimon Glass * @deprecated Please use driver model instead
859d2cb8e8SSimon Glass * Get a GPIO's value. This will work whether the GPIO is an input
869d2cb8e8SSimon Glass * or an output.
879d2cb8e8SSimon Glass *
885f533aebSJoe Hershberger * @param gpio GPIO number
899d2cb8e8SSimon Glass * @return 0 if low, 1 if high, -1 on error
909d2cb8e8SSimon Glass */
915f533aebSJoe Hershberger int gpio_get_value(unsigned gpio);
929d2cb8e8SSimon Glass
939d2cb8e8SSimon Glass /**
945d1c17e9SSimon Glass * @deprecated Please use driver model instead
955f533aebSJoe Hershberger * Set an output GPIO's value. The GPIO must already be an output or
969d2cb8e8SSimon Glass * this function may have no effect.
979d2cb8e8SSimon Glass *
985f533aebSJoe Hershberger * @param gpio GPIO number
999d2cb8e8SSimon Glass * @param value GPIO value (0 for low or 1 for high)
1009d2cb8e8SSimon Glass * @return 0 if ok, -1 on error
1019d2cb8e8SSimon Glass */
1025f533aebSJoe Hershberger int gpio_set_value(unsigned gpio, int value);
10396495d90SSimon Glass
10489e64054SSimon Glass /* State of a GPIO, as reported by get_function() */
1056449a506SSimon Glass enum gpio_func_t {
10696495d90SSimon Glass GPIOF_INPUT = 0,
10796495d90SSimon Glass GPIOF_OUTPUT,
10889e64054SSimon Glass GPIOF_UNUSED, /* Not claimed */
10989e64054SSimon Glass GPIOF_UNKNOWN, /* Not known */
11089e64054SSimon Glass GPIOF_FUNC, /* Not used as a GPIO */
11189e64054SSimon Glass
11289e64054SSimon Glass GPIOF_COUNT,
11396495d90SSimon Glass };
11496495d90SSimon Glass
11554c5d08aSHeiko Schocher struct udevice;
11696495d90SSimon Glass
117ae7123f8SSimon Glass struct gpio_desc {
118ae7123f8SSimon Glass struct udevice *dev; /* Device, NULL for invalid GPIO */
119ae7123f8SSimon Glass unsigned long flags;
120ae7123f8SSimon Glass #define GPIOD_REQUESTED (1 << 0) /* Requested/claimed */
121ae7123f8SSimon Glass #define GPIOD_IS_OUT (1 << 1) /* GPIO is an output */
122f9523961SSimon Glass #define GPIOD_IS_IN (1 << 2) /* GPIO is an input */
123ae7123f8SSimon Glass #define GPIOD_ACTIVE_LOW (1 << 3) /* value has active low */
124ae7123f8SSimon Glass #define GPIOD_IS_OUT_ACTIVE (1 << 4) /* set output active */
125ae7123f8SSimon Glass
126ae7123f8SSimon Glass uint offset; /* GPIO offset within the device */
127ae7123f8SSimon Glass /*
128ae7123f8SSimon Glass * We could consider adding the GPIO label in here. Possibly we could
129ae7123f8SSimon Glass * use this structure for internal GPIO information.
130ae7123f8SSimon Glass */
131ae7123f8SSimon Glass };
132ae7123f8SSimon Glass
13396495d90SSimon Glass /**
134d9a607f2SMasahiro Yamada * dm_gpio_is_valid() - Check if a GPIO is valid
1353669e0e7SSimon Glass *
1363669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags,
1373669e0e7SSimon Glass * previously returned by gpio_request_by_name()
1383669e0e7SSimon Glass * @return true if valid, false if not
1393669e0e7SSimon Glass */
dm_gpio_is_valid(const struct gpio_desc * desc)14017c43f1aSSimon Glass static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
1413669e0e7SSimon Glass {
1423669e0e7SSimon Glass return desc->dev != NULL;
1433669e0e7SSimon Glass }
1443669e0e7SSimon Glass
1453669e0e7SSimon Glass /**
1460757535aSSimon Glass * gpio_get_status() - get the current GPIO status as a string
1470757535aSSimon Glass *
1480757535aSSimon Glass * Obtain the current GPIO status as a string which can be presented to the
1490757535aSSimon Glass * user. A typical string is:
1500757535aSSimon Glass *
1510757535aSSimon Glass * "b4: in: 1 [x] sdmmc_cd"
1520757535aSSimon Glass *
1530757535aSSimon Glass * which means this is GPIO bank b, offset 4, currently set to input, current
1540757535aSSimon Glass * value 1, [x] means that it is requested and the owner is 'sdmmc_cd'
1550757535aSSimon Glass *
1565d1c17e9SSimon Glass * TODO(sjg@chromium.org): This should use struct gpio_desc
1575d1c17e9SSimon Glass *
1580757535aSSimon Glass * @dev: Device to check
1590757535aSSimon Glass * @offset: Offset of device GPIO to check
1600757535aSSimon Glass * @buf: Place to put string
1610757535aSSimon Glass * @buffsize: Size of string including \0
1620757535aSSimon Glass */
1630757535aSSimon Glass int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize);
1640757535aSSimon Glass
1650757535aSSimon Glass /**
1666449a506SSimon Glass * gpio_get_function() - get the current function for a GPIO pin
1676449a506SSimon Glass *
1686449a506SSimon Glass * Note this returns GPIOF_UNUSED if the GPIO is not requested.
1696449a506SSimon Glass *
1705d1c17e9SSimon Glass * TODO(sjg@chromium.org): This should use struct gpio_desc
1715d1c17e9SSimon Glass *
1726449a506SSimon Glass * @dev: Device to check
1736449a506SSimon Glass * @offset: Offset of device GPIO to check
174d9a607f2SMasahiro Yamada * @namep: If non-NULL, this is set to the name given when the GPIO
1756449a506SSimon Glass * was requested, or -1 if it has not been requested
1766449a506SSimon Glass * @return -ENODATA if the driver returned an unknown function,
1776449a506SSimon Glass * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
1786449a506SSimon Glass * GPIOF_UNUSED if the GPIO has not been requested. Otherwise returns the
1796449a506SSimon Glass * function from enum gpio_func_t.
1806449a506SSimon Glass */
1816449a506SSimon Glass int gpio_get_function(struct udevice *dev, int offset, const char **namep);
1826449a506SSimon Glass
1836449a506SSimon Glass /**
1846449a506SSimon Glass * gpio_get_raw_function() - get the current raw function for a GPIO pin
1856449a506SSimon Glass *
1866449a506SSimon Glass * Note this does not return GPIOF_UNUSED - it will always return the GPIO
1876449a506SSimon Glass * driver's view of a pin function, even if it is not correctly set up.
1886449a506SSimon Glass *
1895d1c17e9SSimon Glass * TODO(sjg@chromium.org): This should use struct gpio_desc
1905d1c17e9SSimon Glass *
1916449a506SSimon Glass * @dev: Device to check
1926449a506SSimon Glass * @offset: Offset of device GPIO to check
193d9a607f2SMasahiro Yamada * @namep: If non-NULL, this is set to the name given when the GPIO
1946449a506SSimon Glass * was requested, or -1 if it has not been requested
1956449a506SSimon Glass * @return -ENODATA if the driver returned an unknown function,
1966449a506SSimon Glass * -ENODEV if the device is not active, -EINVAL if the offset is invalid.
1976449a506SSimon Glass * Otherwise returns the function from enum gpio_func_t.
1986449a506SSimon Glass */
1996449a506SSimon Glass int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep);
2006449a506SSimon Glass
2016449a506SSimon Glass /**
202d44f597bSSimon Glass * gpio_requestf() - request a GPIO using a format string for the owner
203d44f597bSSimon Glass *
204d44f597bSSimon Glass * This is a helper function for gpio_request(). It allows you to provide
205d44f597bSSimon Glass * a printf()-format string for the GPIO owner. It calls gpio_request() with
206d44f597bSSimon Glass * the string that is created
207d44f597bSSimon Glass */
208d44f597bSSimon Glass int gpio_requestf(unsigned gpio, const char *fmt, ...)
209d44f597bSSimon Glass __attribute__ ((format (__printf__, 2, 3)));
210d44f597bSSimon Glass
2110dac4d51SSimon Glass struct fdtdec_phandle_args;
2120dac4d51SSimon Glass
213d44f597bSSimon Glass /**
2146c880b77SEric Nelson * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate
2156c880b77SEric Nelson *
2166c880b77SEric Nelson * This routine sets the offset field to args[0] and the flags field to
2176c880b77SEric Nelson * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1].
2186c880b77SEric Nelson */
2196c880b77SEric Nelson int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
2203a57123eSSimon Glass struct ofnode_phandle_args *args);
2216c880b77SEric Nelson
2226c880b77SEric Nelson /**
22396495d90SSimon Glass * struct struct dm_gpio_ops - Driver model GPIO operations
22496495d90SSimon Glass *
22596495d90SSimon Glass * Refer to functions above for description. These function largely copy
22696495d90SSimon Glass * the old API.
22796495d90SSimon Glass *
22896495d90SSimon Glass * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
22996495d90SSimon Glass * new DM GPIO API, this should be really easy to flip over to the Linux
23096495d90SSimon Glass * GPIO API-alike interface.
23196495d90SSimon Glass *
23225ca385dSMarcel Ziswiler * Also it would be useful to standardise additional functions like
23396495d90SSimon Glass * pullup, slew rate and drive strength.
23496495d90SSimon Glass *
235d9a607f2SMasahiro Yamada * gpio_request() and gpio_free() are optional - if NULL then they will
23696495d90SSimon Glass * not be called.
23796495d90SSimon Glass *
23896495d90SSimon Glass * Note that @offset is the offset from the base GPIO of the device. So
23996495d90SSimon Glass * offset 0 is the device's first GPIO and offset o-1 is the last GPIO,
24096495d90SSimon Glass * where o is the number of GPIO lines controlled by the device. A device
24196495d90SSimon Glass * is typically used to control a single bank of GPIOs. Within complex
24296495d90SSimon Glass * SoCs there may be many banks and therefore many devices all referring
24396495d90SSimon Glass * to the different IO addresses within the SoC.
24496495d90SSimon Glass *
24525ca385dSMarcel Ziswiler * The uclass combines all GPIO devices together to provide a consistent
24696495d90SSimon Glass * numbering from 0 to n-1, where n is the number of GPIOs in total across
24796495d90SSimon Glass * all devices. Be careful not to confuse offset with gpio in the parameters.
24896495d90SSimon Glass */
24996495d90SSimon Glass struct dm_gpio_ops {
25054c5d08aSHeiko Schocher int (*request)(struct udevice *dev, unsigned offset, const char *label);
25154c5d08aSHeiko Schocher int (*free)(struct udevice *dev, unsigned offset);
25254c5d08aSHeiko Schocher int (*direction_input)(struct udevice *dev, unsigned offset);
25354c5d08aSHeiko Schocher int (*direction_output)(struct udevice *dev, unsigned offset,
25496495d90SSimon Glass int value);
25554c5d08aSHeiko Schocher int (*get_value)(struct udevice *dev, unsigned offset);
25654c5d08aSHeiko Schocher int (*set_value)(struct udevice *dev, unsigned offset, int value);
25753ecdfb9Smario.six@gdsys.cc int (*get_open_drain)(struct udevice *dev, unsigned offset);
25853ecdfb9Smario.six@gdsys.cc int (*set_open_drain)(struct udevice *dev, unsigned offset, int value);
25989e64054SSimon Glass /**
26089e64054SSimon Glass * get_function() Get the GPIO function
26189e64054SSimon Glass *
26289e64054SSimon Glass * @dev: Device to check
26389e64054SSimon Glass * @offset: GPIO offset within that device
26489e64054SSimon Glass * @return current function - GPIOF_...
26589e64054SSimon Glass */
26654c5d08aSHeiko Schocher int (*get_function)(struct udevice *dev, unsigned offset);
2670dac4d51SSimon Glass
2680dac4d51SSimon Glass /**
2690dac4d51SSimon Glass * xlate() - Translate phandle arguments into a GPIO description
2700dac4d51SSimon Glass *
2710dac4d51SSimon Glass * This function should set up the fields in desc according to the
2720dac4d51SSimon Glass * information in the arguments. The uclass will have set up:
2730dac4d51SSimon Glass *
2740dac4d51SSimon Glass * @desc->dev to @dev
2750dac4d51SSimon Glass * @desc->flags to 0
2766c880b77SEric Nelson * @desc->offset to 0
2770dac4d51SSimon Glass *
2786c880b77SEric Nelson * This method is optional and defaults to gpio_xlate_offs_flags,
2796c880b77SEric Nelson * which will parse offset and the GPIO_ACTIVE_LOW flag in the first
2806c880b77SEric Nelson * two arguments.
2810dac4d51SSimon Glass *
2820dac4d51SSimon Glass * Note that @dev is passed in as a parameter to follow driver model
2830dac4d51SSimon Glass * uclass conventions, even though it is already available as
2840dac4d51SSimon Glass * desc->dev.
2850dac4d51SSimon Glass *
2860dac4d51SSimon Glass * @dev: GPIO device
2870dac4d51SSimon Glass * @desc: Place to put GPIO description
288d9a607f2SMasahiro Yamada * @args: Arguments provided in description
2890dac4d51SSimon Glass * @return 0 if OK, -ve on error
2900dac4d51SSimon Glass */
2910dac4d51SSimon Glass int (*xlate)(struct udevice *dev, struct gpio_desc *desc,
2923a57123eSSimon Glass struct ofnode_phandle_args *args);
29396495d90SSimon Glass };
29496495d90SSimon Glass
29596495d90SSimon Glass /**
29696495d90SSimon Glass * struct gpio_dev_priv - information about a device used by the uclass
29796495d90SSimon Glass *
29896495d90SSimon Glass * The uclass combines all active GPIO devices into a unified numbering
29925ca385dSMarcel Ziswiler * scheme. To do this it maintains some private information about each
30096495d90SSimon Glass * device.
30196495d90SSimon Glass *
30296495d90SSimon Glass * To implement driver model support in your GPIO driver, add a probe
30396495d90SSimon Glass * handler, and set @gpio_count and @bank_name correctly in that handler.
30496495d90SSimon Glass * This tells the uclass the name of the GPIO bank and the number of GPIOs
30596495d90SSimon Glass * it contains.
30696495d90SSimon Glass *
30796495d90SSimon Glass * @bank_name: Name of the GPIO device (e.g 'a' means GPIOs will be called
30896495d90SSimon Glass * 'A0', 'A1', etc.
30996495d90SSimon Glass * @gpio_count: Number of GPIOs in this device
31096495d90SSimon Glass * @gpio_base: Base GPIO number for this device. For the first active device
31196495d90SSimon Glass * this will be 0; the numbering for others will follow sequentially so that
31296495d90SSimon Glass * @gpio_base for device 1 will equal the number of GPIOs in device 0.
313b892d127SSimon Glass * @name: Array of pointers to the name for each GPIO in this bank. The
314b892d127SSimon Glass * value of the pointer will be NULL if the GPIO has not been claimed.
31596495d90SSimon Glass */
31696495d90SSimon Glass struct gpio_dev_priv {
31796495d90SSimon Glass const char *bank_name;
31896495d90SSimon Glass unsigned gpio_count;
31996495d90SSimon Glass unsigned gpio_base;
320b892d127SSimon Glass char **name;
32196495d90SSimon Glass };
32296495d90SSimon Glass
32396495d90SSimon Glass /* Access the GPIO operations for a device */
32496495d90SSimon Glass #define gpio_get_ops(dev) ((struct dm_gpio_ops *)(dev)->driver->ops)
32596495d90SSimon Glass
32696495d90SSimon Glass /**
32796495d90SSimon Glass * gpio_get_bank_info - Return information about a GPIO bank/device
32896495d90SSimon Glass *
32996495d90SSimon Glass * This looks up a device and returns both its GPIO base name and the number
33096495d90SSimon Glass * of GPIOs it controls.
33196495d90SSimon Glass *
33296495d90SSimon Glass * @dev: Device to look up
33396495d90SSimon Glass * @offset_count: Returns number of GPIOs within this bank
33496495d90SSimon Glass * @return bank name of this device
33596495d90SSimon Glass */
33654c5d08aSHeiko Schocher const char *gpio_get_bank_info(struct udevice *dev, int *offset_count);
33796495d90SSimon Glass
33896495d90SSimon Glass /**
33932ec1598SSimon Glass * dm_gpio_lookup_name() - Look up a named GPIO and return its description
34032ec1598SSimon Glass *
34132ec1598SSimon Glass * The name of a GPIO is typically its bank name followed by a number from 0.
34232ec1598SSimon Glass * For example A0 is the first GPIO in bank A. Each bank is a separate driver
34332ec1598SSimon Glass * model device.
34432ec1598SSimon Glass *
34532ec1598SSimon Glass * @name: Name to look up
34632ec1598SSimon Glass * @desc: Returns description, on success
34732ec1598SSimon Glass * @return 0 if OK, -ve on error
34832ec1598SSimon Glass */
34932ec1598SSimon Glass int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc);
35032ec1598SSimon Glass
35132ec1598SSimon Glass /**
3527e044b9aSHeiko Schocher * gpio_hog_lookup_name() - Look up a named GPIO and return the gpio descr.
3537e044b9aSHeiko Schocher *
3547e044b9aSHeiko Schocher * @name: Name to look up
355*25cd3b96SHeiko Schocher * @desc: Returns GPIO description, on success, else NULL
356*25cd3b96SHeiko Schocher * @return: Returns 0 if OK, else -ENODEV
3577e044b9aSHeiko Schocher */
358*25cd3b96SHeiko Schocher int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc);
3597e044b9aSHeiko Schocher
3607e044b9aSHeiko Schocher /**
3617e044b9aSHeiko Schocher * gpio_hog_probe_all() - probe all gpio devices with
3627e044b9aSHeiko Schocher * gpio-hog subnodes.
3637e044b9aSHeiko Schocher *
3647e044b9aSHeiko Schocher * @return: Returns return value from device_probe()
3657e044b9aSHeiko Schocher */
3667e044b9aSHeiko Schocher int gpio_hog_probe_all(void);
3677e044b9aSHeiko Schocher
3687e044b9aSHeiko Schocher /**
36996495d90SSimon Glass * gpio_lookup_name - Look up a GPIO name and return its details
37096495d90SSimon Glass *
37196495d90SSimon Glass * This is used to convert a named GPIO into a device, offset and GPIO
37296495d90SSimon Glass * number.
37396495d90SSimon Glass *
37496495d90SSimon Glass * @name: GPIO name to look up
37596495d90SSimon Glass * @devp: Returns pointer to device which contains this GPIO
37696495d90SSimon Glass * @offsetp: Returns the offset number within this device
37796495d90SSimon Glass * @gpiop: Returns the absolute GPIO number, numbered from 0
37896495d90SSimon Glass */
37954c5d08aSHeiko Schocher int gpio_lookup_name(const char *name, struct udevice **devp,
38096495d90SSimon Glass unsigned int *offsetp, unsigned int *gpiop);
38196495d90SSimon Glass
382e5901c94SSimon Glass /**
383962f5cafSSimon Glass * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
384e5901c94SSimon Glass *
385e5901c94SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1,
386e5901c94SSimon Glass * etc. then returns the resulting integer.
387e5901c94SSimon Glass *
388e5901c94SSimon Glass * @gpio_list: List of GPIOs to collect
389962f5cafSSimon Glass * @return resulting integer value, or -ve on error
390e5901c94SSimon Glass */
391962f5cafSSimon Glass int gpio_get_values_as_int(const int *gpio_list);
392962f5cafSSimon Glass
393962f5cafSSimon Glass /**
394bbf24780SSimon Glass * dm_gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
395bbf24780SSimon Glass *
396bbf24780SSimon Glass * This puts the value of the first GPIO into bit 0, the second into bit 1,
397bbf24780SSimon Glass * etc. then returns the resulting integer.
398bbf24780SSimon Glass *
399bbf24780SSimon Glass * @desc_list: List of GPIOs to collect
400bbf24780SSimon Glass * @count: Number of GPIOs
401bbf24780SSimon Glass * @return resulting integer value, or -ve on error
402bbf24780SSimon Glass */
40317c43f1aSSimon Glass int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count);
404bbf24780SSimon Glass
405bbf24780SSimon Glass /**
406962f5cafSSimon Glass * gpio_claim_vector() - claim a number of GPIOs for input
407962f5cafSSimon Glass *
408962f5cafSSimon Glass * @gpio_num_array: array of gpios to claim, terminated by -1
409962f5cafSSimon Glass * @fmt: format string for GPIO names, e.g. "board_id%d"
410962f5cafSSimon Glass * @return 0 if OK, -ve on error
411962f5cafSSimon Glass */
412962f5cafSSimon Glass int gpio_claim_vector(const int *gpio_num_array, const char *fmt);
4135b5ac645SJeroen Hofstee
4143669e0e7SSimon Glass /**
4153669e0e7SSimon Glass * gpio_request_by_name() - Locate and request a GPIO by name
4163669e0e7SSimon Glass *
4173669e0e7SSimon Glass * This operates by looking up the given list name in the device (device
4183669e0e7SSimon Glass * tree property) and requesting the GPIO for use. The property must exist
4193669e0e7SSimon Glass * in @dev's node.
4203669e0e7SSimon Glass *
4213669e0e7SSimon Glass * Use @flags to specify whether the GPIO should be an input or output. In
4223669e0e7SSimon Glass * principle this can also come from the device tree binding but most
4233669e0e7SSimon Glass * bindings don't provide this information. Specifically, when the GPIO uclass
4243669e0e7SSimon Glass * calls the xlate() method, it can return default flags, which are then
4253669e0e7SSimon Glass * ORed with this @flags.
4263669e0e7SSimon Glass *
4273669e0e7SSimon Glass * If we find that requesting the GPIO is not always needed we could add a
4283669e0e7SSimon Glass * new function or a new GPIOD_NO_REQUEST flag.
4293669e0e7SSimon Glass *
4303669e0e7SSimon Glass * At present driver model has no reference counting so if one device
4313669e0e7SSimon Glass * requests a GPIO which subsequently is unbound, the @desc->dev pointer
4323669e0e7SSimon Glass * will be invalid. However this will only happen if the GPIO device is
4333669e0e7SSimon Glass * unbound, not if it is removed, so this seems like a reasonable limitation
4343669e0e7SSimon Glass * for now. There is no real use case for unbinding drivers in normal
4353669e0e7SSimon Glass * operation.
4363669e0e7SSimon Glass *
4373669e0e7SSimon Glass * The device tree binding is doc/device-tree-bindings/gpio/gpio.txt in
4383669e0e7SSimon Glass * generate terms and each specific device may add additional details in
4393669e0e7SSimon Glass * a binding file in the same directory.
4403669e0e7SSimon Glass *
4413669e0e7SSimon Glass * @dev: Device requesting the GPIO
4423669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios")
4433669e0e7SSimon Glass * @index: Index number of the GPIO in that list use request (0=first)
4443669e0e7SSimon Glass * @desc: Returns GPIO description information. If there is no such
4453669e0e7SSimon Glass * GPIO, dev->dev will be NULL.
4463669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...)
4473669e0e7SSimon Glass * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is
4483669e0e7SSimon Glass * something wrong with the list, or other -ve for another error (e.g.
4493669e0e7SSimon Glass * -EBUSY if a GPIO was already requested)
4503669e0e7SSimon Glass */
4513669e0e7SSimon Glass int gpio_request_by_name(struct udevice *dev, const char *list_name,
4523669e0e7SSimon Glass int index, struct gpio_desc *desc, int flags);
4533669e0e7SSimon Glass
4543669e0e7SSimon Glass /**
4553669e0e7SSimon Glass * gpio_request_list_by_name() - Request a list of GPIOs
4563669e0e7SSimon Glass *
457d9a607f2SMasahiro Yamada * Reads all the GPIOs from a list and requests them. See
4583669e0e7SSimon Glass * gpio_request_by_name() for additional details. Lists should not be
4593669e0e7SSimon Glass * misused to hold unrelated or optional GPIOs. They should only be used
4603669e0e7SSimon Glass * for things like parallel data lines. A zero phandle terminates the list
4613669e0e7SSimon Glass * the list.
4623669e0e7SSimon Glass *
4633669e0e7SSimon Glass * This function will either succeed, and request all GPIOs in the list, or
4643669e0e7SSimon Glass * fail and request none (it will free already-requested GPIOs in case of
4653669e0e7SSimon Glass * an error part-way through).
4663669e0e7SSimon Glass *
4673669e0e7SSimon Glass * @dev: Device requesting the GPIO
4683669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios")
4693669e0e7SSimon Glass * @desc_list: Returns a list of GPIO description information
4703669e0e7SSimon Glass * @max_count: Maximum number of GPIOs to return (@desc_list must be at least
4713669e0e7SSimon Glass * this big)
4723669e0e7SSimon Glass * @flags: Indicates the GPIO input/output settings (GPIOD_...)
4733669e0e7SSimon Glass * @return number of GPIOs requested, or -ve on error
4743669e0e7SSimon Glass */
4753669e0e7SSimon Glass int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
4763669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count,
4773669e0e7SSimon Glass int flags);
4783669e0e7SSimon Glass
4793669e0e7SSimon Glass /**
480efa677fbSSimon Glass * dm_gpio_request() - manually request a GPIO
481efa677fbSSimon Glass *
482efa677fbSSimon Glass * Note: This function should only be used for testing / debugging. Instead.
483efa677fbSSimon Glass * use gpio_request_by_name() to pull GPIOs from the device tree.
484efa677fbSSimon Glass *
485efa677fbSSimon Glass * @desc: GPIO description of GPIO to request (see dm_gpio_lookup_name())
486efa677fbSSimon Glass * @label: Label to attach to the GPIO while claimed
487efa677fbSSimon Glass * @return 0 if OK, -ve on error
488efa677fbSSimon Glass */
489efa677fbSSimon Glass int dm_gpio_request(struct gpio_desc *desc, const char *label);
490efa677fbSSimon Glass
491efa677fbSSimon Glass /**
4923669e0e7SSimon Glass * gpio_get_list_count() - Returns the number of GPIOs in a list
4933669e0e7SSimon Glass *
4943669e0e7SSimon Glass * Counts the GPIOs in a list. See gpio_request_by_name() for additional
4953669e0e7SSimon Glass * details.
4963669e0e7SSimon Glass *
4973669e0e7SSimon Glass * @dev: Device requesting the GPIO
4983669e0e7SSimon Glass * @list_name: Name of GPIO list (e.g. "board-id-gpios")
4993669e0e7SSimon Glass * @return number of GPIOs (0 for an empty property) or -ENOENT if the list
5003669e0e7SSimon Glass * does not exist
5013669e0e7SSimon Glass */
5023669e0e7SSimon Glass int gpio_get_list_count(struct udevice *dev, const char *list_name);
5033669e0e7SSimon Glass
5043669e0e7SSimon Glass /**
5053669e0e7SSimon Glass * gpio_request_by_name_nodev() - request GPIOs without a device
5063669e0e7SSimon Glass *
5073669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a
5083669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model
5093669e0e7SSimon Glass */
510150c5afeSSimon Glass int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
511150c5afeSSimon Glass struct gpio_desc *desc, int flags);
5123669e0e7SSimon Glass
5133669e0e7SSimon Glass /**
5143669e0e7SSimon Glass * gpio_request_list_by_name_nodev() - request GPIOs without a device
5153669e0e7SSimon Glass *
5163669e0e7SSimon Glass * This is a version of gpio_request_list_by_name() that does not use a
5173669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model
5183669e0e7SSimon Glass */
519150c5afeSSimon Glass int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
5203669e0e7SSimon Glass struct gpio_desc *desc_list, int max_count,
5213669e0e7SSimon Glass int flags);
5223669e0e7SSimon Glass
5233669e0e7SSimon Glass /**
5247e044b9aSHeiko Schocher * gpio_dev_request_index() - request single GPIO from gpio device
5257e044b9aSHeiko Schocher *
5267e044b9aSHeiko Schocher * @dev: GPIO device
527*25cd3b96SHeiko Schocher * @nodename: Name of node for which gpio gets requested, used
528*25cd3b96SHeiko Schocher * for the gpio label name
5297e044b9aSHeiko Schocher * @list_name: Name of GPIO list (e.g. "board-id-gpios")
5307e044b9aSHeiko Schocher * @index: Index number of the GPIO in that list use request (0=first)
5317e044b9aSHeiko Schocher * @flags: GPIOD_* flags
532*25cd3b96SHeiko Schocher * @dtflags: GPIO flags read from DT defined see GPIOD_*
533*25cd3b96SHeiko Schocher * @desc: returns GPIO descriptor filled from this function
5347e044b9aSHeiko Schocher * @return: return value from gpio_request_tail()
5357e044b9aSHeiko Schocher */
5367e044b9aSHeiko Schocher int gpio_dev_request_index(struct udevice *dev, const char *nodename,
5377e044b9aSHeiko Schocher char *list_name, int index, int flags,
5387e044b9aSHeiko Schocher int dtflags, struct gpio_desc *desc);
5397e044b9aSHeiko Schocher
5407e044b9aSHeiko Schocher /**
5413669e0e7SSimon Glass * dm_gpio_free() - Free a single GPIO
5423669e0e7SSimon Glass *
5433669e0e7SSimon Glass * This frees a single GPIOs previously returned from gpio_request_by_name().
5443669e0e7SSimon Glass *
5453669e0e7SSimon Glass * @dev: Device which requested the GPIO
5463669e0e7SSimon Glass * @desc: GPIO to free
5473669e0e7SSimon Glass * @return 0 if OK, -ve on error
5483669e0e7SSimon Glass */
5493669e0e7SSimon Glass int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc);
5503669e0e7SSimon Glass
5513669e0e7SSimon Glass /**
5523669e0e7SSimon Glass * gpio_free_list() - Free a list of GPIOs
5533669e0e7SSimon Glass *
5543669e0e7SSimon Glass * This frees a list of GPIOs previously returned from
5553669e0e7SSimon Glass * gpio_request_list_by_name().
5563669e0e7SSimon Glass *
5573669e0e7SSimon Glass * @dev: Device which requested the GPIOs
5583669e0e7SSimon Glass * @desc: List of GPIOs to free
5593669e0e7SSimon Glass * @count: Number of GPIOs in the list
5603669e0e7SSimon Glass * @return 0 if OK, -ve on error
5613669e0e7SSimon Glass */
5623669e0e7SSimon Glass int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count);
5633669e0e7SSimon Glass
5643669e0e7SSimon Glass /**
5653669e0e7SSimon Glass * gpio_free_list_nodev() - free GPIOs without a device
5663669e0e7SSimon Glass *
5673669e0e7SSimon Glass * This is a version of gpio_free_list() that does not use a
5683669e0e7SSimon Glass * device. Avoid it unless the caller is not yet using driver model
5693669e0e7SSimon Glass */
5703669e0e7SSimon Glass int gpio_free_list_nodev(struct gpio_desc *desc, int count);
5713669e0e7SSimon Glass
5723669e0e7SSimon Glass /**
5733669e0e7SSimon Glass * dm_gpio_get_value() - Get the value of a GPIO
5743669e0e7SSimon Glass *
5753669e0e7SSimon Glass * This is the driver model version of the existing gpio_get_value() function
5763669e0e7SSimon Glass * and should be used instead of that.
5773669e0e7SSimon Glass *
5783669e0e7SSimon Glass * For now, these functions have a dm_ prefix since they conflict with
5793669e0e7SSimon Glass * existing names.
5803669e0e7SSimon Glass *
5813669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags,
5823669e0e7SSimon Glass * previously returned by gpio_request_by_name()
5833669e0e7SSimon Glass * @return GPIO value (0 for inactive, 1 for active) or -ve on error
5843669e0e7SSimon Glass */
58517c43f1aSSimon Glass int dm_gpio_get_value(const struct gpio_desc *desc);
5863669e0e7SSimon Glass
58717c43f1aSSimon Glass int dm_gpio_set_value(const struct gpio_desc *desc, int value);
5883669e0e7SSimon Glass
5893669e0e7SSimon Glass /**
59053ecdfb9Smario.six@gdsys.cc * dm_gpio_get_open_drain() - Check if open-drain-mode of a GPIO is active
59153ecdfb9Smario.six@gdsys.cc *
59253ecdfb9Smario.six@gdsys.cc * This checks if open-drain-mode for a GPIO is enabled or not. This method is
59353ecdfb9Smario.six@gdsys.cc * optional.
59453ecdfb9Smario.six@gdsys.cc *
59553ecdfb9Smario.six@gdsys.cc * @desc: GPIO description containing device, offset and flags,
59653ecdfb9Smario.six@gdsys.cc * previously returned by gpio_request_by_name()
59753ecdfb9Smario.six@gdsys.cc * @return Value of open drain mode for GPIO (0 for inactive, 1 for active) or
59853ecdfb9Smario.six@gdsys.cc * -ve on error
59953ecdfb9Smario.six@gdsys.cc */
60053ecdfb9Smario.six@gdsys.cc int dm_gpio_get_open_drain(struct gpio_desc *desc);
60153ecdfb9Smario.six@gdsys.cc
60253ecdfb9Smario.six@gdsys.cc /**
60353ecdfb9Smario.six@gdsys.cc * dm_gpio_set_open_drain() - Switch open-drain-mode of a GPIO on or off
60453ecdfb9Smario.six@gdsys.cc *
60553ecdfb9Smario.six@gdsys.cc * This enables or disables open-drain mode for a GPIO. This method is
60653ecdfb9Smario.six@gdsys.cc * optional; if the driver does not support it, nothing happens when the method
60753ecdfb9Smario.six@gdsys.cc * is called.
60853ecdfb9Smario.six@gdsys.cc *
60953ecdfb9Smario.six@gdsys.cc * In open-drain mode, instead of actively driving the output (Push-pull
61053ecdfb9Smario.six@gdsys.cc * output), the GPIO's pin is connected to the collector (for a NPN transistor)
61153ecdfb9Smario.six@gdsys.cc * or the drain (for a MOSFET) of a transistor, respectively. The pin then
61253ecdfb9Smario.six@gdsys.cc * either forms an open circuit or a connection to ground, depending on the
61353ecdfb9Smario.six@gdsys.cc * state of the transistor.
61453ecdfb9Smario.six@gdsys.cc *
61553ecdfb9Smario.six@gdsys.cc * @desc: GPIO description containing device, offset and flags,
61653ecdfb9Smario.six@gdsys.cc * previously returned by gpio_request_by_name()
61753ecdfb9Smario.six@gdsys.cc * @return 0 if OK, -ve on error
61853ecdfb9Smario.six@gdsys.cc */
61953ecdfb9Smario.six@gdsys.cc int dm_gpio_set_open_drain(struct gpio_desc *desc, int value);
62053ecdfb9Smario.six@gdsys.cc
62153ecdfb9Smario.six@gdsys.cc /**
6223669e0e7SSimon Glass * dm_gpio_set_dir() - Set the direction for a GPIO
6233669e0e7SSimon Glass *
6243669e0e7SSimon Glass * This sets up the direction according tot the provided flags. It will do
6253669e0e7SSimon Glass * nothing unless the direction is actually specified.
6263669e0e7SSimon Glass *
6273669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags,
6283669e0e7SSimon Glass * previously returned by gpio_request_by_name()
6293669e0e7SSimon Glass * @return 0 if OK, -ve on error
6303669e0e7SSimon Glass */
6313669e0e7SSimon Glass int dm_gpio_set_dir(struct gpio_desc *desc);
6323669e0e7SSimon Glass
6333669e0e7SSimon Glass /**
6343669e0e7SSimon Glass * dm_gpio_set_dir_flags() - Set direction using specific flags
6353669e0e7SSimon Glass *
6363669e0e7SSimon Glass * This is like dm_gpio_set_dir() except that the flags value is provided
6373669e0e7SSimon Glass * instead of being used from desc->flags. This is needed because in many
6383669e0e7SSimon Glass * cases the GPIO description does not include direction information.
6393669e0e7SSimon Glass * Note that desc->flags is updated by this function.
6403669e0e7SSimon Glass *
6413669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags,
6423669e0e7SSimon Glass * previously returned by gpio_request_by_name()
6433669e0e7SSimon Glass * @flags: New flags to use
6443669e0e7SSimon Glass * @return 0 if OK, -ve on error, in which case desc->flags is not updated
6453669e0e7SSimon Glass */
6463669e0e7SSimon Glass int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags);
6473669e0e7SSimon Glass
6483669e0e7SSimon Glass /**
6493669e0e7SSimon Glass * gpio_get_number() - Get the global GPIO number of a GPIO
6503669e0e7SSimon Glass *
651d9a607f2SMasahiro Yamada * This should only be used for debugging or interest. It returns the number
6523669e0e7SSimon Glass * that should be used for gpio_get_value() etc. to access this GPIO.
6533669e0e7SSimon Glass *
6543669e0e7SSimon Glass * @desc: GPIO description containing device, offset and flags,
6553669e0e7SSimon Glass * previously returned by gpio_request_by_name()
6563669e0e7SSimon Glass * @return GPIO number, or -ve if not found
6573669e0e7SSimon Glass */
65817c43f1aSSimon Glass int gpio_get_number(const struct gpio_desc *desc);
6593669e0e7SSimon Glass
6605f533aebSJoe Hershberger #endif /* _ASM_GENERIC_GPIO_H_ */
661