spi-clps711x.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * CLPS711X SPI bus driver
  3. *
  4. * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/io.h>
  12. #include <linux/clk.h>
  13. #include <linux/gpio.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/mfd/syscon/clps711x.h>
  20. #include <linux/spi/spi.h>
  21. #define DRIVER_NAME "clps711x-spi"
  22. #define SYNCIO_FRMLEN(x) ((x) << 8)
  23. #define SYNCIO_TXFRMEN (1 << 14)
  24. struct spi_clps711x_data {
  25. void __iomem *syncio;
  26. struct regmap *syscon;
  27. struct clk *spi_clk;
  28. u8 *tx_buf;
  29. u8 *rx_buf;
  30. unsigned int bpw;
  31. int len;
  32. };
  33. static int spi_clps711x_setup(struct spi_device *spi)
  34. {
  35. if (!spi->controller_state) {
  36. int ret;
  37. ret = devm_gpio_request(&spi->master->dev, spi->cs_gpio,
  38. dev_name(&spi->master->dev));
  39. if (ret)
  40. return ret;
  41. spi->controller_state = spi;
  42. }
  43. /* We are expect that SPI-device is not selected */
  44. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  45. return 0;
  46. }
  47. static int spi_clps711x_prepare_message(struct spi_master *master,
  48. struct spi_message *msg)
  49. {
  50. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  51. struct spi_device *spi = msg->spi;
  52. /* Setup mode for transfer */
  53. return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
  54. (spi->mode & SPI_CPHA) ?
  55. SYSCON3_ADCCKNSEN : 0);
  56. }
  57. static int spi_clps711x_transfer_one(struct spi_master *master,
  58. struct spi_device *spi,
  59. struct spi_transfer *xfer)
  60. {
  61. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  62. u8 data;
  63. clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
  64. hw->len = xfer->len;
  65. hw->bpw = xfer->bits_per_word;
  66. hw->tx_buf = (u8 *)xfer->tx_buf;
  67. hw->rx_buf = (u8 *)xfer->rx_buf;
  68. /* Initiate transfer */
  69. data = hw->tx_buf ? *hw->tx_buf++ : 0;
  70. writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
  71. return 1;
  72. }
  73. static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
  74. {
  75. struct spi_master *master = dev_id;
  76. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  77. u8 data;
  78. /* Handle RX */
  79. data = readb(hw->syncio);
  80. if (hw->rx_buf)
  81. *hw->rx_buf++ = data;
  82. /* Handle TX */
  83. if (--hw->len > 0) {
  84. data = hw->tx_buf ? *hw->tx_buf++ : 0;
  85. writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
  86. hw->syncio);
  87. } else
  88. spi_finalize_current_transfer(master);
  89. return IRQ_HANDLED;
  90. }
  91. static int spi_clps711x_probe(struct platform_device *pdev)
  92. {
  93. struct spi_clps711x_data *hw;
  94. struct spi_master *master;
  95. struct resource *res;
  96. int irq, ret;
  97. irq = platform_get_irq(pdev, 0);
  98. if (irq < 0)
  99. return irq;
  100. master = spi_alloc_master(&pdev->dev, sizeof(*hw));
  101. if (!master)
  102. return -ENOMEM;
  103. master->bus_num = -1;
  104. master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
  105. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
  106. master->dev.of_node = pdev->dev.of_node;
  107. master->setup = spi_clps711x_setup;
  108. master->prepare_message = spi_clps711x_prepare_message;
  109. master->transfer_one = spi_clps711x_transfer_one;
  110. hw = spi_master_get_devdata(master);
  111. hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
  112. if (IS_ERR(hw->spi_clk)) {
  113. ret = PTR_ERR(hw->spi_clk);
  114. goto err_out;
  115. }
  116. hw->syscon =
  117. syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3");
  118. if (IS_ERR(hw->syscon)) {
  119. ret = PTR_ERR(hw->syscon);
  120. goto err_out;
  121. }
  122. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  123. hw->syncio = devm_ioremap_resource(&pdev->dev, res);
  124. if (IS_ERR(hw->syncio)) {
  125. ret = PTR_ERR(hw->syncio);
  126. goto err_out;
  127. }
  128. /* Disable extended mode due hardware problems */
  129. regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
  130. /* Clear possible pending interrupt */
  131. readl(hw->syncio);
  132. ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
  133. dev_name(&pdev->dev), master);
  134. if (ret)
  135. goto err_out;
  136. ret = devm_spi_register_master(&pdev->dev, master);
  137. if (!ret)
  138. return 0;
  139. err_out:
  140. spi_master_put(master);
  141. return ret;
  142. }
  143. static const struct of_device_id clps711x_spi_dt_ids[] = {
  144. { .compatible = "cirrus,ep7209-spi", },
  145. { }
  146. };
  147. MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids);
  148. static struct platform_driver clps711x_spi_driver = {
  149. .driver = {
  150. .name = DRIVER_NAME,
  151. .of_match_table = clps711x_spi_dt_ids,
  152. },
  153. .probe = spi_clps711x_probe,
  154. };
  155. module_platform_driver(clps711x_spi_driver);
  156. MODULE_LICENSE("GPL");
  157. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  158. MODULE_DESCRIPTION("CLPS711X SPI bus driver");
  159. MODULE_ALIAS("platform:" DRIVER_NAME);