ctucanfd_platform.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. *
  4. * CTU CAN FD IP Core
  5. *
  6. * Copyright (C) 2015-2018 Ondrej Ille <ondrej.ille@gmail.com> FEE CTU
  7. * Copyright (C) 2018-2021 Ondrej Ille <ondrej.ille@gmail.com> self-funded
  8. * Copyright (C) 2018-2019 Martin Jerabek <martin.jerabek01@gmail.com> FEE CTU
  9. * Copyright (C) 2018-2022 Pavel Pisa <pisa@cmp.felk.cvut.cz> FEE CTU/self-funded
  10. *
  11. * Project advisors:
  12. * Jiri Novak <jnovak@fel.cvut.cz>
  13. * Pavel Pisa <pisa@cmp.felk.cvut.cz>
  14. *
  15. * Department of Measurement (http://meas.fel.cvut.cz/)
  16. * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
  17. * Czech Technical University (http://www.cvut.cz/)
  18. ******************************************************************************/
  19. #include <linux/module.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include "ctucanfd.h"
  25. #define DRV_NAME "ctucanfd"
  26. static void ctucan_platform_set_drvdata(struct device *dev,
  27. struct net_device *ndev)
  28. {
  29. struct platform_device *pdev = container_of(dev, struct platform_device,
  30. dev);
  31. platform_set_drvdata(pdev, ndev);
  32. }
  33. /**
  34. * ctucan_platform_probe - Platform registration call
  35. * @pdev: Handle to the platform device structure
  36. *
  37. * This function does all the memory allocation and registration for the CAN
  38. * device.
  39. *
  40. * Return: 0 on success and failure value on error
  41. */
  42. static int ctucan_platform_probe(struct platform_device *pdev)
  43. {
  44. struct device *dev = &pdev->dev;
  45. void __iomem *addr;
  46. int ret;
  47. unsigned int ntxbufs;
  48. int irq;
  49. /* Get the virtual base address for the device */
  50. addr = devm_platform_ioremap_resource(pdev, 0);
  51. if (IS_ERR(addr)) {
  52. ret = PTR_ERR(addr);
  53. goto err;
  54. }
  55. irq = platform_get_irq(pdev, 0);
  56. if (irq < 0) {
  57. ret = irq;
  58. goto err;
  59. }
  60. /* Number of tx bufs might be change in HW for future. If so,
  61. * it will be passed as property via device tree
  62. */
  63. ntxbufs = 4;
  64. ret = ctucan_probe_common(dev, addr, irq, ntxbufs, 0,
  65. 1, ctucan_platform_set_drvdata);
  66. if (ret < 0)
  67. platform_set_drvdata(pdev, NULL);
  68. err:
  69. return ret;
  70. }
  71. /**
  72. * ctucan_platform_remove - Unregister the device after releasing the resources
  73. * @pdev: Handle to the platform device structure
  74. *
  75. * This function frees all the resources allocated to the device.
  76. * Return: 0 always
  77. */
  78. static void ctucan_platform_remove(struct platform_device *pdev)
  79. {
  80. struct net_device *ndev = platform_get_drvdata(pdev);
  81. struct ctucan_priv *priv = netdev_priv(ndev);
  82. netdev_dbg(ndev, "ctucan_remove");
  83. unregister_candev(ndev);
  84. pm_runtime_disable(&pdev->dev);
  85. netif_napi_del(&priv->napi);
  86. free_candev(ndev);
  87. }
  88. static SIMPLE_DEV_PM_OPS(ctucan_platform_pm_ops, ctucan_suspend, ctucan_resume);
  89. /* Match table for OF platform binding */
  90. static const struct of_device_id ctucan_of_match[] = {
  91. { .compatible = "ctu,ctucanfd-2", },
  92. { .compatible = "ctu,ctucanfd", },
  93. { /* end of list */ },
  94. };
  95. MODULE_DEVICE_TABLE(of, ctucan_of_match);
  96. static struct platform_driver ctucanfd_driver = {
  97. .probe = ctucan_platform_probe,
  98. .remove = ctucan_platform_remove,
  99. .driver = {
  100. .name = DRV_NAME,
  101. .pm = &ctucan_platform_pm_ops,
  102. .of_match_table = ctucan_of_match,
  103. },
  104. };
  105. module_platform_driver(ctucanfd_driver);
  106. MODULE_LICENSE("GPL");
  107. MODULE_AUTHOR("Martin Jerabek");
  108. MODULE_DESCRIPTION("CTU CAN FD for platform");