cyttsp4_spi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Source for:
  3. * Cypress TrueTouch(TM) Standard Product (TTSP) SPI touchscreen driver.
  4. * For use with Cypress Txx4xx parts.
  5. * Supported parts include:
  6. * TMA4XX
  7. * TMA1036
  8. *
  9. * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
  10. * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
  11. * Copyright (C) 2013 Cypress Semiconductor
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * version 2, and only version 2, as published by the
  16. * Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
  24. *
  25. */
  26. #include "cyttsp4_core.h"
  27. #include <linux/delay.h>
  28. #include <linux/input.h>
  29. #include <linux/spi/spi.h>
  30. #define CY_SPI_WR_OP 0x00 /* r/~w */
  31. #define CY_SPI_RD_OP 0x01
  32. #define CY_SPI_BITS_PER_WORD 8
  33. #define CY_SPI_A8_BIT 0x02
  34. #define CY_SPI_WR_HEADER_BYTES 2
  35. #define CY_SPI_RD_HEADER_BYTES 1
  36. #define CY_SPI_CMD_BYTES 2
  37. #define CY_SPI_SYNC_BYTE 0
  38. #define CY_SPI_SYNC_ACK 0x62 /* from TRM *A protocol */
  39. #define CY_SPI_DATA_SIZE (2 * 256)
  40. #define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
  41. static int cyttsp_spi_xfer(struct device *dev, u8 *xfer_buf,
  42. u8 op, u16 reg, u8 *buf, int length)
  43. {
  44. struct spi_device *spi = to_spi_device(dev);
  45. struct spi_message msg;
  46. struct spi_transfer xfer[2];
  47. u8 *wr_buf = &xfer_buf[0];
  48. u8 rd_buf[CY_SPI_CMD_BYTES];
  49. int retval;
  50. int i;
  51. if (length > CY_SPI_DATA_SIZE) {
  52. dev_err(dev, "%s: length %d is too big.\n",
  53. __func__, length);
  54. return -EINVAL;
  55. }
  56. memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
  57. memset(rd_buf, 0, CY_SPI_CMD_BYTES);
  58. wr_buf[0] = op + (((reg >> 8) & 0x1) ? CY_SPI_A8_BIT : 0);
  59. if (op == CY_SPI_WR_OP) {
  60. wr_buf[1] = reg & 0xFF;
  61. if (length > 0)
  62. memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
  63. }
  64. memset(xfer, 0, sizeof(xfer));
  65. spi_message_init(&msg);
  66. /*
  67. We set both TX and RX buffers because Cypress TTSP
  68. requires full duplex operation.
  69. */
  70. xfer[0].tx_buf = wr_buf;
  71. xfer[0].rx_buf = rd_buf;
  72. switch (op) {
  73. case CY_SPI_WR_OP:
  74. xfer[0].len = length + CY_SPI_CMD_BYTES;
  75. spi_message_add_tail(&xfer[0], &msg);
  76. break;
  77. case CY_SPI_RD_OP:
  78. xfer[0].len = CY_SPI_RD_HEADER_BYTES;
  79. spi_message_add_tail(&xfer[0], &msg);
  80. xfer[1].rx_buf = buf;
  81. xfer[1].len = length;
  82. spi_message_add_tail(&xfer[1], &msg);
  83. break;
  84. default:
  85. dev_err(dev, "%s: bad operation code=%d\n", __func__, op);
  86. return -EINVAL;
  87. }
  88. retval = spi_sync(spi, &msg);
  89. if (retval < 0) {
  90. dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
  91. __func__, retval, xfer[1].len, op);
  92. /*
  93. * do not return here since was a bad ACK sequence
  94. * let the following ACK check handle any errors and
  95. * allow silent retries
  96. */
  97. }
  98. if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK) {
  99. dev_dbg(dev, "%s: operation %d failed\n", __func__, op);
  100. for (i = 0; i < CY_SPI_CMD_BYTES; i++)
  101. dev_dbg(dev, "%s: test rd_buf[%d]:0x%02x\n",
  102. __func__, i, rd_buf[i]);
  103. for (i = 0; i < length; i++)
  104. dev_dbg(dev, "%s: test buf[%d]:0x%02x\n",
  105. __func__, i, buf[i]);
  106. return -EIO;
  107. }
  108. return 0;
  109. }
  110. static int cyttsp_spi_read_block_data(struct device *dev, u8 *xfer_buf,
  111. u16 addr, u8 length, void *data)
  112. {
  113. int rc;
  114. rc = cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, NULL, 0);
  115. if (rc)
  116. return rc;
  117. else
  118. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data,
  119. length);
  120. }
  121. static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf,
  122. u16 addr, u8 length, const void *data)
  123. {
  124. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data,
  125. length);
  126. }
  127. static const struct cyttsp4_bus_ops cyttsp_spi_bus_ops = {
  128. .bustype = BUS_SPI,
  129. .write = cyttsp_spi_write_block_data,
  130. .read = cyttsp_spi_read_block_data,
  131. };
  132. static int cyttsp4_spi_probe(struct spi_device *spi)
  133. {
  134. struct cyttsp4 *ts;
  135. int error;
  136. /* Set up SPI*/
  137. spi->bits_per_word = CY_SPI_BITS_PER_WORD;
  138. spi->mode = SPI_MODE_0;
  139. error = spi_setup(spi);
  140. if (error < 0) {
  141. dev_err(&spi->dev, "%s: SPI setup error %d\n",
  142. __func__, error);
  143. return error;
  144. }
  145. ts = cyttsp4_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
  146. CY_SPI_DATA_BUF_SIZE);
  147. return PTR_ERR_OR_ZERO(ts);
  148. }
  149. static int cyttsp4_spi_remove(struct spi_device *spi)
  150. {
  151. struct cyttsp4 *ts = spi_get_drvdata(spi);
  152. cyttsp4_remove(ts);
  153. return 0;
  154. }
  155. static struct spi_driver cyttsp4_spi_driver = {
  156. .driver = {
  157. .name = CYTTSP4_SPI_NAME,
  158. .pm = &cyttsp4_pm_ops,
  159. },
  160. .probe = cyttsp4_spi_probe,
  161. .remove = cyttsp4_spi_remove,
  162. };
  163. module_spi_driver(cyttsp4_spi_driver);
  164. MODULE_LICENSE("GPL");
  165. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
  166. MODULE_AUTHOR("Cypress");
  167. MODULE_ALIAS("spi:cyttsp4");