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 82294cb..e33cde7 100644 (file)
@@ -128,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
@@ -135,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
  *
@@ -260,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
@@ -301,35 +323,54 @@ 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)
        /**
@@ -457,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
@@ -641,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 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.
  *
- * This sets up the direction according to the GPIO flags: desc->flags.
+ * 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
@@ -666,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