dm: gpio: add a default gpio xlate routine

Many drivers use a common form of offset + flags for device
tree nodes. e.g.:
	<&gpio1 2 GPIO_ACTIVE_LOW>

This patch adds a common implementation of this type of parsing
and calls it when a gpio driver doesn't supply its' own xlate
routine.

This will allow removal of the driver-specific versions in a
handful of drivers and simplify the addition of new drivers.

Signed-off-by: Eric Nelson <eric@nelint.com>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Eric Nelson 2016-04-24 16:32:40 -07:00 committed by Simon Glass
parent c0c62d9233
commit 6c880b7719
2 changed files with 37 additions and 12 deletions

View file

@ -6,6 +6,7 @@
#include <common.h> #include <common.h>
#include <dm.h> #include <dm.h>
#include <dt-bindings/gpio/gpio.h>
#include <errno.h> #include <errno.h>
#include <fdtdec.h> #include <fdtdec.h>
#include <malloc.h> #include <malloc.h>
@ -113,19 +114,33 @@ int gpio_lookup_name(const char *name, struct udevice **devp,
return 0; return 0;
} }
int gpio_xlate_offs_flags(struct udevice *dev,
struct gpio_desc *desc,
struct fdtdec_phandle_args *args)
{
if (args->args_count < 1)
return -EINVAL;
desc->offset = args->args[0];
if (args->args_count < 2)
return 0;
if (args->args[1] & GPIO_ACTIVE_LOW)
desc->flags = GPIOD_ACTIVE_LOW;
return 0;
}
static int gpio_find_and_xlate(struct gpio_desc *desc, static int gpio_find_and_xlate(struct gpio_desc *desc,
struct fdtdec_phandle_args *args) struct fdtdec_phandle_args *args)
{ {
struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
/* Use the first argument as the offset by default */ if (ops->xlate)
if (args->args_count > 0) return ops->xlate(desc->dev, desc, args);
desc->offset = args->args[0];
else else
desc->offset = -1; return gpio_xlate_offs_flags(desc->dev, desc, args);
desc->flags = 0;
return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
} }
int dm_gpio_request(struct gpio_desc *desc, const char *label) int dm_gpio_request(struct gpio_desc *desc, const char *label)
@ -605,6 +620,7 @@ static int _gpio_request_by_name_nodev(const void *blob, int node,
desc->dev = NULL; desc->dev = NULL;
desc->offset = 0; desc->offset = 0;
desc->flags = 0;
ret = fdtdec_parse_phandle_with_args(blob, node, list_name, ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
"#gpio-cells", 0, index, &args); "#gpio-cells", 0, index, &args);
if (ret) { if (ret) {

View file

@ -206,6 +206,16 @@ int gpio_requestf(unsigned gpio, const char *fmt, ...)
struct fdtdec_phandle_args; struct fdtdec_phandle_args;
/**
* gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate
*
* This routine sets the offset field to args[0] and the flags field to
* GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1].
*
*/
int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
struct fdtdec_phandle_args *args);
/** /**
* struct struct dm_gpio_ops - Driver model GPIO operations * struct struct dm_gpio_ops - Driver model GPIO operations
* *
@ -258,12 +268,11 @@ struct dm_gpio_ops {
* *
* @desc->dev to @dev * @desc->dev to @dev
* @desc->flags to 0 * @desc->flags to 0
* @desc->offset to the value of the first argument in args, if any, * @desc->offset to 0
* otherwise -1 (which is invalid)
* *
* This method is optional so if the above defaults suit it can be * This method is optional and defaults to gpio_xlate_offs_flags,
* omitted. Typical behaviour is to set up the GPIOD_ACTIVE_LOW flag * which will parse offset and the GPIO_ACTIVE_LOW flag in the first
* in desc->flags. * two arguments.
* *
* Note that @dev is passed in as a parameter to follow driver model * Note that @dev is passed in as a parameter to follow driver model
* uclass conventions, even though it is already available as * uclass conventions, even though it is already available as