cyttsp_spi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Source for:
  4. * Cypress TrueTouch(TM) Standard Product (TTSP) SPI touchscreen driver.
  5. * For use with Cypress Txx3xx parts.
  6. * Supported parts include:
  7. * CY8CTST341
  8. * CY8CTMA340
  9. *
  10. * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
  11. * Copyright (C) 2012 Javier Martinez Canillas <[email protected]>
  12. * Copyright (C) 2013 Cypress Semiconductor
  13. *
  14. * Contact Cypress Semiconductor at www.cypress.com <[email protected]>
  15. */
  16. #include "cyttsp_core.h"
  17. #include <linux/delay.h>
  18. #include <linux/input.h>
  19. #include <linux/spi/spi.h>
  20. #define CY_SPI_NAME "cyttsp-spi"
  21. #define CY_SPI_WR_OP 0x00 /* r/~w */
  22. #define CY_SPI_RD_OP 0x01
  23. #define CY_SPI_CMD_BYTES 4
  24. #define CY_SPI_SYNC_BYTE 2
  25. #define CY_SPI_SYNC_ACK1 0x62 /* from protocol v.2 */
  26. #define CY_SPI_SYNC_ACK2 0x9D /* from protocol v.2 */
  27. #define CY_SPI_DATA_SIZE 128
  28. #define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
  29. #define CY_SPI_BITS_PER_WORD 8
  30. static int cyttsp_spi_xfer(struct device *dev, u8 *xfer_buf,
  31. u8 op, u16 reg, u8 *buf, int length)
  32. {
  33. struct spi_device *spi = to_spi_device(dev);
  34. struct spi_message msg;
  35. struct spi_transfer xfer[2];
  36. u8 *wr_buf = &xfer_buf[0];
  37. u8 *rd_buf = &xfer_buf[CY_SPI_DATA_BUF_SIZE];
  38. int retval;
  39. int i;
  40. if (length > CY_SPI_DATA_SIZE) {
  41. dev_err(dev, "%s: length %d is too big.\n",
  42. __func__, length);
  43. return -EINVAL;
  44. }
  45. memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
  46. memset(rd_buf, 0, CY_SPI_DATA_BUF_SIZE);
  47. wr_buf[0] = 0x00; /* header byte 0 */
  48. wr_buf[1] = 0xFF; /* header byte 1 */
  49. wr_buf[2] = reg; /* reg index */
  50. wr_buf[3] = op; /* r/~w */
  51. if (op == CY_SPI_WR_OP)
  52. memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
  53. memset(xfer, 0, sizeof(xfer));
  54. spi_message_init(&msg);
  55. /*
  56. We set both TX and RX buffers because Cypress TTSP
  57. requires full duplex operation.
  58. */
  59. xfer[0].tx_buf = wr_buf;
  60. xfer[0].rx_buf = rd_buf;
  61. switch (op) {
  62. case CY_SPI_WR_OP:
  63. xfer[0].len = length + CY_SPI_CMD_BYTES;
  64. spi_message_add_tail(&xfer[0], &msg);
  65. break;
  66. case CY_SPI_RD_OP:
  67. xfer[0].len = CY_SPI_CMD_BYTES;
  68. spi_message_add_tail(&xfer[0], &msg);
  69. xfer[1].rx_buf = buf;
  70. xfer[1].len = length;
  71. spi_message_add_tail(&xfer[1], &msg);
  72. break;
  73. default:
  74. dev_err(dev, "%s: bad operation code=%d\n", __func__, op);
  75. return -EINVAL;
  76. }
  77. retval = spi_sync(spi, &msg);
  78. if (retval < 0) {
  79. dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
  80. __func__, retval, xfer[1].len, op);
  81. /*
  82. * do not return here since was a bad ACK sequence
  83. * let the following ACK check handle any errors and
  84. * allow silent retries
  85. */
  86. }
  87. if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK1 ||
  88. rd_buf[CY_SPI_SYNC_BYTE + 1] != CY_SPI_SYNC_ACK2) {
  89. dev_dbg(dev, "%s: operation %d failed\n", __func__, op);
  90. for (i = 0; i < CY_SPI_CMD_BYTES; i++)
  91. dev_dbg(dev, "%s: test rd_buf[%d]:0x%02x\n",
  92. __func__, i, rd_buf[i]);
  93. for (i = 0; i < length; i++)
  94. dev_dbg(dev, "%s: test buf[%d]:0x%02x\n",
  95. __func__, i, buf[i]);
  96. return -EIO;
  97. }
  98. return 0;
  99. }
  100. static int cyttsp_spi_read_block_data(struct device *dev, u8 *xfer_buf,
  101. u16 addr, u8 length, void *data)
  102. {
  103. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data,
  104. length);
  105. }
  106. static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf,
  107. u16 addr, u8 length, const void *data)
  108. {
  109. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data,
  110. length);
  111. }
  112. static const struct cyttsp_bus_ops cyttsp_spi_bus_ops = {
  113. .bustype = BUS_SPI,
  114. .write = cyttsp_spi_write_block_data,
  115. .read = cyttsp_spi_read_block_data,
  116. };
  117. static int cyttsp_spi_probe(struct spi_device *spi)
  118. {
  119. struct cyttsp *ts;
  120. int error;
  121. /* Set up SPI*/
  122. spi->bits_per_word = CY_SPI_BITS_PER_WORD;
  123. spi->mode = SPI_MODE_0;
  124. error = spi_setup(spi);
  125. if (error < 0) {
  126. dev_err(&spi->dev, "%s: SPI setup error %d\n",
  127. __func__, error);
  128. return error;
  129. }
  130. ts = cyttsp_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
  131. CY_SPI_DATA_BUF_SIZE * 2);
  132. if (IS_ERR(ts))
  133. return PTR_ERR(ts);
  134. spi_set_drvdata(spi, ts);
  135. return 0;
  136. }
  137. static const struct of_device_id cyttsp_of_spi_match[] = {
  138. { .compatible = "cypress,cy8ctma340", },
  139. { .compatible = "cypress,cy8ctst341", },
  140. { /* sentinel */ }
  141. };
  142. MODULE_DEVICE_TABLE(of, cyttsp_of_spi_match);
  143. static struct spi_driver cyttsp_spi_driver = {
  144. .driver = {
  145. .name = CY_SPI_NAME,
  146. .pm = &cyttsp_pm_ops,
  147. .of_match_table = cyttsp_of_spi_match,
  148. },
  149. .probe = cyttsp_spi_probe,
  150. };
  151. module_spi_driver(cyttsp_spi_driver);
  152. MODULE_LICENSE("GPL");
  153. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
  154. MODULE_AUTHOR("Cypress");
  155. MODULE_ALIAS("spi:cyttsp");