From patchwork Thu Jul 28 10:27:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez Canillas X-Patchwork-Id: 1015232 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.4) with ESMTP id p6SASX21007691 for ; Thu, 28 Jul 2011 10:28:33 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754880Ab1G1K2c (ORCPT ); Thu, 28 Jul 2011 06:28:32 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:60776 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754403Ab1G1K2b (ORCPT ); Thu, 28 Jul 2011 06:28:31 -0400 Received: by mail-wy0-f174.google.com with SMTP id 8so7448wyg.19 for ; Thu, 28 Jul 2011 03:28:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=VhCg45RDBVHTGqXRIfQYt9Cehl+dOSDh3mpm1VCq9Sk=; b=DKgp7VkOZZdxtZ92nU26m+4+QlC7NikV3cR8I4/UGPyap9/a9UfnO7YGAOkIuLY59W Gq4vBQff9eDSB+rtEI6i9rX62iCuq1lqglcJs6WW5DWorf5QJGLabS1L57LZkSRy1rzH YdEvNrsySffiZXoU2zFlWD+UwVJo9y7M94r9g= Received: by 10.227.198.78 with SMTP id en14mr1172647wbb.92.1311848910334; Thu, 28 Jul 2011 03:28:30 -0700 (PDT) Received: from localhost.localdomain (211.Red-79-146-221.dynamicIP.rima-tde.net [79.146.221.211]) by mx.google.com with ESMTPS id h1sm568208wee.25.2011.07.28.03.28.28 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 28 Jul 2011 03:28:29 -0700 (PDT) From: Javier Martinez Canillas Cc: Kevin McNeely , devel@linuxdriverproject.org, kernel-janitors@vger.kernel.org, linux-input@vger.kernel.org, Steve Kolokowsky , rydberg@euromail.se, Oleg Drokin , William Marone , Javier Martinez Canillas Subject: [PATCH 3/3] staging: cyttsp: Add Cypress TTSP G3 SPI Device Driver Date: Thu, 28 Jul 2011 12:27:42 +0200 Message-Id: <1311848862-24236-4-git-send-email-martinez.javier@gmail.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1311848862-24236-1-git-send-email-martinez.javier@gmail.com> References: <1311848862-24236-1-git-send-email-martinez.javier@gmail.com> To: unlisted-recipients:; (no To-header on input) Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Thu, 28 Jul 2011 10:28:33 +0000 (UTC) Cypress TTSP Gen3 SPI Device Driver. Provides spi communications modules for the Cypress TTSP Gen3 MTDEV Core Driver. The original author of this driver is: Kevin McNeely Signed-off-by: Javier Martinez Canillas --- drivers/staging/cyttsp/Kconfig | 12 ++ drivers/staging/cyttsp/Makefile | 1 + drivers/staging/cyttsp/cyttsp_spi.c | 318 +++++++++++++++++++++++++++++++++++ 3 files changed, 331 insertions(+), 0 deletions(-) create mode 100644 drivers/staging/cyttsp/cyttsp_spi.c diff --git a/drivers/staging/cyttsp/Kconfig b/drivers/staging/cyttsp/Kconfig index b1ffb2a..cb661ca 100644 --- a/drivers/staging/cyttsp/Kconfig +++ b/drivers/staging/cyttsp/Kconfig @@ -31,3 +31,15 @@ config TOUCHSCREEN_CYTTSP_I2C To compile this driver as a module, choose M here: the module will be called cyttsp-i2c. + +config TOUCHSCREEN_CYTTSP_SPI + tristate "Cypress TTSP spi touchscreen" + depends on SPI_MASTER && TOUCHSCREEN_CYTTSP_CORE + help + Say Y here if you have a Cypress TTSP touchscreen + connected to your with an SPI interface. + + If unsure, say N. + + To compile this driver as a module, choose M here: the + module will be called cyttsp-spi. diff --git a/drivers/staging/cyttsp/Makefile b/drivers/staging/cyttsp/Makefile index 9101dc9..687eeaa 100644 --- a/drivers/staging/cyttsp/Makefile +++ b/drivers/staging/cyttsp/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_TOUCHSCREEN_CYTTSP_CORE) += cyttsp_core.o obj-$(CONFIG_TOUCHSCREEN_CYTTSP_I2C) += cyttsp_i2c.o +obj-$(CONFIG_TOUCHSCREEN_CYTTSP_SPI) += cyttsp_spi.o diff --git a/drivers/staging/cyttsp/cyttsp_spi.c b/drivers/staging/cyttsp/cyttsp_spi.c new file mode 100644 index 0000000..108c37f --- /dev/null +++ b/drivers/staging/cyttsp/cyttsp_spi.c @@ -0,0 +1,318 @@ +/* + * Source for: + * Cypress TrueTouch(TM) Standard Product (TTSP) SPI touchscreen driver. + * For use with Cypress Txx3xx parts. + * Supported parts include: + * CY8CTST341 + * CY8CTMA340 + * + * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2, and only version 2, as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contact Cypress Semiconductor at www.cypress.com + * + */ + +#include "cyttsp_core.h" + +#include +#include + +#define CY_SPI_WR_OP 0x00 /* r/~w */ +#define CY_SPI_RD_OP 0x01 +#define CY_SPI_CMD_BYTES 4 +#define CY_SPI_SYNC_BYTE 2 +#define CY_SPI_SYNC_ACK1 0x62 /* from protocol v.2 */ +#define CY_SPI_SYNC_ACK2 0x9D /* from protocol v.2 */ +#define CY_SPI_DATA_SIZE 128 +#define CY_SPI_DATA_BUF_SIZE (CY_SPI_CMD_BYTES + CY_SPI_DATA_SIZE) +#define CY_SPI_BITS_PER_WORD 8 + +struct cyttsp_spi { + struct cyttsp_bus_ops ops; + struct spi_device *spi_client; + void *ttsp_client; + u8 wr_buf[CY_SPI_DATA_BUF_SIZE]; + u8 rd_buf[CY_SPI_DATA_BUF_SIZE]; +}; + +static void spi_complete(void *arg) +{ + complete(arg); +} + +static int spi_sync_tmo(struct cyttsp_spi *ts, struct spi_message *message) +{ + DECLARE_COMPLETION_ONSTACK(done); + int status; + + message->complete = spi_complete; + message->context = &done; + status = spi_async(ts->spi_client, message); + if (status == 0) { + int ret = wait_for_completion_interruptible_timeout(&done, HZ); + if (!ret) { + dev_dbg(ts->ops.dev, "%s: timeout\n", __func__); + status = -EIO; + } else + status = message->status; + } + message->context = NULL; + return status; +} + +static int cyttsp_spi_xfer_(u8 op, struct cyttsp_spi *ts, + u8 reg, u8 *buf, int length) +{ + struct spi_message msg; + struct spi_transfer xfer[2]; + u8 *wr_buf = ts->wr_buf; + u8 *rd_buf = ts->rd_buf; + int retval; + + if (length > CY_SPI_DATA_SIZE) { + dev_dbg(ts->ops.dev, "%s: length %d is too big.\n", + __func__, length); + return -EINVAL; + } + + memset(wr_buf, 0, CY_SPI_DATA_BUF_SIZE); + memset(rd_buf, 0, CY_SPI_DATA_BUF_SIZE); + + wr_buf[0] = 0x00; /* header byte 0 */ + wr_buf[1] = 0xFF; /* header byte 1 */ + wr_buf[2] = reg; /* reg index */ + wr_buf[3] = op; /* r/~w */ + if (op == CY_SPI_WR_OP) + memcpy(wr_buf + CY_SPI_CMD_BYTES, buf, length); + + memset((void *)xfer, 0, sizeof(xfer)); + spi_message_init(&msg); + xfer[0].tx_buf = wr_buf; + xfer[0].rx_buf = rd_buf; + if (op == CY_SPI_WR_OP) { + xfer[0].len = length + CY_SPI_CMD_BYTES; + spi_message_add_tail(&xfer[0], &msg); + } else if (op == CY_SPI_RD_OP) { + xfer[0].len = CY_SPI_CMD_BYTES; + spi_message_add_tail(&xfer[0], &msg); + + xfer[1].rx_buf = buf; + xfer[1].len = length; + spi_message_add_tail(&xfer[1], &msg); + } + + retval = spi_sync_tmo(ts, &msg); + if (retval < 0) { + dev_dbg(ts->ops.dev, "%s: spi sync error %d, len=%d, op=%d\n", + __func__, retval, xfer[1].len, op); + retval = 0; + } + + if (op == CY_SPI_RD_OP) { + if ((rd_buf[CY_SPI_SYNC_BYTE] == CY_SPI_SYNC_ACK1) && + (rd_buf[CY_SPI_SYNC_BYTE+1] == CY_SPI_SYNC_ACK2)) + retval = 0; + else { + int i; + for (i = 0; i < (CY_SPI_CMD_BYTES); i++) + dev_dbg(ts->ops.dev, + "%s: test rd_buf[%d]:0x%02x\n", + __func__, i, rd_buf[i]); + for (i = 0; i < (length); i++) + dev_dbg(ts->ops.dev, + "%s: test buf[%d]:0x%02x\n", + __func__, i, buf[i]); + retval = 1; + } + } + return retval; +} + +static int cyttsp_spi_xfer(u8 op, struct cyttsp_spi *ts, + u8 reg, u8 *buf, int length) +{ + int tries; + int retval; + + if (op == CY_SPI_RD_OP) { + for (tries = CY_NUM_RETRY; tries; tries--) { + retval = cyttsp_spi_xfer_(op, ts, reg, buf, length); + if (retval == 0) + break; + else + msleep(20); + } + } else { + retval = cyttsp_spi_xfer_(op, ts, reg, buf, length); + } + + return retval; +} + +static s32 ttsp_spi_read_block_data(void *handle, u8 addr, + u8 length, void *data) +{ + struct cyttsp_spi *ts = container_of(handle, struct cyttsp_spi, ops); + int retval; + + retval = cyttsp_spi_xfer(CY_SPI_RD_OP, ts, addr, data, length); + if (retval < 0) + dev_dbg(ts->ops.dev, "%s: ttsp_spi_read_block_data failed\n", + __func__); + + /* + * Do not print the above error if the data sync bytes were not found. + * This is a normal condition for the bootloader loader startup and need + * to retry until data sync bytes are found. + */ + if (retval > 0) + retval = -1; /* now signal fail; so retry can be done */ + + return retval; +} + +static s32 ttsp_spi_write_block_data(void *handle, u8 addr, + u8 length, const void *data) +{ + struct cyttsp_spi *ts = container_of(handle, struct cyttsp_spi, ops); + int retval; + + retval = cyttsp_spi_xfer(CY_SPI_WR_OP, ts, addr, (void *)data, length); + if (retval < 0) + dev_dbg(ts->ops.dev, "%s: ttsp_spi_write_block_data failed\n", + __func__); + + if (retval == -EIO) + return 0; + else + return retval; +} + +static s32 ttsp_spi_tch_ext(void *handle, void *values) +{ + struct cyttsp_spi *ts = container_of(handle, struct cyttsp_spi, ops); + int retval = 0; + + /* + * TODO: Add custom touch extension handling code here + * set: retval < 0 for any returned system errors, + * retval = 0 if normal touch handling is required, + * retval > 0 if normal touch handling is *not* required + */ + + if (!ts || !values) + retval = -EINVAL; + + return retval; +} + +static int __devinit cyttsp_spi_probe(struct spi_device *spi) +{ + struct cyttsp_spi *ts; + int retval; + + /* Set up SPI*/ + spi->bits_per_word = CY_SPI_BITS_PER_WORD; + spi->mode = SPI_MODE_0; + retval = spi_setup(spi); + if (retval < 0) { + dev_dbg(&spi->dev, "%s: SPI setup error %d\n", + __func__, retval); + return retval; + } + + ts = kzalloc(sizeof(*ts), GFP_KERNEL); + if (!ts) { + dev_dbg(&spi->dev, "%s: Error, kzalloc\n", __func__); + return -ENOMEM; + } + + ts->spi_client = spi; + dev_set_drvdata(&spi->dev, ts); + ts->ops.write = ttsp_spi_write_block_data; + ts->ops.read = ttsp_spi_read_block_data; + ts->ops.ext = ttsp_spi_tch_ext; + ts->ops.dev = &spi->dev; + + ts->ttsp_client = cyttsp_core_init(&ts->ops, &spi->dev); + if (IS_ERR(ts->ttsp_client)) { + int retval = PTR_ERR(ts->ttsp_client); + kfree(ts); + return retval; + } + + dev_dbg(ts->ops.dev, "%s: Registration complete\n", __func__); + + return 0; +} + +static int __devexit cyttsp_spi_remove(struct spi_device *spi) +{ + struct cyttsp_spi *ts = dev_get_drvdata(&spi->dev); + + cyttsp_core_release(ts->ttsp_client); + kfree(ts); + return 0; +} + +#ifdef CONFIG_PM +static int cyttsp_spi_suspend(struct spi_device *spi, pm_message_t message) +{ + struct cyttsp_spi *ts = dev_get_drvdata(&spi->dev); + + return cyttsp_suspend(ts->ttsp_client); +} + +static int cyttsp_spi_resume(struct spi_device *spi) +{ + struct cyttsp_spi *ts = dev_get_drvdata(&spi->dev); + + return cyttsp_resume(ts->ttsp_client); +} +#endif + +static struct spi_driver cyttsp_spi_driver = { + .driver = { + .name = CY_SPI_NAME, + .bus = &spi_bus_type, + .owner = THIS_MODULE, + }, + .probe = cyttsp_spi_probe, + .remove = __devexit_p(cyttsp_spi_remove), +#ifdef CONFIG_PM + .suspend = cyttsp_spi_suspend, + .resume = cyttsp_spi_resume, +#endif +}; + +static int __init cyttsp_spi_init(void) +{ + return spi_register_driver(&cyttsp_spi_driver); +} +module_init(cyttsp_spi_init); + +static void __exit cyttsp_spi_exit(void) +{ + spi_unregister_driver(&cyttsp_spi_driver); +} +module_exit(cyttsp_spi_exit); + +MODULE_ALIAS("spi:cyttsp"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) SPI driver"); +MODULE_AUTHOR("Cypress"); +