Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10961086ybi; Thu, 25 Jul 2019 07:42:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwaBoxmo3RwqCvlwi0qDc+D4pR81vmsL0vpuHJcSPq5lwS7SKx0yWdzLAKBc+O80r/+Nqv8 X-Received: by 2002:a17:902:2865:: with SMTP id e92mr51925954plb.264.1564065734123; Thu, 25 Jul 2019 07:42:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564065734; cv=none; d=google.com; s=arc-20160816; b=AKvPiZhpPS3t5n8vDHeGEQ9wTQO4W5R4wYknIMRAaOW799qKZokDe5NjdDBc/Td2ym 5x91MhtzeXD9wZmyHAKKyhtyDhrGauW7U73XVpugY5J5FCTzMYtl6vzhajx7NHYHDRi4 DbEZF8fuMYBcgWrZOYCqkqwh9RROL5yEMIa23BJDZ5FbKCZIxAv7y9nLhC7KEiRI8V5K fLKG104HL/lN5hrHTkzK36LL2xLMKVyCbXTcyyjJsrAAl08cl5p+khj1kDEu1u09x81G EYPomdmxD8A2cIkVMnHs/RZyiYJKPazWN9v9OLwAcWtwH2hHVw7/YrbgaMXL0SXDpy6F wAjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=rRtKwLAv+HGdCjSEwGiot0Z0x7eUFWZTIZUWFY+cWR0=; b=P3qz25Y3TIPbZ+f4X6HIA8jVcG2HyuCBuAa2/QPgw3gezH4U/JI5vsvDH4EbodHI9G U9+BstVsF/G8GKJFgVW+7BKuOGAjtyz7eW9ES+hG3iK6vIHzjkyOLycQcYmv56/BF/G2 O9gAaR7Zid3zAnYbxTJV72JaCsZuVQlIMf+rUAV1TzXo/osKaOI48ELMVx2DfxwWnWeS +elSWwDoU+JczJbQM4ZdhXmJAZ655jpR2/cn4gJHfcWxx+sibazyl0EEdR5oarA/7GOV 6OP7EB4D8bLd5RFUnCBs5l70CMm5z2bPmkeqe+6JEp/3jIVA+dSIcjM2n6l7hEdNRHjd bjXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=IfmT6be4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g63si17981055plb.362.2019.07.25.07.41.57; Thu, 25 Jul 2019 07:42:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=IfmT6be4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388207AbfGYOkS (ORCPT + 99 others); Thu, 25 Jul 2019 10:40:18 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:34003 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388164AbfGYOkS (ORCPT ); Thu, 25 Jul 2019 10:40:18 -0400 Received: by mail-lj1-f196.google.com with SMTP id p17so48268026ljg.1; Thu, 25 Jul 2019 07:40:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=rRtKwLAv+HGdCjSEwGiot0Z0x7eUFWZTIZUWFY+cWR0=; b=IfmT6be4nlhH9Srjk9i2gdOdERk2WFbuXIJ+hoQhemJy+G6RXHjsyMqrKbshfOtU8F 5TwOL5lC8bxzSNi/YTw7pVNG2VwREAJQ+oeRXiorM+cyGfZn0/VXc3p0D/xbOGq5H4bF ziZM6v7/AzIqpjXq5JTVDU5cyBRYTm48CTGVUC2MxpBg0dLyTBKPG0IRBJwt7qEV5ff8 f/YnRz0K6KG8myDJ8373OnW7/4Mk4QLetSY1UnCnWD3k4517GEk8VSveIrhDDrJLIjZs t6ZizajXF1Qx49W5Lokwdmo8wvS0x3iFMTGriBrJTz5hLVWzxS+FnYeKWeeoN+IoQlRN LX7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=rRtKwLAv+HGdCjSEwGiot0Z0x7eUFWZTIZUWFY+cWR0=; b=OoOKXL1qY4MEM79Ok7BmeOPj1CP+EU6DawOXMklcOHjoRE2Hdxz5akYH+rAO79+PyO bKHYIcetZ9eBezTLWC+Gu9FyQfguiT6kcTQcux5IKtfhTo+NMzaex4iT+u3thRCKsCIu qMt9nrM/41vBNQDMZAMHJWGF2DXRS47MX1EFTnp9WeMLkxsDDa12u53cE9k1eCH07xcA BRKq5L6Hqwi/nI22jcqktpmn/+Mil6kBSNb+a881J0UKb10UxBXFusm4tcOTNsffENKe 22jq0SqjJOI6NFaloXcLfH1a4+Ek7Tl5qTWoMbahN35LzXjXz3/z1TJ2DZxLg4rn7rvt iT8g== X-Gm-Message-State: APjAAAVVYq88pgLxd2UNoUuR3LFewAihyFQMtDUk/5HaOfY4M102aRBR lNFE1rRwAXi1ruarLysCUos= X-Received: by 2002:a2e:9753:: with SMTP id f19mr46074547ljj.113.1564065614686; Thu, 25 Jul 2019 07:40:14 -0700 (PDT) Received: from localhost ([188.170.223.67]) by smtp.gmail.com with ESMTPSA id b11sm9143315ljf.8.2019.07.25.07.40.11 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 25 Jul 2019 07:40:12 -0700 (PDT) Date: Thu, 25 Jul 2019 17:40:09 +0300 From: Dmitry Torokhov To: Michal =?utf-8?B?Vm9rw6HEjQ==?= Cc: Rob Herring , Mark Rutland , Shawn Guo , Sascha Hauer , Fabio Estevam , linux-input@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Pengutronix Kernel Team Subject: Re: [RFC PATCH v2 0/4] Input: mpr121-polled: Add polled driver for MPR121 Message-ID: <20190725144009.GA27432@penguin> References: <1558098773-47416-1-git-send-email-michal.vokac@ysoft.com> <20190521053705.GI183429@dtor-ws> <20190725085753.GA26665@penguin> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 25, 2019 at 02:58:02PM +0200, Michal Vokáč wrote: > On 25. 07. 19 10:57, Dmitry Torokhov wrote: > > Hi Michal, > > > > On Tue, May 21, 2019 at 08:51:17AM +0200, Michal Vokáč wrote: > > > On 21. 05. 19 7:37, Dmitry Torokhov wrote: > > > > Hi Michal, > > > > > > > > On Fri, May 17, 2019 at 03:12:49PM +0200, Michal Vokáč wrote: > > > > > Hi, > > > > > > > > > > I have to deal with a situation where we have a custom i.MX6 based > > > > > platform in production that uses the MPR121 touchkey controller. > > > > > Unfortunately the chip is connected using only the I2C interface. > > > > > The interrupt line is not used. Back in 2015 (Linux v3.14), my > > > > > colleague modded the existing mpr121_touchkey.c driver to use polling > > > > > instead of interrupt. > > > > > > > > > > For quite some time yet I am in a process of updating the product from > > > > > the ancient Freescale v3.14 kernel to the latest mainline and pushing > > > > > any needed changes upstream. The DT files for our imx6dl-yapp4 platform > > > > > already made it into v5.1-rc. > > > > > > > > > > I rebased and updated our mpr121 patch to the latest mainline. > > > > > It is created as a separate driver, similarly to gpio_keys_polled. > > > > > > > > > > The I2C device is quite susceptible to ESD. An ESD test quite often > > > > > causes reset of the chip or some register randomly changes its value. > > > > > The [PATCH 3/4] adds a write-through register cache. With the cache > > > > > this state can be detected and the device can be re-initialied. > > > > > > > > > > The main question is: Is there any chance that such a polled driver > > > > > could be accepted? Is it correct to implement it as a separate driver > > > > > or should it be done as an option in the existing driver? I can not > > > > > really imagine how I would do that though.. > > > > > > > > > > There are also certain worries that the MPR121 chip may no longer be > > > > > available in nonspecifically distant future. In case of EOL I will need > > > > > to add a polled driver for an other touchkey chip. May it be already > > > > > in mainline or a completely new one. > > > > > > > > I think that my addition of input_polled_dev was ultimately a wrong > > > > thing to do. I am looking into enabling polling mode for regular input > > > > devices as we then can enable polling mode in existing drivers. > > > > > > OK, that sounds good. Especially when one needs to switch from one chip > > > to another that is already in tree, the need for a whole new polling > > > driver is eliminated. > > > > Could you please try the patch below and see if it works for your use > > case? Note that I have not tried running it, but it compiles so it must > > be good ;) > > Hi Dmitry, > Thank you very much for the patch! > I gave it a shot and it seems you forgot to add the input-poller.h file > to the patch.. it does not compile on my side :( Oops ;) Please see the updated patch below. > > > Input: add support for polling to input devices > > > > From: Dmitry Torokhov > > > > Separating "normal" and "polled" input devices was a mistake, as often we want > > to allow the very same device work on both interrupt-driven and polled mode, > > depending on the board on which the device is used. > > > > This introduces new APIs: > > > > - input_setup_polling > > - input_set_poll_interval > > - input_set_min_poll_interval > > - input_set_max_poll_interval > > > > These new APIs allow switching an input device into polled mode with sysfs > > attributes matching drivers using input_polled_dev APIs that will be eventually > > removed. > > After reading this I am not really sure what else needs to be done > to test/use the poller. I suspect I need to modify the input device > driver (mpr121_touchkey.c in my case) like this: > > If the interrupt gpio is not provided in DT, the device driver probe > function should: > - not request the threaded interrupt > - call input_setup_polling and provide it with poll_fn > Can the mpr_touchkey_interrupt function be used as is for this > purpose? The only problem I see is it returns IRQ_HANDLED. I'd factor out code suitable for polling from mpr_touchkey_interrupt() and then do static irqreturn_t mpr_touchkey_interrupt(...) { mpr_touchkey_report(...); return IRQ_HANDLED; } > > - set the poll interval + min/max interval > - register the input device as usual (input_register_device) Yes. > - What about the device_init_wakeup? It does not make sense for > polling I think. We can either trust DT not to have the property for polled case, or add more checks. I think we should simply trust DT. Even if it is wrong it will not cause any issues. > > Just nitpicking - I also ran checkpatch.pl on the patch and it spits > out some warnings. I am not sure whether some of those should not > be fixed. I think in this particular case checkpatch is full of it. Thanks. Input: add support for polling to input devices From: Dmitry Torokhov Separating "normal" and "polled" input devices was a mistake, as often we want to allow the very same device work on both interrupt-driven and polled mode, depending on the board on which the device is used. This introduces new APIs: - input_setup_polling - input_set_poll_interval - input_set_min_poll_interval - input_set_max_poll_interval These new APIs allow switching an input device into polled mode with sysfs attributes matching drivers using input_polled_dev APIs that will be eventually removed. Signed-off-by: Dmitry Torokhov --- drivers/input/Makefile | 2 drivers/input/input-poller.c | 207 ++++++++++++++++++++++++++++++++++++++++++ drivers/input/input-poller.h | 18 ++++ drivers/input/input.c | 36 ++++++- include/linux/input.h | 10 ++ 5 files changed, 265 insertions(+), 8 deletions(-) create mode 100644 drivers/input/input-poller.c create mode 100644 drivers/input/input-poller.h diff --git a/drivers/input/Makefile b/drivers/input/Makefile index 40de6a7be641..e35650930371 100644 --- a/drivers/input/Makefile +++ b/drivers/input/Makefile @@ -6,7 +6,7 @@ # Each configuration option enables a list of files. obj-$(CONFIG_INPUT) += input-core.o -input-core-y := input.o input-compat.o input-mt.o ff-core.o +input-core-y := input.o input-compat.o input-mt.o input-poller.o ff-core.o obj-$(CONFIG_INPUT_FF_MEMLESS) += ff-memless.o obj-$(CONFIG_INPUT_POLLDEV) += input-polldev.o diff --git a/drivers/input/input-poller.c b/drivers/input/input-poller.c new file mode 100644 index 000000000000..008d6f362d60 --- /dev/null +++ b/drivers/input/input-poller.c @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Support for polling mode for input devices. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "input-poller.h" + +struct input_dev_poller { + void (*poll)(struct input_dev *dev); + + unsigned int poll_interval; /* msec */ + unsigned int poll_interval_max; /* msec */ + unsigned int poll_interval_min; /* msec */ + + struct input_dev *input; + struct delayed_work work; +}; + +static void input_dev_poller_queue_work(struct input_dev_poller *poller) +{ + unsigned long delay; + + delay = msecs_to_jiffies(poller->poll_interval); + if (delay >= HZ) + delay = round_jiffies_relative(delay); + + queue_delayed_work(system_freezable_wq, &poller->work, delay); +} + +static void input_dev_poller_work(struct work_struct *work) +{ + struct input_dev_poller *poller = + container_of(work, struct input_dev_poller, work.work); + + poller->poll(poller->input); + input_dev_poller_queue_work(poller); +} + +void input_dev_poller_finalize(struct input_dev_poller *poller) +{ + if (!poller->poll_interval) + poller->poll_interval = 500; + if (!poller->poll_interval_max) + poller->poll_interval_max = poller->poll_interval; +} + +void input_dev_poller_start(struct input_dev_poller *poller) +{ + /* Only start polling if polling is enabled */ + if (poller->poll_interval > 0) { + poller->poll(poller->input); + input_dev_poller_queue_work(poller); + } +} + +void input_dev_poller_stop(struct input_dev_poller *poller) +{ + cancel_delayed_work_sync(&poller->work); +} + +int input_setup_polling(struct input_dev *dev, + void (*poll_fn)(struct input_dev *dev)) +{ + struct input_dev_poller *poller; + + poller = kzalloc(sizeof(*poller), GFP_KERNEL); + if (!poller) { + dev_err(dev->dev.parent ?: &dev->dev, + "%s: unable to allocate poller structure\n", __func__); + return -ENOMEM; + } + + INIT_DELAYED_WORK(&poller->work, input_dev_poller_work); + poller->poll = poll_fn; + + dev->poller = poller; + return 0; +} +EXPORT_SYMBOL(input_setup_polling); + +static bool input_dev_ensure_poller(struct input_dev *dev) +{ + if (!dev->poller) { + dev_err(dev->dev.parent ?: &dev->dev, + "poller structure has not been set up\n"); + return false; + } + + return true; +} + +void input_set_poll_interval(struct input_dev *dev, unsigned int interval) +{ + if (input_dev_ensure_poller(dev)) + dev->poller->poll_interval = interval; +} +EXPORT_SYMBOL(input_set_poll_interval); + +void input_set_min_poll_interval(struct input_dev *dev, unsigned int interval) +{ + if (input_dev_ensure_poller(dev)) + dev->poller->poll_interval_min = interval; +} +EXPORT_SYMBOL(input_set_min_poll_interval); + +void input_set_max_poll_interval(struct input_dev *dev, unsigned int interval) +{ + if (input_dev_ensure_poller(dev)) + dev->poller->poll_interval_max = interval; +} +EXPORT_SYMBOL(input_set_max_poll_interval); + +/* SYSFS interface */ + +static ssize_t input_dev_get_poll_interval(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct input_dev *input = to_input_dev(dev); + + return sprintf(buf, "%d\n", input->poller->poll_interval); +} + +static ssize_t input_dev_set_poll_interval(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct input_dev *input = to_input_dev(dev); + struct input_dev_poller *poller = input->poller; + unsigned int interval; + int err; + + err = kstrtouint(buf, 0, &interval); + if (err) + return err; + + if (interval < poller->poll_interval_min) + return -EINVAL; + + if (interval > poller->poll_interval_max) + return -EINVAL; + + mutex_lock(&input->mutex); + + poller->poll_interval = interval; + + if (input->users) { + cancel_delayed_work_sync(&poller->work); + if (poller->poll_interval > 0) + input_dev_poller_queue_work(poller); + } + + mutex_unlock(&input->mutex); + + return count; +} + +static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, + input_dev_get_poll_interval, input_dev_set_poll_interval); + +static ssize_t input_dev_get_poll_max(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct input_dev *input = to_input_dev(dev); + + return sprintf(buf, "%d\n", input->poller->poll_interval_max); +} + +static DEVICE_ATTR(max, S_IRUGO, input_dev_get_poll_max, NULL); + +static ssize_t input_dev_get_poll_min(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct input_dev *input = to_input_dev(dev); + + return sprintf(buf, "%d\n", input->poller->poll_interval_min); +} + +static DEVICE_ATTR(min, S_IRUGO, input_dev_get_poll_min, NULL); + +static umode_t input_poller_attrs_visible(struct kobject *kobj, + struct attribute *attr, int n) +{ + struct device *dev = kobj_to_dev(kobj); + struct input_dev *input = to_input_dev(dev); + + return input->poller ? attr->mode : 0; +} + +static struct attribute *input_poller_attrs[] = { + &dev_attr_poll.attr, + &dev_attr_max.attr, + &dev_attr_min.attr, + NULL +}; + +struct attribute_group input_poller_attribute_group = { + .is_visible = input_poller_attrs_visible, + .attrs = input_poller_attrs, +}; diff --git a/drivers/input/input-poller.h b/drivers/input/input-poller.h new file mode 100644 index 000000000000..e3fca0be1d32 --- /dev/null +++ b/drivers/input/input-poller.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _INPUT_POLLER_H +#define _INPUT_POLLER_H + +/* + * Support for polling mode for input devices. + */ +#include + +struct input_dev_poller; + +void input_dev_poller_finalize(struct input_dev_poller *poller); +void input_dev_poller_start(struct input_dev_poller *poller); +void input_dev_poller_stop(struct input_dev_poller *poller); + +extern struct attribute_group input_poller_attribute_group; + +#endif /* _INPUT_POLLER_H */ diff --git a/drivers/input/input.c b/drivers/input/input.c index 7494a0dede79..c08aa3596144 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -24,6 +24,7 @@ #include #include #include "input-compat.h" +#include "input-poller.h" MODULE_AUTHOR("Vojtech Pavlik "); MODULE_DESCRIPTION("Input core"); @@ -603,20 +604,31 @@ int input_open_device(struct input_handle *handle) handle->open++; - if (!dev->users++ && dev->open) - retval = dev->open(dev); + if (dev->users++) { + /* + * Device is already opened, so we can exit immediately and + * report success. + */ + goto out; + } - if (retval) { - dev->users--; - if (!--handle->open) { + if (dev->open) { + retval = dev->open(dev); + if (retval) { + dev->users--; + handle->open--; /* * Make sure we are not delivering any more events * through this handle */ synchronize_rcu(); + goto out; } } + if (dev->poller) + input_dev_poller_start(dev->poller); + out: mutex_unlock(&dev->mutex); return retval; @@ -655,8 +667,13 @@ void input_close_device(struct input_handle *handle) __input_release_device(handle); - if (!--dev->users && dev->close) - dev->close(dev); + if (!--dev->users) { + if (dev->poller) + input_dev_poller_stop(dev->poller); + + if (dev->close) + dev->close(dev); + } if (!--handle->open) { /* @@ -1502,6 +1519,7 @@ static const struct attribute_group *input_dev_attr_groups[] = { &input_dev_attr_group, &input_dev_id_attr_group, &input_dev_caps_attr_group, + &input_poller_attribute_group, NULL }; @@ -1511,6 +1529,7 @@ static void input_dev_release(struct device *device) input_ff_destroy(dev); input_mt_destroy_slots(dev); + kfree(dev->poller); kfree(dev->absinfo); kfree(dev->vals); kfree(dev); @@ -2175,6 +2194,9 @@ int input_register_device(struct input_dev *dev) if (!dev->setkeycode) dev->setkeycode = input_default_setkeycode; + if (dev->poller) + input_dev_poller_finalize(dev->poller); + error = device_add(&dev->dev); if (error) goto err_free_vals; diff --git a/include/linux/input.h b/include/linux/input.h index e95a439d8bd5..10346211ff15 100644 --- a/include/linux/input.h +++ b/include/linux/input.h @@ -21,6 +21,8 @@ #include #include +struct input_dev_poller; + /** * struct input_value - input value representation * @type: type of value (EV_KEY, EV_ABS, etc) @@ -156,6 +158,8 @@ struct input_dev { struct ff_device *ff; + struct input_dev_poller *poller; + unsigned int repeat_key; struct timer_list timer; @@ -372,6 +376,12 @@ void input_unregister_device(struct input_dev *); void input_reset_device(struct input_dev *); +int input_setup_polling(struct input_dev *dev, + void (*poll_fn)(struct input_dev *dev)); +void input_set_poll_interval(struct input_dev *dev, unsigned int interval); +void input_set_min_poll_interval(struct input_dev *dev, unsigned int interval); +void input_set_max_poll_interval(struct input_dev *dev, unsigned int interval); + int __must_check input_register_handler(struct input_handler *); void input_unregister_handler(struct input_handler *); -- Dmitry