|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Xilinx SDFEC |
| 4 | + * |
| 5 | + * Copyright (C) 2019 Xilinx, Inc. |
| 6 | + * |
| 7 | + * Description: |
| 8 | + * This driver is developed for SDFEC16 (Soft Decision FEC 16nm) |
| 9 | + * IP. It exposes a char device which supports file operations |
| 10 | + * like open(), close() and ioctl(). |
| 11 | + */ |
| 12 | + |
| 13 | +#include <linux/miscdevice.h> |
| 14 | +#include <linux/io.h> |
| 15 | +#include <linux/interrupt.h> |
| 16 | +#include <linux/kernel.h> |
| 17 | +#include <linux/module.h> |
| 18 | +#include <linux/of_platform.h> |
| 19 | +#include <linux/poll.h> |
| 20 | +#include <linux/slab.h> |
| 21 | +#include <linux/clk.h> |
| 22 | + |
| 23 | +#define DEV_NAME_LEN 12 |
| 24 | + |
| 25 | +static struct idr dev_idr; |
| 26 | +static struct mutex dev_idr_lock; |
| 27 | + |
| 28 | +/** |
| 29 | + * struct xsdfec_dev - Driver data for SDFEC |
| 30 | + * @regs: device physical base address |
| 31 | + * @dev: pointer to device struct |
| 32 | + * @miscdev: Misc device handle |
| 33 | + * @error_data_lock: Error counter and states spinlock |
| 34 | + * @dev_name: Device name |
| 35 | + * @dev_id: Device ID |
| 36 | + * |
| 37 | + * This structure contains necessary state for SDFEC driver to operate |
| 38 | + */ |
| 39 | +struct xsdfec_dev { |
| 40 | + void __iomem *regs; |
| 41 | + struct device *dev; |
| 42 | + struct miscdevice miscdev; |
| 43 | + /* Spinlock to protect state_updated and stats_updated */ |
| 44 | + spinlock_t error_data_lock; |
| 45 | + char dev_name[DEV_NAME_LEN]; |
| 46 | + int dev_id; |
| 47 | +}; |
| 48 | + |
| 49 | +static const struct file_operations xsdfec_fops = { |
| 50 | + .owner = THIS_MODULE, |
| 51 | +}; |
| 52 | + |
| 53 | +static void xsdfec_idr_remove(struct xsdfec_dev *xsdfec) |
| 54 | +{ |
| 55 | + mutex_lock(&dev_idr_lock); |
| 56 | + idr_remove(&dev_idr, xsdfec->dev_id); |
| 57 | + mutex_unlock(&dev_idr_lock); |
| 58 | +} |
| 59 | + |
| 60 | +static int xsdfec_probe(struct platform_device *pdev) |
| 61 | +{ |
| 62 | + struct xsdfec_dev *xsdfec; |
| 63 | + struct device *dev; |
| 64 | + struct resource *res; |
| 65 | + int err; |
| 66 | + |
| 67 | + xsdfec = devm_kzalloc(&pdev->dev, sizeof(*xsdfec), GFP_KERNEL); |
| 68 | + if (!xsdfec) |
| 69 | + return -ENOMEM; |
| 70 | + |
| 71 | + xsdfec->dev = &pdev->dev; |
| 72 | + spin_lock_init(&xsdfec->error_data_lock); |
| 73 | + |
| 74 | + dev = xsdfec->dev; |
| 75 | + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 76 | + xsdfec->regs = devm_ioremap_resource(dev, res); |
| 77 | + if (IS_ERR(xsdfec->regs)) { |
| 78 | + err = PTR_ERR(xsdfec->regs); |
| 79 | + return err; |
| 80 | + } |
| 81 | + |
| 82 | + /* Save driver private data */ |
| 83 | + platform_set_drvdata(pdev, xsdfec); |
| 84 | + |
| 85 | + mutex_lock(&dev_idr_lock); |
| 86 | + err = idr_alloc(&dev_idr, xsdfec->dev_name, 0, 0, GFP_KERNEL); |
| 87 | + mutex_unlock(&dev_idr_lock); |
| 88 | + if (err < 0) |
| 89 | + goto err_xsddev_idr; |
| 90 | + xsdfec->dev_id = err; |
| 91 | + |
| 92 | + snprintf(xsdfec->dev_name, DEV_NAME_LEN, "xsdfec%d", xsdfec->dev_id); |
| 93 | + xsdfec->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 94 | + xsdfec->miscdev.name = xsdfec->dev_name; |
| 95 | + xsdfec->miscdev.fops = &xsdfec_fops; |
| 96 | + xsdfec->miscdev.parent = dev; |
| 97 | + err = misc_register(&xsdfec->miscdev); |
| 98 | + if (err) { |
| 99 | + dev_err(dev, "error:%d. Unable to register device", err); |
| 100 | + return err; |
| 101 | + } |
| 102 | + return 0; |
| 103 | + |
| 104 | +err_xsddev_idr: |
| 105 | + xsdfec_idr_remove(xsdfec); |
| 106 | + |
| 107 | + return err; |
| 108 | +} |
| 109 | + |
| 110 | +static int xsdfec_remove(struct platform_device *pdev) |
| 111 | +{ |
| 112 | + struct xsdfec_dev *xsdfec; |
| 113 | + |
| 114 | + xsdfec = platform_get_drvdata(pdev); |
| 115 | + misc_deregister(&xsdfec->miscdev); |
| 116 | + xsdfec_idr_remove(xsdfec); |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
| 120 | +static const struct of_device_id xsdfec_of_match[] = { |
| 121 | + { |
| 122 | + .compatible = "xlnx,sd-fec-1.1", |
| 123 | + }, |
| 124 | + { /* end of table */ } |
| 125 | +}; |
| 126 | +MODULE_DEVICE_TABLE(of, xsdfec_of_match); |
| 127 | + |
| 128 | +static struct platform_driver xsdfec_driver = { |
| 129 | + .driver = { |
| 130 | + .name = "xilinx-sdfec", |
| 131 | + .of_match_table = xsdfec_of_match, |
| 132 | + }, |
| 133 | + .probe = xsdfec_probe, |
| 134 | + .remove = xsdfec_remove, |
| 135 | +}; |
| 136 | + |
| 137 | +static int __init xsdfec_init(void) |
| 138 | +{ |
| 139 | + int err; |
| 140 | + |
| 141 | + mutex_init(&dev_idr_lock); |
| 142 | + idr_init(&dev_idr); |
| 143 | + err = platform_driver_register(&xsdfec_driver); |
| 144 | + if (err < 0) { |
| 145 | + pr_err("%s Unabled to register SDFEC driver", __func__); |
| 146 | + return err; |
| 147 | + } |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +static void __exit xsdfec_exit(void) |
| 152 | +{ |
| 153 | + platform_driver_unregister(&xsdfec_driver); |
| 154 | + idr_destroy(&dev_idr); |
| 155 | +} |
| 156 | + |
| 157 | +module_init(xsdfec_init); |
| 158 | +module_exit(xsdfec_exit); |
| 159 | + |
| 160 | +MODULE_AUTHOR("Xilinx, Inc"); |
| 161 | +MODULE_DESCRIPTION("Xilinx SD-FEC16 Driver"); |
| 162 | +MODULE_LICENSE("GPL"); |
0 commit comments