0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include "cyttsp_core.h"
0018
0019 #include <linux/delay.h>
0020 #include <linux/input.h>
0021 #include <linux/spi/spi.h>
0022
0023 #define CY_SPI_NAME "cyttsp-spi"
0024
0025 #define CY_SPI_WR_OP 0x00
0026 #define CY_SPI_RD_OP 0x01
0027 #define CY_SPI_CMD_BYTES 4
0028 #define CY_SPI_SYNC_BYTE 2
0029 #define CY_SPI_SYNC_ACK1 0x62
0030 #define CY_SPI_SYNC_ACK2 0x9D
0031 #define CY_SPI_DATA_SIZE 128
0032 #define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
0033 #define CY_SPI_BITS_PER_WORD 8
0034
0035 static int cyttsp_spi_xfer(struct device *dev, u8 *xfer_buf,
0036 u8 op, u16 reg, u8 *buf, int length)
0037 {
0038 struct spi_device *spi = to_spi_device(dev);
0039 struct spi_message msg;
0040 struct spi_transfer xfer[2];
0041 u8 *wr_buf = &xfer_buf[0];
0042 u8 *rd_buf = &xfer_buf[CY_SPI_DATA_BUF_SIZE];
0043 int retval;
0044 int i;
0045
0046 if (length > CY_SPI_DATA_SIZE) {
0047 dev_err(dev, "%s: length %d is too big.\n",
0048 __func__, length);
0049 return -EINVAL;
0050 }
0051
0052 memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
0053 memset(rd_buf, 0, CY_SPI_DATA_BUF_SIZE);
0054
0055 wr_buf[0] = 0x00;
0056 wr_buf[1] = 0xFF;
0057 wr_buf[2] = reg;
0058 wr_buf[3] = op;
0059 if (op == CY_SPI_WR_OP)
0060 memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
0061
0062 memset(xfer, 0, sizeof(xfer));
0063 spi_message_init(&msg);
0064
0065
0066
0067
0068
0069 xfer[0].tx_buf = wr_buf;
0070 xfer[0].rx_buf = rd_buf;
0071 switch (op) {
0072 case CY_SPI_WR_OP:
0073 xfer[0].len = length + CY_SPI_CMD_BYTES;
0074 spi_message_add_tail(&xfer[0], &msg);
0075 break;
0076
0077 case CY_SPI_RD_OP:
0078 xfer[0].len = CY_SPI_CMD_BYTES;
0079 spi_message_add_tail(&xfer[0], &msg);
0080
0081 xfer[1].rx_buf = buf;
0082 xfer[1].len = length;
0083 spi_message_add_tail(&xfer[1], &msg);
0084 break;
0085
0086 default:
0087 dev_err(dev, "%s: bad operation code=%d\n", __func__, op);
0088 return -EINVAL;
0089 }
0090
0091 retval = spi_sync(spi, &msg);
0092 if (retval < 0) {
0093 dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
0094 __func__, retval, xfer[1].len, op);
0095
0096
0097
0098
0099
0100
0101 }
0102
0103 if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK1 ||
0104 rd_buf[CY_SPI_SYNC_BYTE + 1] != CY_SPI_SYNC_ACK2) {
0105 dev_dbg(dev, "%s: operation %d failed\n", __func__, op);
0106
0107 for (i = 0; i < CY_SPI_CMD_BYTES; i++)
0108 dev_dbg(dev, "%s: test rd_buf[%d]:0x%02x\n",
0109 __func__, i, rd_buf[i]);
0110 for (i = 0; i < length; i++)
0111 dev_dbg(dev, "%s: test buf[%d]:0x%02x\n",
0112 __func__, i, buf[i]);
0113
0114 return -EIO;
0115 }
0116
0117 return 0;
0118 }
0119
0120 static int cyttsp_spi_read_block_data(struct device *dev, u8 *xfer_buf,
0121 u16 addr, u8 length, void *data)
0122 {
0123 return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data,
0124 length);
0125 }
0126
0127 static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf,
0128 u16 addr, u8 length, const void *data)
0129 {
0130 return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data,
0131 length);
0132 }
0133
0134 static const struct cyttsp_bus_ops cyttsp_spi_bus_ops = {
0135 .bustype = BUS_SPI,
0136 .write = cyttsp_spi_write_block_data,
0137 .read = cyttsp_spi_read_block_data,
0138 };
0139
0140 static int cyttsp_spi_probe(struct spi_device *spi)
0141 {
0142 struct cyttsp *ts;
0143 int error;
0144
0145
0146 spi->bits_per_word = CY_SPI_BITS_PER_WORD;
0147 spi->mode = SPI_MODE_0;
0148 error = spi_setup(spi);
0149 if (error < 0) {
0150 dev_err(&spi->dev, "%s: SPI setup error %d\n",
0151 __func__, error);
0152 return error;
0153 }
0154
0155 ts = cyttsp_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
0156 CY_SPI_DATA_BUF_SIZE * 2);
0157 if (IS_ERR(ts))
0158 return PTR_ERR(ts);
0159
0160 spi_set_drvdata(spi, ts);
0161
0162 return 0;
0163 }
0164
0165 static const struct of_device_id cyttsp_of_spi_match[] = {
0166 { .compatible = "cypress,cy8ctma340", },
0167 { .compatible = "cypress,cy8ctst341", },
0168 { }
0169 };
0170 MODULE_DEVICE_TABLE(of, cyttsp_of_spi_match);
0171
0172 static struct spi_driver cyttsp_spi_driver = {
0173 .driver = {
0174 .name = CY_SPI_NAME,
0175 .pm = &cyttsp_pm_ops,
0176 .of_match_table = cyttsp_of_spi_match,
0177 },
0178 .probe = cyttsp_spi_probe,
0179 };
0180
0181 module_spi_driver(cyttsp_spi_driver);
0182
0183 MODULE_LICENSE("GPL");
0184 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
0185 MODULE_AUTHOR("Cypress");
0186 MODULE_ALIAS("spi:cyttsp");