Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4410424imu; Tue, 29 Jan 2019 00:45:29 -0800 (PST) X-Google-Smtp-Source: ALg8bN7qZJ7VTSFVLCiiUgmPNZnhNLVZim0XjuLQ0GO7xwtT4zVkmYiVIze7Ho/3aQggYAvEBwO+ X-Received: by 2002:a17:902:f082:: with SMTP id go2mr25177335plb.115.1548751529277; Tue, 29 Jan 2019 00:45:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548751529; cv=none; d=google.com; s=arc-20160816; b=uHSWMv0VCb27s5TYB5R2wn1ekzpt6z7eHBoZ3I1PUhVwVKJJ677zJx5sW/DQAn9BQg iWVS6tgUhKIE6nn95u73SX6wqxgdgXe+Cl8NbN70CBg0FTbg7irUuc37oSx6SQIR8lfZ WgyN5uT8QftxY+jEsNYeb4AgN5YxYJxkqJYeNBDiT707pbcJYRDRZlN0MZO3tLpWuoKr el/frkw5rrQIFg5osXXMYQV14C3ISexJlUP14oWFpLyNV3JGJPpNXQLpevWaT+F4bGS1 AC5Kt34JLEsJ7W7e16O3YFIchfRRBfHlojVsgM4SfI0lbNJFGurT6o8rbWA+8oc61YWJ cElw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vHL+0UNr5kGvk4GaQolbiJExCUhkATZNz3ED6gc++tc=; b=tZdcGf3PuI8oz5wnT+/i57NgoW16LI/Jaat+m4rGgRdS1x31GsuKZyi2p1LlXkVd8/ FmxCXwfc6QEhZwHQIm4OzaxQ2C+CDPxhGMcN5r6gXVfiflZYxszI2h2OmDUw0S5qfF2H LD6u+hJXxkhYYNE3tipEsDstjPu/A0FERqHxzUf2QZxAgDCjHL5uO0Q4xTEVWPAGe7Lg hYbbGGQpBDycfTxm97AQkZfpHo9vh4xfTXSt/IqIK+fhmdlKTiKm68Kg+af9+FGIvBjL +/NKdF7h+/6uqpVlcYKwFRG5PwRvhu5GX81zchXiaNMRdzc5d399LLLB5tKaEl9EyBCK y/hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=gUlsvIER; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w189si33557929pfb.151.2019.01.29.00.45.13; Tue, 29 Jan 2019 00:45:29 -0800 (PST) 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=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=gUlsvIER; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728127AbfA2Iol (ORCPT + 99 others); Tue, 29 Jan 2019 03:44:41 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40516 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727941AbfA2Iob (ORCPT ); Tue, 29 Jan 2019 03:44:31 -0500 Received: by mail-wm1-f66.google.com with SMTP id f188so16678341wmf.5 for ; Tue, 29 Jan 2019 00:44:29 -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=vHL+0UNr5kGvk4GaQolbiJExCUhkATZNz3ED6gc++tc=; b=gUlsvIERtEqpZBuGE4P4LLxHAjBiyPnHXdPNkJPj1AWHGntJldc5s4zryCYbVpGE5J +ZRykGd0M1asMw0Aq5Bzg+2IC4dUjoXJENa8GQGwdSoZSO/spj0RZsGr+ivEk6vdwWg/ aT3wMc3F0/Q7nLlQqfLejiNdk4L3zFtU53aZHQD+j2LJxywJO9Zu2ZO3oVquHTz4/FmY rTs6uo+RiNk+JfWi6J8OZRmVDFU8Bxo0AMsSJPlKdMevfN75VZvO1s3PGsYmON2sGBsV RC8/NGQJw05k7xpPFC9PHkZhMsrz3v9Fb+4Fmypb8fxz05mwH5H4yHvH2ONQb1ghxyTD msvw== 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=vHL+0UNr5kGvk4GaQolbiJExCUhkATZNz3ED6gc++tc=; b=SjVN2Qzz/juwfxxF5vFQL3BnQ+KhDC3qfichPSx6Y1JYO8e/cvtkbh0oKRqm5o/Vgt yshQLwNX/nxLo2sAwdO47LMk9Xst6kbdrFta9KjYHmZSsUVXJulQcMYC0QU5seB95J07 BAMiYn9In/6j4FWXixGjfnxxlP08JHRYU6zFS716U3nhCK/HsJrhGQdJquDcsDdh/uoq /b0UsSVc7/gW8YUg4Bi60zPnGDh1B+IiKrlthxYpDRjhw1h1G+bWITBolcZCHhYxBsLB 1iKNBLl/3EMCIN33dkbBpxLEoZZX5cEb1rxQ5USG911YuHT+HOk537AZT25ZfgOiGPh0 eIDQ== X-Gm-Message-State: AJcUukczRftO6Hmm0Mez+FBHd/pbVvc4AwEKQ5+Cqb47767El0HiA0j1 gkwQ3QTLk63pkG0n3DrnkVx8fg== X-Received: by 2002:a1c:bbd6:: with SMTP id l205mr19060518wmf.97.1548751468765; Tue, 29 Jan 2019 00:44:28 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id c1sm3979319wmb.14.2019.01.29.00.44.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Jan 2019 00:44:28 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Thomas Gleixner , Marc Zyngier , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 9/9] gpio: mockup: rework debugfs interface Date: Tue, 29 Jan 2019 09:44:11 +0100 Message-Id: <20190129084411.30495-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190129084411.30495-1-brgl@bgdev.pl> References: <20190129084411.30495-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 | 113 ++++++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 14 deletions(-) diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c index c498b0fbbec8..0b8de6e127eb 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,66 @@ 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); - gpiod_set_value_cansleep(desc, val); - irq_sim_fire(&chip->irqsim, priv->offset); + if (test_bit(FLAG_REQUESTED, &desc->flags) && + !test_bit(FLAG_IS_OUT, &desc->flags)) { + curr = __gpio_mockup_get(chip, priv->offset); + if (curr == val) + goto out; + + irq_type = val == 0 ? IRQ_TYPE_EDGE_FALLING + : IRQ_TYPE_EDGE_RISING; + irq_sim_fire_type(&chip->irqsim, priv->offset, irq_type); + } + + /* Change the value unless we're actively driving the line. */ + if (!test_bit(FLAG_REQUESTED, &desc->flags) || + !test_bit(FLAG_IS_OUT, &desc->flags)) + __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 +342,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 +350,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 +426,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 +500,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"); -- 2.19.1