From patchwork Wed Jan 23 14:15:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1029968 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="lVKAXl+s"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43l6lY1yxDz9sBn for ; Thu, 24 Jan 2019 01:16:09 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725995AbfAWOQI (ORCPT ); Wed, 23 Jan 2019 09:16:08 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:41768 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726346AbfAWOP5 (ORCPT ); Wed, 23 Jan 2019 09:15:57 -0500 Received: by mail-wr1-f68.google.com with SMTP id x10so2624972wrs.8 for ; Wed, 23 Jan 2019 06:15:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=J5KSV3ziNybYTZ8uWoEllizkkf7KNygL/2Y9AB2R1io=; b=lVKAXl+sH4MgPdy9dIGIKleYdxhLHa1MQ9/IDsvvo4q+SuWng1FR+vS1cDq8WbH8vD lIGSMTLIJrl/ipmjv8bau+GqEJ4RqQlBbn04cAOYOS+TuWp3F+/NVH7VNfbzJ+rTDptd H5NzwD6M/uHI72cctEVtp9BZQgl5WmpEjpM4/D4zZ0ulEKVHEKKr1F0dV2e8dvBLFeJV m5fGFECPBy1BU61uIwxcz9P7rTrgS6DSF6nNk8ft2q6FUE8gnsf9H/eytY7W6U01e4Uv tTtrEuAYyHXGh6+sR0lbm60fZeRBeuYZvzphTo7CXOhNxJcFGcr3m4i7aPmPzvWNRtKc JqFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=J5KSV3ziNybYTZ8uWoEllizkkf7KNygL/2Y9AB2R1io=; b=oNUb8QHJye9mb9ekaFXM2cG37jb32x8EVm4OBW+SPFCZ7iZP2AV93irxumFeD4majU 9Xwryar9+uPu+UuxuRL94mRngKesnAfMrJgL8Y+TTTNbxNcyR3W7vXjxofEEeFYBd7lO 4S8g1OcTo7mAlrSYR/+qV21g64sJamJcLrxwA65RVJJnjKDQuhQ+325Nj0nrDkwY2bvb A7cEakvXZUeahQkDXddjeMCbLRluXwG/WR47w06+f+tivInhL8Fb525bPki51dng76Do Urrba96G9eUk6nH3EBMmd5bNZ0uKwvmzvouQT4kOUztV/K+x0Khzt3wrQsfeYFswlsPr sF1g== X-Gm-Message-State: AJcUukdnUW3S5h9KG5W1cWxVdxwOY+cXlQjvsFEur68iJO9KOq57GiVA WAUo7BMiTgupC1adyWFnPUkk6w== X-Google-Smtp-Source: ALg8bN7fztgGY1xIAWjhH1T70kljs/gr97mp84iz/H0PGb39IeYOkE6ait84QzhI0+w1qG158O60+w== X-Received: by 2002:adf:891a:: with SMTP id s26mr2965672wrs.44.1548252956130; Wed, 23 Jan 2019 06:15:56 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id a8sm29072997wme.23.2019.01.23.06.15.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Jan 2019 06:15:55 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Thomas Gleixner , Marc Zyngier , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?b?w7ZuaWc=?= Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 9/9] gpio: mockup: rework debugfs interface Date: Wed, 23 Jan 2019 15:15:38 +0100 Message-Id: <20190123141538.29408-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190123141538.29408-1-brgl@bgdev.pl> References: <20190123141538.29408-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Modify the way the debugfs interface works in gpio-mockup. Introduce the concept of dummy pull config which will keep the mockup lines in known state. The pull values can be modified by writing to the debugfs files corresponding to lines. Lines in input mode always report the current pull value, lines in output mode change the line value but it will revert back to the one specified by current pull when released. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpio-mockup.c | 114 ++++++++++++++++++++++++++++++++----- 1 file changed, 100 insertions(+), 14 deletions(-) diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c index c498b0fbbec8..a981ecd7c291 100644 --- a/drivers/gpio/gpio-mockup.c +++ b/drivers/gpio/gpio-mockup.c @@ -47,6 +47,7 @@ enum { struct gpio_mockup_line_status { int dir; int value; + int pull; }; struct gpio_mockup_chip { @@ -188,15 +189,56 @@ static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset) return irq_sim_irqnum(&chip->irqsim, offset); } -static ssize_t gpio_mockup_event_write(struct file *file, - const char __user *usr_buf, - size_t size, loff_t *ppos) +static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_mockup_chip *chip = gpiochip_get_data(gc); + + __gpio_mockup_set(chip, offset, chip->lines[offset].pull); +} + +static ssize_t gpio_mockup_debugfs_read(struct file *file, + char __user *usr_buf, + size_t size, loff_t *ppos) +{ + struct gpio_mockup_dbgfs_private *priv; + struct gpio_mockup_chip *chip; + struct seq_file *sfile; + struct gpio_chip *gc; + char buf[3]; + int val, rv; + + if (*ppos != 0) + return 0; + + sfile = file->private_data; + priv = sfile->private; + chip = priv->chip; + gc = &chip->gc; + + val = gpio_mockup_get(gc, priv->offset); + snprintf(buf, sizeof(buf), "%d\n", val); + + rv = copy_to_user(usr_buf, buf, sizeof(buf)); + if (rv) + return rv; + + return sizeof(buf) - 1; +} + +static ssize_t gpio_mockup_debugfs_write(struct file *file, + const char __user *usr_buf, + size_t size, loff_t *ppos) { struct gpio_mockup_dbgfs_private *priv; struct gpio_mockup_chip *chip; struct seq_file *sfile; struct gpio_desc *desc; - int rv, val; + unsigned int irq_type; + struct gpio_chip *gc; + int rv, val, curr; + + if (*ppos != 0) + return -EINVAL; rv = kstrtoint_from_user(usr_buf, size, 0, &val); if (rv) @@ -206,24 +248,67 @@ static ssize_t gpio_mockup_event_write(struct file *file, sfile = file->private_data; priv = sfile->private; - desc = priv->desc; chip = priv->chip; + gc = &chip->gc; + desc = &gc->gpiodev->descs[priv->offset]; + + mutex_lock(&chip->lock); + + if (!test_bit(FLAG_IS_OUT, &desc->flags)) { + if (test_bit(FLAG_REQUESTED, &desc->flags)) { + curr = __gpio_mockup_get(chip, priv->offset); + if (curr == val) + goto out; - gpiod_set_value_cansleep(desc, val); - irq_sim_fire(&chip->irqsim, priv->offset); + irq_type = irq_sim_get_type(&chip->irqsim, + priv->offset); + + if ((val == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING)) || + (val == 1 && (irq_type & IRQ_TYPE_EDGE_RISING))) + irq_sim_fire(&chip->irqsim, priv->offset); + } + + __gpio_mockup_set(chip, priv->offset, val); + } + +out: + chip->lines[priv->offset].pull = val; + mutex_unlock(&chip->lock); return size; } -static int gpio_mockup_event_open(struct inode *inode, struct file *file) +static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file) { return single_open(file, NULL, inode->i_private); } -static const struct file_operations gpio_mockup_event_ops = { +/* + * Each mockup chip is represented by a directory named after the chip's device + * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by + * a file using the line's offset as the name under the chip's directory. + * + * Reading from the line's file yields the current *value*, writing to the + * line's file changes the current *pull*. Default pull for mockup lines is + * down. + * + * Examples: + * - when a line pulled down is requested in output mode and driven high, its + * value will return to 0 once it's released + * - when the line is requested in output mode and driven high, writing 0 to + * the corresponding debugfs file will change the pull to down but the + * reported value will still be 1 until the line is released + * - line requested in input mode always reports the same value as its pull + * configuration + * - when the line is requested in input mode and monitored for events, writing + * the same value to the debugfs file will be a noop, while writing the + * opposite value will generate a dummy interrupt with an appropriate edge + */ +static const struct file_operations gpio_mockup_debugfs_ops = { .owner = THIS_MODULE, - .open = gpio_mockup_event_open, - .write = gpio_mockup_event_write, + .open = gpio_mockup_debugfs_open, + .read = gpio_mockup_debugfs_read, + .write = gpio_mockup_debugfs_write, .llseek = no_llseek, }; @@ -258,7 +343,7 @@ static void gpio_mockup_debugfs_setup(struct device *dev, priv->desc = &gc->gpiodev->descs[i]; evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, - &gpio_mockup_event_ops); + &gpio_mockup_debugfs_ops); if (IS_ERR_OR_NULL(evfile)) goto err; } @@ -266,7 +351,7 @@ static void gpio_mockup_debugfs_setup(struct device *dev, return; err: - dev_err(dev, "error creating debugfs event files\n"); + dev_err(dev, "error creating debugfs files\n"); } static int gpio_mockup_name_lines(struct device *dev, @@ -342,6 +427,7 @@ static int gpio_mockup_probe(struct platform_device *pdev) gc->direction_input = gpio_mockup_dirin; gc->get_direction = gpio_mockup_get_direction; gc->to_irq = gpio_mockup_to_irq; + gc->free = gpio_mockup_free; chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL); @@ -415,7 +501,7 @@ static int __init gpio_mockup_init(void) return -EINVAL; } - gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); + gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL); if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) gpio_mockup_err("error creating debugfs directory\n");