Skip to content

Commit 6723718

Browse files
robherringgregkh
authored andcommitted
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: 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>
1 parent 066eaa6 commit 6723718

1 file changed

Lines changed: 19 additions & 119 deletions

File tree

drivers/char/xilinx_hwicap/xilinx_hwicap.c

Lines changed: 19 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,13 @@
8484
#include <linux/sysctl.h>
8585
#include <linux/fs.h>
8686
#include <linux/cdev.h>
87+
#include <linux/of.h>
8788
#include <linux/platform_device.h>
89+
#include <linux/property.h>
8890
#include <linux/slab.h>
8991
#include <linux/io.h>
9092
#include <linux/uaccess.h>
9193

92-
#ifdef CONFIG_OF
93-
/* For open firmware. */
94-
#include <linux/of_address.h>
95-
#include <linux/of_device.h>
96-
#include <linux/of_platform.h>
97-
#endif
98-
9994
#include "xilinx_hwicap.h"
10095
#include "buffer_icap.h"
10196
#include "fifo_icap.h"
@@ -601,14 +596,14 @@ static const struct file_operations hwicap_fops = {
601596
.llseek = noop_llseek,
602597
};
603598

604-
static int hwicap_setup(struct device *dev, int id,
605-
const struct resource *regs_res,
599+
static int hwicap_setup(struct platform_device *pdev, int id,
606600
const struct hwicap_driver_config *config,
607601
const struct config_registers *config_regs)
608602
{
609603
dev_t devt;
610604
struct hwicap_drvdata *drvdata = NULL;
611-
int retval = 0;
605+
struct device *dev = &pdev->dev;
606+
int retval;
612607

613608
dev_info(dev, "Xilinx icap port driver\n");
614609

@@ -636,72 +631,39 @@ static int hwicap_setup(struct device *dev, int id,
636631

637632
devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);
638633

639-
drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
634+
drvdata = devm_kzalloc(dev, sizeof(struct hwicap_drvdata), GFP_KERNEL);
640635
if (!drvdata) {
641636
retval = -ENOMEM;
642-
goto failed0;
637+
goto failed;
643638
}
644639
dev_set_drvdata(dev, (void *)drvdata);
645640

646-
if (!regs_res) {
647-
dev_err(dev, "Couldn't get registers resource\n");
648-
retval = -EFAULT;
649-
goto failed1;
650-
}
651-
652-
drvdata->mem_start = regs_res->start;
653-
drvdata->mem_end = regs_res->end;
654-
drvdata->mem_size = resource_size(regs_res);
655-
656-
if (!request_mem_region(drvdata->mem_start,
657-
drvdata->mem_size, DRIVER_NAME)) {
658-
dev_err(dev, "Couldn't lock memory region at %Lx\n",
659-
(unsigned long long) regs_res->start);
660-
retval = -EBUSY;
661-
goto failed1;
641+
drvdata->base_address = devm_platform_ioremap_resource(pdev, 0);
642+
if (!drvdata->base_address) {
643+
retval = -ENODEV;
644+
goto failed;
662645
}
663646

664647
drvdata->devt = devt;
665648
drvdata->dev = dev;
666-
drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
667-
if (!drvdata->base_address) {
668-
dev_err(dev, "ioremap() failed\n");
669-
retval = -ENOMEM;
670-
goto failed2;
671-
}
672-
673649
drvdata->config = config;
674650
drvdata->config_regs = config_regs;
675651

676652
mutex_init(&drvdata->sem);
677653
drvdata->is_open = 0;
678654

679-
dev_info(dev, "ioremap %llx to %p with size %llx\n",
680-
(unsigned long long) drvdata->mem_start,
681-
drvdata->base_address,
682-
(unsigned long long) drvdata->mem_size);
683-
684655
cdev_init(&drvdata->cdev, &hwicap_fops);
685656
drvdata->cdev.owner = THIS_MODULE;
686657
retval = cdev_add(&drvdata->cdev, devt, 1);
687658
if (retval) {
688659
dev_err(dev, "cdev_add() failed\n");
689-
goto failed3;
660+
goto failed;
690661
}
691662

692663
device_create(&icap_class, dev, devt, NULL, "%s%d", DRIVER_NAME, id);
693664
return 0; /* success */
694665

695-
failed3:
696-
iounmap(drvdata->base_address);
697-
698-
failed2:
699-
release_mem_region(regs_res->start, drvdata->mem_size);
700-
701-
failed1:
702-
kfree(drvdata);
703-
704-
failed0:
666+
failed:
705667
mutex_lock(&icap_sem);
706668
probed_devices[id] = 0;
707669
mutex_unlock(&icap_sem);
@@ -723,75 +685,22 @@ static struct hwicap_driver_config fifo_icap_config = {
723685
.reset = fifo_icap_reset,
724686
};
725687

726-
#ifdef CONFIG_OF
727-
static int hwicap_of_probe(struct platform_device *op,
728-
const struct hwicap_driver_config *config)
729-
{
730-
struct resource res;
731-
const unsigned int *id;
732-
const char *family;
733-
int rc;
734-
const struct config_registers *regs;
735-
736-
737-
rc = of_address_to_resource(op->dev.of_node, 0, &res);
738-
if (rc) {
739-
dev_err(&op->dev, "invalid address\n");
740-
return rc;
741-
}
742-
743-
id = of_get_property(op->dev.of_node, "port-number", NULL);
744-
745-
/* It's most likely that we're using V4, if the family is not
746-
* specified
747-
*/
748-
regs = &v4_config_registers;
749-
family = of_get_property(op->dev.of_node, "xlnx,family", NULL);
750-
751-
if (family) {
752-
if (!strcmp(family, "virtex2p"))
753-
regs = &v2_config_registers;
754-
else if (!strcmp(family, "virtex4"))
755-
regs = &v4_config_registers;
756-
else if (!strcmp(family, "virtex5"))
757-
regs = &v5_config_registers;
758-
else if (!strcmp(family, "virtex6"))
759-
regs = &v6_config_registers;
760-
}
761-
return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
762-
regs);
763-
}
764-
#else
765-
static inline int hwicap_of_probe(struct platform_device *op,
766-
const struct hwicap_driver_config *config)
767-
{
768-
return -EINVAL;
769-
}
770-
#endif /* CONFIG_OF */
771-
772-
static const struct of_device_id hwicap_of_match[];
773688
static int hwicap_drv_probe(struct platform_device *pdev)
774689
{
775-
const struct of_device_id *match;
776-
struct resource *res;
777690
const struct config_registers *regs;
691+
const struct hwicap_driver_config *config;
778692
const char *family;
693+
int id = -1;
779694

780-
match = of_match_device(hwicap_of_match, &pdev->dev);
781-
if (match)
782-
return hwicap_of_probe(pdev, match->data);
695+
config = device_get_match_data(&pdev->dev);
783696

784-
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
785-
if (!res)
786-
return -ENODEV;
697+
of_property_read_u32(pdev->dev.of_node, "port-number", &id);
787698

788699
/* It's most likely that we're using V4, if the family is not
789700
* specified
790701
*/
791702
regs = &v4_config_registers;
792-
family = pdev->dev.platform_data;
793-
794-
if (family) {
703+
if (!of_property_read_string(pdev->dev.of_node, "xlnx,family", &family)) {
795704
if (!strcmp(family, "virtex2p"))
796705
regs = &v2_config_registers;
797706
else if (!strcmp(family, "virtex4"))
@@ -801,9 +710,7 @@ static int hwicap_drv_probe(struct platform_device *pdev)
801710
else if (!strcmp(family, "virtex6"))
802711
regs = &v6_config_registers;
803712
}
804-
805-
return hwicap_setup(&pdev->dev, pdev->id, res,
806-
&buffer_icap_config, regs);
713+
return hwicap_setup(pdev, id, config, regs);
807714
}
808715

809716
static void hwicap_drv_remove(struct platform_device *pdev)
@@ -815,26 +722,19 @@ static void hwicap_drv_remove(struct platform_device *pdev)
815722

816723
device_destroy(&icap_class, drvdata->devt);
817724
cdev_del(&drvdata->cdev);
818-
iounmap(drvdata->base_address);
819-
release_mem_region(drvdata->mem_start, drvdata->mem_size);
820-
kfree(drvdata);
821725

822726
mutex_lock(&icap_sem);
823727
probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
824728
mutex_unlock(&icap_sem);
825729
}
826730

827-
#ifdef CONFIG_OF
828731
/* Match table for device tree binding */
829732
static const struct of_device_id hwicap_of_match[] = {
830733
{ .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
831734
{ .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
832735
{},
833736
};
834737
MODULE_DEVICE_TABLE(of, hwicap_of_match);
835-
#else
836-
#define hwicap_of_match NULL
837-
#endif
838738

839739
static struct platform_driver hwicap_platform_driver = {
840740
.probe = hwicap_drv_probe,

0 commit comments

Comments
 (0)