spi-clps711x.c 4.0 KB

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