cyttsp4_spi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, u8 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. if (reg > 255)
  59. wr_buf[0] = op + CY_SPI_A8_BIT;
  60. else
  61. wr_buf[0] = op;
  62. if (op == CY_SPI_WR_OP)
  63. wr_buf[1] = reg % 256;
  64. if (op == CY_SPI_WR_OP && length > 0)
  65. memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
  66. memset(xfer, 0, sizeof(xfer));
  67. spi_message_init(&msg);
  68. /*
  69. We set both TX and RX buffers because Cypress TTSP
  70. requires full duplex operation.
  71. */
  72. xfer[0].tx_buf = wr_buf;
  73. xfer[0].rx_buf = rd_buf;
  74. switch (op) {
  75. case CY_SPI_WR_OP:
  76. xfer[0].len = length + CY_SPI_CMD_BYTES;
  77. spi_message_add_tail(&xfer[0], &msg);
  78. break;
  79. case CY_SPI_RD_OP:
  80. xfer[0].len = CY_SPI_RD_HEADER_BYTES;
  81. spi_message_add_tail(&xfer[0], &msg);
  82. xfer[1].rx_buf = buf;
  83. xfer[1].len = length;
  84. spi_message_add_tail(&xfer[1], &msg);
  85. break;
  86. default:
  87. dev_err(dev, "%s: bad operation code=%d\n", __func__, op);
  88. return -EINVAL;
  89. }
  90. retval = spi_sync(spi, &msg);
  91. if (retval < 0) {
  92. dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
  93. __func__, retval, xfer[1].len, op);
  94. /*
  95. * do not return here since was a bad ACK sequence
  96. * let the following ACK check handle any errors and
  97. * allow silent retries
  98. */
  99. }
  100. if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK) {
  101. dev_dbg(dev, "%s: operation %d failed\n", __func__, op);
  102. for (i = 0; i < CY_SPI_CMD_BYTES; i++)
  103. dev_dbg(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(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 device *dev, u8 *xfer_buf,
  113. u8 addr, u8 length, void *data)
  114. {
  115. int rc;
  116. rc = cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, NULL, 0);
  117. if (rc)
  118. return rc;
  119. else
  120. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data,
  121. length);
  122. }
  123. static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf,
  124. u8 addr, u8 length, const void *data)
  125. {
  126. return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data,
  127. length);
  128. }
  129. static const struct cyttsp4_bus_ops cyttsp_spi_bus_ops = {
  130. .bustype = BUS_SPI,
  131. .write = cyttsp_spi_write_block_data,
  132. .read = cyttsp_spi_read_block_data,
  133. };
  134. static int cyttsp4_spi_probe(struct spi_device *spi)
  135. {
  136. struct cyttsp4 *ts;
  137. int error;
  138. /* Set up SPI*/
  139. spi->bits_per_word = CY_SPI_BITS_PER_WORD;
  140. spi->mode = SPI_MODE_0;
  141. error = spi_setup(spi);
  142. if (error < 0) {
  143. dev_err(&spi->dev, "%s: SPI setup error %d\n",
  144. __func__, error);
  145. return error;
  146. }
  147. ts = cyttsp4_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
  148. CY_SPI_DATA_BUF_SIZE);
  149. if (IS_ERR(ts))
  150. return PTR_ERR(ts);
  151. return 0;
  152. }
  153. static int cyttsp4_spi_remove(struct spi_device *spi)
  154. {
  155. struct cyttsp4 *ts = spi_get_drvdata(spi);
  156. cyttsp4_remove(ts);
  157. return 0;
  158. }
  159. static struct spi_driver cyttsp4_spi_driver = {
  160. .driver = {
  161. .name = CYTTSP4_SPI_NAME,
  162. .owner = THIS_MODULE,
  163. .pm = &cyttsp4_pm_ops,
  164. },
  165. .probe = cyttsp4_spi_probe,
  166. .remove = cyttsp4_spi_remove,
  167. };
  168. module_spi_driver(cyttsp4_spi_driver);
  169. MODULE_LICENSE("GPL");
  170. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
  171. MODULE_AUTHOR("Cypress");
  172. MODULE_ALIAS("spi:cyttsp4");