char: xilinx_hwicap: Modernize driver probe
authorRob Herring <robh@kernel.org>
Fri, 6 Oct 2023 21:42:28 +0000 (16:42 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 18 Oct 2023 08:01:34 +0000 (10:01 +0200)
Rework Xilinx hwicap driver probe to use current best practices using
devres APIs, device_get_match_data(), and typed firmware property accessors.

There's no longer any non-DT probing, so CONFIG_OF ifdefs can be dropped.

Signed-off-by: Rob Herring <robh@kernel.org>
Link: https://lore.kernel.org/r/20231006214228.337064-1-robh@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/xilinx_hwicap/xilinx_hwicap.c

index f60bb6151402f015b40b6affd879eb16a34c033d..019cf6079cecd61f96f03603f27aa526e75077ba 100644 (file)
 #include <linux/sysctl.h>
 #include <linux/fs.h>
 #include <linux/cdev.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/slab.h>
 #include <linux/io.h>
 #include <linux/uaccess.h>
 
-#ifdef CONFIG_OF
-/* For open firmware. */
-#include <linux/of_address.h>
-#include <linux/of_device.h>
-#include <linux/of_platform.h>
-#endif
-
 #include "xilinx_hwicap.h"
 #include "buffer_icap.h"
 #include "fifo_icap.h"
@@ -601,14 +596,14 @@ static const struct file_operations hwicap_fops = {
        .llseek = noop_llseek,
 };
 
-static int hwicap_setup(struct device *dev, int id,
-               const struct resource *regs_res,
+static int hwicap_setup(struct platform_device *pdev, int id,
                const struct hwicap_driver_config *config,
                const struct config_registers *config_regs)
 {
        dev_t devt;
        struct hwicap_drvdata *drvdata = NULL;
-       int retval = 0;
+       struct device *dev = &pdev->dev;
+       int retval;
 
        dev_info(dev, "Xilinx icap port driver\n");
 
@@ -636,72 +631,39 @@ static int hwicap_setup(struct device *dev, int id,
 
        devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);
 
-       drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
+       drvdata = devm_kzalloc(dev, sizeof(struct hwicap_drvdata), GFP_KERNEL);
        if (!drvdata) {
                retval = -ENOMEM;
-               goto failed0;
+               goto failed;
        }
        dev_set_drvdata(dev, (void *)drvdata);
 
-       if (!regs_res) {
-               dev_err(dev, "Couldn't get registers resource\n");
-               retval = -EFAULT;
-               goto failed1;
-       }
-
-       drvdata->mem_start = regs_res->start;
-       drvdata->mem_end = regs_res->end;
-       drvdata->mem_size = resource_size(regs_res);
-
-       if (!request_mem_region(drvdata->mem_start,
-                                       drvdata->mem_size, DRIVER_NAME)) {
-               dev_err(dev, "Couldn't lock memory region at %Lx\n",
-                       (unsigned long long) regs_res->start);
-               retval = -EBUSY;
-               goto failed1;
+       drvdata->base_address = devm_platform_ioremap_resource(pdev, 0);
+       if (!drvdata->base_address) {
+               retval = -ENODEV;
+               goto failed;
        }
 
        drvdata->devt = devt;
        drvdata->dev = dev;
-       drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
-       if (!drvdata->base_address) {
-               dev_err(dev, "ioremap() failed\n");
-               retval = -ENOMEM;
-               goto failed2;
-       }
-
        drvdata->config = config;
        drvdata->config_regs = config_regs;
 
        mutex_init(&drvdata->sem);
        drvdata->is_open = 0;
 
-       dev_info(dev, "ioremap %llx to %p with size %llx\n",
-                (unsigned long long) drvdata->mem_start,
-                drvdata->base_address,
-                (unsigned long long) drvdata->mem_size);
-
        cdev_init(&drvdata->cdev, &hwicap_fops);
        drvdata->cdev.owner = THIS_MODULE;
        retval = cdev_add(&drvdata->cdev, devt, 1);
        if (retval) {
                dev_err(dev, "cdev_add() failed\n");
-               goto failed3;
+               goto failed;
        }
 
        device_create(&icap_class, dev, devt, NULL, "%s%d", DRIVER_NAME, id);
        return 0;               /* success */
 
- failed3:
-       iounmap(drvdata->base_address);
-
- failed2:
-       release_mem_region(regs_res->start, drvdata->mem_size);
-
- failed1:
-       kfree(drvdata);
-
- failed0:
+ failed:
        mutex_lock(&icap_sem);
        probed_devices[id] = 0;
        mutex_unlock(&icap_sem);
@@ -723,75 +685,22 @@ static struct hwicap_driver_config fifo_icap_config = {
        .reset = fifo_icap_reset,
 };
 
-#ifdef CONFIG_OF
-static int hwicap_of_probe(struct platform_device *op,
-                                    const struct hwicap_driver_config *config)
-{
-       struct resource res;
-       const unsigned int *id;
-       const char *family;
-       int rc;
-       const struct config_registers *regs;
-
-
-       rc = of_address_to_resource(op->dev.of_node, 0, &res);
-       if (rc) {
-               dev_err(&op->dev, "invalid address\n");
-               return rc;
-       }
-
-       id = of_get_property(op->dev.of_node, "port-number", NULL);
-
-       /* It's most likely that we're using V4, if the family is not
-        * specified
-        */
-       regs = &v4_config_registers;
-       family = of_get_property(op->dev.of_node, "xlnx,family", NULL);
-
-       if (family) {
-               if (!strcmp(family, "virtex2p"))
-                       regs = &v2_config_registers;
-               else if (!strcmp(family, "virtex4"))
-                       regs = &v4_config_registers;
-               else if (!strcmp(family, "virtex5"))
-                       regs = &v5_config_registers;
-               else if (!strcmp(family, "virtex6"))
-                       regs = &v6_config_registers;
-       }
-       return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
-                       regs);
-}
-#else
-static inline int hwicap_of_probe(struct platform_device *op,
-                                 const struct hwicap_driver_config *config)
-{
-       return -EINVAL;
-}
-#endif /* CONFIG_OF */
-
-static const struct of_device_id hwicap_of_match[];
 static int hwicap_drv_probe(struct platform_device *pdev)
 {
-       const struct of_device_id *match;
-       struct resource *res;
        const struct config_registers *regs;
+       const struct hwicap_driver_config *config;
        const char *family;
+       int id = -1;
 
-       match = of_match_device(hwicap_of_match, &pdev->dev);
-       if (match)
-               return hwicap_of_probe(pdev, match->data);
+       config = device_get_match_data(&pdev->dev);
 
-       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       if (!res)
-               return -ENODEV;
+       of_property_read_u32(pdev->dev.of_node, "port-number", &id);
 
        /* It's most likely that we're using V4, if the family is not
         * specified
         */
        regs = &v4_config_registers;
-       family = pdev->dev.platform_data;
-
-       if (family) {
+       if (!of_property_read_string(pdev->dev.of_node, "xlnx,family", &family)) {
                if (!strcmp(family, "virtex2p"))
                        regs = &v2_config_registers;
                else if (!strcmp(family, "virtex4"))
@@ -801,9 +710,7 @@ static int hwicap_drv_probe(struct platform_device *pdev)
                else if (!strcmp(family, "virtex6"))
                        regs = &v6_config_registers;
        }
-
-       return hwicap_setup(&pdev->dev, pdev->id, res,
-                       &buffer_icap_config, regs);
+       return hwicap_setup(pdev, id, config, regs);
 }
 
 static void hwicap_drv_remove(struct platform_device *pdev)
@@ -815,16 +722,12 @@ static void hwicap_drv_remove(struct platform_device *pdev)
 
        device_destroy(&icap_class, drvdata->devt);
        cdev_del(&drvdata->cdev);
-       iounmap(drvdata->base_address);
-       release_mem_region(drvdata->mem_start, drvdata->mem_size);
-       kfree(drvdata);
 
        mutex_lock(&icap_sem);
        probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
        mutex_unlock(&icap_sem);
 }
 
-#ifdef CONFIG_OF
 /* Match table for device tree binding */
 static const struct of_device_id hwicap_of_match[] = {
        { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
@@ -832,9 +735,6 @@ static const struct of_device_id hwicap_of_match[] = {
        {},
 };
 MODULE_DEVICE_TABLE(of, hwicap_of_match);
-#else
-#define hwicap_of_match NULL
-#endif
 
 static struct platform_driver hwicap_platform_driver = {
        .probe = hwicap_drv_probe,