diff mbox series

[1/4] of: add __of_device_is_status() and makes more generic status check

Message ID 871qcttd8v.wl-kuninori.morimoto.gx@renesas.com (mailing list archive)
State Not Applicable, archived
Headers show
Series drivers: clk: renesas: enable all clocks which is assinged to non Linux system | expand

Commit Message

Kuninori Morimoto Nov. 14, 2023, midnight UTC
Linux Kernel has __of_device_is_available() / __of_device_is_fail(),
these are checking if the status was "okay" / "ok" / "fail" / "fail-".

Add more generic __of_device_is_status() function for these.

Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Tested-by: Yusuke Goda <yusuke.goda.sx@renesas.com>
---
 drivers/of/base.c | 53 ++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 21 deletions(-)

Comments

Rob Herring (Arm) Nov. 16, 2023, 7:17 p.m. UTC | #1
On Tue, Nov 14, 2023 at 12:00:49AM +0000, Kuninori Morimoto wrote:
> Linux Kernel has __of_device_is_available() / __of_device_is_fail(),
> these are checking if the status was "okay" / "ok" / "fail" / "fail-".
> 
> Add more generic __of_device_is_status() function for these.
> 
> Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
> Tested-by: Yusuke Goda <yusuke.goda.sx@renesas.com>
> ---
>  drivers/of/base.c | 53 ++++++++++++++++++++++++++++-------------------
>  1 file changed, 32 insertions(+), 21 deletions(-)
> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 8d93cb6ea9cd..d67cb650dcd6 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -415,15 +415,8 @@ int of_machine_is_compatible(const char *compat)
>  }
>  EXPORT_SYMBOL(of_machine_is_compatible);
>  
> -/**
> - *  __of_device_is_available - check if a device is available for use
> - *
> - *  @device: Node to check for availability, with locks already held
> - *
> - *  Return: True if the status property is absent or set to "okay" or "ok",
> - *  false otherwise
> - */
> -static bool __of_device_is_available(const struct device_node *device)
> +static bool __of_device_is_status(const struct device_node *device,
> +				  const char * const*strings, bool default_ret)
>  {
>  	const char *status;
>  	int statlen;
> @@ -433,16 +426,41 @@ static bool __of_device_is_available(const struct device_node *device)
>  
>  	status = __of_get_property(device, "status", &statlen);
>  	if (status == NULL)
> -		return true;
> +		return default_ret;
>  
>  	if (statlen > 0) {
> -		if (!strcmp(status, "okay") || !strcmp(status, "ok"))
> -			return true;
> +		while (*strings) {
> +			unsigned int len = strlen(*strings);
> +
> +			if ((*strings)[len - 1] == '-') {
> +				if (!strncmp(status, *strings, len))
> +					return true;
> +			} else {
> +				if (!strcmp(status, *strings))
> +					return true;
> +			}
> +			strings++;
> +		}
>  	}
>  
>  	return false;
>  }
>  
> +/**
> + *  __of_device_is_available - check if a device is available for use
> + *
> + *  @device: Node to check for availability, with locks already held
> + *
> + *  Return: True if the status property is absent or set to "okay" or "ok",
> + *  false otherwise
> + */
> +static bool __of_device_is_available(const struct device_node *device)
> +{
> +	static const char * const ok[] = {"okay", "ok", NULL};
> +
> +	return __of_device_is_status(device, ok, true);

Available is special compared to any other status check. Rather than 
passing a value to return, I would make this:

return __of_device_is_status(device, ok) || !__of_get_property(device, "status", NULL);

Rob
diff mbox series

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 8d93cb6ea9cd..d67cb650dcd6 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -415,15 +415,8 @@  int of_machine_is_compatible(const char *compat)
 }
 EXPORT_SYMBOL(of_machine_is_compatible);
 
-/**
- *  __of_device_is_available - check if a device is available for use
- *
- *  @device: Node to check for availability, with locks already held
- *
- *  Return: True if the status property is absent or set to "okay" or "ok",
- *  false otherwise
- */
-static bool __of_device_is_available(const struct device_node *device)
+static bool __of_device_is_status(const struct device_node *device,
+				  const char * const*strings, bool default_ret)
 {
 	const char *status;
 	int statlen;
@@ -433,16 +426,41 @@  static bool __of_device_is_available(const struct device_node *device)
 
 	status = __of_get_property(device, "status", &statlen);
 	if (status == NULL)
-		return true;
+		return default_ret;
 
 	if (statlen > 0) {
-		if (!strcmp(status, "okay") || !strcmp(status, "ok"))
-			return true;
+		while (*strings) {
+			unsigned int len = strlen(*strings);
+
+			if ((*strings)[len - 1] == '-') {
+				if (!strncmp(status, *strings, len))
+					return true;
+			} else {
+				if (!strcmp(status, *strings))
+					return true;
+			}
+			strings++;
+		}
 	}
 
 	return false;
 }
 
+/**
+ *  __of_device_is_available - check if a device is available for use
+ *
+ *  @device: Node to check for availability, with locks already held
+ *
+ *  Return: True if the status property is absent or set to "okay" or "ok",
+ *  false otherwise
+ */
+static bool __of_device_is_available(const struct device_node *device)
+{
+	static const char * const ok[] = {"okay", "ok", NULL};
+
+	return __of_device_is_status(device, ok, true);
+}
+
 /**
  *  of_device_is_available - check if a device is available for use
  *
@@ -474,16 +492,9 @@  EXPORT_SYMBOL(of_device_is_available);
  */
 static bool __of_device_is_fail(const struct device_node *device)
 {
-	const char *status;
-
-	if (!device)
-		return false;
-
-	status = __of_get_property(device, "status", NULL);
-	if (status == NULL)
-		return false;
+	static const char * const fail[] = {"fail", "fail-", NULL};
 
-	return !strcmp(status, "fail") || !strncmp(status, "fail-", 5);
+	return __of_device_is_status(device, fail, false);
 }
 
 /**