diff options
Diffstat (limited to 'drivers/input/touchscreen/rmi4/rmi_spi.c')
| -rw-r--r-- | drivers/input/touchscreen/rmi4/rmi_spi.c | 869 |
1 files changed, 869 insertions, 0 deletions
diff --git a/drivers/input/touchscreen/rmi4/rmi_spi.c b/drivers/input/touchscreen/rmi4/rmi_spi.c new file mode 100644 index 00000000000..41f72657f82 --- /dev/null +++ b/drivers/input/touchscreen/rmi4/rmi_spi.c | |||
| @@ -0,0 +1,869 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2011 Synaptics Incorporated | ||
| 3 | * Copyright (c) 2011 Unixphere | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 18 | */ | ||
| 19 | #include <linux/kernel.h> | ||
| 20 | #include <linux/module.h> | ||
| 21 | #include <linux/spi/spi.h> | ||
| 22 | #include <linux/delay.h> | ||
| 23 | #include <linux/slab.h> | ||
| 24 | #include <linux/completion.h> | ||
| 25 | #include <linux/sched.h> | ||
| 26 | #include <linux/gpio.h> | ||
| 27 | #include <linux/rmi.h> | ||
| 28 | |||
| 29 | #define COMMS_DEBUG 0 | ||
| 30 | |||
| 31 | #define RMI_PROTOCOL_VERSION_ADDRESS 0xa0fd | ||
| 32 | #define SPI_V2_UNIFIED_READ 0xc0 | ||
| 33 | #define SPI_V2_WRITE 0x40 | ||
| 34 | #define SPI_V2_PREPARE_SPLIT_READ 0xc8 | ||
| 35 | #define SPI_V2_EXECUTE_SPLIT_READ 0xca | ||
| 36 | |||
| 37 | #define RMI_SPI_BLOCK_DELAY_US 65 | ||
| 38 | #define RMI_SPI_BYTE_DELAY_US 65 | ||
| 39 | #define RMI_SPI_WRITE_DELAY_US 0 | ||
| 40 | |||
| 41 | #define RMI_V1_READ_FLAG 0x80 | ||
| 42 | |||
| 43 | #define RMI_PAGE_SELECT_REGISTER 0x00FF | ||
| 44 | #define RMI_SPI_PAGE(addr) (((addr) >> 8) & 0x80) | ||
| 45 | |||
| 46 | static char *spi_v1_proto_name = "spi"; | ||
| 47 | static char *spi_v2_proto_name = "spiv2"; | ||
| 48 | |||
| 49 | struct rmi_spi_data { | ||
| 50 | struct mutex page_mutex; | ||
| 51 | int page; | ||
| 52 | int (*set_page) (struct rmi_phys_device *phys, u8 page); | ||
| 53 | bool split_read_pending; | ||
| 54 | int enabled; | ||
| 55 | int irq; | ||
| 56 | int irq_flags; | ||
| 57 | struct rmi_phys_device *phys; | ||
| 58 | struct completion irq_comp; | ||
| 59 | }; | ||
| 60 | |||
| 61 | static irqreturn_t rmi_spi_hard_irq(int irq, void *p) | ||
| 62 | { | ||
| 63 | struct rmi_phys_device *phys = p; | ||
| 64 | struct rmi_spi_data *data = phys->data; | ||
| 65 | struct rmi_device_platform_data *pdata = phys->dev->platform_data; | ||
| 66 | |||
| 67 | if (data->split_read_pending && | ||
| 68 | gpio_get_value(irq_to_gpio(irq)) == pdata->irq_polarity) { | ||
| 69 | phys->info.attn_count++; | ||
| 70 | complete(&data->irq_comp); | ||
| 71 | return IRQ_HANDLED; | ||
| 72 | } | ||
| 73 | |||
| 74 | return IRQ_WAKE_THREAD; | ||
| 75 | } | ||
| 76 | |||
| 77 | static irqreturn_t rmi_spi_irq_thread(int irq, void *p) | ||
| 78 | { | ||
| 79 | struct rmi_phys_device *phys = p; | ||
| 80 | struct rmi_device *rmi_dev = phys->rmi_dev; | ||
| 81 | struct rmi_driver *driver = rmi_dev->driver; | ||
| 82 | struct rmi_device_platform_data *pdata = phys->dev->platform_data; | ||
| 83 | |||
| 84 | if (gpio_get_value(irq_to_gpio(irq)) == pdata->irq_polarity) { | ||
| 85 | phys->info.attn_count++; | ||
| 86 | if (driver && driver->irq_handler) | ||
| 87 | driver->irq_handler(rmi_dev, irq); | ||
| 88 | } | ||
| 89 | |||
| 90 | return IRQ_HANDLED; | ||
| 91 | } | ||
| 92 | |||
| 93 | static int rmi_spi_xfer(struct rmi_phys_device *phys, | ||
| 94 | const u8 *txbuf, unsigned n_tx, u8 *rxbuf, unsigned n_rx) | ||
| 95 | { | ||
| 96 | struct spi_device *client = to_spi_device(phys->dev); | ||
| 97 | struct rmi_spi_data *v2_data = phys->data; | ||
| 98 | struct rmi_device_platform_data *pdata = phys->dev->platform_data; | ||
| 99 | int status; | ||
| 100 | struct spi_message message; | ||
| 101 | struct spi_transfer *xfers; | ||
| 102 | int total_bytes = n_tx + n_rx; | ||
| 103 | u8 local_buf[total_bytes]; | ||
| 104 | int xfer_count = 0; | ||
| 105 | int xfer_index = 0; | ||
| 106 | int block_delay = n_rx > 0 ? pdata->spi_data.block_delay_us : 0; | ||
| 107 | int byte_delay = n_rx > 1 ? pdata->spi_data.read_delay_us : 0; | ||
| 108 | int write_delay = n_tx > 1 ? pdata->spi_data.write_delay_us : 0; | ||
| 109 | #if COMMS_DEBUG | ||
| 110 | int i; | ||
| 111 | #endif | ||
| 112 | // pr_info("in function ____%s____ \n", __func__); | ||
| 113 | |||
| 114 | if (v2_data->split_read_pending) { | ||
| 115 | block_delay = | ||
| 116 | n_rx > 0 ? pdata->spi_data.split_read_block_delay_us : 0; | ||
| 117 | byte_delay = | ||
| 118 | n_tx > 1 ? pdata->spi_data.split_read_byte_delay_us : 0; | ||
| 119 | write_delay = 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | if (n_tx) { | ||
| 123 | phys->info.tx_count++; | ||
| 124 | phys->info.tx_bytes += n_tx; | ||
| 125 | if (write_delay) | ||
| 126 | xfer_count += n_tx; | ||
| 127 | else | ||
| 128 | xfer_count += 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | if (n_rx) { | ||
| 132 | phys->info.rx_count++; | ||
| 133 | phys->info.rx_bytes += n_rx; | ||
| 134 | if (byte_delay) | ||
| 135 | xfer_count += n_rx; | ||
| 136 | else | ||
| 137 | xfer_count += 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | xfers = kcalloc(xfer_count, | ||
| 141 | sizeof(struct spi_transfer), GFP_KERNEL); | ||
| 142 | if (!xfers) | ||
| 143 | return -ENOMEM; | ||
| 144 | |||
| 145 | spi_message_init(&message); | ||
| 146 | |||
| 147 | if (n_tx) { | ||
| 148 | if (write_delay) { | ||
| 149 | for (xfer_index = 0; xfer_index < n_tx; | ||
| 150 | xfer_index++) { | ||
| 151 | memset(&xfers[xfer_index], 0, | ||
| 152 | sizeof(struct spi_transfer)); | ||
| 153 | xfers[xfer_index].len = 1; | ||
| 154 | xfers[xfer_index].delay_usecs = write_delay; | ||
| 155 | xfers[xfer_index].cs_change = 1; | ||
| 156 | xfers[xfer_index].tx_buf = txbuf + xfer_index; | ||
| 157 | spi_message_add_tail(&xfers[xfer_index], | ||
| 158 | &message); | ||
| 159 | } | ||
| 160 | } else { | ||
| 161 | memset(&xfers[0], 0, sizeof(struct spi_transfer)); | ||
| 162 | xfers[0].len = n_tx; | ||
| 163 | spi_message_add_tail(&xfers[0], &message); | ||
| 164 | memcpy(local_buf, txbuf, n_tx); | ||
| 165 | xfers[0].tx_buf = local_buf; | ||
| 166 | xfer_index++; | ||
| 167 | } | ||
| 168 | if (block_delay){ | ||
| 169 | xfers[xfer_index-1].delay_usecs = block_delay; | ||
| 170 | xfers[xfer_index].cs_change = 1; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | if (n_rx) { | ||
| 174 | if (byte_delay) { | ||
| 175 | int buffer_offset = n_tx; | ||
| 176 | for (; xfer_index < xfer_count; xfer_index++) { | ||
| 177 | memset(&xfers[xfer_index], 0, | ||
| 178 | sizeof(struct spi_transfer)); | ||
| 179 | xfers[xfer_index].len = 1; | ||
| 180 | xfers[xfer_index].delay_usecs = byte_delay; | ||
| 181 | xfers[xfer_index].cs_change = 1; | ||
| 182 | xfers[xfer_index].rx_buf = | ||
| 183 | local_buf + buffer_offset; | ||
| 184 | buffer_offset++; | ||
| 185 | spi_message_add_tail(&xfers[xfer_index], | ||
| 186 | &message); | ||
| 187 | } | ||
| 188 | } else { | ||
| 189 | memset(&xfers[xfer_index], 0, | ||
| 190 | sizeof(struct spi_transfer)); | ||
| 191 | xfers[xfer_index].len = n_rx; | ||
| 192 | xfers[xfer_index].rx_buf = local_buf + n_tx; | ||
| 193 | spi_message_add_tail(&xfers[xfer_index], &message); | ||
| 194 | xfer_index++; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | #if COMMS_DEBUG | ||
| 199 | if (n_tx) { | ||
| 200 | dev_info(&client->dev, "SPI sends %d bytes: ", n_tx); | ||
| 201 | for (i = 0; i < n_tx; i++) | ||
| 202 | // dev_info(&client->dev, "%02X ", txbuf[i]); | ||
| 203 | pr_info(": %02X ", txbuf[i]); | ||
| 204 | // dev_info(&client->dev, "\n"); | ||
| 205 | pr_info("\n"); | ||
| 206 | } | ||
| 207 | #endif | ||
| 208 | |||
| 209 | /* do the i/o */ | ||
| 210 | if (pdata->spi_data.cs_assert) { | ||
| 211 | status = pdata->spi_data.cs_assert( | ||
| 212 | pdata->spi_data.cs_assert_data, true); | ||
| 213 | if (!status) { | ||
| 214 | dev_err(phys->dev, "Failed to assert CS."); | ||
| 215 | /* nonzero means error */ | ||
| 216 | status = -1; | ||
| 217 | goto error_exit; | ||
| 218 | } else | ||
| 219 | status = 0; | ||
| 220 | } | ||
| 221 | |||
| 222 | if (pdata->spi_data.pre_delay_us) | ||
| 223 | udelay(pdata->spi_data.pre_delay_us); | ||
| 224 | |||
| 225 | status = spi_sync(client, &message); | ||
| 226 | |||
| 227 | if (pdata->spi_data.post_delay_us) | ||
| 228 | udelay(pdata->spi_data.post_delay_us); | ||
| 229 | |||
| 230 | if (pdata->spi_data.cs_assert) { | ||
| 231 | status = pdata->spi_data.cs_assert( | ||
| 232 | pdata->spi_data.cs_assert_data, false); | ||
| 233 | if (!status) { | ||
| 234 | dev_err(phys->dev, "Failed to deassert CS."); | ||
| 235 | /* nonzero means error */ | ||
| 236 | status = -1; | ||
| 237 | goto error_exit; | ||
| 238 | } else | ||
| 239 | status = 0; | ||
| 240 | } | ||
| 241 | |||
| 242 | if (status == 0) { | ||
| 243 | memcpy(rxbuf, local_buf + n_tx, n_rx); | ||
| 244 | status = message.status; | ||
| 245 | } else { | ||
| 246 | phys->info.tx_errs++; | ||
| 247 | phys->info.rx_errs++; | ||
| 248 | dev_err(phys->dev, "spi_sync failed with error code %d.", | ||
| 249 | status); | ||
| 250 | } | ||
| 251 | |||
| 252 | #if COMMS_DEBUG | ||
| 253 | if (n_rx) { | ||
| 254 | dev_info(&client->dev, "SPI received %d bytes: ", n_rx); | ||
| 255 | for (i = 0; i < n_rx; i++) | ||
| 256 | // dev_info(&client->dev, "%02X ", rxbuf[i]); | ||
| 257 | pr_info(": %02X ", rxbuf[i]); | ||
| 258 | // dev_info(&client->dev, "\n"); | ||
| 259 | pr_info("\n"); | ||
| 260 | } | ||
| 261 | #endif | ||
| 262 | |||
| 263 | error_exit: | ||
| 264 | kfree(xfers); | ||
| 265 | return status; | ||
| 266 | } | ||
| 267 | |||
| 268 | static int rmi_spi_v2_write_block(struct rmi_phys_device *phys, u16 addr, | ||
| 269 | u8 *buf, int len) | ||
| 270 | { | ||
| 271 | struct rmi_spi_data *data = phys->data; | ||
| 272 | u8 txbuf[len + 4]; | ||
| 273 | int error; | ||
| 274 | // pr_info("in function ____%s____ \n", __func__); | ||
| 275 | |||
| 276 | txbuf[0] = SPI_V2_WRITE; | ||
| 277 | txbuf[1] = (addr >> 8) & 0x00FF; | ||
| 278 | txbuf[2] = addr & 0x00FF; | ||
| 279 | txbuf[3] = len; | ||
| 280 | |||
| 281 | memcpy(&txbuf[4], buf, len); | ||
| 282 | |||
| 283 | mutex_lock(&data->page_mutex); | ||
| 284 | |||
| 285 | if (RMI_SPI_PAGE(addr) != data->page) { | ||
| 286 | error = data->set_page(phys, RMI_SPI_PAGE(addr)); | ||
| 287 | if (error < 0) | ||
| 288 | goto exit; | ||
| 289 | } | ||
| 290 | |||
| 291 | error = rmi_spi_xfer(phys, buf, len + 4, NULL, 0); | ||
| 292 | if (error < 0) | ||
| 293 | goto exit; | ||
| 294 | error = len; | ||
| 295 | |||
| 296 | exit: | ||
| 297 | mutex_unlock(&data->page_mutex); | ||
| 298 | return error; | ||
| 299 | } | ||
| 300 | |||
| 301 | static int rmi_spi_v2_write(struct rmi_phys_device *phys, u16 addr, u8 data) | ||
| 302 | { | ||
| 303 | int error = rmi_spi_v2_write_block(phys, addr, &data, 1); | ||
| 304 | // pr_info("in function ____%s____ \n", __func__); | ||
| 305 | |||
| 306 | return (error == 1) ? 0 : error; | ||
| 307 | } | ||
| 308 | |||
| 309 | static int rmi_spi_v1_write_block(struct rmi_phys_device *phys, u16 addr, | ||
| 310 | u8 *buf, int len) | ||
| 311 | { | ||
| 312 | struct rmi_spi_data *data = phys->data; | ||
| 313 | unsigned char txbuf[len + 2]; | ||
| 314 | int error; | ||
| 315 | // pr_info("in function ____%s____ \n", __func__); | ||
| 316 | |||
| 317 | txbuf[0] = addr >> 8; | ||
| 318 | txbuf[1] = addr; | ||
| 319 | memcpy(txbuf+2, buf, len); | ||
| 320 | |||
| 321 | mutex_lock(&data->page_mutex); | ||
| 322 | |||
| 323 | if (RMI_SPI_PAGE(addr) != data->page) { | ||
| 324 | error = data->set_page(phys, RMI_SPI_PAGE(addr)); | ||
| 325 | if (error < 0) | ||
| 326 | goto exit; | ||
| 327 | } | ||
| 328 | |||
| 329 | error = rmi_spi_xfer(phys, txbuf, len + 2, NULL, 0); | ||
| 330 | if (error < 0) | ||
| 331 | goto exit; | ||
| 332 | error = len; | ||
| 333 | |||
| 334 | exit: | ||
| 335 | mutex_unlock(&data->page_mutex); | ||
| 336 | return error; | ||
| 337 | } | ||
| 338 | |||
| 339 | static int rmi_spi_v1_write(struct rmi_phys_device *phys, u16 addr, u8 data) | ||
| 340 | { | ||
| 341 | int error = rmi_spi_v1_write_block(phys, addr, &data, 1); | ||
| 342 | // pr_info("in function ____%s____ \n", __func__); | ||
| 343 | |||
| 344 | return (error == 1) ? 0 : error; | ||
| 345 | } | ||
| 346 | |||
| 347 | static int rmi_spi_v2_split_read_block(struct rmi_phys_device *phys, u16 addr, | ||
| 348 | u8 *buf, int len) | ||
| 349 | { | ||
| 350 | struct rmi_spi_data *data = phys->data; | ||
| 351 | u8 txbuf[4]; | ||
| 352 | u8 rxbuf[len + 1]; /* one extra byte for read length */ | ||
| 353 | int error; | ||
| 354 | // pr_info("in function ____%s____ \n", __func__); | ||
| 355 | |||
| 356 | txbuf[0] = SPI_V2_PREPARE_SPLIT_READ; | ||
| 357 | txbuf[1] = (addr >> 8) & 0x00FF; | ||
| 358 | txbuf[2] = addr & 0x00ff; | ||
| 359 | txbuf[3] = len; | ||
| 360 | |||
| 361 | mutex_lock(&data->page_mutex); | ||
| 362 | |||
| 363 | if (RMI_SPI_PAGE(addr) != data->page) { | ||
| 364 | error = data->set_page(phys, RMI_SPI_PAGE(addr)); | ||
| 365 | if (error < 0) | ||
| 366 | goto exit; | ||
| 367 | } | ||
| 368 | |||
| 369 | data->split_read_pending = true; | ||
| 370 | |||
| 371 | error = rmi_spi_xfer(phys, txbuf, 4, NULL, 0); | ||
| 372 | if (error < 0) { | ||
| 373 | data->split_read_pending = false; | ||
| 374 | goto exit; | ||
| 375 | } | ||
| 376 | |||
| 377 | wait_for_completion(&data->irq_comp); | ||
| 378 | |||
| 379 | txbuf[0] = SPI_V2_EXECUTE_SPLIT_READ; | ||
| 380 | txbuf[1] = 0; | ||
| 381 | |||
| 382 | error = rmi_spi_xfer(phys, txbuf, 2, rxbuf, len + 1); | ||
| 383 | data->split_read_pending = false; | ||
| 384 | if (error < 0) | ||
| 385 | goto exit; | ||
| 386 | |||
| 387 | /* first byte is length */ | ||
| 388 | if (rxbuf[0] != len) { | ||
| 389 | error = -EIO; | ||
| 390 | goto exit; | ||
| 391 | } | ||
| 392 | |||
| 393 | memcpy(buf, rxbuf + 1, len); | ||
| 394 | error = len; | ||
| 395 | |||
| 396 | exit: | ||
| 397 | mutex_unlock(&data->page_mutex); | ||
| 398 | return error; | ||
| 399 | } | ||
| 400 | |||
| 401 | static int rmi_spi_v2_read_block(struct rmi_phys_device *phys, u16 addr, | ||
| 402 | u8 *buf, int len) | ||
| 403 | { | ||
| 404 | struct rmi_spi_data *data = phys->data; | ||
| 405 | u8 txbuf[4]; | ||
| 406 | int error; | ||
| 407 | // pr_info("in function ____%s____ \n", __func__); | ||
| 408 | |||
| 409 | txbuf[0] = SPI_V2_UNIFIED_READ; | ||
| 410 | txbuf[1] = (addr >> 8) & 0x00FF; | ||
| 411 | txbuf[2] = addr & 0x00ff; | ||
| 412 | txbuf[3] = len; | ||
| 413 | |||
| 414 | mutex_lock(&data->page_mutex); | ||
| 415 | |||
| 416 | if (RMI_SPI_PAGE(addr) != data->page) { | ||
| 417 | error = data->set_page(phys, RMI_SPI_PAGE(addr)); | ||
| 418 | if (error < 0) | ||
| 419 | goto exit; | ||
| 420 | } | ||
| 421 | |||
| 422 | error = rmi_spi_xfer(phys, txbuf, 4, buf, len); | ||
| 423 | if (error < 0) | ||
| 424 | goto exit; | ||
| 425 | error = len; | ||
| 426 | |||
| 427 | exit: | ||
| 428 | mutex_unlock(&data->page_mutex); | ||
| 429 | return error; | ||
| 430 | } | ||
| 431 | |||
| 432 | static int rmi_spi_v2_read(struct rmi_phys_device *phys, u16 addr, u8 *buf) | ||
| 433 | { | ||
| 434 | int error = rmi_spi_v2_read_block(phys, addr, buf, 1); | ||
| 435 | |||
| 436 | return (error == 1) ? 0 : error; | ||
| 437 | } | ||
| 438 | |||
| 439 | static int rmi_spi_v1_read_block(struct rmi_phys_device *phys, u16 addr, | ||
| 440 | u8 *buf, int len) | ||
| 441 | { | ||
| 442 | struct rmi_spi_data *data = phys->data; | ||
| 443 | u8 txbuf[2]; | ||
| 444 | int error; | ||
| 445 | // pr_info("in function ____%s____ \n", __func__); | ||
| 446 | |||
| 447 | txbuf[0] = (addr >> 8) | RMI_V1_READ_FLAG; | ||
| 448 | txbuf[1] = addr; | ||
| 449 | |||
| 450 | mutex_lock(&data->page_mutex); | ||
| 451 | |||
| 452 | if (RMI_SPI_PAGE(addr) != data->page) { | ||
| 453 | error = data->set_page(phys, RMI_SPI_PAGE(addr)); | ||
| 454 | if (error < 0) | ||
| 455 | goto exit; | ||
| 456 | } | ||
| 457 | |||
| 458 | error = rmi_spi_xfer(phys, txbuf, 2, buf, len); | ||
| 459 | // pr_info(" back in function %s, rmi_spi_xfer returned %d\n", __func__, error); | ||
| 460 | |||
| 461 | if (error < 0) | ||
| 462 | goto exit; | ||
| 463 | error = len; | ||
| 464 | |||
| 465 | exit: | ||
| 466 | mutex_unlock(&data->page_mutex); | ||
| 467 | return error; | ||
| 468 | } | ||
| 469 | |||
| 470 | static int rmi_spi_v1_read(struct rmi_phys_device *phys, u16 addr, u8 *buf) | ||
| 471 | { | ||
| 472 | int error = rmi_spi_v1_read_block(phys, addr, buf, 1); | ||
| 473 | pr_info("in function ____%s____ \n", __func__); | ||
| 474 | |||
| 475 | return (error == 1) ? 0 : error; | ||
| 476 | } | ||
| 477 | |||
| 478 | #define RMI_SPI_PAGE_SELECT_WRITE_LENGTH 1 | ||
| 479 | |||
| 480 | static int rmi_spi_v1_set_page(struct rmi_phys_device *phys, u8 page) | ||
| 481 | { | ||
| 482 | struct rmi_spi_data *data = phys->data; | ||
| 483 | u8 txbuf[] = {RMI_PAGE_SELECT_REGISTER >> 8, | ||
| 484 | RMI_PAGE_SELECT_REGISTER & 0xFF, page}; | ||
| 485 | int error; | ||
| 486 | pr_info("in function ____%s____ \n", __func__); | ||
| 487 | |||
| 488 | error = rmi_spi_xfer(phys, txbuf, sizeof(txbuf), NULL, 0); | ||
| 489 | if (error < 0) { | ||
| 490 | dev_err(phys->dev, "Failed to set page select, code: %d.\n", | ||
| 491 | error); | ||
| 492 | return error; | ||
| 493 | } | ||
| 494 | |||
| 495 | data->page = page; | ||
| 496 | |||
| 497 | return RMI_SPI_PAGE_SELECT_WRITE_LENGTH; | ||
| 498 | } | ||
| 499 | |||
| 500 | static int rmi_spi_v2_set_page(struct rmi_phys_device *phys, u8 page) | ||
| 501 | { | ||
| 502 | struct rmi_spi_data *data = phys->data; | ||
| 503 | |||
| 504 | u8 txbuf[] = {SPI_V2_WRITE, RMI_PAGE_SELECT_REGISTER >> 8, | ||
| 505 | RMI_PAGE_SELECT_REGISTER & 0xFF, | ||
| 506 | RMI_SPI_PAGE_SELECT_WRITE_LENGTH, page}; | ||
| 507 | int error; | ||
| 508 | pr_info("in function ____%s____ \n", __func__); | ||
| 509 | |||
| 510 | error = rmi_spi_xfer(phys, txbuf, sizeof(txbuf), NULL, 0); | ||
| 511 | if (error < 0) { | ||
| 512 | dev_err(phys->dev, "Failed to set page select, code: %d.\n", | ||
| 513 | error); | ||
| 514 | return error; | ||
| 515 | } | ||
| 516 | |||
| 517 | data->page = page; | ||
| 518 | |||
| 519 | return RMI_SPI_PAGE_SELECT_WRITE_LENGTH; | ||
| 520 | } | ||
| 521 | |||
| 522 | |||
| 523 | static int acquire_attn_irq(struct rmi_spi_data *data) | ||
| 524 | { | ||
| 525 | int retval = 0; | ||
| 526 | pr_info("in function ____%s____ \n", __func__); | ||
| 527 | pr_info(" irq = %d\n", data->irq); | ||
| 528 | pr_info(" rmi_spi_hard_irq = 0x%8x\n", rmi_spi_hard_irq); | ||
| 529 | pr_info(" rmi_spi_irq_thread = 0x%8x\n", rmi_spi_irq_thread); | ||
| 530 | pr_info(" data->irq_flags = 0x%8x\n", data->irq_flags); | ||
| 531 | pr_info(" dev_name(data->phys->dev) = %s\n", dev_name(data->phys->dev)); | ||
| 532 | pr_info(" data->phys = 0x%8x\n", data->phys); | ||
| 533 | |||
| 534 | retval = request_threaded_irq(data->irq, rmi_spi_hard_irq, | ||
| 535 | rmi_spi_irq_thread, data->irq_flags, | ||
| 536 | dev_name(data->phys->dev), data->phys); | ||
| 537 | |||
| 538 | pr_info(" retval = = %d\n", retval); | ||
| 539 | return retval; | ||
| 540 | } | ||
| 541 | |||
| 542 | static int enable_device(struct rmi_phys_device *phys) | ||
| 543 | { | ||
| 544 | int retval = 0; | ||
| 545 | |||
| 546 | struct rmi_spi_data *data = phys->data; | ||
| 547 | |||
| 548 | if (data->enabled) { | ||
| 549 | dev_info(phys->dev, "Physical device already enabled.\n"); | ||
| 550 | return 0; | ||
| 551 | } | ||
| 552 | |||
| 553 | retval = acquire_attn_irq(data); | ||
| 554 | if (retval) | ||
| 555 | goto error_exit; | ||
| 556 | |||
| 557 | data->enabled = true; | ||
| 558 | dev_info(phys->dev, "Physical device enabled.\n"); | ||
| 559 | return 0; | ||
| 560 | |||
| 561 | error_exit: | ||
| 562 | dev_err(phys->dev, "Failed to enable physical device. Code=%d.\n", | ||
| 563 | retval); | ||
| 564 | return retval; | ||
| 565 | } | ||
| 566 | |||
| 567 | |||
| 568 | static void disable_device(struct rmi_phys_device *phys) | ||
| 569 | { | ||
| 570 | struct rmi_spi_data *data = phys->data; | ||
| 571 | |||
| 572 | pr_info("in function ____%s____ \n", __func__); | ||
| 573 | if (!data->enabled) { | ||
| 574 | dev_warn(phys->dev, "Physical device already disabled.\n"); | ||
| 575 | return; | ||
| 576 | } | ||
| 577 | disable_irq(data->irq); | ||
| 578 | free_irq(data->irq, data->phys); | ||
| 579 | |||
| 580 | dev_info(phys->dev, "Physical device disabled.\n"); | ||
| 581 | data->enabled = false; | ||
| 582 | } | ||
| 583 | |||
| 584 | |||
| 585 | |||
| 586 | #define DUMMY_READ_SLEEP_US 10 | ||
| 587 | |||
| 588 | static int rmi_spi_check_device(struct rmi_phys_device *rmi_phys) | ||
| 589 | { | ||
| 590 | u8 buf[6]; | ||
| 591 | int error; | ||
| 592 | int i; | ||
| 593 | |||
| 594 | pr_info("in function ____%s____ \n", __func__); | ||
| 595 | |||
| 596 | /* Some SPI subsystems return 0 for the very first read you do. So | ||
| 597 | * we use this dummy read to get that out of the way. | ||
| 598 | */ | ||
| 599 | error = rmi_spi_v1_read_block(rmi_phys, PDT_START_SCAN_LOCATION, | ||
| 600 | buf, sizeof(buf)); | ||
| 601 | if (error < 0) { | ||
| 602 | dev_err(rmi_phys->dev, "dummy read failed with %d.\n", error); | ||
| 603 | return error; | ||
| 604 | } | ||
| 605 | udelay(DUMMY_READ_SLEEP_US); | ||
| 606 | |||
| 607 | /* Force page select to 0. | ||
| 608 | */ | ||
| 609 | error = rmi_spi_v1_set_page(rmi_phys, 0x00); | ||
| 610 | if (error < 0) | ||
| 611 | return error; | ||
| 612 | |||
| 613 | /* Now read the first PDT entry. We know where this is, and if the | ||
| 614 | * RMI4 device is out there, these 6 bytes will be something other | ||
| 615 | * than all 0x00 or 0xFF. We need to check for 0x00 and 0xFF, | ||
| 616 | * because many (maybe all) SPI implementations will return all 0x00 | ||
| 617 | * or all 0xFF on read if the device is not connected. | ||
| 618 | */ | ||
| 619 | error = rmi_spi_v1_read_block(rmi_phys, PDT_START_SCAN_LOCATION, | ||
| 620 | buf, sizeof(buf)); | ||
| 621 | if (error < 0) { | ||
| 622 | dev_err(rmi_phys->dev, "probe read failed with %d.\n", error); | ||
| 623 | return error; | ||
| 624 | } | ||
| 625 | |||
| 626 | dev_info(rmi_phys->dev, "probe read succeeded with %d.\n", error); | ||
| 627 | for (i = 0; i < sizeof(buf); i++) { | ||
| 628 | if (buf[i] != 0x00 && buf[i] != 0xFF) | ||
| 629 | return error; | ||
| 630 | } | ||
| 631 | |||
| 632 | dev_err(rmi_phys->dev, "probe read returned invalid block.\n"); | ||
| 633 | return -ENODEV; | ||
| 634 | } | ||
| 635 | |||
| 636 | static int __devinit rmi_spi_probe(struct spi_device *spi) | ||
| 637 | { | ||
| 638 | struct rmi_phys_device *rmi_phys; | ||
| 639 | struct rmi_spi_data *data; | ||
| 640 | struct rmi_device_platform_data *pdata = spi->dev.platform_data; | ||
| 641 | u8 buf[2]; | ||
| 642 | int error; | ||
| 643 | |||
| 644 | pr_info("%s: probe for rmi_spi device\n", __func__); | ||
| 645 | |||
| 646 | if (!pdata) { | ||
| 647 | dev_err(&spi->dev, "no platform data\n"); | ||
| 648 | return -EINVAL; | ||
| 649 | } | ||
| 650 | |||
| 651 | if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) | ||
| 652 | return -EINVAL; | ||
| 653 | |||
| 654 | spi->bits_per_word = 8; | ||
| 655 | spi->mode = SPI_MODE_3; | ||
| 656 | error = spi_setup(spi); | ||
| 657 | if (error < 0) { | ||
| 658 | dev_err(&spi->dev, "spi_setup failed!\n"); | ||
| 659 | return error; | ||
| 660 | } | ||
| 661 | |||
| 662 | rmi_phys = kzalloc(sizeof(struct rmi_phys_device), GFP_KERNEL); | ||
| 663 | if (!rmi_phys) | ||
| 664 | return -ENOMEM; | ||
| 665 | |||
| 666 | data = kzalloc(sizeof(struct rmi_spi_data), GFP_KERNEL); | ||
| 667 | if (!data) { | ||
| 668 | error = -ENOMEM; | ||
| 669 | goto err_phys; | ||
| 670 | } | ||
| 671 | data->enabled = true; /* We plan to come up enabled. */ | ||
| 672 | data->irq = gpio_to_irq(pdata->irq); | ||
| 673 | data->irq_flags = (pdata->irq_polarity == RMI_IRQ_ACTIVE_HIGH) ? | ||
| 674 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | ||
| 675 | data->phys = rmi_phys; | ||
| 676 | |||
| 677 | rmi_phys->data = data; | ||
| 678 | rmi_phys->dev = &spi->dev; | ||
| 679 | |||
| 680 | rmi_phys->write = rmi_spi_v1_write; | ||
| 681 | rmi_phys->write_block = rmi_spi_v1_write_block; | ||
| 682 | rmi_phys->read = rmi_spi_v1_read; | ||
| 683 | rmi_phys->read_block = rmi_spi_v1_read_block; | ||
| 684 | rmi_phys->enable_device = enable_device; | ||
| 685 | rmi_phys->disable_device = disable_device; | ||
| 686 | data->set_page = rmi_spi_v1_set_page; | ||
| 687 | |||
| 688 | rmi_phys->info.proto = spi_v1_proto_name; | ||
| 689 | |||
| 690 | mutex_init(&data->page_mutex); | ||
| 691 | |||
| 692 | pr_info("%s: setting the driverdata on the device\n", __func__); | ||
| 693 | |||
| 694 | dev_set_drvdata(&spi->dev, rmi_phys); | ||
| 695 | |||
| 696 | pr_info("%s: done setting driverdata %s\n", __func__, dev_name(&spi->dev)); | ||
| 697 | |||
| 698 | |||
| 699 | pdata->spi_data.block_delay_us = pdata->spi_data.block_delay_us ? | ||
| 700 | pdata->spi_data.block_delay_us : RMI_SPI_BLOCK_DELAY_US; | ||
| 701 | pdata->spi_data.read_delay_us = pdata->spi_data.read_delay_us ? | ||
| 702 | pdata->spi_data.read_delay_us : RMI_SPI_BYTE_DELAY_US; | ||
| 703 | pdata->spi_data.write_delay_us = pdata->spi_data.write_delay_us ? | ||
| 704 | pdata->spi_data.write_delay_us : RMI_SPI_BYTE_DELAY_US; | ||
| 705 | pdata->spi_data.split_read_block_delay_us = | ||
| 706 | pdata->spi_data.split_read_block_delay_us ? | ||
| 707 | pdata->spi_data.split_read_block_delay_us : | ||
| 708 | RMI_SPI_BLOCK_DELAY_US; | ||
| 709 | pdata->spi_data.split_read_byte_delay_us = | ||
| 710 | pdata->spi_data.split_read_byte_delay_us ? | ||
| 711 | pdata->spi_data.split_read_byte_delay_us : | ||
| 712 | RMI_SPI_BYTE_DELAY_US; | ||
| 713 | |||
| 714 | pr_info("%s configuring GPIOs\n", __func__); | ||
| 715 | |||
| 716 | if (pdata->gpio_config) { | ||
| 717 | error = pdata->gpio_config(&spi->dev, true); | ||
| 718 | if (error < 0) { | ||
| 719 | dev_err(&spi->dev, "Failed to setup GPIOs, code: %d.\n", | ||
| 720 | error); | ||
| 721 | goto err_data; | ||
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | error = rmi_spi_check_device(rmi_phys); | ||
| 726 | if (error < 0) | ||
| 727 | goto err_data; | ||
| 728 | |||
| 729 | /* check if this is an SPI v2 device */ | ||
| 730 | dev_info(&spi->dev, "%s: checking SPI version on RMI device\n", __func__); | ||
| 731 | error = rmi_spi_v1_read_block(rmi_phys, RMI_PROTOCOL_VERSION_ADDRESS, | ||
| 732 | buf, 2); | ||
| 733 | |||
| 734 | if (error < 0) { | ||
| 735 | dev_info(&spi->dev, "failed to get SPI version number!\n"); | ||
| 736 | dev_err(&spi->dev, "failed to get SPI version number!\n"); | ||
| 737 | goto err_data; | ||
| 738 | } | ||
| 739 | |||
| 740 | dev_info(&spi->dev, "SPI version is %d", buf[0]); | ||
| 741 | |||
| 742 | if (buf[0] == 1) { | ||
| 743 | /* SPIv2 */ | ||
| 744 | rmi_phys->write = rmi_spi_v2_write; | ||
| 745 | rmi_phys->write_block = rmi_spi_v2_write_block; | ||
| 746 | rmi_phys->read = rmi_spi_v2_read; | ||
| 747 | data->set_page = rmi_spi_v2_set_page; | ||
| 748 | |||
| 749 | rmi_phys->info.proto = spi_v2_proto_name; | ||
| 750 | |||
| 751 | if (pdata->irq > 0) { | ||
| 752 | init_completion(&data->irq_comp); | ||
| 753 | rmi_phys->read_block = rmi_spi_v2_split_read_block; | ||
| 754 | } else { | ||
| 755 | rmi_phys->read_block = rmi_spi_v2_read_block; | ||
| 756 | } | ||
| 757 | } else if (buf[0] != 0) { | ||
| 758 | dev_err(&spi->dev, "Unrecognized SPI version %d.\n", buf[0]); | ||
| 759 | error = -ENODEV; | ||
| 760 | goto err_data; | ||
| 761 | } | ||
| 762 | |||
| 763 | error = rmi_register_phys_device(rmi_phys); | ||
| 764 | if (error) { | ||
| 765 | dev_err(&spi->dev, "failed to register physical driver\n"); | ||
| 766 | goto err_data; | ||
| 767 | } | ||
| 768 | |||
| 769 | if (pdata->irq > 0) { | ||
| 770 | error = acquire_attn_irq(data); | ||
| 771 | if (error < 0) { | ||
| 772 | dev_err(&spi->dev, "request_threaded_irq failed %d\n", | ||
| 773 | pdata->irq); | ||
| 774 | goto err_unregister; | ||
| 775 | } | ||
| 776 | } | ||
| 777 | |||
| 778 | #if defined(CONFIG_RMI4_DEV) | ||
| 779 | pr_info(" CONFIG_RMI4_DEV is defined\n"); | ||
| 780 | |||
| 781 | error = gpio_export(pdata->irq, false); | ||
| 782 | if (error) { | ||
| 783 | dev_warn(&spi->dev, "WARNING: Failed to export ATTN gpio!\n"); | ||
| 784 | error = 0; | ||
| 785 | } else { | ||
| 786 | error = gpio_export_link(&(rmi_phys->rmi_dev->dev), "attn", | ||
| 787 | pdata->irq); | ||
| 788 | if (error) { | ||
| 789 | dev_warn(&(rmi_phys->rmi_dev->dev), "WARNING: " | ||
| 790 | "Failed to symlink ATTN gpio!\n"); | ||
| 791 | error = 0; | ||
| 792 | } else { | ||
| 793 | dev_info(&(rmi_phys->rmi_dev->dev), | ||
| 794 | "%s: Exported GPIO %d.", __func__, pdata->irq); | ||
| 795 | } | ||
| 796 | } | ||
| 797 | #endif /* CONFIG_RMI4_DEV */ | ||
| 798 | |||
| 799 | dev_info(&spi->dev, "registered RMI SPI driver\n"); | ||
| 800 | return 0; | ||
| 801 | |||
| 802 | err_unregister: | ||
| 803 | rmi_unregister_phys_device(rmi_phys); | ||
| 804 | err_data: | ||
| 805 | kfree(data); | ||
| 806 | err_phys: | ||
| 807 | kfree(rmi_phys); | ||
| 808 | return error; | ||
| 809 | } | ||
| 810 | |||
| 811 | static int __devexit rmi_spi_remove(struct spi_device *spi) | ||
| 812 | { | ||
| 813 | struct rmi_phys_device *phys = dev_get_drvdata(&spi->dev); | ||
| 814 | struct rmi_device_platform_data *pd = spi->dev.platform_data; | ||
| 815 | pr_info("in function ____%s____ \n", __func__); | ||
| 816 | |||
| 817 | rmi_unregister_phys_device(phys); | ||
| 818 | kfree(phys->data); | ||
| 819 | kfree(phys); | ||
| 820 | |||
| 821 | if (pd->gpio_config) | ||
| 822 | pd->gpio_config(&spi->dev, false); | ||
| 823 | |||
| 824 | return 0; | ||
| 825 | } | ||
| 826 | |||
| 827 | static const struct spi_device_id rmi_id[] = { | ||
| 828 | { "rmi", 0 }, | ||
| 829 | { "rmi_spi", 0 }, | ||
| 830 | { } | ||
| 831 | }; | ||
| 832 | MODULE_DEVICE_TABLE(spi, rmi_id); | ||
| 833 | |||
| 834 | static struct spi_driver rmi_spi_driver = { | ||
| 835 | .driver = { | ||
| 836 | .owner = THIS_MODULE, | ||
| 837 | .name = "rmi_spi", | ||
| 838 | //.mod_name = "rmi_spi", | ||
| 839 | //.bus = &spi_bus_type, | ||
| 840 | }, | ||
| 841 | .id_table = rmi_id, | ||
| 842 | .probe = rmi_spi_probe, | ||
| 843 | .remove = __devexit_p(rmi_spi_remove), | ||
| 844 | }; | ||
| 845 | |||
| 846 | static int __init rmi_spi_init(void) | ||
| 847 | { | ||
| 848 | pr_info("%s: registering synaptics spi driver (ref=124)\n", __func__); | ||
| 849 | pr_info(" driver.owner = 0x%x\n", (unsigned int)rmi_spi_driver.driver.owner); | ||
| 850 | pr_info(" driver.name = %s\n", rmi_spi_driver.driver.name); | ||
| 851 | pr_info(" id_table[0].name = %s\n", rmi_spi_driver.id_table[0].name ); | ||
| 852 | pr_info(" id_table[1].name = %s\n", rmi_spi_driver.id_table[1].name ); | ||
| 853 | pr_info(" probe function ptr = 0x%x\n", (unsigned int)rmi_spi_driver.probe ); | ||
| 854 | |||
| 855 | |||
| 856 | return spi_register_driver(&rmi_spi_driver); | ||
| 857 | } | ||
| 858 | |||
| 859 | static void __exit rmi_spi_exit(void) | ||
| 860 | { | ||
| 861 | spi_unregister_driver(&rmi_spi_driver); | ||
| 862 | } | ||
| 863 | |||
| 864 | MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>"); | ||
| 865 | MODULE_DESCRIPTION("RMI SPI driver"); | ||
| 866 | MODULE_LICENSE("GPL"); | ||
| 867 | |||
| 868 | module_init(rmi_spi_init); | ||
| 869 | module_exit(rmi_spi_exit); | ||
