spi-dw-mmio-quad.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Memory-mapped interface driver for DW ehance-spi core
  4. *
  5. * Copyright (c) 2021, ailibaba-inc corperation.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/acpi.h>
  19. #include <linux/property.h>
  20. #include <linux/regmap.h>
  21. #include "spi-dw-quad.h"
  22. #define DRIVER_NAME "dw_qspi_mmio"
  23. struct dw_qspi_mmio {
  24. struct dw_spi dws;
  25. struct clk *clk;
  26. struct clk *pclk;
  27. void *priv;
  28. };
  29. static int dw_qspi_mmio_probe(struct platform_device *pdev)
  30. {
  31. int (*init_func)(struct platform_device *pdev,
  32. struct dw_qspi_mmio *dwsmmio);
  33. struct dw_qspi_mmio *dwsmmio;
  34. struct dw_spi *dws;
  35. int ret;
  36. int num_cs,rx_sample_dly;
  37. dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_qspi_mmio),
  38. GFP_KERNEL);
  39. if (!dwsmmio)
  40. return -ENOMEM;
  41. dws = &dwsmmio->dws;
  42. /* Get basic io resource and map it */
  43. dws->regs = devm_platform_ioremap_resource(pdev, 0);
  44. if (IS_ERR(dws->regs)) {
  45. dev_err(&pdev->dev, "QSPI region map failed\n");
  46. return PTR_ERR(dws->regs);
  47. }
  48. dws->irq = platform_get_irq(pdev, 0);
  49. if (dws->irq < 0)
  50. return dws->irq; /* -ENXIO */
  51. dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
  52. if (IS_ERR(dwsmmio->clk))
  53. return PTR_ERR(dwsmmio->clk);
  54. ret = clk_prepare_enable(dwsmmio->clk);
  55. if (ret)
  56. return ret;
  57. /* Optional clock needed to access the registers */
  58. dwsmmio->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
  59. if (IS_ERR(dwsmmio->pclk)) {
  60. ret = PTR_ERR(dwsmmio->pclk);
  61. goto out_clk;
  62. }
  63. ret = clk_prepare_enable(dwsmmio->pclk);
  64. if (ret)
  65. goto out_clk;
  66. /* set bus number */
  67. dws->bus_num = pdev->id;
  68. /* get supported freq in max */
  69. dws->max_freq = clk_get_rate(dwsmmio->clk);
  70. //dws->slave_cs = devm_gpiod_get_index_optional(&pdev->dev, "cs", 0,GPIOD_OUT_LOW);
  71. dws->slave_cs = devm_gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
  72. if (IS_ERR(dws->slave_cs))
  73. return PTR_ERR(dws->slave_cs);
  74. /* get reg width of controler */
  75. device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
  76. /* get chip select count of controler */
  77. num_cs = 1;
  78. device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
  79. dws->num_cs = num_cs;
  80. rx_sample_dly = 4;
  81. device_property_read_u32(&pdev->dev, "rx-sample-dly", &rx_sample_dly);
  82. printk("get gpio succes %d\n",rx_sample_dly);
  83. dws->rx_sample_delay = rx_sample_dly;
  84. init_func = device_get_match_data(&pdev->dev);
  85. if (init_func) {
  86. ret = init_func(pdev, dwsmmio);
  87. if (ret)
  88. goto out;
  89. }
  90. ret = dw_qspi_add_host(&pdev->dev, dws);
  91. if (ret){
  92. goto out;
  93. dw_drv_log("probe failed \n");
  94. }
  95. platform_set_drvdata(pdev, dwsmmio);
  96. dw_drv_log("dw_qspi_probe success \n");
  97. return 0;
  98. out:
  99. clk_disable_unprepare(dwsmmio->pclk);
  100. out_clk:
  101. clk_disable_unprepare(dwsmmio->clk);
  102. return ret;
  103. }
  104. static int dw_qspi_mmio_remove(struct platform_device *pdev)
  105. {
  106. struct dw_qspi_mmio *dwsmmio = platform_get_drvdata(pdev);
  107. dw_qspi_remove_host(&dwsmmio->dws);
  108. clk_disable_unprepare(dwsmmio->pclk);
  109. clk_disable_unprepare(dwsmmio->clk);
  110. return 0;
  111. }
  112. static const struct of_device_id dw_qspi_mmio_of_match[] = {
  113. { .compatible = "snps,dw-apb-ssi-quad", },
  114. { /* end of table */}
  115. };
  116. MODULE_DEVICE_TABLE(of, dw_qspi_mmio_of_match);
  117. static struct platform_driver dw_qspi_mmio_driver = {
  118. .probe = dw_qspi_mmio_probe,
  119. .remove = dw_qspi_mmio_remove,
  120. .driver = {
  121. .name = DRIVER_NAME,
  122. .of_match_table = dw_qspi_mmio_of_match,
  123. },
  124. };
  125. module_platform_driver(dw_qspi_mmio_driver);
  126. MODULE_AUTHOR("linghui zeng <linghui.zlh@linux.alibaba.com>");
  127. MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW ehance-spi Core");
  128. MODULE_LICENSE("GPL v2");