Commit 67237183 authored by Rob Herring's avatar Rob Herring Committed by Greg Kroah-Hartman

char: xilinx_hwicap: Modernize driver probe

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: default avatarRob Herring <robh@kernel.org>
Link: https://lore.kernel.org/r/20231006214228.337064-1-robh@kernel.orgSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 066eaa69
...@@ -84,18 +84,13 @@ ...@@ -84,18 +84,13 @@
#include <linux/sysctl.h> #include <linux/sysctl.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/cdev.h> #include <linux/cdev.h>
#include <linux/of.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/uaccess.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 "xilinx_hwicap.h"
#include "buffer_icap.h" #include "buffer_icap.h"
#include "fifo_icap.h" #include "fifo_icap.h"
...@@ -601,14 +596,14 @@ static const struct file_operations hwicap_fops = { ...@@ -601,14 +596,14 @@ static const struct file_operations hwicap_fops = {
.llseek = noop_llseek, .llseek = noop_llseek,
}; };
static int hwicap_setup(struct device *dev, int id, static int hwicap_setup(struct platform_device *pdev, int id,
const struct resource *regs_res,
const struct hwicap_driver_config *config, const struct hwicap_driver_config *config,
const struct config_registers *config_regs) const struct config_registers *config_regs)
{ {
dev_t devt; dev_t devt;
struct hwicap_drvdata *drvdata = NULL; struct hwicap_drvdata *drvdata = NULL;
int retval = 0; struct device *dev = &pdev->dev;
int retval;
dev_info(dev, "Xilinx icap port driver\n"); dev_info(dev, "Xilinx icap port driver\n");
...@@ -636,72 +631,39 @@ static int hwicap_setup(struct device *dev, int id, ...@@ -636,72 +631,39 @@ static int hwicap_setup(struct device *dev, int id,
devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + 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) { if (!drvdata) {
retval = -ENOMEM; retval = -ENOMEM;
goto failed0; goto failed;
} }
dev_set_drvdata(dev, (void *)drvdata); dev_set_drvdata(dev, (void *)drvdata);
if (!regs_res) { drvdata->base_address = devm_platform_ioremap_resource(pdev, 0);
dev_err(dev, "Couldn't get registers resource\n"); if (!drvdata->base_address) {
retval = -EFAULT; retval = -ENODEV;
goto failed1; goto failed;
}
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->devt = devt; drvdata->devt = devt;
drvdata->dev = dev; 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 = config;
drvdata->config_regs = config_regs; drvdata->config_regs = config_regs;
mutex_init(&drvdata->sem); mutex_init(&drvdata->sem);
drvdata->is_open = 0; 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); cdev_init(&drvdata->cdev, &hwicap_fops);
drvdata->cdev.owner = THIS_MODULE; drvdata->cdev.owner = THIS_MODULE;
retval = cdev_add(&drvdata->cdev, devt, 1); retval = cdev_add(&drvdata->cdev, devt, 1);
if (retval) { if (retval) {
dev_err(dev, "cdev_add() failed\n"); dev_err(dev, "cdev_add() failed\n");
goto failed3; goto failed;
} }
device_create(&icap_class, dev, devt, NULL, "%s%d", DRIVER_NAME, id); device_create(&icap_class, dev, devt, NULL, "%s%d", DRIVER_NAME, id);
return 0; /* success */ return 0; /* success */
failed3: failed:
iounmap(drvdata->base_address);
failed2:
release_mem_region(regs_res->start, drvdata->mem_size);
failed1:
kfree(drvdata);
failed0:
mutex_lock(&icap_sem); mutex_lock(&icap_sem);
probed_devices[id] = 0; probed_devices[id] = 0;
mutex_unlock(&icap_sem); mutex_unlock(&icap_sem);
...@@ -723,75 +685,22 @@ static struct hwicap_driver_config fifo_icap_config = { ...@@ -723,75 +685,22 @@ static struct hwicap_driver_config fifo_icap_config = {
.reset = fifo_icap_reset, .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) 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 config_registers *regs;
const struct hwicap_driver_config *config;
const char *family; const char *family;
int id = -1;
match = of_match_device(hwicap_of_match, &pdev->dev); config = device_get_match_data(&pdev->dev);
if (match)
return hwicap_of_probe(pdev, match->data);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); of_property_read_u32(pdev->dev.of_node, "port-number", &id);
if (!res)
return -ENODEV;
/* It's most likely that we're using V4, if the family is not /* It's most likely that we're using V4, if the family is not
* specified * specified
*/ */
regs = &v4_config_registers; regs = &v4_config_registers;
family = pdev->dev.platform_data; if (!of_property_read_string(pdev->dev.of_node, "xlnx,family", &family)) {
if (family) {
if (!strcmp(family, "virtex2p")) if (!strcmp(family, "virtex2p"))
regs = &v2_config_registers; regs = &v2_config_registers;
else if (!strcmp(family, "virtex4")) else if (!strcmp(family, "virtex4"))
...@@ -801,9 +710,7 @@ static int hwicap_drv_probe(struct platform_device *pdev) ...@@ -801,9 +710,7 @@ static int hwicap_drv_probe(struct platform_device *pdev)
else if (!strcmp(family, "virtex6")) else if (!strcmp(family, "virtex6"))
regs = &v6_config_registers; regs = &v6_config_registers;
} }
return hwicap_setup(pdev, id, config, regs);
return hwicap_setup(&pdev->dev, pdev->id, res,
&buffer_icap_config, regs);
} }
static void hwicap_drv_remove(struct platform_device *pdev) static void hwicap_drv_remove(struct platform_device *pdev)
...@@ -815,16 +722,12 @@ 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); device_destroy(&icap_class, drvdata->devt);
cdev_del(&drvdata->cdev); cdev_del(&drvdata->cdev);
iounmap(drvdata->base_address);
release_mem_region(drvdata->mem_start, drvdata->mem_size);
kfree(drvdata);
mutex_lock(&icap_sem); mutex_lock(&icap_sem);
probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0; probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
mutex_unlock(&icap_sem); mutex_unlock(&icap_sem);
} }
#ifdef CONFIG_OF
/* Match table for device tree binding */ /* Match table for device tree binding */
static const struct of_device_id hwicap_of_match[] = { static const struct of_device_id hwicap_of_match[] = {
{ .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config}, { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
...@@ -832,9 +735,6 @@ static const struct of_device_id hwicap_of_match[] = { ...@@ -832,9 +735,6 @@ static const struct of_device_id hwicap_of_match[] = {
{}, {},
}; };
MODULE_DEVICE_TABLE(of, hwicap_of_match); MODULE_DEVICE_TABLE(of, hwicap_of_match);
#else
#define hwicap_of_match NULL
#endif
static struct platform_driver hwicap_platform_driver = { static struct platform_driver hwicap_platform_driver = {
.probe = hwicap_drv_probe, .probe = hwicap_drv_probe,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment