cyttsp_spi.c 5.1 KB

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