of: Add __of_device_is_status() and makes more generic status check
authorKuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Wed, 10 Jan 2024 01:14:34 +0000 (01:14 +0000)
committerGeert Uytterhoeven <geert+renesas@glider.be>
Tue, 23 Jan 2024 08:38:16 +0000 (09:38 +0100)
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>
Reviewed-by: Rob Herring <robh@kernel.org>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Link: https://lore.kernel.org/r/87cyuagfba.wl-kuninori.morimoto.gx@renesas.com
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
drivers/of/base.c

index b0ad8fc06e80e099ab6eba7ebe10039875bc85c4..afb54bf5c887acdcd9f9786f0defa62b7f1c46fa 100644 (file)
@@ -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)
 {
        const char *status;
        int statlen;
@@ -433,16 +426,45 @@ static bool __of_device_is_available(const struct device_node *device)
 
        status = __of_get_property(device, "status", &statlen);
        if (status == NULL)
-               return true;
+               return false;
 
        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};
+
+       if (!device)
+               return false;
+
+       return !__of_get_property(device, "status", NULL) ||
+               __of_device_is_status(device, ok);
+}
+
 /**
  *  of_device_is_available - check if a device is available for use
  *
@@ -474,16 +496,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);
 }
 
 /**