cyttsp_spi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * 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 "cyttsp_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_CMD_BYTES 4
  33. #define CY_SPI_SYNC_BYTE 2
  34. #define CY_SPI_SYNC_ACK1 0x62 /* from protocol v.2 */
  35. #define CY_SPI_SYNC_ACK2 0x9D /* from protocol v.2 */
  36. #define CY_SPI_DATA_SIZE 128
  37. #define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
  38. #define CY_SPI_BITS_PER_WORD 8
  39. static int cyttsp_spi_xfer(struct device *dev, u8 *xfer_buf,
  40. u8 op, u16 reg, u8 *buf, int length)
  41. {
  42. struct spi_device *spi = to_spi_device(dev);
  43. struct spi_message msg;
  44. struct spi_transfer xfer[2];
  45. u8 *wr_buf = &xfer_buf[0];
  46. u8 *rd_buf = &xfer_buf[CY_SPI_DATA_BUF_SIZE];
  47. int retval;
  48. int i;
  49. if (length > CY_SPI_DATA_SIZE) {
  50. dev_err(dev, "%s: length %d is too big.\n",
  51. __func__, length);
  52. return -EINVAL;
  53. }
  54. memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
  55. memset(rd_buf, 0, CY_SPI_DATA_BUF_SIZE);
  56. wr_buf[0] = 0x00; /* header byte 0 */
  57. wr_buf[1] = 0xFF; /* header byte 1 */
  58. wr_buf[2] = reg; /* reg index */
  59. wr_buf[3] = op; /* r/~w */
  60. if (op == CY_SPI_WR_OP)
  61. memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
  62. memset(xfer, 0, sizeof(xfer));
  63. spi_message_init(&msg);
  64. /*
  65. We set both TX and RX buffers because Cypress TTSP
  66. requires full duplex operation.
  67. */
  68. xfer[0].tx_buf = wr_buf;
  69. xfer[0].rx_buf = rd_buf;
  70. switch (op) {
  71. case CY_SPI_WR_OP:
  72. xfer[0].len = length + CY_SPI_CMD_BYTES;
  73. spi_message_add_tail(&xfer[0], &msg);
  74. break;
  75. case CY_SPI_RD_OP:
  76. xfer[0].len = CY_SPI_CMD_BYTES;
  77. spi_message_add_tail(&xfer[0], &msg);
  78. xfer[1].rx_buf = buf;
  79. xfer[1].len = length;
  80. spi_message_add_tail(&xfer[1], &msg);
  81. break;
  82. default:
  83. dev_err(dev, "%s: bad operation code=%d\n", __func__, op);
  84. return -EINVAL;
  85. }
  86. retval = spi_sync(spi, &msg);
  87. if (retval < 0) {
  88. dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
  89. __func__, retval, xfer[1].len, op);
  90. /*
  91. * do not return here since was a bad ACK sequence
  92. * let the following ACK check handle any errors and
  93. * allow silent retries
  94. */
  95. }
  96. if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK1 ||
  97. rd_buf[CY_SPI_SYNC_BYTE + 1] != CY_SPI_SYNC_ACK2) {
  98. dev_dbg(dev, "%s: operation %d failed\n", __func__, op);
  99. for (i = 0; i < CY_SPI_CMD_BYTES; i++)
  100. dev_dbg(dev, "%s: test rd_buf[%d]:0x%02x\n",
  101. __func__, i, rd_buf[i]);
  102. for (i = 0; i < length; i++)
  103. dev_dbg(dev, "%s: test buf[%d]:0x%02x\n",
  104. __func__, i, buf[i]);
  105. return -EIO;
  106. }
  107. return 0;
  108. }
  109. static int cyttsp_spi_read_block_data(struct device *dev, u8 *xfer_buf,
  110. u16 addr, u8 length, void *data)
  111. {
  112. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data,
  113. length);
  114. }
  115. static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf,
  116. u16 addr, u8 length, const void *data)
  117. {
  118. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data,
  119. length);
  120. }
  121. static const struct cyttsp_bus_ops cyttsp_spi_bus_ops = {
  122. .bustype = BUS_SPI,
  123. .write = cyttsp_spi_write_block_data,
  124. .read = cyttsp_spi_read_block_data,
  125. };
  126. static int cyttsp_spi_probe(struct spi_device *spi)
  127. {
  128. struct cyttsp *ts;
  129. int error;
  130. /* Set up SPI*/
  131. spi->bits_per_word = CY_SPI_BITS_PER_WORD;
  132. spi->mode = SPI_MODE_0;
  133. error = spi_setup(spi);
  134. if (error < 0) {
  135. dev_err(&spi->dev, "%s: SPI setup error %d\n",
  136. __func__, error);
  137. return error;
  138. }
  139. ts = cyttsp_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
  140. CY_SPI_DATA_BUF_SIZE * 2);
  141. if (IS_ERR(ts))
  142. return PTR_ERR(ts);
  143. spi_set_drvdata(spi, ts);
  144. return 0;
  145. }
  146. static int cyttsp_spi_remove(struct spi_device *spi)
  147. {
  148. struct cyttsp *ts = spi_get_drvdata(spi);
  149. cyttsp_remove(ts);
  150. return 0;
  151. }
  152. static struct spi_driver cyttsp_spi_driver = {
  153. .driver = {
  154. .name = CY_SPI_NAME,
  155. .pm = &cyttsp_pm_ops,
  156. },
  157. .probe = cyttsp_spi_probe,
  158. .remove = cyttsp_spi_remove,
  159. };
  160. module_spi_driver(cyttsp_spi_driver);
  161. MODULE_LICENSE("GPL");
  162. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
  163. MODULE_AUTHOR("Cypress");
  164. MODULE_ALIAS("spi:cyttsp");