Merge branch 'next' of https://gitlab.denx.de/u-boot/custodians/u-boot-marvell into...
[platform/kernel/u-boot.git] / include / asm-generic / gpio.h
index 859f41a..e33cde7 100644 (file)
@@ -8,7 +8,9 @@
 #define _ASM_GENERIC_GPIO_H_
 
 #include <dm/ofnode.h>
+#include <linux/bitops.h>
 
+struct acpi_gpio;
 struct ofnode_phandle_args;
 
 /*
@@ -126,6 +128,12 @@ struct gpio_desc {
 #define GPIOD_PULL_UP          BIT(7)  /* GPIO has pull-up enabled */
 #define GPIOD_PULL_DOWN                BIT(8)  /* GPIO has pull-down enabled */
 
+/* Flags for updating the above */
+#define GPIOD_MASK_DIR         (GPIOD_IS_OUT | GPIOD_IS_IN | \
+                                       GPIOD_IS_OUT_ACTIVE)
+#define GPIOD_MASK_DSTYPE      (GPIOD_OPEN_DRAIN | GPIOD_OPEN_SOURCE)
+#define GPIOD_MASK_PULL                (GPIOD_PULL_UP | GPIOD_PULL_DOWN)
+
        uint offset;            /* GPIO offset within the device */
        /*
         * We could consider adding the GPIO label in here. Possibly we could
@@ -133,12 +141,6 @@ struct gpio_desc {
         */
 };
 
-/* helper to compute the value of the gpio output */
-#define GPIOD_FLAGS_OUTPUT_MASK (GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE)
-#define GPIOD_FLAGS_OUTPUT(flags) \
-       (((((flags) & GPIOD_FLAGS_OUTPUT_MASK) == GPIOD_IS_OUT_ACTIVE) || \
-         (((flags) & GPIOD_FLAGS_OUTPUT_MASK) == GPIOD_ACTIVE_LOW)))
-
 /**
  * dm_gpio_is_valid() - Check if a GPIO is valid
  *
@@ -258,10 +260,32 @@ int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
 struct dm_gpio_ops {
        int (*request)(struct udevice *dev, unsigned offset, const char *label);
        int (*rfree)(struct udevice *dev, unsigned int offset);
+
+       /**
+        * direction_input() - deprecated
+        *
+        * Equivalent to set_flags(...GPIOD_IS_IN)
+        */
        int (*direction_input)(struct udevice *dev, unsigned offset);
+
+       /**
+        * direction_output() - deprecated
+        *
+        * Equivalent to set_flags(...GPIOD_IS_OUT) with GPIOD_IS_OUT_ACTIVE
+        * also set if @value
+        */
        int (*direction_output)(struct udevice *dev, unsigned offset,
                                int value);
+
        int (*get_value)(struct udevice *dev, unsigned offset);
+
+       /**
+        * set_value() - Sets the GPIO value of an output
+        *
+        * If the driver provides an @set_flags() method then that is used
+        * in preference to this, with GPIOD_IS_OUT_ACTIVE set according to
+        * @value.
+        */
        int (*set_value)(struct udevice *dev, unsigned offset, int value);
        /**
         * get_function() Get the GPIO function
@@ -299,35 +323,68 @@ struct dm_gpio_ops {
                     struct ofnode_phandle_args *args);
 
        /**
-        * set_dir_flags() - Set GPIO dir flags
+        * set_flags() - Adjust GPIO flags
         *
         * This function should set up the GPIO configuration according to the
-        * information provide by the direction flags bitfield.
+        * information provided by @flags.
         *
-        * This method is optional.
+        * If any flags cannot be set (e.g. the driver or hardware does not
+        * support them or this particular GPIO does not have the requested
+        * feature), the driver should return -EINVAL.
+        *
+        * The uclass checks that flags do not obviously conflict (e.g. input
+        * and output). If the driver finds other conflicts it should return
+        * -ERECALLCONFLICT
+        *
+        * Note that GPIOD_ACTIVE_LOW should be ignored, since the uclass
+        * adjusts for it automatically. For example, for an output GPIO,
+        * GPIOD_ACTIVE_LOW causes GPIOD_IS_OUT_ACTIVE to be inverted by the
+        * uclass, so the driver always sees the value that should be set at the
+        * pin (1=high, 0=low).
+        *
+        * This method is required and should be implemented by new drivers. At
+        * some point, it will supersede direction_input() and
+        * direction_output(), which wil be removed.
         *
         * @dev:        GPIO device
         * @offset:     GPIO offset within that device
-        * @flags:      GPIO configuration to use
-        * @return 0 if OK, -ve on error
+        * @flags:      New flags value (GPIOD_...)
+        *
+        * @return 0 if OK, -EINVAL if unsupported, -ERECALLCONFLICT if flags
+        *      conflict in some *      non-obvious way and were not applied,
+        *      other -ve on error
         */
-       int (*set_dir_flags)(struct udevice *dev, unsigned int offset,
-                            ulong flags);
+       int (*set_flags)(struct udevice *dev, unsigned int offset, ulong flags);
 
        /**
-        * get_dir_flags() - Get GPIO dir flags
+        * get_flags() - Get GPIO flags
         *
-        * This function return the GPIO direction flags used.
+        * This function return the GPIO flags used. It should read this from
+        * the hardware directly.
         *
         * This method is optional.
         *
         * @dev:        GPIO device
         * @offset:     GPIO offset within that device
-        * @flags:      place to put the used direction flags by GPIO
+        * @flagsp:     place to put the current flags value
         * @return 0 if OK, -ve on error
         */
-       int (*get_dir_flags)(struct udevice *dev, unsigned int offset,
-                            ulong *flags);
+       int (*get_flags)(struct udevice *dev, unsigned int offset,
+                        ulong *flagsp);
+
+#if CONFIG_IS_ENABLED(ACPIGEN)
+       /**
+        * get_acpi() - Get the ACPI info for a GPIO
+        *
+        * This converts a GPIO to an ACPI structure for adding to the ACPI
+        * tables.
+        *
+        * @desc:       GPIO description to convert
+        * @gpio:       Output ACPI GPIO information
+        * @return ACPI pin number or -ve on error
+        */
+       int (*get_acpi)(const struct gpio_desc *desc, struct acpi_gpio *gpio);
+#endif
 };
 
 /**
@@ -441,6 +498,31 @@ int gpio_get_values_as_int(const int *gpio_list);
 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count);
 
 /**
+ * dm_gpio_get_values_as_int_base3() - Create a base-3 int from a list of GPIOs
+ *
+ * This uses pull-ups/pull-downs to figure out whether a GPIO line is externally
+ * pulled down, pulled up or floating. This allows three different strap values
+ * for each pin:
+ *    0 : external pull-down
+ *    1 : external pull-up
+ *    2 : floating
+ *
+ * With this it is possible to obtain more combinations from the same number of
+ * strapping pins, when compared to dm_gpio_get_values_as_int(). The external
+ * pull resistors should be made stronger that the internal SoC pull resistors,
+ * for this to work.
+ *
+ * With 2 pins, 6 combinations are possible, compared with 4
+ * With 3 pins, 27 are possible, compared with 8
+ *
+ * @desc_list: List of GPIOs to collect
+ * @count: Number of GPIOs
+ * @return resulting integer value, or -ve on error
+ */
+int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
+                                   int count);
+
+/**
  * gpio_claim_vector() - claim a number of GPIOs for input
  *
  * @gpio_num_array:    array of gpios to claim, terminated by -1
@@ -480,7 +562,7 @@ int gpio_claim_vector(const int *gpio_num_array, const char *fmt);
  * @list_name: Name of GPIO list (e.g. "board-id-gpios")
  * @index:     Index number of the GPIO in that list use request (0=first)
  * @desc:      Returns GPIO description information. If there is no such
- *             GPIO, dev->dev will be NULL.
+ *             GPIO, @desc->dev will be NULL.
  * @flags:     Indicates the GPIO input/output settings (GPIOD_...)
  * @return 0 if OK, -ENOENT if the GPIO does not exist, -EINVAL if there is
  * something wrong with the list, or other -ve for another error (e.g.
@@ -625,15 +707,23 @@ int dm_gpio_get_value(const struct gpio_desc *desc);
 int dm_gpio_set_value(const struct gpio_desc *desc, int value);
 
 /**
- * dm_gpio_set_dir() - Set the direction for a GPIO
+ * dm_gpio_clrset_flags() - Update flags
  *
- * This sets up the direction according to the GPIO flags: desc->flags.
+ * This updates the flags as directled. Note that desc->flags is updated by this
+ * function on success. If any changes cannot be made, best efforts are made.
+ *
+ * By use of @clr and @set any of flags can be individually updated, or left
+ * alone
  *
  * @desc:      GPIO description containing device, offset and flags,
  *             previously returned by gpio_request_by_name()
- * @return 0 if OK, -ve on error
+ * @clr:       Flags to clear (GPIOD_...)
+ * @set:       Flags to set (GPIOD_...)
+ * @return 0 if OK, -EINVAL if the flags had obvious conflicts,
+ * -ERECALLCONFLICT if there was a non-obvious hardware conflict when attempting
+ * to set the flags
  */
-int dm_gpio_set_dir(struct gpio_desc *desc);
+int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set);
 
 /**
  * dm_gpio_set_dir_flags() - Set direction using description and added flags
@@ -650,16 +740,31 @@ int dm_gpio_set_dir(struct gpio_desc *desc);
 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags);
 
 /**
- * dm_gpio_get_dir_flags() - Get direction flags
+ * dm_gpios_clrset_flags() - Sets flags for a set of GPIOs
+ *
+ * This clears and sets flags individually for each GPIO.
+ *
+ * @desc:      List of GPIOs to update
+ * @count:     Number of GPIOs in the list
+ * @clr:       Flags to clear (GPIOD_...), e.g. GPIOD_MASK_DIR if you are
+ *             changing the direction
+ * @set:       Flags to set (GPIOD_...)
+ * @return 0 if OK, -ve on error
+ */
+int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
+                         ulong set);
+
+/**
+ * dm_gpio_get_flags() - Get flags
  *
- * read the current direction flags
+ * Read the current flags
  *
  * @desc:      GPIO description containing device, offset and flags,
  *             previously returned by gpio_request_by_name()
  * @flags:     place to put the used flags
  * @return 0 if OK, -ve on error, in which case desc->flags is not updated
  */
-int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags);
+int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flags);
 
 /**
  * gpio_get_number() - Get the global GPIO number of a GPIO
@@ -673,4 +778,63 @@ int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags);
  */
 int gpio_get_number(const struct gpio_desc *desc);
 
+/**
+ * gpio_get_acpi() - Get the ACPI pin for a GPIO
+ *
+ * This converts a GPIO to an ACPI pin number for adding to the ACPI
+ * tables. If the GPIO is invalid, the pin_count and pins[0] are set to 0
+ *
+ * @desc:      GPIO description to convert
+ * @gpio:      Output ACPI GPIO information
+ * @return ACPI pin number or -ve on error
+ */
+int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio);
+
+/**
+ * devm_gpiod_get_index - Resource-managed gpiod_get()
+ * @dev:       GPIO consumer
+ * @con_id:    function within the GPIO consumer
+ * @index:     index of the GPIO to obtain in the consumer
+ * @flags:     optional GPIO initialization flags
+ *
+ * Managed gpiod_get(). GPIO descriptors returned from this function are
+ * automatically disposed on device unbind.
+ * Return the GPIO descriptor corresponding to the function con_id of device
+ * dev, -ENOENT if no GPIO has been assigned to the requested function, or
+ * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
+ */
+struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
+                                      unsigned int index, int flags);
+
+#define devm_gpiod_get(dev, id, flags) devm_gpiod_get_index(dev, id, 0, flags)
+/**
+ * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
+ * @dev: GPIO consumer, can be NULL for system-global GPIOs
+ * @con_id: function within the GPIO consumer
+ * @index:     index of the GPIO to obtain in the consumer
+ * @flags: optional GPIO initialization flags
+ *
+ * This is equivalent to devm_gpiod_get(), except that when no GPIO was
+ * assigned to the requested function it will return NULL. This is convenient
+ * for drivers that need to handle optional GPIOs.
+ */
+struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
+                                               const char *id,
+                                               unsigned int index,
+                                               int flags);
+
+#define devm_gpiod_get_optional(dev, id, flags) \
+       devm_gpiod_get_index_optional(dev, id, 0, flags)
+
+/**
+ * devm_gpiod_put - Resource-managed gpiod_put()
+ * @dev:       GPIO consumer
+ * @desc:      GPIO descriptor to dispose of
+ *
+ * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
+ * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
+ * will be disposed of by the resource management code.
+ */
+void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc);
+
 #endif /* _ASM_GENERIC_GPIO_H_ */