0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include "cyttsp4_core.h"
0018
0019 #include <linux/delay.h>
0020 #include <linux/input.h>
0021 #include <linux/spi/spi.h>
0022
0023 #define CY_SPI_WR_OP 0x00
0024 #define CY_SPI_RD_OP 0x01
0025 #define CY_SPI_BITS_PER_WORD 8
0026 #define CY_SPI_A8_BIT 0x02
0027 #define CY_SPI_WR_HEADER_BYTES 2
0028 #define CY_SPI_RD_HEADER_BYTES 1
0029 #define CY_SPI_CMD_BYTES 2
0030 #define CY_SPI_SYNC_BYTE 0
0031 #define CY_SPI_SYNC_ACK 0x62
0032 #define CY_SPI_DATA_SIZE (2 * 256)
0033
0034 #define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE)
0035
0036 static int cyttsp_spi_xfer(struct device *dev, u8 *xfer_buf,
0037 u8 op, u16 reg, u8 *buf, int length)
0038 {
0039 struct spi_device *spi = to_spi_device(dev);
0040 struct spi_message msg;
0041 struct spi_transfer xfer[2];
0042 u8 *wr_buf = &xfer_buf[0];
0043 u8 rd_buf[CY_SPI_CMD_BYTES];
0044 int retval;
0045 int i;
0046
0047 if (length > CY_SPI_DATA_SIZE) {
0048 dev_err(dev, "%s: length %d is too big.\n",
0049 __func__, length);
0050 return -EINVAL;
0051 }
0052
0053 memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE);
0054 memset(rd_buf, 0, CY_SPI_CMD_BYTES);
0055
0056 wr_buf[0] = op + (((reg >> 8) & 0x1) ? CY_SPI_A8_BIT : 0);
0057 if (op == CY_SPI_WR_OP) {
0058 wr_buf[1] = reg & 0xFF;
0059 if (length > 0)
0060 memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length);
0061 }
0062
0063 memset(xfer, 0, sizeof(xfer));
0064 spi_message_init(&msg);
0065
0066
0067
0068
0069
0070 xfer[0].tx_buf = wr_buf;
0071 xfer[0].rx_buf = rd_buf;
0072 switch (op) {
0073 case CY_SPI_WR_OP:
0074 xfer[0].len = length + CY_SPI_CMD_BYTES;
0075 spi_message_add_tail(&xfer[0], &msg);
0076 break;
0077
0078 case CY_SPI_RD_OP:
0079 xfer[0].len = CY_SPI_RD_HEADER_BYTES;
0080 spi_message_add_tail(&xfer[0], &msg);
0081
0082 xfer[1].rx_buf = buf;
0083 xfer[1].len = length;
0084 spi_message_add_tail(&xfer[1], &msg);
0085 break;
0086
0087 default:
0088 dev_err(dev, "%s: bad operation code=%d\n", __func__, op);
0089 return -EINVAL;
0090 }
0091
0092 retval = spi_sync(spi, &msg);
0093 if (retval < 0) {
0094 dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n",
0095 __func__, retval, xfer[1].len, op);
0096
0097
0098
0099
0100
0101
0102 }
0103
0104 if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK) {
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 int rc;
0124
0125 rc = cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, NULL, 0);
0126 if (rc)
0127 return rc;
0128 else
0129 return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data,
0130 length);
0131 }
0132
0133 static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf,
0134 u16 addr, u8 length, const void *data)
0135 {
0136 return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data,
0137 length);
0138 }
0139
0140 static const struct cyttsp4_bus_ops cyttsp_spi_bus_ops = {
0141 .bustype = BUS_SPI,
0142 .write = cyttsp_spi_write_block_data,
0143 .read = cyttsp_spi_read_block_data,
0144 };
0145
0146 static int cyttsp4_spi_probe(struct spi_device *spi)
0147 {
0148 struct cyttsp4 *ts;
0149 int error;
0150
0151
0152 spi->bits_per_word = CY_SPI_BITS_PER_WORD;
0153 spi->mode = SPI_MODE_0;
0154 error = spi_setup(spi);
0155 if (error < 0) {
0156 dev_err(&spi->dev, "%s: SPI setup error %d\n",
0157 __func__, error);
0158 return error;
0159 }
0160
0161 ts = cyttsp4_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq,
0162 CY_SPI_DATA_BUF_SIZE);
0163
0164 return PTR_ERR_OR_ZERO(ts);
0165 }
0166
0167 static void cyttsp4_spi_remove(struct spi_device *spi)
0168 {
0169 struct cyttsp4 *ts = spi_get_drvdata(spi);
0170 cyttsp4_remove(ts);
0171 }
0172
0173 static struct spi_driver cyttsp4_spi_driver = {
0174 .driver = {
0175 .name = CYTTSP4_SPI_NAME,
0176 .pm = &cyttsp4_pm_ops,
0177 },
0178 .probe = cyttsp4_spi_probe,
0179 .remove = cyttsp4_spi_remove,
0180 };
0181
0182 module_spi_driver(cyttsp4_spi_driver);
0183
0184 MODULE_LICENSE("GPL");
0185 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver");
0186 MODULE_AUTHOR("Cypress");
0187 MODULE_ALIAS("spi:cyttsp4");