* [PATCH v2 1/7] gpio: mockup: readability tweaks
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
@ 2017-02-06 12:10 ` Bartosz Golaszewski
2017-02-06 13:20 ` Linus Walleij
2017-02-06 12:10 ` [PATCH v2 2/7] gpio: mockup: code shrink Bartosz Golaszewski
` (6 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:10 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang,
Thomas Gleixner
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
The following patch tries to improve the readability of the mockup
driver.
The driver is called gpio-mockup, so add the same prefix to all
functions and structures.
Add some newlines and use a temporary pointer in gpio_mockup_add().
Drop the name of the direction enum and rename the enum values to
better reflect their purpose.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
drivers/gpio/gpio-mockup.c | 123 ++++++++++++++++++++++++---------------------
1 file changed, 66 insertions(+), 57 deletions(-)
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index 82a9efd..5f6ed4b 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -16,12 +16,12 @@
#include <linux/gpio/driver.h>
#include <linux/platform_device.h>
-#define GPIO_NAME "gpio-mockup"
-#define MAX_GC 10
+#define GPIO_MOCKUP_NAME "gpio-mockup"
+#define GPIO_MOCKUP_MAX_GC 10
-enum direction {
- OUT,
- IN
+enum {
+ DIR_IN = 0,
+ DIR_OUT,
};
/*
@@ -29,98 +29,104 @@ enum direction {
* @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
* @value: Configures status of the gpio as 0(low) or 1(high)
*/
-struct gpio_pin_status {
- enum direction dir;
+struct gpio_mockup_line_status {
+ int dir;
bool value;
};
-struct mockup_gpio_controller {
+struct gpio_mockup_chip {
struct gpio_chip gc;
- struct gpio_pin_status *stats;
+ struct gpio_mockup_line_status *lines;
};
-static int gpio_mockup_ranges[MAX_GC << 1];
+static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];
static int gpio_mockup_params_nr;
module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
-static const char pins_name_start = 'A';
+static const char gpio_mockup_name_start = 'A';
-static int mockup_gpio_get(struct gpio_chip *gc, unsigned int offset)
+static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
{
- struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
+ struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
- return cntr->stats[offset].value;
+ return chip->lines[offset].value;
}
-static void mockup_gpio_set(struct gpio_chip *gc, unsigned int offset,
+static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset,
int value)
{
- struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
+ struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
- cntr->stats[offset].value = !!value;
+ chip->lines[offset].value = !!value;
}
-static int mockup_gpio_dirout(struct gpio_chip *gc, unsigned int offset,
+static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset,
int value)
{
- struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
+ struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
+
+ gpio_mockup_set(gc, offset, value);
+ chip->lines[offset].dir = DIR_OUT;
- mockup_gpio_set(gc, offset, value);
- cntr->stats[offset].dir = OUT;
return 0;
}
-static int mockup_gpio_dirin(struct gpio_chip *gc, unsigned int offset)
+static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
{
- struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
+ struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
+
+ chip->lines[offset].dir = DIR_IN;
- cntr->stats[offset].dir = IN;
return 0;
}
-static int mockup_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
+static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
{
- struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
+ struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
- return cntr->stats[offset].dir;
+ return chip->lines[offset].dir;
}
-static int mockup_gpio_add(struct device *dev,
- struct mockup_gpio_controller *cntr,
+static int gpio_mockup_add(struct device *dev,
+ struct gpio_mockup_chip *chip,
const char *name, int base, int ngpio)
{
+ struct gpio_chip *gc = &chip->gc;
int ret;
- cntr->gc.base = base;
- cntr->gc.ngpio = ngpio;
- cntr->gc.label = name;
- cntr->gc.owner = THIS_MODULE;
- cntr->gc.parent = dev;
- cntr->gc.get = mockup_gpio_get;
- cntr->gc.set = mockup_gpio_set;
- cntr->gc.direction_output = mockup_gpio_dirout;
- cntr->gc.direction_input = mockup_gpio_dirin;
- cntr->gc.get_direction = mockup_gpio_get_direction;
- cntr->stats = devm_kzalloc(dev, sizeof(*cntr->stats) * cntr->gc.ngpio,
+ gc->base = base;
+ gc->ngpio = ngpio;
+ gc->label = name;
+ gc->owner = THIS_MODULE;
+ gc->parent = dev;
+ gc->get = gpio_mockup_get;
+ gc->set = gpio_mockup_set;
+ gc->direction_output = gpio_mockup_dirout;
+ gc->direction_input = gpio_mockup_dirin;
+ gc->get_direction = gpio_mockup_get_direction;
+
+ chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio,
GFP_KERNEL);
- if (!cntr->stats) {
+ if (!chip->lines) {
ret = -ENOMEM;
goto err;
}
- ret = devm_gpiochip_add_data(dev, &cntr->gc, cntr);
+
+ ret = devm_gpiochip_add_data(dev, &chip->gc, chip);
if (ret)
goto err;
dev_info(dev, "gpio<%d..%d> add successful!", base, base + ngpio);
return 0;
+
err:
dev_err(dev, "gpio<%d..%d> add failed!", base, base + ngpio);
return ret;
}
-static int mockup_gpio_probe(struct platform_device *pdev)
+static int gpio_mockup_probe(struct platform_device *pdev)
{
- struct mockup_gpio_controller *cntr;
+ struct gpio_mockup_chip *chips;
struct device *dev = &pdev->dev;
int ret, i, base, ngpio;
char *chip_name;
@@ -128,15 +134,17 @@ static int mockup_gpio_probe(struct platform_device *pdev)
if (gpio_mockup_params_nr < 2)
return -EINVAL;
- cntr = devm_kzalloc(dev, sizeof(*cntr) * (gpio_mockup_params_nr >> 1),
- GFP_KERNEL);
- if (!cntr)
+ chips = devm_kzalloc(dev,
+ sizeof(*chips) * (gpio_mockup_params_nr >> 1),
+ GFP_KERNEL);
+ if (!chips)
return -ENOMEM;
- platform_set_drvdata(pdev, cntr);
+ platform_set_drvdata(pdev, chips);
for (i = 0; i < gpio_mockup_params_nr >> 1; i++) {
base = gpio_mockup_ranges[i * 2];
+
if (base == -1)
ngpio = gpio_mockup_ranges[i * 2 + 1];
else
@@ -144,16 +152,17 @@ static int mockup_gpio_probe(struct platform_device *pdev)
if (ngpio >= 0) {
chip_name = devm_kasprintf(dev, GFP_KERNEL,
- "%s-%c", GPIO_NAME,
- pins_name_start + i);
+ "%s-%c", GPIO_MOCKUP_NAME,
+ gpio_mockup_name_start + i);
if (!chip_name)
return -ENOMEM;
- ret = mockup_gpio_add(dev, &cntr[i],
+ ret = gpio_mockup_add(dev, &chips[i],
chip_name, base, ngpio);
} else {
ret = -1;
}
+
if (ret) {
if (base < 0)
dev_err(dev, "gpio<%d..%d> add failed\n",
@@ -169,11 +178,11 @@ static int mockup_gpio_probe(struct platform_device *pdev)
return 0;
}
-static struct platform_driver mockup_gpio_driver = {
+static struct platform_driver gpio_mockup_driver = {
.driver = {
- .name = GPIO_NAME,
+ .name = GPIO_MOCKUP_NAME,
},
- .probe = mockup_gpio_probe,
+ .probe = gpio_mockup_probe,
};
static struct platform_device *pdev;
@@ -181,7 +190,7 @@ static int __init mock_device_init(void)
{
int err;
- pdev = platform_device_alloc(GPIO_NAME, -1);
+ pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1);
if (!pdev)
return -ENOMEM;
@@ -191,7 +200,7 @@ static int __init mock_device_init(void)
return err;
}
- err = platform_driver_register(&mockup_gpio_driver);
+ err = platform_driver_register(&gpio_mockup_driver);
if (err) {
platform_device_unregister(pdev);
return err;
@@ -202,7 +211,7 @@ static int __init mock_device_init(void)
static void __exit mock_device_exit(void)
{
- platform_driver_unregister(&mockup_gpio_driver);
+ platform_driver_unregister(&gpio_mockup_driver);
platform_device_unregister(pdev);
}
--
2.9.3
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v2 1/7] gpio: mockup: readability tweaks
2017-02-06 12:10 ` [PATCH v2 1/7] gpio: mockup: readability tweaks Bartosz Golaszewski
@ 2017-02-06 13:20 ` Linus Walleij
0 siblings, 0 replies; 17+ messages in thread
From: Linus Walleij @ 2017-02-06 13:20 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Alexandre Courbot, Bamvor Jian Zhang, Thomas Gleixner,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
On Mon, Feb 6, 2017 at 1:10 PM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:
> The following patch tries to improve the readability of the mockup
> driver.
>
> The driver is called gpio-mockup, so add the same prefix to all
> functions and structures.
>
> Add some newlines and use a temporary pointer in gpio_mockup_add().
>
> Drop the name of the direction enum and rename the enum values to
> better reflect their purpose.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Patch applied.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 2/7] gpio: mockup: code shrink
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
2017-02-06 12:10 ` [PATCH v2 1/7] gpio: mockup: readability tweaks Bartosz Golaszewski
@ 2017-02-06 12:10 ` Bartosz Golaszewski
2017-02-06 13:21 ` Linus Walleij
2017-02-06 12:10 ` [PATCH v2 3/7] gpio: mockup: implement naming the lines Bartosz Golaszewski
` (5 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:10 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang,
Thomas Gleixner
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
Moving a couple of lines around allows us to shrink the code a bit
while keeping the same functionality.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
drivers/gpio/gpio-mockup.c | 29 ++++++++---------------------
1 file changed, 8 insertions(+), 21 deletions(-)
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index 5f6ed4b..d425601 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -92,7 +92,6 @@ static int gpio_mockup_add(struct device *dev,
const char *name, int base, int ngpio)
{
struct gpio_chip *gc = &chip->gc;
- int ret;
gc->base = base;
gc->ngpio = ngpio;
@@ -107,21 +106,10 @@ static int gpio_mockup_add(struct device *dev,
chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio,
GFP_KERNEL);
- if (!chip->lines) {
- ret = -ENOMEM;
- goto err;
- }
-
- ret = devm_gpiochip_add_data(dev, &chip->gc, chip);
- if (ret)
- goto err;
-
- dev_info(dev, "gpio<%d..%d> add successful!", base, base + ngpio);
- return 0;
+ if (!chip->lines)
+ return -ENOMEM;
-err:
- dev_err(dev, "gpio<%d..%d> add failed!", base, base + ngpio);
- return ret;
+ return devm_gpiochip_add_data(dev, &chip->gc, chip);
}
static int gpio_mockup_probe(struct platform_device *pdev)
@@ -164,15 +152,14 @@ static int gpio_mockup_probe(struct platform_device *pdev)
}
if (ret) {
- if (base < 0)
- dev_err(dev, "gpio<%d..%d> add failed\n",
- base, ngpio);
- else
- dev_err(dev, "gpio<%d..%d> add failed\n",
- base, base + ngpio);
+ dev_err(dev, "gpio<%d..%d> add failed\n",
+ base, base < 0 ? ngpio : base + ngpio);
return ret;
}
+
+ dev_info(dev, "gpio<%d..%d> add successful!",
+ base, base + ngpio);
}
return 0;
--
2.9.3
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v2 3/7] gpio: mockup: implement naming the lines
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
2017-02-06 12:10 ` [PATCH v2 1/7] gpio: mockup: readability tweaks Bartosz Golaszewski
2017-02-06 12:10 ` [PATCH v2 2/7] gpio: mockup: code shrink Bartosz Golaszewski
@ 2017-02-06 12:10 ` Bartosz Golaszewski
2017-02-06 13:22 ` Linus Walleij
2017-02-06 12:10 ` [PATCH v2 4/7] irqdesc: add memory managed version of irq_alloc_descs() Bartosz Golaszewski
` (4 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:10 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang,
Thomas Gleixner
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
In order to allow testing line lookup by name from user space, add
a new boolean parameter that indicates whether we want the lines to
be named. The name is created by concatenating the chip name and the
line offset value.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
drivers/gpio/gpio-mockup.c | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index d425601..0ce9acc3 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -15,6 +15,7 @@
#include <linux/module.h>
#include <linux/gpio/driver.h>
#include <linux/platform_device.h>
+#include <linux/slab.h>
#define GPIO_MOCKUP_NAME "gpio-mockup"
#define GPIO_MOCKUP_MAX_GC 10
@@ -43,6 +44,10 @@ static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];
static int gpio_mockup_params_nr;
module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
+static bool gpio_mockup_named_lines;
+module_param_named(gpio_mockup_named_lines,
+ gpio_mockup_named_lines, bool, 0400);
+
static const char gpio_mockup_name_start = 'A';
static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
@@ -87,11 +92,35 @@ static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
return chip->lines[offset].dir;
}
+static int gpio_mockup_name_lines(struct device *dev,
+ struct gpio_mockup_chip *chip)
+{
+ struct gpio_chip *gc = &chip->gc;
+ char **names;
+ int i;
+
+ names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, GFP_KERNEL);
+ if (!names)
+ return -ENOMEM;
+
+ for (i = 0; i < gc->ngpio; i++) {
+ names[i] = devm_kasprintf(dev, GFP_KERNEL,
+ "%s-%d", gc->label, i);
+ if (!names[i])
+ return -ENOMEM;
+ }
+
+ gc->names = (const char *const *)names;
+
+ return 0;
+}
+
static int gpio_mockup_add(struct device *dev,
struct gpio_mockup_chip *chip,
const char *name, int base, int ngpio)
{
struct gpio_chip *gc = &chip->gc;
+ int ret;
gc->base = base;
gc->ngpio = ngpio;
@@ -109,6 +138,12 @@ static int gpio_mockup_add(struct device *dev,
if (!chip->lines)
return -ENOMEM;
+ if (gpio_mockup_named_lines) {
+ ret = gpio_mockup_name_lines(dev, chip);
+ if (ret)
+ return ret;
+ }
+
return devm_gpiochip_add_data(dev, &chip->gc, chip);
}
--
2.9.3
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v2 3/7] gpio: mockup: implement naming the lines
2017-02-06 12:10 ` [PATCH v2 3/7] gpio: mockup: implement naming the lines Bartosz Golaszewski
@ 2017-02-06 13:22 ` Linus Walleij
0 siblings, 0 replies; 17+ messages in thread
From: Linus Walleij @ 2017-02-06 13:22 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Alexandre Courbot, Bamvor Jian Zhang, Thomas Gleixner,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
On Mon, Feb 6, 2017 at 1:10 PM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:
> In order to allow testing line lookup by name from user space, add
> a new boolean parameter that indicates whether we want the lines to
> be named. The name is created by concatenating the chip name and the
> line offset value.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Patch applied.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 4/7] irqdesc: add memory managed version of irq_alloc_descs()
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
` (2 preceding siblings ...)
2017-02-06 12:10 ` [PATCH v2 3/7] gpio: mockup: implement naming the lines Bartosz Golaszewski
@ 2017-02-06 12:10 ` Bartosz Golaszewski
2017-02-06 13:26 ` Linus Walleij
2017-02-06 12:10 ` [PATCH v2 5/7] gpio: mockup: add a dummy irqchip Bartosz Golaszewski
` (3 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:10 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang,
Thomas Gleixner
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
Add a devres flavor of __devm_irq_alloc_descs() and corresponding
helper macros.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
include/linux/irq.h | 19 +++++++++++++++++++
kernel/irq/devres.c | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/include/linux/irq.h b/include/linux/irq.h
index e798755..d915cae 100644
--- a/include/linux/irq.h
+++ b/include/linux/irq.h
@@ -715,6 +715,10 @@ unsigned int arch_dynirq_lower_bound(unsigned int from);
int __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
struct module *owner, const struct cpumask *affinity);
+int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
+ unsigned int cnt, int node, struct module *owner,
+ const struct cpumask *affinity);
+
/* use macros to avoid needing export.h for THIS_MODULE */
#define irq_alloc_descs(irq, from, cnt, node) \
__irq_alloc_descs(irq, from, cnt, node, THIS_MODULE, NULL)
@@ -731,6 +735,21 @@ int __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
#define irq_alloc_descs_from(from, cnt, node) \
irq_alloc_descs(-1, from, cnt, node)
+#define devm_irq_alloc_descs(dev, irq, from, cnt, node) \
+ __devm_irq_alloc_descs(dev, irq, from, cnt, node, THIS_MODULE, NULL)
+
+#define devm_irq_alloc_desc(dev, node) \
+ devm_irq_alloc_descs(dev, -1, 0, 1, node)
+
+#define devm_irq_alloc_desc_at(dev, at, node) \
+ devm_irq_alloc_descs(dev, at, at, 1, node)
+
+#define devm_irq_alloc_desc_from(dev, from, node) \
+ devm_irq_alloc_descs(dev, -1, from, 1, node)
+
+#define devm_irq_alloc_descs_from(dev, from, cnt, node) \
+ devm_irq_alloc_descs(dev, -1, from, cnt, node)
+
void irq_free_descs(unsigned int irq, unsigned int cnt);
static inline void irq_free_desc(unsigned int irq)
{
diff --git a/kernel/irq/devres.c b/kernel/irq/devres.c
index 74d90a7..d1e70d6 100644
--- a/kernel/irq/devres.c
+++ b/kernel/irq/devres.c
@@ -2,6 +2,7 @@
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/gfp.h>
+#include <linux/irq.h>
/*
* Device resource management aware IRQ request/free implementation.
@@ -137,3 +138,40 @@ void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
free_irq(irq, dev_id);
}
EXPORT_SYMBOL(devm_free_irq);
+
+struct irq_desc_devres {
+ int from;
+ int cnt;
+};
+
+static void devm_irq_desc_release(struct device *dev, void *res)
+{
+ struct irq_desc_devres *this = res;
+
+ irq_free_descs(this->from, this->cnt);
+}
+
+int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
+ unsigned int cnt, int node, struct module *owner,
+ const struct cpumask *affinity)
+{
+ struct irq_desc_devres *dr;
+ int base;
+
+ dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL);
+ if (!dr)
+ return -ENOMEM;
+
+ base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity);
+ if (base < 0) {
+ devres_free(dr);
+ return base;
+ }
+
+ dr->from = base;
+ dr->cnt = cnt;
+ devres_add(dev, dr);
+
+ return base;
+}
+EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs);
--
2.9.3
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v2 4/7] irqdesc: add memory managed version of irq_alloc_descs()
2017-02-06 12:10 ` [PATCH v2 4/7] irqdesc: add memory managed version of irq_alloc_descs() Bartosz Golaszewski
@ 2017-02-06 13:26 ` Linus Walleij
0 siblings, 0 replies; 17+ messages in thread
From: Linus Walleij @ 2017-02-06 13:26 UTC (permalink / raw)
To: Bartosz Golaszewski, Thomas Gleixner, Marc Zyngier
Cc: Alexandre Courbot, Bamvor Jian Zhang, linux-gpio@vger.kernel.org,
linux-kernel@vger.kernel.org
On Mon, Feb 6, 2017 at 1:10 PM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:
> Add a devres flavor of __devm_irq_alloc_descs() and corresponding
> helper macros.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Oh that was interesting. I will leave the IRQ subsystems specific
review to Thomas and Marc, but I think you need to also update
Documentation/driver-model/devres.txt
where you find a long list of all memorymanaged API calls.
Maybe the list is a bit silly actually, but anyway.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 5/7] gpio: mockup: add a dummy irqchip
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
` (3 preceding siblings ...)
2017-02-06 12:10 ` [PATCH v2 4/7] irqdesc: add memory managed version of irq_alloc_descs() Bartosz Golaszewski
@ 2017-02-06 12:10 ` Bartosz Golaszewski
2017-02-06 13:29 ` Linus Walleij
2017-02-06 12:10 ` [PATCH v2 6/7] gpiolib: include <gpio/consumer.h> from gpiolib.h Bartosz Golaszewski
` (2 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:10 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang,
Thomas Gleixner
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 | 1 +
drivers/gpio/gpio-mockup.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+)
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 2d77392..db6ff78 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -298,6 +298,7 @@ config GPIO_MOCKUP
tristate "GPIO Testing Driver"
depends on GPIOLIB && SYSFS
select GPIO_SYSFS
+ select GPIOLIB_IRQCHIP
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..7d82d35 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 = devm_irq_alloc_descs(dev, -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);
}
--
2.9.3
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v2 5/7] gpio: mockup: add a dummy irqchip
2017-02-06 12:10 ` [PATCH v2 5/7] gpio: mockup: add a dummy irqchip Bartosz Golaszewski
@ 2017-02-06 13:29 ` Linus Walleij
2017-02-06 13:31 ` Bartosz Golaszewski
0 siblings, 1 reply; 17+ messages in thread
From: Linus Walleij @ 2017-02-06 13:29 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Alexandre Courbot, Bamvor Jian Zhang, Thomas Gleixner,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
On Mon, Feb 6, 2017 at 1:10 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>
This is obviously exactly how we should do this.
However since it needs the devm* helpers from the irq
subsystem I can't apply them until the irqchip maintainers
merge or ack that patch.
If you want it in soon I suggest resending it with
non-memorymanaged accessors for the descs and later
make a separate patch to switch over to the devm_*
versions when/if it is accepted.
If you're not in a hurry, just wait :)
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v2 5/7] gpio: mockup: add a dummy irqchip
2017-02-06 13:29 ` Linus Walleij
@ 2017-02-06 13:31 ` Bartosz Golaszewski
2017-02-06 13:41 ` Linus Walleij
0 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 13:31 UTC (permalink / raw)
To: Linus Walleij
Cc: Alexandre Courbot, Bamvor Jian Zhang, Thomas Gleixner,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
2017-02-06 14:29 GMT+01:00 Linus Walleij <linus.walleij@linaro.org>:
> On Mon, Feb 6, 2017 at 1:10 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>
>
> This is obviously exactly how we should do this.
>
> However since it needs the devm* helpers from the irq
> subsystem I can't apply them until the irqchip maintainers
> merge or ack that patch.
>
> If you want it in soon I suggest resending it with
> non-memorymanaged accessors for the descs and later
> make a separate patch to switch over to the devm_*
> versions when/if it is accepted.
>
> If you're not in a hurry, just wait :)
>
> Yours,
> Linus Walleij
I think I'll do it, since it would be nice to have it go in for 4.11
(if you're ready to merge it that late into the release cycle).
Thanks,
Bartosz
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v2 5/7] gpio: mockup: add a dummy irqchip
2017-02-06 13:31 ` Bartosz Golaszewski
@ 2017-02-06 13:41 ` Linus Walleij
0 siblings, 0 replies; 17+ messages in thread
From: Linus Walleij @ 2017-02-06 13:41 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Alexandre Courbot, Bamvor Jian Zhang, Thomas Gleixner,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
On Mon, Feb 6, 2017 at 2:31 PM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:
> 2017-02-06 14:29 GMT+01:00 Linus Walleij <linus.walleij@linaro.org>:
>> On Mon, Feb 6, 2017 at 1:10 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>
>>
>> This is obviously exactly how we should do this.
>>
>> However since it needs the devm* helpers from the irq
>> subsystem I can't apply them until the irqchip maintainers
>> merge or ack that patch.
>>
>> If you want it in soon I suggest resending it with
>> non-memorymanaged accessors for the descs and later
>> make a separate patch to switch over to the devm_*
>> versions when/if it is accepted.
>>
>> If you're not in a hurry, just wait :)
>>
>> Yours,
>> Linus Walleij
>
> I think I'll do it, since it would be nice to have it go in for 4.11
> (if you're ready to merge it that late into the release cycle).
I will merge this. Expanding the testing is paramount.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 6/7] gpiolib: include <gpio/consumer.h> from gpiolib.h
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
` (4 preceding siblings ...)
2017-02-06 12:10 ` [PATCH v2 5/7] gpio: mockup: add a dummy irqchip Bartosz Golaszewski
@ 2017-02-06 12:10 ` Bartosz Golaszewski
2017-02-06 13:30 ` Linus Walleij
2017-02-06 12:10 ` [PATCH v2 7/7] gpio: mockup: implement event injecting over debugfs Bartosz Golaszewski
2017-02-06 12:12 ` [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
7 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:10 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang,
Thomas Gleixner
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
We want pull gpiolib.h in from the gpio mockup driver, but gpiod_flags
is defined in consumer.h, so we need that too indirectly.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
drivers/gpio/gpiolib.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index 2495b7e..067e5e5 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -13,6 +13,7 @@
#define GPIOLIB_H
#include <linux/gpio/driver.h>
+#include <linux/gpio/consumer.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/module.h>
--
2.9.3
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v2 6/7] gpiolib: include <gpio/consumer.h> from gpiolib.h
2017-02-06 12:10 ` [PATCH v2 6/7] gpiolib: include <gpio/consumer.h> from gpiolib.h Bartosz Golaszewski
@ 2017-02-06 13:30 ` Linus Walleij
0 siblings, 0 replies; 17+ messages in thread
From: Linus Walleij @ 2017-02-06 13:30 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Alexandre Courbot, Bamvor Jian Zhang, Thomas Gleixner,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
On Mon, Feb 6, 2017 at 1:10 PM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:
> We want pull gpiolib.h in from the gpio mockup driver, but gpiod_flags
> is defined in consumer.h, so we need that too indirectly.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Can't you just add the include to the mockup driver...?
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2 7/7] gpio: mockup: implement event injecting over debugfs
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
` (5 preceding siblings ...)
2017-02-06 12:10 ` [PATCH v2 6/7] gpiolib: include <gpio/consumer.h> from gpiolib.h Bartosz Golaszewski
@ 2017-02-06 12:10 ` Bartosz Golaszewski
2017-02-06 12:12 ` [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
7 siblings, 0 replies; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:10 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang,
Thomas Gleixner
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 | 115 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 114 insertions(+), 1 deletion(-)
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index 7d82d35..449918b 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -19,6 +19,10 @@
#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 +51,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 +69,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 +187,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 +309,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)
@@ -277,6 +384,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;
@@ -298,6 +410,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] 17+ messages in thread
* Re: [PATCH v2 0/7] gpio: mockup: extensions for testing purposes
2017-02-06 12:10 [PATCH v2 0/7] gpio: mockup: extensions for testing purposes Bartosz Golaszewski
` (6 preceding siblings ...)
2017-02-06 12:10 ` [PATCH v2 7/7] gpio: mockup: implement event injecting over debugfs Bartosz Golaszewski
@ 2017-02-06 12:12 ` Bartosz Golaszewski
7 siblings, 0 replies; 17+ messages in thread
From: Bartosz Golaszewski @ 2017-02-06 12:12 UTC (permalink / raw)
To: Linus Walleij, Alexandre Courbot, Bamvor Jian Zhang
Cc: linux-gpio, LKML, Bartosz Golaszewski
2017-02-06 13:10 GMT+01:00 Bartosz Golaszewski <bgolaszewski@baylibre.com>:
> I would like to create an automated test-suite for libgpiod, but
> the gpio-mockup driver is quite limited when it comes to current
> user space functionality - I can't test neither line event
> notifications nor finding GPIO lines by name.
>
> This series proposes to extend the gpio framework by allowing to
> inject line events from the kernel code and by providing a debugfs
> interface for that to the gpio-mockup driver. We also allow the
> user to request that the mockup driver name the lines.
>
> The first two patches only contain coding style changes which, I
> believe, will make the driver easier to maintain.
>
> The third patch adds the option to have the lines named.
>
> The fourth patch adds a devres flavor of irq_alloc_descs() to be used
> in patch 5, which actually adds the dummy irqchip.
>
> The last two patches implement the debugfs directory structure that
> can be used by the user space to inject line events.
>
> v1 -> v2:
> - made the event injection self-contained within the driver by using
> the irq_work as suggested by Lars-Peter Clauses
s/ Lars-Peter Clauses/ Lars-Peter Clausen/
sorry!
Thanks,
Bartosz
^ permalink raw reply [flat|nested] 17+ messages in thread