All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] gpio: mockup: extensions for testing purposes
@ 2017-02-06 14:11 Bartosz Golaszewski
  2017-02-06 14:11 ` [PATCH v3 1/2] gpio: mockup: add a dummy irqchip Bartosz Golaszewski
  2017-02-06 14:11 ` [PATCH v3 2/2] gpio: mockup: implement event injecting over debugfs Bartosz Golaszewski
  0 siblings, 2 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 14:11 UTC (permalink / raw)
  To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang, Lars-Peter Clausen
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

Hi Linus,

this is a follow-up to the gpio-mockup series. I dropped the resource
managed version of irq_alloc_descs() to avoid needing an ack from the
irq subsystem maintainers. I omitted all the already applied patches
too.

Rebased on top of gpio/devel and retested.

v2 -> v3:
- dropped the resource managed irq allocation and added a remove
  function instead
- also selected IRQ_WORK in gpio Kconfig
- included <gpio/consumer.h> from gpio-mockup.c instead of indirectly
  from gpiolib.h

Bartosz Golaszewski (2):
  gpio: mockup: add a dummy irqchip
  gpio: mockup: implement event injecting over debugfs

 drivers/gpio/Kconfig       |   2 +
 drivers/gpio/gpio-mockup.c | 195 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 196 insertions(+), 1 deletion(-)

-- 
2.9.3

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/2] gpio: mockup: add a dummy irqchip
  2017-02-06 14:11 [PATCH v3 0/2] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
@ 2017-02-06 14:11 ` Bartosz Golaszewski
  2017-02-13  7:36   ` Linus Walleij
  2017-02-06 14:11 ` [PATCH v3 2/2] gpio: mockup: implement event injecting over debugfs Bartosz Golaszewski
  1 sibling, 1 reply; 5+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 14:11 UTC (permalink / raw)
  To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang, Lars-Peter Clausen
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

Setup a dummy irqchip that will allow us to inject line events for
testing purposes.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/gpio/Kconfig       |  2 ++
 drivers/gpio/gpio-mockup.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index d4cec61..0504307 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -298,6 +298,8 @@ config GPIO_MOCKUP
 	tristate "GPIO Testing Driver"
 	depends on GPIOLIB && SYSFS
 	select GPIO_SYSFS
+	select GPIOLIB_IRQCHIP
+	select IRQ_WORK
 	help
 	  This enables GPIO Testing driver, which provides a way to test GPIO
 	  subsystem through sysfs(or char device) and debugfs. GPIO_SYSFS
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index 0ce9acc3..03beadb 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -16,6 +16,9 @@
 #include <linux/gpio/driver.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irq_work.h>
 
 #define GPIO_MOCKUP_NAME	"gpio-mockup"
 #define	GPIO_MOCKUP_MAX_GC	10
@@ -35,9 +38,15 @@ struct gpio_mockup_line_status {
 	bool value;
 };
 
+struct gpio_mockup_irq_context {
+	struct irq_work work;
+	int irq;
+};
+
 struct gpio_mockup_chip {
 	struct gpio_chip gc;
 	struct gpio_mockup_line_status *lines;
+	struct gpio_mockup_irq_context irq_ctx;
 };
 
 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];
@@ -115,6 +124,57 @@ static int gpio_mockup_name_lines(struct device *dev,
 	return 0;
 }
 
+static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset)
+{
+	return chip->irq_base + offset;
+}
+
+/*
+ * While we should generally support irqmask and irqunmask, this driver is
+ * for testing purposes only so we don't care.
+ */
+static void gpio_mockup_irqmask(struct irq_data *d) { }
+static void gpio_mockup_irqunmask(struct irq_data *d) { }
+
+static struct irq_chip gpio_mockup_irqchip = {
+	.name		= GPIO_MOCKUP_NAME,
+	.irq_mask	= gpio_mockup_irqmask,
+	.irq_unmask	= gpio_mockup_irqunmask,
+};
+
+static void gpio_mockup_handle_irq(struct irq_work *work)
+{
+	struct gpio_mockup_irq_context *irq_ctx;
+
+	irq_ctx = container_of(work, struct gpio_mockup_irq_context, work);
+	handle_simple_irq(irq_to_desc(irq_ctx->irq));
+}
+
+static int gpio_mockup_irqchip_setup(struct device *dev,
+				     struct gpio_mockup_chip *chip)
+{
+	struct gpio_chip *gc = &chip->gc;
+	int irq_base, i;
+
+	irq_base = irq_alloc_descs(-1, 0, gc->ngpio, 0);
+	if (irq_base < 0)
+		return irq_base;
+
+	gc->irq_base = irq_base;
+	gc->irqchip = &gpio_mockup_irqchip;
+
+	for (i = 0; i < gc->ngpio; i++) {
+		irq_set_chip(irq_base + i, gc->irqchip);
+		irq_set_handler(irq_base + i, &handle_simple_irq);
+		irq_modify_status(irq_base + i,
+				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
+	}
+
+	init_irq_work(&chip->irq_ctx.work, gpio_mockup_handle_irq);
+
+	return 0;
+}
+
 static int gpio_mockup_add(struct device *dev,
 			   struct gpio_mockup_chip *chip,
 			   const char *name, int base, int ngpio)
@@ -132,6 +192,7 @@ static int gpio_mockup_add(struct device *dev,
 	gc->direction_output = gpio_mockup_dirout;
 	gc->direction_input = gpio_mockup_dirin;
 	gc->get_direction = gpio_mockup_get_direction;
+	gc->to_irq = gpio_mockup_to_irq;
 
 	chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio,
 				   GFP_KERNEL);
@@ -144,6 +205,10 @@ static int gpio_mockup_add(struct device *dev,
 			return ret;
 	}
 
+	ret = gpio_mockup_irqchip_setup(dev, chip);
+	if (ret)
+		return ret;
+
 	return devm_gpiochip_add_data(dev, &chip->gc, chip);
 }
 
@@ -200,11 +265,25 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	return 0;
 }
 
+static int gpio_mockup_remove(struct platform_device *pdev)
+{
+	struct gpio_mockup_chip *chips;
+	int i;
+
+	chips = platform_get_drvdata(pdev);
+
+	for (i = 0; i < gpio_mockup_params_nr >> 1; i++)
+		irq_free_descs(chips[i].gc.irq_base, chips[i].gc.ngpio);
+
+	return 0;
+}
+
 static struct platform_driver gpio_mockup_driver = {
 	.driver = {
 		.name = GPIO_MOCKUP_NAME,
 	},
 	.probe = gpio_mockup_probe,
+	.remove = gpio_mockup_remove,
 };
 
 static struct platform_device *pdev;
-- 
2.9.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/2] gpio: mockup: implement event injecting over debugfs
  2017-02-06 14:11 [PATCH v3 0/2] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
  2017-02-06 14:11 ` [PATCH v3 1/2] gpio: mockup: add a dummy irqchip Bartosz Golaszewski
@ 2017-02-06 14:11 ` Bartosz Golaszewski
  2017-02-13  7:38   ` Linus Walleij
  1 sibling, 1 reply; 5+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 14:11 UTC (permalink / raw)
  To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang, Lars-Peter Clausen
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

Create a debugfs directory for every mockup chip and a single file
for every line. Writing (0 or 1) to these files allows the user to
inject line events (falling or rising edge respectively).

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/gpio/gpio-mockup.c | 116 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 115 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index 03beadb..06dac72 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -14,11 +14,16 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/gpio/driver.h>
+#include <linux/gpio/consumer.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_work.h>
+#include <linux/debugfs.h>
+#include <linux/uaccess.h>
+
+#include "gpiolib.h"
 
 #define GPIO_MOCKUP_NAME	"gpio-mockup"
 #define	GPIO_MOCKUP_MAX_GC	10
@@ -47,6 +52,13 @@ struct gpio_mockup_chip {
 	struct gpio_chip gc;
 	struct gpio_mockup_line_status *lines;
 	struct gpio_mockup_irq_context irq_ctx;
+	struct dentry *dbg_dir;
+};
+
+struct gpio_mockup_dbgfs_private {
+	struct gpio_mockup_chip *chip;
+	struct gpio_desc *desc;
+	int offset;
 };
 
 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];
@@ -58,6 +70,7 @@ module_param_named(gpio_mockup_named_lines,
 		   gpio_mockup_named_lines, bool, 0400);
 
 static const char gpio_mockup_name_start = 'A';
+static struct dentry *gpio_mockup_dbg_dir;
 
 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
 {
@@ -175,6 +188,94 @@ static int gpio_mockup_irqchip_setup(struct device *dev,
 	return 0;
 }
 
+static ssize_t gpio_mockup_event_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;
+	struct gpio_chip *gc;
+	int status, val;
+	char buf;
+
+	sfile = file->private_data;
+	priv = sfile->private;
+	desc = priv->desc;
+	chip = priv->chip;
+	gc = &chip->gc;
+
+	status = copy_from_user(&buf, usr_buf, 1);
+	if (status)
+		return status;
+
+	if (buf == '0')
+		val = 0;
+	else if (buf == '1')
+		val = 1;
+	else
+		return -EINVAL;
+
+	gpiod_set_value_cansleep(desc, val);
+	priv->chip->irq_ctx.irq = gc->irq_base + priv->offset;
+	irq_work_queue(&priv->chip->irq_ctx.work);
+
+	return size;
+}
+
+static int gpio_mockup_event_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, NULL, inode->i_private);
+}
+
+static const struct file_operations gpio_mockup_event_ops = {
+	.owner = THIS_MODULE,
+	.open = gpio_mockup_event_open,
+	.write = gpio_mockup_event_write,
+	.llseek = no_llseek,
+};
+
+static void gpio_mockup_debugfs_setup(struct device *dev,
+				      struct gpio_mockup_chip *chip)
+{
+	struct gpio_mockup_dbgfs_private *priv;
+	struct dentry *evfile;
+	struct gpio_chip *gc;
+	char *name;
+	int i;
+
+	gc = &chip->gc;
+
+	chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir);
+	if (!chip->dbg_dir)
+		goto err;
+
+	for (i = 0; i < gc->ngpio; i++) {
+		name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
+		if (!name)
+			goto err;
+
+		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+		if (!priv)
+			goto err;
+
+		priv->chip = chip;
+		priv->offset = i;
+		priv->desc = &gc->gpiodev->descs[i];
+
+		evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
+					     &gpio_mockup_event_ops);
+		if (!evfile)
+			goto err;
+	}
+
+	return;
+
+err:
+	dev_err(dev, "error creating debugfs directory\n");
+}
+
 static int gpio_mockup_add(struct device *dev,
 			   struct gpio_mockup_chip *chip,
 			   const char *name, int base, int ngpio)
@@ -209,7 +310,14 @@ static int gpio_mockup_add(struct device *dev,
 	if (ret)
 		return ret;
 
-	return devm_gpiochip_add_data(dev, &chip->gc, chip);
+	ret = devm_gpiochip_add_data(dev, &chip->gc, chip);
+	if (ret)
+		return ret;
+
+	if (gpio_mockup_dbg_dir)
+		gpio_mockup_debugfs_setup(dev, chip);
+
+	return 0;
 }
 
 static int gpio_mockup_probe(struct platform_device *pdev)
@@ -291,6 +399,11 @@ static int __init mock_device_init(void)
 {
 	int err;
 
+	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
+	if (!gpio_mockup_dbg_dir)
+		pr_err("%s: error creating debugfs directory\n",
+		       GPIO_MOCKUP_NAME);
+
 	pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1);
 	if (!pdev)
 		return -ENOMEM;
@@ -312,6 +425,7 @@ static int __init mock_device_init(void)
 
 static void __exit mock_device_exit(void)
 {
+	debugfs_remove_recursive(gpio_mockup_dbg_dir);
 	platform_driver_unregister(&gpio_mockup_driver);
 	platform_device_unregister(pdev);
 }
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 1/2] gpio: mockup: add a dummy irqchip
  2017-02-06 14:11 ` [PATCH v3 1/2] gpio: mockup: add a dummy irqchip Bartosz Golaszewski
@ 2017-02-13  7:36   ` Linus Walleij
  0 siblings, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2017-02-13  7:36 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Alexandre Courbot, Bamvor Jian Zhang, Lars-Peter Clausen,
	linux-gpio, linux-kernel

On Mon, Feb 6, 2017 at 3:11 PM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:

> Setup a dummy irqchip that will allow us to inject line events for
> testing purposes.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Patch applied.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 2/2] gpio: mockup: implement event injecting over debugfs
  2017-02-06 14:11 ` [PATCH v3 2/2] gpio: mockup: implement event injecting over debugfs Bartosz Golaszewski
@ 2017-02-13  7:38   ` Linus Walleij
  0 siblings, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2017-02-13  7:38 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Alexandre Courbot, Bamvor Jian Zhang, Lars-Peter Clausen,
	linux-gpio, linux-kernel

On Mon, Feb 6, 2017 at 3:11 PM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:

> Create a debugfs directory for every mockup chip and a single file
> for every line. Writing (0 or 1) to these files allows the user to
> inject line events (falling or rising edge respectively).
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Patch applied.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-02-13  7:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-06 14:11 [PATCH v3 0/2] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
2017-02-06 14:11 ` [PATCH v3 1/2] gpio: mockup: add a dummy irqchip Bartosz Golaszewski
2017-02-13  7:36   ` Linus Walleij
2017-02-06 14:11 ` [PATCH v3 2/2] gpio: mockup: implement event injecting over debugfs Bartosz Golaszewski
2017-02-13  7:38   ` Linus Walleij

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.