From patchwork Mon Feb 20 10:33:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Vogelaar X-Patchwork-Id: 9582479 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0688660578 for ; Mon, 20 Feb 2017 10:34:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EB491287E2 for ; Mon, 20 Feb 2017 10:34:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DFDFD28853; Mon, 20 Feb 2017 10:34:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BCB2B287E2 for ; Mon, 20 Feb 2017 10:34:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752560AbdBTKeC (ORCPT ); Mon, 20 Feb 2017 05:34:02 -0500 Received: from mout.kundenserver.de ([217.72.192.75]:49472 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751402AbdBTKeB (ORCPT ); Mon, 20 Feb 2017 05:34:01 -0500 Received: from gt-cryptga1.gigatronik.com ([80.152.21.172]) by mrelayeu.kundenserver.de (mreue103 [212.227.15.183]) with ESMTPSA (Nemesis) id 0LzYuY-1cJ3bz1qbq-014jM2; Mon, 20 Feb 2017 11:33:57 +0100 Received: from gt-cryptga1.gt.local (localhost [127.0.0.1]) by gt-cryptga1.gigatronik.com (Postfix) with ESMTP id 19DB710006D; Mon, 20 Feb 2017 11:33:57 +0100 (CET) Received: from s-mex3.GT.local (s-mex3.gt.local [192.168.168.48]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by gt-cryptga1.gigatronik.com (Postfix) with ESMTPS id 1124E10006B; Mon, 20 Feb 2017 11:33:57 +0100 (CET) Received: from S-MEX2.GT.local (192.168.172.78) by s-mex3.GT.local (192.168.168.48) with Microsoft SMTP Server (TLS) id 14.3.235.1; Mon, 20 Feb 2017 11:33:56 +0100 Received: from m-ex1.GT.local (192.168.101.51) by S-MEX2.GT.local (192.168.168.41) with Microsoft SMTP Server (TLS) id 14.3.319.2; Mon, 20 Feb 2017 11:33:55 +0100 Received: from GT.local (192.168.208.68) by M-EX1.GT.local (192.168.101.51) with Microsoft SMTP Server (TLS) id 14.3.210.2; Mon, 20 Feb 2017 11:33:54 +0100 From: Patrick Vogelaar To: CC: , , , Patrick Vogelaar Subject: [PATCH 1/2] add driver for cypress cy8cmbr3102 Date: Mon, 20 Feb 2017 11:33:32 +0100 Message-ID: <1487586813-4122-2-git-send-email-Patrick.Vogelaar@gigatronik.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1487586813-4122-1-git-send-email-Patrick.Vogelaar@gigatronik.com> References: <1487586813-4122-1-git-send-email-Patrick.Vogelaar@gigatronik.com> MIME-Version: 1.0 X-Originating-IP: [192.168.208.68] X-KSE-AntiSpam-Interceptor-Info: white sender email list X-KSE-AttachmentFiltering-Interceptor-Info: protection disabled X-KSE-ServerInfo: M-EX1.GT.local, 9 X-KSE-Antivirus-Interceptor-Info: scan successful X-KSE-Antivirus-Info: Clean, bases: 20.02.2017 07:41:00 X-KSE-AntiSpam-Interceptor-Info: trusted connection X-KSE-AttachmentFiltering-Interceptor-Info: protection disabled X-Provags-ID: V03:K0:2LXjuw3KaB5MEwqg5ez2Qqd4Hi1KHdpnKaWQfjuVAEmqlKymRro IWIdksiP1f69XTlgmFYiTaAwWmYDrqvUFdXPSS0QF7/n1Hla86+lfAUqaHlyTdQNwSU+0Px 6rC/OgoXi6SUT5VMgjU23+/tlEBt15StRyFsWqO+rWfOkeUpm8IP7iXod+yJ157f8pfjvqH Su88pAtaFdHVNYaLB4u4g== X-UI-Out-Filterresults: notjunk:1; V01:K0:syIDBo+pTYY=:3fgVPzrpVVxqRXlNpPor/u ycKyCevn78n7KbomT4aQ1CwgNM963nG4wjtI/KtGi9WHL2Ag04tXfTZxJMtKL1B7xYvlPqZOm zFq27rb3zZkgrS/nrZ+qjzdubleFoC3WyJ3IwPwWzVi23C27ACjxza6IZ5YPi3Toigut02yTy 4GWwA2PzVXZXjDDpCI2qGgnjJLZN3MXuoTPEkAmxGV6u2BNlEoCw45mdd6ydJ72MMLIFAro/t 1v+HjekLNqlye1Jf0P+Lywlzoa4uG5yD1ADqOhSyCQ6HHzDI/LjyKhiYXFFZS13TotNqmpFy6 Dlt+BqTP5xbC5lQhRU2JdWLytJSFoxQOqSY83yaOKn24tMNAJgjCjANiG5xzBAaX13cUyWZCF UUNOSsBx/7gKzcn807zyiEMQMLZSDRZUzTPCB0uQ6uTMteMtYjHmSRHJHnqH+Hz+q3unmdXKr w8xeMI9iVjjBglnizGIn0XISrdJIhFDi3zf3wLvP6dBOWaFEpw+DlO7F+ih5/4pkvn16CTRo2 du083DBihoaQyIHnS0Zg/V/v1dw6pls914/X5GcbfUIJBvlgqzOQIoYYE57yGAwgiOh5IvYRR glECj+POs098RqvqYYyrwosy/AI1YWvdp+sIS9ztf3nzYSkP1a0gkNf1iAipL/+OTwtjI/s1e Z59do7DKm8tQWbXnMU25ducxJXWKmAVr1Hbq80F3IROCq7EB9ifkdL8Dz5SGpdYxQkrCKWG/j DD4L2DINuldRG5WP Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Patrick Vogelaar --- drivers/input/misc/Kconfig | 12 +++ drivers/input/misc/Makefile | 1 + drivers/input/misc/cy8cmbr3102.c | 222 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 235 insertions(+) create mode 100644 drivers/input/misc/cy8cmbr3102.c diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig index 5b6c522..be3071e 100644 --- a/drivers/input/misc/Kconfig +++ b/drivers/input/misc/Kconfig @@ -822,4 +822,16 @@ config INPUT_HISI_POWERKEY To compile this driver as a module, choose M here: the module will be called hisi_powerkey. +config INPUT_CY8CMBR3102 + tristate "Cypress CY8CMBR3102 CapSense Express controller" + depends on I2C + select INPUT_POLLDEV + default n + help + Say yes here to enable support for the Cypress CY8CMBR3102 + CapSense Express controller. + + To compile this driver as a module, choose M here: the module + will be called cy8cmbr3102. + endif diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile index b10523f..f9959ed 100644 --- a/drivers/input/misc/Makefile +++ b/drivers/input/misc/Makefile @@ -77,3 +77,4 @@ obj-$(CONFIG_INPUT_WM831X_ON) += wm831x-on.o obj-$(CONFIG_INPUT_XEN_KBDDEV_FRONTEND) += xen-kbdfront.o obj-$(CONFIG_INPUT_YEALINK) += yealink.o obj-$(CONFIG_INPUT_IDEAPAD_SLIDEBAR) += ideapad_slidebar.o +obj-$(CONFIG_INPUT_CY8CMBR3102) += cy8cmbr3102.o diff --git a/drivers/input/misc/cy8cmbr3102.c b/drivers/input/misc/cy8cmbr3102.c new file mode 100644 index 0000000..e2f5167 --- /dev/null +++ b/drivers/input/misc/cy8cmbr3102.c @@ -0,0 +1,222 @@ +/* Driver for Cypress CY8CMBR3102 CapSense Express Controller + * + * (C) 2017 by Gigatronik Technologies GmbH + * Author: Patrick Vogelaar + * All rights reserved. + * + * This code is based on mma8450.c and atmel_captouch.c. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * 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. + * + * NOTE: This implementation does not implement the full range of functions the + * Cypress CY8CMBR3102 CapSense Express controller provides. It only implements + * its use for connected touch buttons (yet). + */ + +#define DRV_VERSION "0.1" + +#include +#include +#include +#include +#include +#include +#include +#include + +/* I2C Registers */ +#define CY8CMBR3102_DEVICE_ID_REG 0x90 +#define CY8CMBR3102_BUTTON_STAT 0xAA + + +#define CY8CMBR3102_MAX_NUM_OF_BUTTONS 0x02 +#define CY8CMBR3102_DRV_NAME "cy8cmbr3102" +#define CY8CMBR3102_POLL_INTERVAL 200 +#define CY8CMBR3102_POLL_INTERVAL_MAX 300 +#define CY8CMBR3102_DEVICE_ID 2561 +#define CY8CMBR3102_MAX_RETRY 5 + +/* + * @i2c_client: I2C slave device client pointer + * @idev: Input (polled) device pointer + * @num_btn: Number of buttons + * @keycodes: map of button# to KeyCode + * @cy8cmbr3102_lock: mutex lock + */ +struct cy8cmbr3102_device { + struct i2c_client *client; + struct input_polled_dev *idev; + u32 num_btn; + u32 keycodes[CY8CMBR3102_MAX_NUM_OF_BUTTONS]; + struct mutex cy8cmbr3102_lock; +}; + +static const struct i2c_device_id cy8cmbr3102_idtable[] = { + {"cy8cmbr3102", 0}, + {} +}; +MODULE_DEVICE_TABLE(i2c, cy8cmbr3102_idtable); + +static void cy8cmbr3102_poll(struct input_polled_dev *idev) +{ + struct cy8cmbr3102_device *dev = idev->private; + int i, ret, btn_state; + + mutex_lock(&dev->cy8cmbr3102_lock); + + ret = i2c_smbus_read_word_data(dev->client, CY8CMBR3102_BUTTON_STAT); + if (ret < 0) { + dev_err(&dev->client->dev, "i2c io error: %d\n", ret); + mutex_unlock(&dev->cy8cmbr3102_lock); + return; + } + + for (i = 0; i < dev->num_btn; i++) { + btn_state = ret & (0x1 << i); + input_report_key(idev->input, dev->keycodes[i], btn_state); + input_sync(idev->input); + } + + mutex_unlock(&dev->cy8cmbr3102_lock); +} + +static int cy8cmbr3102_remove(struct i2c_client *client) +{ + struct cy8cmbr3102_device *dev = i2c_get_clientdata(client); + struct input_polled_dev *idev = dev->idev; + + dev_dbg(&client->dev, "%s\n", __func__); + + mutex_destroy(&dev->cy8cmbr3102_lock); + input_unregister_polled_device(idev); + + return 0; +} + +static int cy8cmbr3102_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct cy8cmbr3102_device *drvdata; + struct device *dev = &client->dev; + struct device_node *node; + int i, err, ret; + + dev_dbg(&client->dev, "%s\n", __func__); + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA | + I2C_FUNC_SMBUS_WORD_DATA | + I2C_FUNC_SMBUS_I2C_BLOCK)) { + dev_err(dev, "needed i2c functionality is not supported\n"); + return -EINVAL; + } + + drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); + if (!drvdata) + return -ENOMEM; + + drvdata->client = client; + i2c_set_clientdata(client, drvdata); + + /* device is in low-power mode and needs to be waken up */ + for (i = 0; (i < CY8CMBR3102_MAX_RETRY) && + (ret != CY8CMBR3102_DEVICE_ID); i++) { + ret = i2c_smbus_read_word_data(drvdata->client, + CY8CMBR3102_DEVICE_ID_REG); + dev_dbg(dev, "DEVICE_ID (%i): %i\n", i, ret); + } + + if (ret < 0) { + dev_err(&client->dev, "i2c io error: %d\n", ret); + return -EIO; + } else if (ret != CY8CMBR3102_DEVICE_ID) { + dev_err(&client->dev, "read device ID %i is not equal to %i!\n", + ret, CY8CMBR3102_DEVICE_ID); + return -ENXIO; + } + dev_dbg(dev, "device identified by device ID\n"); + + drvdata->idev = devm_input_allocate_polled_device(dev); + if (!drvdata->idev) { + dev_err(dev, "failed to allocate input device\n"); + return -ENOMEM; + } + + node = dev->of_node; + if (!node) { + dev_err(dev, "failed to find matching node in device tree\n"); + return -EINVAL; + } + + if (of_property_read_bool(node, "autorepeat")) + __set_bit(EV_REP, drvdata->idev->input->evbit); + + drvdata->num_btn = of_property_count_u32_elems(node, "linux,keycodes"); + if (drvdata->num_btn > CY8CMBR3102_MAX_NUM_OF_BUTTONS) + drvdata->num_btn = CY8CMBR3102_MAX_NUM_OF_BUTTONS; + + err = of_property_read_u32_array(node, "linux,keycodes", + drvdata->keycodes, drvdata->num_btn); + + if (err) { + dev_err(dev, "failed to read linux,keycodes property: %d\n", + err); + return err; + } + + for (i = 0; i < drvdata->num_btn; i++) + __set_bit(drvdata->keycodes[i], drvdata->idev->input->keybit); + + drvdata->idev->input->id.bustype = BUS_I2C; + drvdata->idev->input->id.product = 0x3102; + drvdata->idev->input->id.version = 0; + drvdata->idev->input->name = CY8CMBR3102_DRV_NAME; + drvdata->idev->poll = cy8cmbr3102_poll; + drvdata->idev->poll_interval = CY8CMBR3102_POLL_INTERVAL; + drvdata->idev->poll_interval_max = CY8CMBR3102_POLL_INTERVAL_MAX; + drvdata->idev->private = drvdata; + drvdata->idev->input->keycode = drvdata->keycodes; + drvdata->idev->input->keycodemax = drvdata->num_btn; + drvdata->idev->input->keycodesize = sizeof(drvdata->keycodes[0]); + __set_bit(EV_KEY, drvdata->idev->input->evbit); + + mutex_init(&drvdata->cy8cmbr3102_lock); + + err = input_register_polled_device(drvdata->idev); + if (err) + return err; + + dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); + return 0; +} + +#ifdef CONFIG_OF +static const struct of_device_id of_cy8cmbr3102_match[] = { + {.compatible = "cypress,cy8cmbr3102", }, + {} +}; +MODULE_DEVICE_TABLE(of, of_cy8cmbr3102_match); +#endif + +static struct i2c_driver cy8cmbr3102_driver = { + .driver = { + .name = "cy8cmbr3102", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(of_cy8cmbr3102_match), + }, + .probe = cy8cmbr3102_probe, + .remove = cy8cmbr3102_remove, + .id_table = cy8cmbr3102_idtable, +}; +module_i2c_driver(cy8cmbr3102_driver); + +MODULE_AUTHOR("Patrick Vogelaar "); +MODULE_DESCRIPTION("Cypress CY8CMBR3102 CapSense Express controller"); +MODULE_LICENSE("GPL"); +MODULE_VERSION(DRV_VERSION);