cyttsp4_spi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Txx4xx parts.
  6. * Supported parts include:
  7. * TMA4XX
  8. * TMA1036
  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 "cyttsp4_core.h"
  17. #include <linux/delay.h>
  18. #include <linux/input.h>
  19. #include <linux/spi/spi.h>
  20. #define CY_SPI_WR_OP 0x00 /* r/~w */
  21. #define CY_SPI_RD_OP 0x01
  22. #define CY_SPI_BITS_PER_WORD 8
  23. #define CY_SPI_A8_BIT 0x02
  24. #define CY_SPI_WR_HEADER_BYTES 2
  25. #define CY_SPI_RD_HEADER_BYTES 1
  26. #define CY_SPI_CMD_BYTES 2
  27. #define CY_SPI_SYNC_BYTE 0
  28. #define CY_SPI_SYNC_ACK 0x62 /* from TRM *A protocol */
  29. #define CY_SPI_DATA_SIZE (2 * 256)
  30. #define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
  31. static int cyttsp_spi_xfer(struct device *dev, u8 *xfer_buf,
  32. u8 op, u16 reg, u8 *buf, int length)
  33. {
  34. struct spi_device *spi = to_spi_device(dev);
  35. struct spi_message msg;
  36. struct spi_transfer xfer[2];
  37. u8 *wr_buf = &xfer_buf[0];
  38. u8 rd_buf[CY_SPI_CMD_BYTES];
  39. int retval;
  40. int i;
  41. if (length > CY_SPI_DATA_SIZE) {
  42. dev_err(dev, "%s: length %d is too big.\n",
  43. __func__, length);
  44. return -EINVAL;
  45. }
  46. memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
  47. memset(rd_buf, 0, CY_SPI_CMD_BYTES);
  48. wr_buf[0] = op + (((reg >> 8) & 0x1) ? CY_SPI_A8_BIT : 0);
  49. if (op == CY_SPI_WR_OP) {
  50. wr_buf[1] = reg & 0xFF;
  51. if (length > 0)
  52. memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
  53. }
  54. memset(xfer, 0, sizeof(xfer));
  55. spi_message_init(&msg);
  56. /*
  57. We set both TX and RX buffers because Cypress TTSP
  58. requires full duplex operation.
  59. */
  60. xfer[0].tx_buf = wr_buf;
  61. xfer[0].rx_buf = rd_buf;
  62. switch (op) {
  63. case CY_SPI_WR_OP:
  64. xfer[0].len = length + CY_SPI_CMD_BYTES;
  65. spi_message_add_tail(&xfer[0], &msg);
  66. break;
  67. case CY_SPI_RD_OP:
  68. xfer[0].len = CY_SPI_RD_HEADER_BYTES;
  69. spi_message_add_tail(&xfer[0], &msg);
  70. xfer[1].rx_buf = buf;
  71. xfer[1].len = length;
  72. spi_message_add_tail(&xfer[1], &msg);
  73. break;
  74. default:
  75. dev_err(dev, "%s: bad operation code=%d\n", __func__, op);
  76. return -EINVAL;
  77. }
  78. retval = spi_sync(spi, &msg);
  79. if (retval < 0) {
  80. dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
  81. __func__, retval, xfer[1].len, op);
  82. /*
  83. * do not return here since was a bad ACK sequence
  84. * let the following ACK check handle any errors and
  85. * allow silent retries
  86. */
  87. }
  88. if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK) {
  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. int rc;
  104. rc = cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, NULL, 0);
  105. if (rc)
  106. return rc;
  107. else
  108. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data,
  109. length);
  110. }
  111. static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf,
  112. u16 addr, u8 length, const void *data)
  113. {
  114. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data,
  115. length);
  116. }
  117. static const struct cyttsp4_bus_ops cyttsp_spi_bus_ops = {
  118. .bustype = BUS_SPI,
  119. .write = cyttsp_spi_write_block_data,
  120. .read = cyttsp_spi_read_block_data,
  121. };
  122. static int cyttsp4_spi_probe(struct spi_device *spi)
  123. {
  124. struct cyttsp4 *ts;
  125. int error;
  126. /* Set up SPI*/
  127. spi->bits_per_word = CY_SPI_BITS_PER_WORD;
  128. spi->mode = SPI_MODE_0;
  129. error = spi_setup(spi);
  130. if (error < 0) {
  131. dev_err(&spi->dev, "%s: SPI setup error %d\n",
  132. __func__, error);
  133. return error;
  134. }
  135. ts = cyttsp4_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
  136. CY_SPI_DATA_BUF_SIZE);
  137. return PTR_ERR_OR_ZERO(ts);
  138. }
  139. static void cyttsp4_spi_remove(struct spi_device *spi)
  140. {
  141. struct cyttsp4 *ts = spi_get_drvdata(spi);
  142. cyttsp4_remove(ts);
  143. }
  144. static struct spi_driver cyttsp4_spi_driver = {
  145. .driver = {
  146. .name = CYTTSP4_SPI_NAME,
  147. .pm = &cyttsp4_pm_ops,
  148. },
  149. .probe = cyttsp4_spi_probe,
  150. .remove = cyttsp4_spi_remove,
  151. };
  152. module_spi_driver(cyttsp4_spi_driver);
  153. MODULE_LICENSE("GPL");
  154. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
  155. MODULE_AUTHOR("Cypress");
  156. MODULE_ALIAS("spi:cyttsp4");