* [PATCH] gpio: ath79: Convert to the state container design pattern
@ 2015-09-01 9:38 Alban Bedel
2015-09-25 16:39 ` Linus Walleij
0 siblings, 1 reply; 4+ messages in thread
From: Alban Bedel @ 2015-09-01 9:38 UTC (permalink / raw)
To: linux-gpio
Cc: Linus Walleij, Alexandre Courbot, Ralf Baechle, Gabor Juhos,
linux-mips, linux-kernel, Alban Bedel
Turn the ath79 driver into a true driver supporting multiple
instances. While at it also removed unneed includes and make use of
the BIT() macro.
Signed-off-by: Alban Bedel <albeu@free.fr>
---
This patch apply on top of my previous MIPS GPIO patches that are pending
for 4.3, so it might be better to take it thru the MIPS tree.
Alban
---
drivers/gpio/gpio-ath79.c | 129 +++++++++++++++++++++++-----------------------
1 file changed, 65 insertions(+), 64 deletions(-)
diff --git a/drivers/gpio/gpio-ath79.c b/drivers/gpio/gpio-ath79.c
index 03b9953..e5827a5 100644
--- a/drivers/gpio/gpio-ath79.c
+++ b/drivers/gpio/gpio-ath79.c
@@ -12,61 +12,51 @@
* by the Free Software Foundation.
*/
-#include <linux/kernel.h>
-#include <linux/init.h>
-#include <linux/module.h>
-#include <linux/types.h>
-#include <linux/spinlock.h>
-#include <linux/io.h>
-#include <linux/ioport.h>
-#include <linux/gpio.h>
+#include <linux/gpio/driver.h>
#include <linux/platform_data/gpio-ath79.h>
#include <linux/of_device.h>
#include <asm/mach-ath79/ar71xx_regs.h>
-static void __iomem *ath79_gpio_base;
-static u32 ath79_gpio_count;
-static DEFINE_SPINLOCK(ath79_gpio_lock);
+struct ath79_gpio_ctrl {
+ struct gpio_chip chip;
+ void __iomem *base;
+ spinlock_t lock;
+};
-static void __ath79_gpio_set_value(unsigned gpio, int value)
-{
- void __iomem *base = ath79_gpio_base;
-
- if (value)
- __raw_writel(1 << gpio, base + AR71XX_GPIO_REG_SET);
- else
- __raw_writel(1 << gpio, base + AR71XX_GPIO_REG_CLEAR);
-}
-
-static int __ath79_gpio_get_value(unsigned gpio)
-{
- return (__raw_readl(ath79_gpio_base + AR71XX_GPIO_REG_IN) >> gpio) & 1;
-}
-
-static int ath79_gpio_get_value(struct gpio_chip *chip, unsigned offset)
-{
- return __ath79_gpio_get_value(offset);
-}
+#define to_ath79_gpio_ctrl(c) container_of(c, struct ath79_gpio_ctrl, chip)
static void ath79_gpio_set_value(struct gpio_chip *chip,
- unsigned offset, int value)
+ unsigned gpio, int value)
{
- __ath79_gpio_set_value(offset, value);
+ struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
+
+ if (value)
+ __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_SET);
+ else
+ __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_CLEAR);
+}
+
+static int ath79_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
+{
+ struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
+
+ return (__raw_readl(ctrl->base + AR71XX_GPIO_REG_IN) >> gpio) & 1;
}
static int ath79_gpio_direction_input(struct gpio_chip *chip,
unsigned offset)
{
- void __iomem *base = ath79_gpio_base;
+ struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
unsigned long flags;
- spin_lock_irqsave(&ath79_gpio_lock, flags);
+ spin_lock_irqsave(&ctrl->lock, flags);
- __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) & ~(1 << offset),
- base + AR71XX_GPIO_REG_OE);
+ __raw_writel(
+ __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & ~BIT(offset),
+ ctrl->base + AR71XX_GPIO_REG_OE);
- spin_unlock_irqrestore(&ath79_gpio_lock, flags);
+ spin_unlock_irqrestore(&ctrl->lock, flags);
return 0;
}
@@ -74,35 +64,37 @@ static int ath79_gpio_direction_input(struct gpio_chip *chip,
static int ath79_gpio_direction_output(struct gpio_chip *chip,
unsigned offset, int value)
{
- void __iomem *base = ath79_gpio_base;
+ struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
unsigned long flags;
- spin_lock_irqsave(&ath79_gpio_lock, flags);
+ spin_lock_irqsave(&ctrl->lock, flags);
if (value)
- __raw_writel(1 << offset, base + AR71XX_GPIO_REG_SET);
+ __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET);
else
- __raw_writel(1 << offset, base + AR71XX_GPIO_REG_CLEAR);
+ __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR);
- __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) | (1 << offset),
- base + AR71XX_GPIO_REG_OE);
+ __raw_writel(
+ __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset),
+ ctrl->base + AR71XX_GPIO_REG_OE);
- spin_unlock_irqrestore(&ath79_gpio_lock, flags);
+ spin_unlock_irqrestore(&ctrl->lock, flags);
return 0;
}
static int ar934x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
- void __iomem *base = ath79_gpio_base;
+ struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
unsigned long flags;
- spin_lock_irqsave(&ath79_gpio_lock, flags);
+ spin_lock_irqsave(&ctrl->lock, flags);
- __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) | (1 << offset),
- base + AR71XX_GPIO_REG_OE);
+ __raw_writel(
+ __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset),
+ ctrl->base + AR71XX_GPIO_REG_OE);
- spin_unlock_irqrestore(&ath79_gpio_lock, flags);
+ spin_unlock_irqrestore(&ctrl->lock, flags);
return 0;
}
@@ -110,25 +102,26 @@ static int ar934x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
static int ar934x_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
int value)
{
- void __iomem *base = ath79_gpio_base;
+ struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
unsigned long flags;
- spin_lock_irqsave(&ath79_gpio_lock, flags);
+ spin_lock_irqsave(&ctrl->lock, flags);
if (value)
- __raw_writel(1 << offset, base + AR71XX_GPIO_REG_SET);
+ __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET);
else
- __raw_writel(1 << offset, base + AR71XX_GPIO_REG_CLEAR);
+ __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR);
- __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) & ~(1 << offset),
- base + AR71XX_GPIO_REG_OE);
+ __raw_writel(
+ __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & BIT(offset),
+ ctrl->base + AR71XX_GPIO_REG_OE);
- spin_unlock_irqrestore(&ath79_gpio_lock, flags);
+ spin_unlock_irqrestore(&ctrl->lock, flags);
return 0;
}
-static struct gpio_chip ath79_gpio_chip = {
+static const struct gpio_chip ath79_gpio_chip = {
.label = "ath79",
.get = ath79_gpio_get_value,
.set = ath79_gpio_set_value,
@@ -147,10 +140,16 @@ static int ath79_gpio_probe(struct platform_device *pdev)
{
struct ath79_gpio_platform_data *pdata = pdev->dev.platform_data;
struct device_node *np = pdev->dev.of_node;
+ struct ath79_gpio_ctrl *ctrl;
struct resource *res;
+ u32 ath79_gpio_count;
bool oe_inverted;
int err;
+ ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
+ if (!ctrl)
+ return -ENOMEM;
+
if (np) {
err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
if (err) {
@@ -171,19 +170,21 @@ static int ath79_gpio_probe(struct platform_device *pdev)
}
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- ath79_gpio_base = devm_ioremap_nocache(
+ ctrl->base = devm_ioremap_nocache(
&pdev->dev, res->start, resource_size(res));
- if (!ath79_gpio_base)
+ if (!ctrl->base)
return -ENOMEM;
- ath79_gpio_chip.dev = &pdev->dev;
- ath79_gpio_chip.ngpio = ath79_gpio_count;
+ spin_lock_init(&ctrl->lock);
+ memcpy(&ctrl->chip, &ath79_gpio_chip, sizeof(ctrl->chip));
+ ctrl->chip.dev = &pdev->dev;
+ ctrl->chip.ngpio = ath79_gpio_count;
if (oe_inverted) {
- ath79_gpio_chip.direction_input = ar934x_gpio_direction_input;
- ath79_gpio_chip.direction_output = ar934x_gpio_direction_output;
+ ctrl->chip.direction_input = ar934x_gpio_direction_input;
+ ctrl->chip.direction_output = ar934x_gpio_direction_output;
}
- err = gpiochip_add(&ath79_gpio_chip);
+ err = gpiochip_add(&ctrl->chip);
if (err) {
dev_err(&pdev->dev,
"cannot add AR71xx GPIO chip, error=%d", err);
--
2.0.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] gpio: ath79: Convert to the state container design pattern
2015-09-01 9:38 [PATCH] gpio: ath79: Convert to the state container design pattern Alban Bedel
@ 2015-09-25 16:39 ` Linus Walleij
2015-10-02 6:52 ` Alban
0 siblings, 1 reply; 4+ messages in thread
From: Linus Walleij @ 2015-09-25 16:39 UTC (permalink / raw)
To: Alban Bedel
Cc: linux-gpio@vger.kernel.org, Alexandre Courbot, Ralf Baechle,
Gabor Juhos, Linux MIPS, linux-kernel@vger.kernel.org
On Tue, Sep 1, 2015 at 2:38 AM, Alban Bedel <albeu@free.fr> wrote:
> Turn the ath79 driver into a true driver supporting multiple
> instances. While at it also removed unneed includes and make use of
> the BIT() macro.
>
> Signed-off-by: Alban Bedel <albeu@free.fr>
> ---
>
> This patch apply on top of my previous MIPS GPIO patches that are pending
> for 4.3, so it might be better to take it thru the MIPS tree.
Patch applied (for v4.4).
Would you consider sending a patch adding yourself as maintainer
for this file in MAINTAINERS?
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] gpio: ath79: Convert to the state container design pattern
2015-09-25 16:39 ` Linus Walleij
@ 2015-10-02 6:52 ` Alban
2015-10-05 7:41 ` Linus Walleij
0 siblings, 1 reply; 4+ messages in thread
From: Alban @ 2015-10-02 6:52 UTC (permalink / raw)
To: Linus Walleij
Cc: Aban Bedel, linux-gpio@vger.kernel.org, Alexandre Courbot,
Ralf Baechle, Gabor Juhos, Linux MIPS,
linux-kernel@vger.kernel.org
On Fri, 25 Sep 2015 09:39:51 -0700
Linus Walleij <linus.walleij@linaro.org> wrote:
> On Tue, Sep 1, 2015 at 2:38 AM, Alban Bedel <albeu@free.fr> wrote:
>
> > Turn the ath79 driver into a true driver supporting multiple
> > instances. While at it also removed unneed includes and make use of
> > the BIT() macro.
> >
> > Signed-off-by: Alban Bedel <albeu@free.fr>
> > ---
> >
> > This patch apply on top of my previous MIPS GPIO patches that are
> > pending for 4.3, so it might be better to take it thru the MIPS
> > tree.
>
> Patch applied (for v4.4).
>
> Would you consider sending a patch adding yourself as maintainer
> for this file in MAINTAINERS?
Yes, I'll send that soon.
However while looking at adding IRQ support I stumbled upon the bgpio
API from the generic gpio driver. I think it might make more sense
to simply move to this API instead of doing the above cleanup. I'll
send a patch for this in the next days.
Alban
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] gpio: ath79: Convert to the state container design pattern
2015-10-02 6:52 ` Alban
@ 2015-10-05 7:41 ` Linus Walleij
0 siblings, 0 replies; 4+ messages in thread
From: Linus Walleij @ 2015-10-05 7:41 UTC (permalink / raw)
To: Alban
Cc: linux-gpio@vger.kernel.org, Alexandre Courbot, Ralf Baechle,
Gabor Juhos, Linux MIPS, linux-kernel@vger.kernel.org
On Fri, Oct 2, 2015 at 8:52 AM, Alban <albeu@free.fr> wrote:
>> Would you consider sending a patch adding yourself as maintainer
>> for this file in MAINTAINERS?
>
> Yes, I'll send that soon.
Thanks!
> However while looking at adding IRQ support I stumbled upon the bgpio
> API from the generic gpio driver. I think it might make more sense
> to simply move to this API instead of doing the above cleanup. I'll
> send a patch for this in the next days.
Yeah maybe that is better. For IRQ support please use GPIOLIB_IRQCHIP
if you can. I think I saw that OpenWRT has implemented IRQ support for
ath79 with some outoftree patch right?
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-10-05 7:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-01 9:38 [PATCH] gpio: ath79: Convert to the state container design pattern Alban Bedel
2015-09-25 16:39 ` Linus Walleij
2015-10-02 6:52 ` Alban
2015-10-05 7:41 ` Linus Walleij
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).