Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 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> |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 11 | * Copyright (C) 2013 Cypress Semiconductor |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 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 | * |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 23 | * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com> |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include "cyttsp_core.h" |
| 28 | |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/input.h> |
| 31 | #include <linux/spi/spi.h> |
| 32 | |
| 33 | #define CY_SPI_WR_OP 0x00 /* r/~w */ |
| 34 | #define CY_SPI_RD_OP 0x01 |
| 35 | #define CY_SPI_CMD_BYTES 4 |
| 36 | #define CY_SPI_SYNC_BYTE 2 |
| 37 | #define CY_SPI_SYNC_ACK1 0x62 /* from protocol v.2 */ |
| 38 | #define CY_SPI_SYNC_ACK2 0x9D /* from protocol v.2 */ |
| 39 | #define CY_SPI_DATA_SIZE 128 |
| 40 | #define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE) |
| 41 | #define CY_SPI_BITS_PER_WORD 8 |
| 42 | |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 43 | static int cyttsp_spi_xfer(struct device *dev, u8 *xfer_buf, |
Ferruh Yigit | 62f548d | 2013-07-04 14:02:57 -0700 | [diff] [blame] | 44 | u8 op, u16 reg, u8 *buf, int length) |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 45 | { |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 46 | struct spi_device *spi = to_spi_device(dev); |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 47 | struct spi_message msg; |
| 48 | struct spi_transfer xfer[2]; |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 49 | u8 *wr_buf = &xfer_buf[0]; |
| 50 | u8 *rd_buf = &xfer_buf[CY_SPI_DATA_BUF_SIZE]; |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 51 | int retval; |
| 52 | int i; |
| 53 | |
| 54 | if (length > CY_SPI_DATA_SIZE) { |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 55 | dev_err(dev, "%s: length %d is too big.\n", |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 56 | __func__, length); |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
| 60 | memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE); |
| 61 | memset(rd_buf, 0, CY_SPI_DATA_BUF_SIZE); |
| 62 | |
| 63 | wr_buf[0] = 0x00; /* header byte 0 */ |
| 64 | wr_buf[1] = 0xFF; /* header byte 1 */ |
| 65 | wr_buf[2] = reg; /* reg index */ |
| 66 | wr_buf[3] = op; /* r/~w */ |
| 67 | if (op == CY_SPI_WR_OP) |
| 68 | memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length); |
| 69 | |
| 70 | memset(xfer, 0, sizeof(xfer)); |
| 71 | spi_message_init(&msg); |
| 72 | |
| 73 | /* |
| 74 | We set both TX and RX buffers because Cypress TTSP |
| 75 | requires full duplex operation. |
| 76 | */ |
| 77 | xfer[0].tx_buf = wr_buf; |
| 78 | xfer[0].rx_buf = rd_buf; |
| 79 | switch (op) { |
| 80 | case CY_SPI_WR_OP: |
| 81 | xfer[0].len = length + CY_SPI_CMD_BYTES; |
| 82 | spi_message_add_tail(&xfer[0], &msg); |
| 83 | break; |
| 84 | |
| 85 | case CY_SPI_RD_OP: |
| 86 | xfer[0].len = CY_SPI_CMD_BYTES; |
| 87 | spi_message_add_tail(&xfer[0], &msg); |
| 88 | |
| 89 | xfer[1].rx_buf = buf; |
| 90 | xfer[1].len = length; |
| 91 | spi_message_add_tail(&xfer[1], &msg); |
| 92 | break; |
| 93 | |
| 94 | default: |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 95 | dev_err(dev, "%s: bad operation code=%d\n", __func__, op); |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | retval = spi_sync(spi, &msg); |
| 100 | if (retval < 0) { |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 101 | dev_dbg(dev, "%s: spi_sync() error %d, len=%d, op=%d\n", |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 102 | __func__, retval, xfer[1].len, op); |
| 103 | |
| 104 | /* |
| 105 | * do not return here since was a bad ACK sequence |
| 106 | * let the following ACK check handle any errors and |
| 107 | * allow silent retries |
| 108 | */ |
| 109 | } |
| 110 | |
| 111 | if (rd_buf[CY_SPI_SYNC_BYTE] != CY_SPI_SYNC_ACK1 || |
| 112 | rd_buf[CY_SPI_SYNC_BYTE + 1] != CY_SPI_SYNC_ACK2) { |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 113 | dev_dbg(dev, "%s: operation %d failed\n", __func__, op); |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 114 | |
| 115 | for (i = 0; i < CY_SPI_CMD_BYTES; i++) |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 116 | dev_dbg(dev, "%s: test rd_buf[%d]:0x%02x\n", |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 117 | __func__, i, rd_buf[i]); |
| 118 | for (i = 0; i < length; i++) |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 119 | dev_dbg(dev, "%s: test buf[%d]:0x%02x\n", |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 120 | __func__, i, buf[i]); |
| 121 | |
| 122 | return -EIO; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 128 | static int cyttsp_spi_read_block_data(struct device *dev, u8 *xfer_buf, |
Ferruh Yigit | 62f548d | 2013-07-04 14:02:57 -0700 | [diff] [blame] | 129 | u16 addr, u8 length, void *data) |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 130 | { |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 131 | return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_RD_OP, addr, data, |
| 132 | length); |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 135 | static int cyttsp_spi_write_block_data(struct device *dev, u8 *xfer_buf, |
Ferruh Yigit | 62f548d | 2013-07-04 14:02:57 -0700 | [diff] [blame] | 136 | u16 addr, u8 length, const void *data) |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 137 | { |
Ferruh Yigit | 9664877 | 2013-06-30 18:46:56 -0700 | [diff] [blame] | 138 | return cyttsp_spi_xfer(dev, xfer_buf, CY_SPI_WR_OP, addr, (void *)data, |
| 139 | length); |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static const struct cyttsp_bus_ops cyttsp_spi_bus_ops = { |
| 143 | .bustype = BUS_SPI, |
| 144 | .write = cyttsp_spi_write_block_data, |
| 145 | .read = cyttsp_spi_read_block_data, |
| 146 | }; |
| 147 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 148 | static int cyttsp_spi_probe(struct spi_device *spi) |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 149 | { |
| 150 | struct cyttsp *ts; |
| 151 | int error; |
| 152 | |
| 153 | /* Set up SPI*/ |
| 154 | spi->bits_per_word = CY_SPI_BITS_PER_WORD; |
| 155 | spi->mode = SPI_MODE_0; |
| 156 | error = spi_setup(spi); |
| 157 | if (error < 0) { |
| 158 | dev_err(&spi->dev, "%s: SPI setup error %d\n", |
| 159 | __func__, error); |
| 160 | return error; |
| 161 | } |
| 162 | |
| 163 | ts = cyttsp_probe(&cyttsp_spi_bus_ops, &spi->dev, spi->irq, |
| 164 | CY_SPI_DATA_BUF_SIZE * 2); |
| 165 | if (IS_ERR(ts)) |
| 166 | return PTR_ERR(ts); |
| 167 | |
| 168 | spi_set_drvdata(spi, ts); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 173 | static int cyttsp_spi_remove(struct spi_device *spi) |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 174 | { |
| 175 | struct cyttsp *ts = spi_get_drvdata(spi); |
| 176 | |
| 177 | cyttsp_remove(ts); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static struct spi_driver cyttsp_spi_driver = { |
| 183 | .driver = { |
| 184 | .name = CY_SPI_NAME, |
| 185 | .owner = THIS_MODULE, |
| 186 | .pm = &cyttsp_pm_ops, |
| 187 | }, |
| 188 | .probe = cyttsp_spi_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 189 | .remove = cyttsp_spi_remove, |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 190 | }; |
| 191 | |
Axel Lin | ca83922 | 2012-03-16 23:05:26 -0700 | [diff] [blame] | 192 | module_spi_driver(cyttsp_spi_driver); |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 193 | |
Javier Martinez Canillas | 4065d1e | 2012-01-31 00:18:00 -0800 | [diff] [blame] | 194 | MODULE_LICENSE("GPL"); |
| 195 | MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver"); |
| 196 | MODULE_AUTHOR("Cypress"); |
| 197 | MODULE_ALIAS("spi:cyttsp"); |