Commit 8d7cf837 authored by Shawn Guo's avatar Shawn Guo Committed by Grant Likely

gpio/mxs: Change gpio-mxs into an upstanding gpio driver

The patch makes necessary changes on gpio-mxs as below to turn it
into an upstanding gpio driver.

 * Clean up the gpio port definition stuff

 * Use readl/writel to replace mach-specific accessors
   __raw_readl/__raw_writel

 * Change mxs_gpio_init into mxs_gpio_probe function

And it then migrates mach-mxs to the updated driver by adding
corresponding platform devices.
Signed-off-by: default avatarShawn Guo <shawn.guo@linaro.org>
Acked-by: default avatarArnd Bergmann <arnd@arndb.de>
Acked-by: default avatarSascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: default avatarGrant Likely <grant.likely@secretlab.ca>
parent 7b2fa570
...@@ -88,3 +88,14 @@ int __init mxs_add_amba_device(const struct amba_device *dev) ...@@ -88,3 +88,14 @@ int __init mxs_add_amba_device(const struct amba_device *dev)
return amba_device_register(adev, &iomem_resource); return amba_device_register(adev, &iomem_resource);
} }
struct device mxs_apbh_bus = {
.init_name = "mxs_apbh",
.parent = &platform_bus,
};
static int __init mxs_device_init(void)
{
return device_register(&mxs_apbh_bus);
}
core_initcall(mxs_device_init);
...@@ -6,4 +6,5 @@ obj-$(CONFIG_MXS_HAVE_PLATFORM_FLEXCAN) += platform-flexcan.o ...@@ -6,4 +6,5 @@ obj-$(CONFIG_MXS_HAVE_PLATFORM_FLEXCAN) += platform-flexcan.o
obj-$(CONFIG_MXS_HAVE_PLATFORM_MXS_I2C) += platform-mxs-i2c.o obj-$(CONFIG_MXS_HAVE_PLATFORM_MXS_I2C) += platform-mxs-i2c.o
obj-$(CONFIG_MXS_HAVE_PLATFORM_MXS_MMC) += platform-mxs-mmc.o obj-$(CONFIG_MXS_HAVE_PLATFORM_MXS_MMC) += platform-mxs-mmc.o
obj-$(CONFIG_MXS_HAVE_PLATFORM_MXS_PWM) += platform-mxs-pwm.o obj-$(CONFIG_MXS_HAVE_PLATFORM_MXS_PWM) += platform-mxs-pwm.o
obj-y += platform-gpio-mxs.o
obj-$(CONFIG_MXS_HAVE_PLATFORM_MXSFB) += platform-mxsfb.o obj-$(CONFIG_MXS_HAVE_PLATFORM_MXSFB) += platform-mxsfb.o
/*
* Copyright 2011 Freescale Semiconductor, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*/
#include <linux/compiler.h>
#include <linux/err.h>
#include <linux/init.h>
#include <mach/mx23.h>
#include <mach/mx28.h>
#include <mach/devices-common.h>
struct platform_device *__init mxs_add_gpio(
int id, resource_size_t iobase, int irq)
{
struct resource res[] = {
{
.start = iobase,
.end = iobase + SZ_8K - 1,
.flags = IORESOURCE_MEM,
}, {
.start = irq,
.end = irq,
.flags = IORESOURCE_IRQ,
},
};
return platform_device_register_resndata(&mxs_apbh_bus,
"gpio-mxs", id, res, ARRAY_SIZE(res), NULL, 0);
}
static int __init mxs_add_mxs_gpio(void)
{
if (cpu_is_mx23()) {
mxs_add_gpio(0, MX23_PINCTRL_BASE_ADDR, MX23_INT_GPIO0);
mxs_add_gpio(1, MX23_PINCTRL_BASE_ADDR, MX23_INT_GPIO1);
mxs_add_gpio(2, MX23_PINCTRL_BASE_ADDR, MX23_INT_GPIO2);
}
if (cpu_is_mx28()) {
mxs_add_gpio(0, MX28_PINCTRL_BASE_ADDR, MX28_INT_GPIO0);
mxs_add_gpio(1, MX28_PINCTRL_BASE_ADDR, MX28_INT_GPIO1);
mxs_add_gpio(2, MX28_PINCTRL_BASE_ADDR, MX28_INT_GPIO2);
mxs_add_gpio(3, MX28_PINCTRL_BASE_ADDR, MX28_INT_GPIO3);
mxs_add_gpio(4, MX28_PINCTRL_BASE_ADDR, MX28_INT_GPIO4);
}
return 0;
}
postcore_initcall(mxs_add_mxs_gpio);
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/amba/bus.h> #include <linux/amba/bus.h>
extern struct device mxs_apbh_bus;
struct platform_device *mxs_add_platform_device_dmamask( struct platform_device *mxs_add_platform_device_dmamask(
const char *name, int id, const char *name, int id,
const struct resource *res, unsigned int num_resources, const struct resource *res, unsigned int num_resources,
......
...@@ -41,5 +41,4 @@ void __init mx23_map_io(void) ...@@ -41,5 +41,4 @@ void __init mx23_map_io(void)
void __init mx23_init_irq(void) void __init mx23_init_irq(void)
{ {
icoll_init_irq(); icoll_init_irq();
mx23_register_gpios();
} }
...@@ -41,5 +41,4 @@ void __init mx28_map_io(void) ...@@ -41,5 +41,4 @@ void __init mx28_map_io(void)
void __init mx28_init_irq(void) void __init mx28_init_irq(void)
{ {
icoll_init_irq(); icoll_init_irq();
mx28_register_gpios();
} }
...@@ -25,12 +25,12 @@ ...@@ -25,12 +25,12 @@
#include <linux/io.h> #include <linux/io.h>
#include <linux/irq.h> #include <linux/irq.h>
#include <linux/gpio.h> #include <linux/gpio.h>
#include <mach/mx23.h> #include <linux/platform_device.h>
#include <mach/mx28.h> #include <linux/slab.h>
#include <asm-generic/bug.h> #include <mach/mxs.h>
static struct mxs_gpio_port *mxs_gpio_ports; #define MXS_SET 0x4
static int gpio_table_size; #define MXS_CLR 0x8
#define PINCTRL_DOUT(n) ((cpu_is_mx23() ? 0x0500 : 0x0700) + (n) * 0x10) #define PINCTRL_DOUT(n) ((cpu_is_mx23() ? 0x0500 : 0x0700) + (n) * 0x10)
#define PINCTRL_DIN(n) ((cpu_is_mx23() ? 0x0600 : 0x0900) + (n) * 0x10) #define PINCTRL_DIN(n) ((cpu_is_mx23() ? 0x0600 : 0x0900) + (n) * 0x10)
...@@ -61,36 +61,42 @@ struct mxs_gpio_port { ...@@ -61,36 +61,42 @@ struct mxs_gpio_port {
static void clear_gpio_irqstatus(struct mxs_gpio_port *port, u32 index) static void clear_gpio_irqstatus(struct mxs_gpio_port *port, u32 index)
{ {
__mxs_clrl(1 << index, port->base + PINCTRL_IRQSTAT(port->id)); writel(1 << index, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
} }
static void set_gpio_irqenable(struct mxs_gpio_port *port, u32 index, static void set_gpio_irqenable(struct mxs_gpio_port *port, u32 index,
int enable) int enable)
{ {
if (enable) { if (enable) {
__mxs_setl(1 << index, port->base + PINCTRL_IRQEN(port->id)); writel(1 << index,
__mxs_setl(1 << index, port->base + PINCTRL_PIN2IRQ(port->id)); port->base + PINCTRL_IRQEN(port->id) + MXS_SET);
writel(1 << index,
port->base + PINCTRL_PIN2IRQ(port->id) + MXS_SET);
} else { } else {
__mxs_clrl(1 << index, port->base + PINCTRL_IRQEN(port->id)); writel(1 << index,
port->base + PINCTRL_IRQEN(port->id) + MXS_CLR);
} }
} }
static void mxs_gpio_ack_irq(struct irq_data *d) static void mxs_gpio_ack_irq(struct irq_data *d)
{ {
struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
u32 gpio = irq_to_gpio(d->irq); u32 gpio = irq_to_gpio(d->irq);
clear_gpio_irqstatus(&mxs_gpio_ports[gpio / 32], gpio & 0x1f); clear_gpio_irqstatus(port, gpio & 0x1f);
} }
static void mxs_gpio_mask_irq(struct irq_data *d) static void mxs_gpio_mask_irq(struct irq_data *d)
{ {
struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
u32 gpio = irq_to_gpio(d->irq); u32 gpio = irq_to_gpio(d->irq);
set_gpio_irqenable(&mxs_gpio_ports[gpio / 32], gpio & 0x1f, 0); set_gpio_irqenable(port, gpio & 0x1f, 0);
} }
static void mxs_gpio_unmask_irq(struct irq_data *d) static void mxs_gpio_unmask_irq(struct irq_data *d)
{ {
struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
u32 gpio = irq_to_gpio(d->irq); u32 gpio = irq_to_gpio(d->irq);
set_gpio_irqenable(&mxs_gpio_ports[gpio / 32], gpio & 0x1f, 1); set_gpio_irqenable(port, gpio & 0x1f, 1);
} }
static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset); static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset);
...@@ -99,7 +105,7 @@ static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type) ...@@ -99,7 +105,7 @@ static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
{ {
u32 gpio = irq_to_gpio(d->irq); u32 gpio = irq_to_gpio(d->irq);
u32 pin_mask = 1 << (gpio & 31); u32 pin_mask = 1 << (gpio & 31);
struct mxs_gpio_port *port = &mxs_gpio_ports[gpio / 32]; struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
void __iomem *pin_addr; void __iomem *pin_addr;
int edge; int edge;
...@@ -123,16 +129,16 @@ static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type) ...@@ -123,16 +129,16 @@ static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
/* set level or edge */ /* set level or edge */
pin_addr = port->base + PINCTRL_IRQLEV(port->id); pin_addr = port->base + PINCTRL_IRQLEV(port->id);
if (edge & GPIO_INT_LEV_MASK) if (edge & GPIO_INT_LEV_MASK)
__mxs_setl(pin_mask, pin_addr); writel(pin_mask, pin_addr + MXS_SET);
else else
__mxs_clrl(pin_mask, pin_addr); writel(pin_mask, pin_addr + MXS_CLR);
/* set polarity */ /* set polarity */
pin_addr = port->base + PINCTRL_IRQPOL(port->id); pin_addr = port->base + PINCTRL_IRQPOL(port->id);
if (edge & GPIO_INT_POL_MASK) if (edge & GPIO_INT_POL_MASK)
__mxs_setl(pin_mask, pin_addr); writel(pin_mask, pin_addr + MXS_SET);
else else
__mxs_clrl(pin_mask, pin_addr); writel(pin_mask, pin_addr + MXS_CLR);
clear_gpio_irqstatus(port, gpio & 0x1f); clear_gpio_irqstatus(port, gpio & 0x1f);
...@@ -143,13 +149,13 @@ static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type) ...@@ -143,13 +149,13 @@ static int mxs_gpio_set_irq_type(struct irq_data *d, unsigned int type)
static void mxs_gpio_irq_handler(u32 irq, struct irq_desc *desc) static void mxs_gpio_irq_handler(u32 irq, struct irq_desc *desc)
{ {
u32 irq_stat; u32 irq_stat;
struct mxs_gpio_port *port = (struct mxs_gpio_port *)irq_get_handler_data(irq); struct mxs_gpio_port *port = irq_get_handler_data(irq);
u32 gpio_irq_no_base = port->virtual_irq_start; u32 gpio_irq_no_base = port->virtual_irq_start;
desc->irq_data.chip->irq_ack(&desc->irq_data); desc->irq_data.chip->irq_ack(&desc->irq_data);
irq_stat = __raw_readl(port->base + PINCTRL_IRQSTAT(port->id)) & irq_stat = readl(port->base + PINCTRL_IRQSTAT(port->id)) &
__raw_readl(port->base + PINCTRL_IRQEN(port->id)); readl(port->base + PINCTRL_IRQEN(port->id));
while (irq_stat != 0) { while (irq_stat != 0) {
int irqoffset = fls(irq_stat) - 1; int irqoffset = fls(irq_stat) - 1;
...@@ -171,7 +177,7 @@ static int mxs_gpio_set_wake_irq(struct irq_data *d, unsigned int enable) ...@@ -171,7 +177,7 @@ static int mxs_gpio_set_wake_irq(struct irq_data *d, unsigned int enable)
{ {
u32 gpio = irq_to_gpio(d->irq); u32 gpio = irq_to_gpio(d->irq);
u32 gpio_idx = gpio & 0x1f; u32 gpio_idx = gpio & 0x1f;
struct mxs_gpio_port *port = &mxs_gpio_ports[gpio / 32]; struct mxs_gpio_port *port = irq_data_get_irq_chip_data(d);
if (enable) { if (enable) {
if (port->irq_high && (gpio_idx >= 16)) if (port->irq_high && (gpio_idx >= 16))
...@@ -205,9 +211,9 @@ static void mxs_set_gpio_direction(struct gpio_chip *chip, unsigned offset, ...@@ -205,9 +211,9 @@ static void mxs_set_gpio_direction(struct gpio_chip *chip, unsigned offset,
void __iomem *pin_addr = port->base + PINCTRL_DOE(port->id); void __iomem *pin_addr = port->base + PINCTRL_DOE(port->id);
if (dir) if (dir)
__mxs_setl(1 << offset, pin_addr); writel(1 << offset, pin_addr + MXS_SET);
else else
__mxs_clrl(1 << offset, pin_addr); writel(1 << offset, pin_addr + MXS_CLR);
} }
static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset) static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset)
...@@ -215,7 +221,7 @@ static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset) ...@@ -215,7 +221,7 @@ static int mxs_gpio_get(struct gpio_chip *chip, unsigned offset)
struct mxs_gpio_port *port = struct mxs_gpio_port *port =
container_of(chip, struct mxs_gpio_port, chip); container_of(chip, struct mxs_gpio_port, chip);
return (__raw_readl(port->base + PINCTRL_DIN(port->id)) >> offset) & 1; return (readl(port->base + PINCTRL_DIN(port->id)) >> offset) & 1;
} }
static void mxs_gpio_set(struct gpio_chip *chip, unsigned offset, int value) static void mxs_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
...@@ -225,9 +231,9 @@ static void mxs_gpio_set(struct gpio_chip *chip, unsigned offset, int value) ...@@ -225,9 +231,9 @@ static void mxs_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
void __iomem *pin_addr = port->base + PINCTRL_DOUT(port->id); void __iomem *pin_addr = port->base + PINCTRL_DOUT(port->id);
if (value) if (value)
__mxs_setl(1 << offset, pin_addr); writel(1 << offset, pin_addr + MXS_SET);
else else
__mxs_clrl(1 << offset, pin_addr); writel(1 << offset, pin_addr + MXS_CLR);
} }
static int mxs_gpio_to_irq(struct gpio_chip *chip, unsigned offset) static int mxs_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
...@@ -252,87 +258,113 @@ static int mxs_gpio_direction_output(struct gpio_chip *chip, ...@@ -252,87 +258,113 @@ static int mxs_gpio_direction_output(struct gpio_chip *chip,
return 0; return 0;
} }
int __init mxs_gpio_init(struct mxs_gpio_port *port, int cnt) static int __devinit mxs_gpio_probe(struct platform_device *pdev)
{ {
int i, j; static void __iomem *base;
struct mxs_gpio_port *port;
struct resource *iores = NULL;
int err, i;
port = kzalloc(sizeof(struct mxs_gpio_port), GFP_KERNEL);
if (!port)
return -ENOMEM;
port->id = pdev->id;
port->virtual_irq_start = MXS_GPIO_IRQ_START + port->id * 32;
/*
* map memory region only once, as all the gpio ports
* share the same one
*/
if (!base) {
iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!iores) {
err = -ENODEV;
goto out_kfree;
}
/* save for local usage */ if (!request_mem_region(iores->start, resource_size(iores),
mxs_gpio_ports = port; pdev->name)) {
gpio_table_size = cnt; err = -EBUSY;
goto out_kfree;
}
pr_info("MXS GPIO hardware\n"); base = ioremap(iores->start, resource_size(iores));
if (!base) {
err = -ENOMEM;
goto out_release_mem;
}
}
port->base = base;
for (i = 0; i < cnt; i++) { port->irq = platform_get_irq(pdev, 0);
/* disable the interrupt and clear the status */ if (port->irq < 0) {
__raw_writel(0, port[i].base + PINCTRL_PIN2IRQ(i)); err = -EINVAL;
__raw_writel(0, port[i].base + PINCTRL_IRQEN(i)); goto out_iounmap;
}
/* clear address has to be used to clear IRQSTAT bits */ /* disable the interrupt and clear the status */
__mxs_clrl(~0U, port[i].base + PINCTRL_IRQSTAT(i)); writel(0, port->base + PINCTRL_PIN2IRQ(port->id));
writel(0, port->base + PINCTRL_IRQEN(port->id));
for (j = port[i].virtual_irq_start; /* clear address has to be used to clear IRQSTAT bits */
j < port[i].virtual_irq_start + 32; j++) { writel(~0U, port->base + PINCTRL_IRQSTAT(port->id) + MXS_CLR);
irq_set_chip_and_handler(j, &gpio_irq_chip,
handle_level_irq);
set_irq_flags(j, IRQF_VALID);
}
/* setup one handler for each entry */ for (i = port->virtual_irq_start;
irq_set_chained_handler(port[i].irq, mxs_gpio_irq_handler); i < port->virtual_irq_start + 32; i++) {
irq_set_handler_data(port[i].irq, &port[i]); irq_set_chip_and_handler(i, &gpio_irq_chip,
handle_level_irq);
/* register gpio chip */ set_irq_flags(i, IRQF_VALID);
port[i].chip.direction_input = mxs_gpio_direction_input; irq_set_chip_data(i, port);
port[i].chip.direction_output = mxs_gpio_direction_output;
port[i].chip.get = mxs_gpio_get;
port[i].chip.set = mxs_gpio_set;
port[i].chip.to_irq = mxs_gpio_to_irq;
port[i].chip.base = i * 32;
port[i].chip.ngpio = 32;
/* its a serious configuration bug when it fails */
BUG_ON(gpiochip_add(&port[i].chip) < 0);
} }
return 0; /* setup one handler for each entry */
} irq_set_chained_handler(port->irq, mxs_gpio_irq_handler);
irq_set_handler_data(port->irq, port);
#define MX23_GPIO_BASE MX23_IO_ADDRESS(MX23_PINCTRL_BASE_ADDR) /* register gpio chip */
#define MX28_GPIO_BASE MX28_IO_ADDRESS(MX28_PINCTRL_BASE_ADDR) port->chip.direction_input = mxs_gpio_direction_input;
port->chip.direction_output = mxs_gpio_direction_output;
port->chip.get = mxs_gpio_get;
port->chip.set = mxs_gpio_set;
port->chip.to_irq = mxs_gpio_to_irq;
port->chip.base = port->id * 32;
port->chip.ngpio = 32;
#define DEFINE_MXS_GPIO_PORT(_base, _irq, _id) \ err = gpiochip_add(&port->chip);
{ \ if (err)
.chip.label = "gpio-" #_id, \ goto out_iounmap;
.id = _id, \
.irq = _irq, \
.base = _base, \
.virtual_irq_start = MXS_GPIO_IRQ_START + (_id) * 32, \
}
#ifdef CONFIG_SOC_IMX23 return 0;
static struct mxs_gpio_port mx23_gpio_ports[] = {
DEFINE_MXS_GPIO_PORT(MX23_GPIO_BASE, MX23_INT_GPIO0, 0),
DEFINE_MXS_GPIO_PORT(MX23_GPIO_BASE, MX23_INT_GPIO1, 1),
DEFINE_MXS_GPIO_PORT(MX23_GPIO_BASE, MX23_INT_GPIO2, 2),
};
int __init mx23_register_gpios(void) out_iounmap:
{ if (iores)
return mxs_gpio_init(mx23_gpio_ports, ARRAY_SIZE(mx23_gpio_ports)); iounmap(port->base);
out_release_mem:
if (iores)
release_mem_region(iores->start, resource_size(iores));
out_kfree:
kfree(port);
dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
return err;
} }
#endif
static struct platform_driver mxs_gpio_driver = {
#ifdef CONFIG_SOC_IMX28 .driver = {
static struct mxs_gpio_port mx28_gpio_ports[] = { .name = "gpio-mxs",
DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO0, 0), .owner = THIS_MODULE,
DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO1, 1), },
DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO2, 2), .probe = mxs_gpio_probe,
DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO3, 3),
DEFINE_MXS_GPIO_PORT(MX28_GPIO_BASE, MX28_INT_GPIO4, 4),
}; };
int __init mx28_register_gpios(void) static int __init mxs_gpio_init(void)
{ {
return mxs_gpio_init(mx28_gpio_ports, ARRAY_SIZE(mx28_gpio_ports)); return platform_driver_register(&mxs_gpio_driver);
} }
#endif postcore_initcall(mxs_gpio_init);
MODULE_AUTHOR("Freescale Semiconductor, "
"Daniel Mack <danielncaiaq.de>, "
"Juergen Beisert <kernel@pengutronix.de>");
MODULE_DESCRIPTION("Freescale MXS GPIO");
MODULE_LICENSE("GPL");
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment