spi-clps711x.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CLPS711X SPI bus driver
  4. *
  5. * Copyright (C) 2012-2016 Alexander Shiyan <[email protected]>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/clk.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/mfd/syscon/clps711x.h>
  17. #include <linux/spi/spi.h>
  18. #define DRIVER_NAME "clps711x-spi"
  19. #define SYNCIO_FRMLEN(x) ((x) << 8)
  20. #define SYNCIO_TXFRMEN (1 << 14)
  21. struct spi_clps711x_data {
  22. void __iomem *syncio;
  23. struct regmap *syscon;
  24. struct clk *spi_clk;
  25. u8 *tx_buf;
  26. u8 *rx_buf;
  27. unsigned int bpw;
  28. int len;
  29. };
  30. static int spi_clps711x_prepare_message(struct spi_master *master,
  31. struct spi_message *msg)
  32. {
  33. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  34. struct spi_device *spi = msg->spi;
  35. /* Setup mode for transfer */
  36. return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
  37. (spi->mode & SPI_CPHA) ?
  38. SYSCON3_ADCCKNSEN : 0);
  39. }
  40. static int spi_clps711x_transfer_one(struct spi_master *master,
  41. struct spi_device *spi,
  42. struct spi_transfer *xfer)
  43. {
  44. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  45. u8 data;
  46. clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
  47. hw->len = xfer->len;
  48. hw->bpw = xfer->bits_per_word;
  49. hw->tx_buf = (u8 *)xfer->tx_buf;
  50. hw->rx_buf = (u8 *)xfer->rx_buf;
  51. /* Initiate transfer */
  52. data = hw->tx_buf ? *hw->tx_buf++ : 0;
  53. writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
  54. return 1;
  55. }
  56. static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
  57. {
  58. struct spi_master *master = dev_id;
  59. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  60. u8 data;
  61. /* Handle RX */
  62. data = readb(hw->syncio);
  63. if (hw->rx_buf)
  64. *hw->rx_buf++ = data;
  65. /* Handle TX */
  66. if (--hw->len > 0) {
  67. data = hw->tx_buf ? *hw->tx_buf++ : 0;
  68. writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
  69. hw->syncio);
  70. } else
  71. spi_finalize_current_transfer(master);
  72. return IRQ_HANDLED;
  73. }
  74. static int spi_clps711x_probe(struct platform_device *pdev)
  75. {
  76. struct device_node *np = pdev->dev.of_node;
  77. struct spi_clps711x_data *hw;
  78. struct spi_master *master;
  79. int irq, ret;
  80. irq = platform_get_irq(pdev, 0);
  81. if (irq < 0)
  82. return irq;
  83. master = spi_alloc_master(&pdev->dev, sizeof(*hw));
  84. if (!master)
  85. return -ENOMEM;
  86. master->use_gpio_descriptors = true;
  87. master->bus_num = -1;
  88. master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
  89. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
  90. master->dev.of_node = pdev->dev.of_node;
  91. master->prepare_message = spi_clps711x_prepare_message;
  92. master->transfer_one = spi_clps711x_transfer_one;
  93. hw = spi_master_get_devdata(master);
  94. hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
  95. if (IS_ERR(hw->spi_clk)) {
  96. ret = PTR_ERR(hw->spi_clk);
  97. goto err_out;
  98. }
  99. hw->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
  100. if (IS_ERR(hw->syscon)) {
  101. ret = PTR_ERR(hw->syscon);
  102. goto err_out;
  103. }
  104. hw->syncio = devm_platform_ioremap_resource(pdev, 0);
  105. if (IS_ERR(hw->syncio)) {
  106. ret = PTR_ERR(hw->syncio);
  107. goto err_out;
  108. }
  109. /* Disable extended mode due hardware problems */
  110. regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
  111. /* Clear possible pending interrupt */
  112. readl(hw->syncio);
  113. ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
  114. dev_name(&pdev->dev), master);
  115. if (ret)
  116. goto err_out;
  117. ret = devm_spi_register_master(&pdev->dev, master);
  118. if (!ret)
  119. return 0;
  120. err_out:
  121. spi_master_put(master);
  122. return ret;
  123. }
  124. static const struct of_device_id clps711x_spi_dt_ids[] = {
  125. { .compatible = "cirrus,ep7209-spi", },
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids);
  129. static struct platform_driver clps711x_spi_driver = {
  130. .driver = {
  131. .name = DRIVER_NAME,
  132. .of_match_table = clps711x_spi_dt_ids,
  133. },
  134. .probe = spi_clps711x_probe,
  135. };
  136. module_platform_driver(clps711x_spi_driver);
  137. MODULE_LICENSE("GPL");
  138. MODULE_AUTHOR("Alexander Shiyan <[email protected]>");
  139. MODULE_DESCRIPTION("CLPS711X SPI bus driver");
  140. MODULE_ALIAS("platform:" DRIVER_NAME);