firmware: arm_scpi: improve handling of protocol and firmware version subfields
authorHeiner Kallweit <hkallweit1@gmail.com>
Tue, 5 Dec 2017 22:16:55 +0000 (23:16 +0100)
committerSudeep Holla <sudeep.holla@arm.com>
Fri, 23 Feb 2018 15:12:41 +0000 (15:12 +0000)
By using FIELD_GET and proper masks we can avoid quite some shifting
and masking macro magic and make the code better readable.

Tested-by: Kevin Hilman <khilman@baylibre.com>
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
drivers/firmware/arm_scpi.c

index 9eeb53b766e052197870be14cafa286bcaa8ec06..63441e403f60e2b4545f823016d6edacacd19d32 100644 (file)
@@ -28,6 +28,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/bitmap.h>
+#include <linux/bitfield.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/export.h>
 #define MAX_DVFS_DOMAINS       8
 #define MAX_DVFS_OPPS          16
 
-#define PROTOCOL_REV_MINOR_BITS        16
-#define PROTOCOL_REV_MINOR_MASK        ((1U << PROTOCOL_REV_MINOR_BITS) - 1)
-#define PROTOCOL_REV_MAJOR(x)  ((x) >> PROTOCOL_REV_MINOR_BITS)
-#define PROTOCOL_REV_MINOR(x)  ((x) & PROTOCOL_REV_MINOR_MASK)
+#define PROTO_REV_MAJOR_MASK   GENMASK(31, 16)
+#define PROTO_REV_MINOR_MASK   GENMASK(15, 0)
 
-#define FW_REV_MAJOR_BITS      24
-#define FW_REV_MINOR_BITS      16
-#define FW_REV_PATCH_MASK      ((1U << FW_REV_MINOR_BITS) - 1)
-#define FW_REV_MINOR_MASK      ((1U << FW_REV_MAJOR_BITS) - 1)
-#define FW_REV_MAJOR(x)                ((x) >> FW_REV_MAJOR_BITS)
-#define FW_REV_MINOR(x)                (((x) & FW_REV_MINOR_MASK) >> FW_REV_MINOR_BITS)
-#define FW_REV_PATCH(x)                ((x) & FW_REV_PATCH_MASK)
+#define FW_REV_MAJOR_MASK      GENMASK(31, 24)
+#define FW_REV_MINOR_MASK      GENMASK(23, 16)
+#define FW_REV_PATCH_MASK      GENMASK(15, 0)
 
 #define MAX_RX_TIMEOUT         (msecs_to_jiffies(30))
 
@@ -861,9 +856,9 @@ static ssize_t protocol_version_show(struct device *dev,
 {
        struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
 
-       return sprintf(buf, "%d.%d\n",
-                      PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
-                      PROTOCOL_REV_MINOR(scpi_info->protocol_version));
+       return sprintf(buf, "%lu.%lu\n",
+               FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version),
+               FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version));
 }
 static DEVICE_ATTR_RO(protocol_version);
 
@@ -872,10 +867,10 @@ static ssize_t firmware_version_show(struct device *dev,
 {
        struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
 
-       return sprintf(buf, "%d.%d.%d\n",
-                      FW_REV_MAJOR(scpi_info->firmware_version),
-                      FW_REV_MINOR(scpi_info->firmware_version),
-                      FW_REV_PATCH(scpi_info->firmware_version));
+       return sprintf(buf, "%lu.%lu.%lu\n",
+               FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version),
+               FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version),
+               FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version));
 }
 static DEVICE_ATTR_RO(firmware_version);
 
@@ -1031,12 +1026,12 @@ static int scpi_probe(struct platform_device *pdev)
                return ret;
        }
 
-       _dev_info(dev, "SCP Protocol %d.%d Firmware %d.%d.%d version\n",
-                 PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
-                 PROTOCOL_REV_MINOR(scpi_info->protocol_version),
-                 FW_REV_MAJOR(scpi_info->firmware_version),
-                 FW_REV_MINOR(scpi_info->firmware_version),
-                 FW_REV_PATCH(scpi_info->firmware_version));
+       dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n",
+                FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version),
+                FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version),
+                FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version),
+                FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version),
+                FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version));
        scpi_info->scpi_ops = &scpi_ops;
 
        ret = devm_device_add_groups(dev, versions_groups);