mmp_spi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * linux/drivers/video/mmp/hw/mmp_spi.c
  3. * using the spi in LCD controler for commands send
  4. *
  5. * Copyright (C) 2012 Marvell Technology Group Ltd.
  6. * Authors: Guoqing Li <ligq@marvell.com>
  7. * Lisa Du <cldu@marvell.com>
  8. * Zhou Zhu <zzhu3@marvell.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/delay.h>
  26. #include <linux/err.h>
  27. #include <linux/io.h>
  28. #include <linux/spi/spi.h>
  29. #include "mmp_ctrl.h"
  30. /**
  31. * spi_write - write command to the SPI port
  32. * @data: can be 8/16/32-bit, MSB justified data to write.
  33. * @len: data length.
  34. *
  35. * Wait bus transfer complete IRQ.
  36. * The caller is expected to perform the necessary locking.
  37. *
  38. * Returns:
  39. * %-ETIMEDOUT timeout occurred
  40. * 0 success
  41. */
  42. static inline int lcd_spi_write(struct spi_device *spi, u32 data)
  43. {
  44. int timeout = 100000, isr, ret = 0;
  45. u32 tmp;
  46. void *reg_base =
  47. *(void **)spi_master_get_devdata(spi->master);
  48. /* clear ISR */
  49. writel_relaxed(~SPI_IRQ_MASK, reg_base + SPU_IRQ_ISR);
  50. switch (spi->bits_per_word) {
  51. case 8:
  52. writel_relaxed((u8)data, reg_base + LCD_SPU_SPI_TXDATA);
  53. break;
  54. case 16:
  55. writel_relaxed((u16)data, reg_base + LCD_SPU_SPI_TXDATA);
  56. break;
  57. case 32:
  58. writel_relaxed((u32)data, reg_base + LCD_SPU_SPI_TXDATA);
  59. break;
  60. default:
  61. dev_err(&spi->dev, "Wrong spi bit length\n");
  62. }
  63. /* SPI start to send command */
  64. tmp = readl_relaxed(reg_base + LCD_SPU_SPI_CTRL);
  65. tmp &= ~CFG_SPI_START_MASK;
  66. tmp |= CFG_SPI_START(1);
  67. writel(tmp, reg_base + LCD_SPU_SPI_CTRL);
  68. isr = readl_relaxed(reg_base + SPU_IRQ_ISR);
  69. while (!(isr & SPI_IRQ_ENA_MASK)) {
  70. udelay(100);
  71. isr = readl_relaxed(reg_base + SPU_IRQ_ISR);
  72. if (!--timeout) {
  73. ret = -ETIMEDOUT;
  74. dev_err(&spi->dev, "spi cmd send time out\n");
  75. break;
  76. }
  77. }
  78. tmp = readl_relaxed(reg_base + LCD_SPU_SPI_CTRL);
  79. tmp &= ~CFG_SPI_START_MASK;
  80. tmp |= CFG_SPI_START(0);
  81. writel_relaxed(tmp, reg_base + LCD_SPU_SPI_CTRL);
  82. writel_relaxed(~SPI_IRQ_MASK, reg_base + SPU_IRQ_ISR);
  83. return ret;
  84. }
  85. static int lcd_spi_setup(struct spi_device *spi)
  86. {
  87. void *reg_base =
  88. *(void **)spi_master_get_devdata(spi->master);
  89. u32 tmp;
  90. tmp = CFG_SCLKCNT(16) |
  91. CFG_TXBITS(spi->bits_per_word) |
  92. CFG_SPI_SEL(1) | CFG_SPI_ENA(1) |
  93. CFG_SPI_3W4WB(1);
  94. writel(tmp, reg_base + LCD_SPU_SPI_CTRL);
  95. /*
  96. * After set mode it need a time to pull up the spi singals,
  97. * or it would cause the wrong waveform when send spi command,
  98. * especially on pxa910h
  99. */
  100. tmp = readl_relaxed(reg_base + SPU_IOPAD_CONTROL);
  101. if ((tmp & CFG_IOPADMODE_MASK) != IOPAD_DUMB18SPI)
  102. writel_relaxed(IOPAD_DUMB18SPI |
  103. (tmp & ~CFG_IOPADMODE_MASK),
  104. reg_base + SPU_IOPAD_CONTROL);
  105. udelay(20);
  106. return 0;
  107. }
  108. static int lcd_spi_one_transfer(struct spi_device *spi, struct spi_message *m)
  109. {
  110. struct spi_transfer *t;
  111. int i;
  112. list_for_each_entry(t, &m->transfers, transfer_list) {
  113. switch (spi->bits_per_word) {
  114. case 8:
  115. for (i = 0; i < t->len; i++)
  116. lcd_spi_write(spi, ((u8 *)t->tx_buf)[i]);
  117. break;
  118. case 16:
  119. for (i = 0; i < t->len/2; i++)
  120. lcd_spi_write(spi, ((u16 *)t->tx_buf)[i]);
  121. break;
  122. case 32:
  123. for (i = 0; i < t->len/4; i++)
  124. lcd_spi_write(spi, ((u32 *)t->tx_buf)[i]);
  125. break;
  126. default:
  127. dev_err(&spi->dev, "Wrong spi bit length\n");
  128. }
  129. }
  130. m->status = 0;
  131. if (m->complete)
  132. m->complete(m->context);
  133. return 0;
  134. }
  135. int lcd_spi_register(struct mmphw_ctrl *ctrl)
  136. {
  137. struct spi_master *master;
  138. void **p_regbase;
  139. int err;
  140. master = spi_alloc_master(ctrl->dev, sizeof(void *));
  141. if (!master) {
  142. dev_err(ctrl->dev, "unable to allocate SPI master\n");
  143. return -ENOMEM;
  144. }
  145. p_regbase = spi_master_get_devdata(master);
  146. *p_regbase = ctrl->reg_base;
  147. /* set bus num to 5 to avoid conflict with other spi hosts */
  148. master->bus_num = 5;
  149. master->num_chipselect = 1;
  150. master->setup = lcd_spi_setup;
  151. master->transfer = lcd_spi_one_transfer;
  152. err = spi_register_master(master);
  153. if (err < 0) {
  154. dev_err(ctrl->dev, "unable to register SPI master\n");
  155. spi_master_put(master);
  156. return err;
  157. }
  158. dev_info(&master->dev, "registered\n");
  159. return 0;
  160. }