platform.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Platform driver for the Synopsys DesignWare DMA Controller
  4. *
  5. * Copyright (C) 2007-2008 Atmel Corporation
  6. * Copyright (C) 2010-2011 ST Microelectronics
  7. * Copyright (C) 2013 Intel Corporation
  8. *
  9. * Some parts of this driver are derived from the original dw_dmac.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/clk.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dmaengine.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/of.h>
  19. #include <linux/acpi.h>
  20. #include "internal.h"
  21. #define DRV_NAME "dw_dmac"
  22. static int dw_probe(struct platform_device *pdev)
  23. {
  24. const struct dw_dma_chip_pdata *match;
  25. struct dw_dma_chip_pdata *data;
  26. struct dw_dma_chip *chip;
  27. struct device *dev = &pdev->dev;
  28. int err;
  29. match = device_get_match_data(dev);
  30. if (!match)
  31. return -ENODEV;
  32. data = devm_kmemdup(&pdev->dev, match, sizeof(*match), GFP_KERNEL);
  33. if (!data)
  34. return -ENOMEM;
  35. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  36. if (!chip)
  37. return -ENOMEM;
  38. chip->irq = platform_get_irq(pdev, 0);
  39. if (chip->irq < 0)
  40. return chip->irq;
  41. chip->regs = devm_platform_ioremap_resource(pdev, 0);
  42. if (IS_ERR(chip->regs))
  43. return PTR_ERR(chip->regs);
  44. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  45. if (err)
  46. return err;
  47. if (!data->pdata)
  48. data->pdata = dev_get_platdata(dev);
  49. if (!data->pdata)
  50. data->pdata = dw_dma_parse_dt(pdev);
  51. chip->dev = dev;
  52. chip->id = pdev->id;
  53. chip->pdata = data->pdata;
  54. data->chip = chip;
  55. chip->clk = devm_clk_get_optional(chip->dev, "hclk");
  56. if (IS_ERR(chip->clk))
  57. return PTR_ERR(chip->clk);
  58. err = clk_prepare_enable(chip->clk);
  59. if (err)
  60. return err;
  61. pm_runtime_enable(&pdev->dev);
  62. err = data->probe(chip);
  63. if (err)
  64. goto err_dw_dma_probe;
  65. platform_set_drvdata(pdev, data);
  66. dw_dma_of_controller_register(chip->dw);
  67. dw_dma_acpi_controller_register(chip->dw);
  68. return 0;
  69. err_dw_dma_probe:
  70. pm_runtime_disable(&pdev->dev);
  71. clk_disable_unprepare(chip->clk);
  72. return err;
  73. }
  74. static int dw_remove(struct platform_device *pdev)
  75. {
  76. struct dw_dma_chip_pdata *data = platform_get_drvdata(pdev);
  77. struct dw_dma_chip *chip = data->chip;
  78. int ret;
  79. dw_dma_acpi_controller_free(chip->dw);
  80. dw_dma_of_controller_free(chip->dw);
  81. ret = data->remove(chip);
  82. if (ret)
  83. dev_warn(chip->dev, "can't remove device properly: %d\n", ret);
  84. pm_runtime_disable(&pdev->dev);
  85. clk_disable_unprepare(chip->clk);
  86. return 0;
  87. }
  88. static void dw_shutdown(struct platform_device *pdev)
  89. {
  90. struct dw_dma_chip_pdata *data = platform_get_drvdata(pdev);
  91. struct dw_dma_chip *chip = data->chip;
  92. /*
  93. * We have to call do_dw_dma_disable() to stop any ongoing transfer. On
  94. * some platforms we can't do that since DMA device is powered off.
  95. * Moreover we have no possibility to check if the platform is affected
  96. * or not. That's why we call pm_runtime_get_sync() / pm_runtime_put()
  97. * unconditionally. On the other hand we can't use
  98. * pm_runtime_suspended() because runtime PM framework is not fully
  99. * used by the driver.
  100. */
  101. pm_runtime_get_sync(chip->dev);
  102. do_dw_dma_disable(chip);
  103. pm_runtime_put_sync_suspend(chip->dev);
  104. clk_disable_unprepare(chip->clk);
  105. }
  106. #ifdef CONFIG_OF
  107. static const struct of_device_id dw_dma_of_id_table[] = {
  108. { .compatible = "snps,dma-spear1340", .data = &dw_dma_chip_pdata },
  109. {}
  110. };
  111. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  112. #endif
  113. #ifdef CONFIG_ACPI
  114. static const struct acpi_device_id dw_dma_acpi_id_table[] = {
  115. { "INTL9C60", (kernel_ulong_t)&dw_dma_chip_pdata },
  116. { "80862286", (kernel_ulong_t)&dw_dma_chip_pdata },
  117. { "808622C0", (kernel_ulong_t)&dw_dma_chip_pdata },
  118. /* Elkhart Lake iDMA 32-bit (PSE DMA) */
  119. { "80864BB4", (kernel_ulong_t)&idma32_chip_pdata },
  120. { "80864BB5", (kernel_ulong_t)&idma32_chip_pdata },
  121. { "80864BB6", (kernel_ulong_t)&idma32_chip_pdata },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
  125. #endif
  126. #ifdef CONFIG_PM_SLEEP
  127. static int dw_suspend_late(struct device *dev)
  128. {
  129. struct dw_dma_chip_pdata *data = dev_get_drvdata(dev);
  130. struct dw_dma_chip *chip = data->chip;
  131. do_dw_dma_disable(chip);
  132. clk_disable_unprepare(chip->clk);
  133. return 0;
  134. }
  135. static int dw_resume_early(struct device *dev)
  136. {
  137. struct dw_dma_chip_pdata *data = dev_get_drvdata(dev);
  138. struct dw_dma_chip *chip = data->chip;
  139. int ret;
  140. ret = clk_prepare_enable(chip->clk);
  141. if (ret)
  142. return ret;
  143. return do_dw_dma_enable(chip);
  144. }
  145. #endif /* CONFIG_PM_SLEEP */
  146. static const struct dev_pm_ops dw_dev_pm_ops = {
  147. SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
  148. };
  149. static struct platform_driver dw_driver = {
  150. .probe = dw_probe,
  151. .remove = dw_remove,
  152. .shutdown = dw_shutdown,
  153. .driver = {
  154. .name = DRV_NAME,
  155. .pm = &dw_dev_pm_ops,
  156. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  157. .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
  158. },
  159. };
  160. static int __init dw_init(void)
  161. {
  162. return platform_driver_register(&dw_driver);
  163. }
  164. subsys_initcall(dw_init);
  165. static void __exit dw_exit(void)
  166. {
  167. platform_driver_unregister(&dw_driver);
  168. }
  169. module_exit(dw_exit);
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");
  172. MODULE_ALIAS("platform:" DRV_NAME);