spi-clps711x.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * CLPS711X SPI bus driver
  3. *
  4. * Copyright (C) 2012-2014 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/delay.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/mfd/syscon/clps711x.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/platform_data/spi-clps711x.h>
  23. #define DRIVER_NAME "spi-clps711x"
  24. #define SYNCIO_FRMLEN(x) ((x) << 8)
  25. #define SYNCIO_TXFRMEN (1 << 14)
  26. struct spi_clps711x_data {
  27. void __iomem *syncio;
  28. struct regmap *syscon;
  29. struct clk *spi_clk;
  30. u8 *tx_buf;
  31. u8 *rx_buf;
  32. unsigned int bpw;
  33. int len;
  34. };
  35. static int spi_clps711x_setup(struct spi_device *spi)
  36. {
  37. /* We are expect that SPI-device is not selected */
  38. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  39. return 0;
  40. }
  41. static int spi_clps711x_prepare_message(struct spi_master *master,
  42. struct spi_message *msg)
  43. {
  44. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  45. struct spi_device *spi = msg->spi;
  46. /* Setup mode for transfer */
  47. return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
  48. (spi->mode & SPI_CPHA) ?
  49. SYSCON3_ADCCKNSEN : 0);
  50. }
  51. static int spi_clps711x_transfer_one(struct spi_master *master,
  52. struct spi_device *spi,
  53. struct spi_transfer *xfer)
  54. {
  55. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  56. u8 data;
  57. clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
  58. hw->len = xfer->len;
  59. hw->bpw = xfer->bits_per_word;
  60. hw->tx_buf = (u8 *)xfer->tx_buf;
  61. hw->rx_buf = (u8 *)xfer->rx_buf;
  62. /* Initiate transfer */
  63. data = hw->tx_buf ? *hw->tx_buf++ : 0;
  64. writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
  65. return 1;
  66. }
  67. static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
  68. {
  69. struct spi_master *master = dev_id;
  70. struct spi_clps711x_data *hw = spi_master_get_devdata(master);
  71. u8 data;
  72. /* Handle RX */
  73. data = readb(hw->syncio);
  74. if (hw->rx_buf)
  75. *hw->rx_buf++ = data;
  76. /* Handle TX */
  77. if (--hw->len > 0) {
  78. data = hw->tx_buf ? *hw->tx_buf++ : 0;
  79. writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
  80. hw->syncio);
  81. } else
  82. spi_finalize_current_transfer(master);
  83. return IRQ_HANDLED;
  84. }
  85. static int spi_clps711x_probe(struct platform_device *pdev)
  86. {
  87. struct spi_clps711x_data *hw;
  88. struct spi_clps711x_pdata *pdata = dev_get_platdata(&pdev->dev);
  89. struct spi_master *master;
  90. struct resource *res;
  91. int i, irq, ret;
  92. if (!pdata) {
  93. dev_err(&pdev->dev, "No platform data supplied\n");
  94. return -EINVAL;
  95. }
  96. if (pdata->num_chipselect < 1) {
  97. dev_err(&pdev->dev, "At least one CS must be defined\n");
  98. return -EINVAL;
  99. }
  100. irq = platform_get_irq(pdev, 0);
  101. if (irq < 0)
  102. return irq;
  103. master = spi_alloc_master(&pdev->dev, sizeof(*hw));
  104. if (!master)
  105. return -ENOMEM;
  106. master->cs_gpios = devm_kzalloc(&pdev->dev, sizeof(int) *
  107. pdata->num_chipselect, GFP_KERNEL);
  108. if (!master->cs_gpios) {
  109. ret = -ENOMEM;
  110. goto err_out;
  111. }
  112. master->bus_num = pdev->id;
  113. master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
  114. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
  115. master->num_chipselect = pdata->num_chipselect;
  116. master->setup = spi_clps711x_setup;
  117. master->prepare_message = spi_clps711x_prepare_message;
  118. master->transfer_one = spi_clps711x_transfer_one;
  119. hw = spi_master_get_devdata(master);
  120. for (i = 0; i < master->num_chipselect; i++) {
  121. master->cs_gpios[i] = pdata->chipselect[i];
  122. ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
  123. DRIVER_NAME);
  124. if (ret) {
  125. dev_err(&pdev->dev, "Can't get CS GPIO %i\n", i);
  126. goto err_out;
  127. }
  128. }
  129. hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
  130. if (IS_ERR(hw->spi_clk)) {
  131. ret = PTR_ERR(hw->spi_clk);
  132. goto err_out;
  133. }
  134. hw->syscon = syscon_regmap_lookup_by_pdevname("syscon.3");
  135. if (IS_ERR(hw->syscon)) {
  136. ret = PTR_ERR(hw->syscon);
  137. goto err_out;
  138. }
  139. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. hw->syncio = devm_ioremap_resource(&pdev->dev, res);
  141. if (IS_ERR(hw->syncio)) {
  142. ret = PTR_ERR(hw->syncio);
  143. goto err_out;
  144. }
  145. /* Disable extended mode due hardware problems */
  146. regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
  147. /* Clear possible pending interrupt */
  148. readl(hw->syncio);
  149. ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
  150. dev_name(&pdev->dev), master);
  151. if (ret)
  152. goto err_out;
  153. ret = devm_spi_register_master(&pdev->dev, master);
  154. if (!ret) {
  155. dev_info(&pdev->dev,
  156. "SPI bus driver initialized. Master clock %u Hz\n",
  157. master->max_speed_hz);
  158. return 0;
  159. }
  160. dev_err(&pdev->dev, "Failed to register master\n");
  161. err_out:
  162. spi_master_put(master);
  163. return ret;
  164. }
  165. static struct platform_driver clps711x_spi_driver = {
  166. .driver = {
  167. .name = DRIVER_NAME,
  168. },
  169. .probe = spi_clps711x_probe,
  170. };
  171. module_platform_driver(clps711x_spi_driver);
  172. MODULE_LICENSE("GPL");
  173. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  174. MODULE_DESCRIPTION("CLPS711X SPI bus driver");
  175. MODULE_ALIAS("platform:" DRIVER_NAME);