[U-Boot] [PATCH 01/11] drivers: core: device: add support to check dt compatible for a device/machine

Simon Glass
2016-04-09T18:35:23Z

Hi Mugunthan,

On 7 April 2016 at 09:17, Mugunthan V N <mugunthanvnm@ti.com> wrote:
> Provide an api to check whether the given device or machine is
> compatible with the given compat string which helps in making
> decisions in drivers based on device or machine compatible.
>
> Idea taken from Linux.
>
> Signed-off-by: Mugunthan V N <mugunthanvnm@ti.com>
> ---
> drivers/core/device.c | 27 +++++++++++++++++++++++++++
> include/dm/device.h | 23 +++++++++++++++++++++++
> 2 files changed, 50 insertions(+)
>
> diff --git a/drivers/core/device.c b/drivers/core/device.c
> index cb24a61..445b22e 100644
> --- a/drivers/core/device.c
> +++ b/drivers/core/device.c
> @@ -691,3 +691,30 @@ int device_set_name(struct udevice *dev, const char *name)
>
> return 0;
> }
> +
> +bool of_device_is_compatible(int offset, const char *compat)

struct udevice *dev should be the first parameter. We may one day stop
using offsets and use a pointer, so the less we have to change the
better.

> +{
> + const void *fdt = gd->fdt_blob;
> + const char *str;
> + int str_len;
> + int len = 0;
> +
> + str = fdt_getprop(fdt, 0, "compatible", &str_len);
> + if(!str) {
> + debug("compatible field not found in node(%d)\n", offset);
> + return false;
> + }
> +
> + while (len < str_len) {

Can you use fdt_node_check_compatible() here?

> + if (!strcmp(compat, &str[len]))
> + return true;
> + len += strlen(&str[len]) + 1;
> + }
> +
> + return false;
> +}
> +
> +bool of_machine_is_compatible(const char *compat)
> +{
> + return of_device_is_compatible(0, compat);
> +}
> diff --git a/include/dm/device.h b/include/dm/device.h
> index 1cf8150..9a1b3d0 100644
> --- a/include/dm/device.h
> +++ b/include/dm/device.h
> @@ -510,6 +510,29 @@ bool device_is_last_sibling(struct udevice *dev);
> int device_set_name(struct udevice *dev, const char *name);
>
> /**
> + * of_device_is_compatible() - check if the device is compatible with the compat
> + *
> + * This allows to check whether the device is comaptible with the compat.
> + *
> + * @offset: Offset to the device
> + * @compat: Compatible string which needs to verified in the given
> + * device offset
> + * @return true if OK, false if the compatible is not found
> + */
> +bool of_device_is_compatible(int offset, const char *compat);
> +
> +/**
> + * of_machine_is_compatible() - check if the machine is compatible with
> + * the compat
> + *
> + * This allows to check whether the machine is comaptible with the compat.
> + *
> + * @compat: Compatible string which needs to verified
> + * @return true if OK, false if the compatible is not found
> + */
> +bool of_machine_is_compatible(const char *compat);
> +
> +/**
> * device_is_on_pci_bus - Test if a device is on a PCI bus
> *
> * @dev: device to test
> --
> 2.8.1.101.g72d917a
>

Regards,
Simon