Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*******************************************************************************
0003  *
0004  * CTU CAN FD IP Core
0005  *
0006  * Copyright (C) 2015-2018 Ondrej Ille <ondrej.ille@gmail.com> FEE CTU
0007  * Copyright (C) 2018-2021 Ondrej Ille <ondrej.ille@gmail.com> self-funded
0008  * Copyright (C) 2018-2019 Martin Jerabek <martin.jerabek01@gmail.com> FEE CTU
0009  * Copyright (C) 2018-2022 Pavel Pisa <pisa@cmp.felk.cvut.cz> FEE CTU/self-funded
0010  *
0011  * Project advisors:
0012  *     Jiri Novak <jnovak@fel.cvut.cz>
0013  *     Pavel Pisa <pisa@cmp.felk.cvut.cz>
0014  *
0015  * Department of Measurement         (http://meas.fel.cvut.cz/)
0016  * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
0017  * Czech Technical University        (http://www.cvut.cz/)
0018  ******************************************************************************/
0019 
0020 #include <linux/module.h>
0021 #include <linux/netdevice.h>
0022 #include <linux/of.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/pm_runtime.h>
0025 
0026 #include "ctucanfd.h"
0027 
0028 #define DRV_NAME    "ctucanfd"
0029 
0030 static void ctucan_platform_set_drvdata(struct device *dev,
0031                     struct net_device *ndev)
0032 {
0033     struct platform_device *pdev = container_of(dev, struct platform_device,
0034                             dev);
0035 
0036     platform_set_drvdata(pdev, ndev);
0037 }
0038 
0039 /**
0040  * ctucan_platform_probe - Platform registration call
0041  * @pdev:   Handle to the platform device structure
0042  *
0043  * This function does all the memory allocation and registration for the CAN
0044  * device.
0045  *
0046  * Return: 0 on success and failure value on error
0047  */
0048 static int ctucan_platform_probe(struct platform_device *pdev)
0049 {
0050     struct resource *res; /* IO mem resources */
0051     struct device   *dev = &pdev->dev;
0052     void __iomem *addr;
0053     int ret;
0054     unsigned int ntxbufs;
0055     int irq;
0056 
0057     /* Get the virtual base address for the device */
0058     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0059     addr = devm_ioremap_resource(dev, res);
0060     if (IS_ERR(addr)) {
0061         dev_err(dev, "Cannot remap address.\n");
0062         ret = PTR_ERR(addr);
0063         goto err;
0064     }
0065     irq = platform_get_irq(pdev, 0);
0066     if (irq < 0) {
0067         ret = irq;
0068         goto err;
0069     }
0070 
0071     /* Number of tx bufs might be change in HW for future. If so,
0072      * it will be passed as property via device tree
0073      */
0074     ntxbufs = 4;
0075     ret = ctucan_probe_common(dev, addr, irq, ntxbufs, 0,
0076                   1, ctucan_platform_set_drvdata);
0077 
0078     if (ret < 0)
0079         platform_set_drvdata(pdev, NULL);
0080 
0081 err:
0082     return ret;
0083 }
0084 
0085 /**
0086  * ctucan_platform_remove - Unregister the device after releasing the resources
0087  * @pdev:   Handle to the platform device structure
0088  *
0089  * This function frees all the resources allocated to the device.
0090  * Return: 0 always
0091  */
0092 static int ctucan_platform_remove(struct platform_device *pdev)
0093 {
0094     struct net_device *ndev = platform_get_drvdata(pdev);
0095     struct ctucan_priv *priv = netdev_priv(ndev);
0096 
0097     netdev_dbg(ndev, "ctucan_remove");
0098 
0099     unregister_candev(ndev);
0100     pm_runtime_disable(&pdev->dev);
0101     netif_napi_del(&priv->napi);
0102     free_candev(ndev);
0103 
0104     return 0;
0105 }
0106 
0107 static SIMPLE_DEV_PM_OPS(ctucan_platform_pm_ops, ctucan_suspend, ctucan_resume);
0108 
0109 /* Match table for OF platform binding */
0110 static const struct of_device_id ctucan_of_match[] = {
0111     { .compatible = "ctu,ctucanfd-2", },
0112     { .compatible = "ctu,ctucanfd", },
0113     { /* end of list */ },
0114 };
0115 MODULE_DEVICE_TABLE(of, ctucan_of_match);
0116 
0117 static struct platform_driver ctucanfd_driver = {
0118     .probe  = ctucan_platform_probe,
0119     .remove = ctucan_platform_remove,
0120     .driver = {
0121         .name = DRV_NAME,
0122         .pm = &ctucan_platform_pm_ops,
0123         .of_match_table = ctucan_of_match,
0124     },
0125 };
0126 
0127 module_platform_driver(ctucanfd_driver);
0128 
0129 MODULE_LICENSE("GPL");
0130 MODULE_AUTHOR("Martin Jerabek");
0131 MODULE_DESCRIPTION("CTU CAN FD for platform");