2010-12-18 16:39:31 +03:00
/*
* MXC GPIO support . ( c ) 2008 Daniel Mack < daniel @ caiaq . de >
* Copyright 2008 Juergen Beisert , kernel @ pengutronix . de
*
* Based on code from Freescale ,
* Copyright ( C ) 2004 - 2010 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
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*/
# include <linux/init.h>
# include <linux/interrupt.h>
# include <linux/io.h>
# include <linux/irq.h>
# include <linux/gpio.h>
2012-05-04 10:29:22 +04:00
# include <linux/of.h>
# include <linux/of_address.h>
# include <linux/of_device.h>
2011-06-06 19:37:58 +04:00
# include <linux/platform_device.h>
# include <linux/slab.h>
2011-06-06 18:31:29 +04:00
# include <linux/basic_mmio_gpio.h>
2011-07-03 21:38:09 +04:00
# include <linux/module.h>
2010-12-18 16:39:31 +03:00
2011-06-06 19:37:58 +04:00
# define MXS_SET 0x4
# define MXS_CLR 0x8
2010-12-18 16:39:31 +03:00
2012-05-03 19:32:52 +04:00
# define PINCTRL_DOUT(p) ((is_imx23_gpio(p) ? 0x0500 : 0x0700) + (p->id) * 0x10)
# define PINCTRL_DIN(p) ((is_imx23_gpio(p) ? 0x0600 : 0x0900) + (p->id) * 0x10)
# define PINCTRL_DOE(p) ((is_imx23_gpio(p) ? 0x0700 : 0x0b00) + (p->id) * 0x10)
# define PINCTRL_PIN2IRQ(p) ((is_imx23_gpio(p) ? 0x0800 : 0x1000) + (p->id) * 0x10)
# define PINCTRL_IRQEN(p) ((is_imx23_gpio(p) ? 0x0900 : 0x1100) + (p->id) * 0x10)
# define PINCTRL_IRQLEV(p) ((is_imx23_gpio(p) ? 0x0a00 : 0x1200) + (p->id) * 0x10)
# define PINCTRL_IRQPOL(p) ((is_imx23_gpio(p) ? 0x0b00 : 0x1300) + (p->id) * 0x10)
# define PINCTRL_IRQSTAT(p) ((is_imx23_gpio(p) ? 0x0c00 : 0x1400) + (p->id) * 0x10)
2010-12-18 16:39:31 +03:00
# define GPIO_INT_FALL_EDGE 0x0
# define GPIO_INT_LOW_LEV 0x1
# define GPIO_INT_RISE_EDGE 0x2
# define GPIO_INT_HIGH_LEV 0x3
# define GPIO_INT_LEV_MASK (1 << 0)
# define GPIO_INT_POL_MASK (1 << 1)
2011-08-13 20:14:06 +04:00
# define irq_to_gpio(irq) ((irq) - MXS_GPIO_IRQ_START)
2012-05-03 19:32:52 +04:00
enum mxs_gpio_id {
IMX23_GPIO ,
IMX28_GPIO ,
} ;
2011-06-06 19:37:58 +04:00
struct mxs_gpio_port {
void __iomem * base ;
int id ;
int irq ;
int virtual_irq_start ;
2011-06-06 18:31:29 +04:00
struct bgpio_chip bgc ;
2012-05-03 19:32:52 +04:00
enum mxs_gpio_id devid ;
2011-06-06 19:37:58 +04:00
} ;
2012-05-03 19:32:52 +04:00
static inline int is_imx23_gpio ( struct mxs_gpio_port * port )
{
return port - > devid = = IMX23_GPIO ;
}
static inline int is_imx28_gpio ( struct mxs_gpio_port * port )
{
return port - > devid = = IMX28_GPIO ;
}
2010-12-18 16:39:31 +03:00
/* Note: This driver assumes 32 GPIOs are handled in one register */
2011-02-18 23:31:41 +03:00
static int mxs_gpio_set_irq_type ( struct irq_data * d , unsigned int type )
2010-12-18 16:39:31 +03:00
{
2011-02-18 23:31:41 +03:00
u32 gpio = irq_to_gpio ( d - > irq ) ;
2010-12-18 16:39:31 +03:00
u32 pin_mask = 1 < < ( gpio & 31 ) ;
2011-06-07 18:00:54 +04:00
struct irq_chip_generic * gc = irq_data_get_irq_chip_data ( d ) ;
struct mxs_gpio_port * port = gc - > private ;
2010-12-18 16:39:31 +03:00
void __iomem * pin_addr ;
int edge ;
switch ( type ) {
case IRQ_TYPE_EDGE_RISING :
edge = GPIO_INT_RISE_EDGE ;
break ;
case IRQ_TYPE_EDGE_FALLING :
edge = GPIO_INT_FALL_EDGE ;
break ;
case IRQ_TYPE_LEVEL_LOW :
edge = GPIO_INT_LOW_LEV ;
break ;
case IRQ_TYPE_LEVEL_HIGH :
edge = GPIO_INT_HIGH_LEV ;
break ;
default :
return - EINVAL ;
}
/* set level or edge */
2012-05-03 19:32:52 +04:00
pin_addr = port - > base + PINCTRL_IRQLEV ( port ) ;
2010-12-18 16:39:31 +03:00
if ( edge & GPIO_INT_LEV_MASK )
2011-06-06 19:37:58 +04:00
writel ( pin_mask , pin_addr + MXS_SET ) ;
2010-12-18 16:39:31 +03:00
else
2011-06-06 19:37:58 +04:00
writel ( pin_mask , pin_addr + MXS_CLR ) ;
2010-12-18 16:39:31 +03:00
/* set polarity */
2012-05-03 19:32:52 +04:00
pin_addr = port - > base + PINCTRL_IRQPOL ( port ) ;
2010-12-18 16:39:31 +03:00
if ( edge & GPIO_INT_POL_MASK )
2011-06-06 19:37:58 +04:00
writel ( pin_mask , pin_addr + MXS_SET ) ;
2010-12-18 16:39:31 +03:00
else
2011-06-06 19:37:58 +04:00
writel ( pin_mask , pin_addr + MXS_CLR ) ;
2010-12-18 16:39:31 +03:00
2011-06-07 18:00:54 +04:00
writel ( 1 < < ( gpio & 0x1f ) ,
2012-05-03 19:32:52 +04:00
port - > base + PINCTRL_IRQSTAT ( port ) + MXS_CLR ) ;
2010-12-18 16:39:31 +03:00
return 0 ;
}
/* MXS has one interrupt *per* gpio port */
static void mxs_gpio_irq_handler ( u32 irq , struct irq_desc * desc )
{
u32 irq_stat ;
2011-06-06 19:37:58 +04:00
struct mxs_gpio_port * port = irq_get_handler_data ( irq ) ;
2010-12-18 16:39:31 +03:00
u32 gpio_irq_no_base = port - > virtual_irq_start ;
2011-01-25 18:54:22 +03:00
desc - > irq_data . chip - > irq_ack ( & desc - > irq_data ) ;
2012-05-03 19:32:52 +04:00
irq_stat = readl ( port - > base + PINCTRL_IRQSTAT ( port ) ) &
readl ( port - > base + PINCTRL_IRQEN ( port ) ) ;
2010-12-18 16:39:31 +03:00
while ( irq_stat ! = 0 ) {
int irqoffset = fls ( irq_stat ) - 1 ;
generic_handle_irq ( gpio_irq_no_base + irqoffset ) ;
irq_stat & = ~ ( 1 < < irqoffset ) ;
}
}
/*
* Set interrupt number " irq " in the GPIO as a wake - up source .
* While system is running , all registered GPIO interrupts need to have
* wake - up enabled . When system is suspended , only selected GPIO interrupts
* need to have wake - up enabled .
* @ param irq interrupt source number
* @ param enable enable as wake - up if equal to non - zero
* @ return This function returns 0 on success .
*/
2011-02-18 23:31:41 +03:00
static int mxs_gpio_set_wake_irq ( struct irq_data * d , unsigned int enable )
2010-12-18 16:39:31 +03:00
{
2011-06-07 18:00:54 +04:00
struct irq_chip_generic * gc = irq_data_get_irq_chip_data ( d ) ;
struct mxs_gpio_port * port = gc - > private ;
2010-12-18 16:39:31 +03:00
2011-06-07 18:00:53 +04:00
if ( enable )
enable_irq_wake ( port - > irq ) ;
else
disable_irq_wake ( port - > irq ) ;
2010-12-18 16:39:31 +03:00
return 0 ;
}
2011-06-07 18:00:54 +04:00
static void __init mxs_gpio_init_gc ( struct mxs_gpio_port * port )
{
struct irq_chip_generic * gc ;
struct irq_chip_type * ct ;
gc = irq_alloc_generic_chip ( " gpio-mxs " , 1 , port - > virtual_irq_start ,
port - > base , handle_level_irq ) ;
gc - > private = port ;
ct = gc - > chip_types ;
2011-07-19 17:16:56 +04:00
ct - > chip . irq_ack = irq_gc_ack_set_bit ;
2011-06-07 18:00:54 +04:00
ct - > chip . irq_mask = irq_gc_mask_clr_bit ;
ct - > chip . irq_unmask = irq_gc_mask_set_bit ;
ct - > chip . irq_set_type = mxs_gpio_set_irq_type ;
2011-07-19 17:16:56 +04:00
ct - > chip . irq_set_wake = mxs_gpio_set_wake_irq ;
2012-05-03 19:32:52 +04:00
ct - > regs . ack = PINCTRL_IRQSTAT ( port ) + MXS_CLR ;
ct - > regs . mask = PINCTRL_IRQEN ( port ) ;
2011-06-07 18:00:54 +04:00
irq_setup_generic_chip ( gc , IRQ_MSK ( 32 ) , 0 , IRQ_NOREQUEST , 0 ) ;
}
2010-12-18 16:39:31 +03:00
2011-06-06 18:31:29 +04:00
static int mxs_gpio_to_irq ( struct gpio_chip * gc , unsigned offset )
2010-12-18 16:39:31 +03:00
{
2011-06-06 18:31:29 +04:00
struct bgpio_chip * bgc = to_bgpio_chip ( gc ) ;
2010-12-18 16:39:31 +03:00
struct mxs_gpio_port * port =
2011-06-06 18:31:29 +04:00
container_of ( bgc , struct mxs_gpio_port , bgc ) ;
2010-12-18 16:39:31 +03:00
return port - > virtual_irq_start + offset ;
}
2012-05-03 19:32:52 +04:00
static struct platform_device_id mxs_gpio_ids [ ] = {
{
. name = " imx23-gpio " ,
. driver_data = IMX23_GPIO ,
} , {
. name = " imx28-gpio " ,
. driver_data = IMX28_GPIO ,
} , {
/* sentinel */
}
} ;
MODULE_DEVICE_TABLE ( platform , mxs_gpio_ids ) ;
2012-05-04 10:29:22 +04:00
static const struct of_device_id mxs_gpio_dt_ids [ ] = {
{ . compatible = " fsl,imx23-gpio " , . data = ( void * ) IMX23_GPIO , } ,
{ . compatible = " fsl,imx28-gpio " , . data = ( void * ) IMX28_GPIO , } ,
{ /* sentinel */ }
} ;
MODULE_DEVICE_TABLE ( of , mxs_gpio_dt_ids ) ;
2011-06-06 19:37:58 +04:00
static int __devinit mxs_gpio_probe ( struct platform_device * pdev )
2010-12-18 16:39:31 +03:00
{
2012-05-04 10:29:22 +04:00
const struct of_device_id * of_id =
of_match_device ( mxs_gpio_dt_ids , & pdev - > dev ) ;
struct device_node * np = pdev - > dev . of_node ;
struct device_node * parent ;
2011-06-06 19:37:58 +04:00
static void __iomem * base ;
struct mxs_gpio_port * port ;
struct resource * iores = NULL ;
2011-06-07 18:00:54 +04:00
int err ;
2011-06-06 19:37:58 +04:00
2012-05-04 06:30:14 +04:00
port = devm_kzalloc ( & pdev - > dev , sizeof ( * port ) , GFP_KERNEL ) ;
2011-06-06 19:37:58 +04:00
if ( ! port )
return - ENOMEM ;
2012-05-04 10:29:22 +04:00
if ( np ) {
port - > id = of_alias_get_id ( np , " gpio " ) ;
if ( port - > id < 0 )
return port - > id ;
port - > devid = ( enum mxs_gpio_id ) of_id - > data ;
} else {
port - > id = pdev - > id ;
port - > devid = pdev - > id_entry - > driver_data ;
}
2011-06-06 19:37:58 +04:00
port - > virtual_irq_start = MXS_GPIO_IRQ_START + port - > id * 32 ;
2012-05-04 06:30:14 +04:00
port - > irq = platform_get_irq ( pdev , 0 ) ;
if ( port - > irq < 0 )
return port - > irq ;
2011-06-06 19:37:58 +04:00
/*
* map memory region only once , as all the gpio ports
* share the same one
*/
if ( ! base ) {
2012-05-04 10:29:22 +04:00
if ( np ) {
parent = of_get_parent ( np ) ;
base = of_iomap ( parent , 0 ) ;
of_node_put ( parent ) ;
} else {
iores = platform_get_resource ( pdev , IORESOURCE_MEM , 0 ) ;
base = devm_request_and_ioremap ( & pdev - > dev , iores ) ;
2011-06-06 19:37:58 +04:00
}
2012-05-04 06:30:14 +04:00
if ( ! base )
return - EADDRNOTAVAIL ;
2011-06-06 19:37:58 +04:00
}
port - > base = base ;
2010-12-18 16:39:31 +03:00
2011-06-07 18:00:54 +04:00
/*
* select the pin interrupt functionality but initially
* disable the interrupts
*/
2012-05-03 19:32:52 +04:00
writel ( ~ 0U , port - > base + PINCTRL_PIN2IRQ ( port ) ) ;
writel ( 0 , port - > base + PINCTRL_IRQEN ( port ) ) ;
2010-12-18 16:39:31 +03:00
2011-06-06 19:37:58 +04:00
/* clear address has to be used to clear IRQSTAT bits */
2012-05-03 19:32:52 +04:00
writel ( ~ 0U , port - > base + PINCTRL_IRQSTAT ( port ) + MXS_CLR ) ;
2010-12-18 16:39:31 +03:00
2011-06-07 18:00:54 +04:00
/* gpio-mxs can be a generic irq chip */
mxs_gpio_init_gc ( port ) ;
2010-12-18 16:39:31 +03:00
2011-06-06 19:37:58 +04:00
/* setup one handler for each entry */
irq_set_chained_handler ( port - > irq , mxs_gpio_irq_handler ) ;
irq_set_handler_data ( port - > irq , port ) ;
2010-12-18 16:39:31 +03:00
2011-06-06 18:31:29 +04:00
err = bgpio_init ( & port - > bgc , & pdev - > dev , 4 ,
2012-05-03 19:32:52 +04:00
port - > base + PINCTRL_DIN ( port ) ,
port - > base + PINCTRL_DOUT ( port ) , NULL ,
2012-05-26 23:57:47 +04:00
port - > base + PINCTRL_DOE ( port ) , NULL , 0 ) ;
2011-06-06 19:37:58 +04:00
if ( err )
2012-05-04 06:30:14 +04:00
return err ;
2010-12-18 16:39:31 +03:00
2011-06-06 18:31:29 +04:00
port - > bgc . gc . to_irq = mxs_gpio_to_irq ;
port - > bgc . gc . base = port - > id * 32 ;
err = gpiochip_add ( & port - > bgc . gc ) ;
2012-05-04 06:30:14 +04:00
if ( err ) {
bgpio_remove ( & port - > bgc ) ;
return err ;
}
2011-06-06 18:31:29 +04:00
2011-06-06 19:37:58 +04:00
return 0 ;
2011-01-24 14:57:46 +03:00
}
2011-06-06 19:37:58 +04:00
static struct platform_driver mxs_gpio_driver = {
. driver = {
. name = " gpio-mxs " ,
. owner = THIS_MODULE ,
2012-05-04 10:29:22 +04:00
. of_match_table = mxs_gpio_dt_ids ,
2011-06-06 19:37:58 +04:00
} ,
. probe = mxs_gpio_probe ,
2012-05-03 19:32:52 +04:00
. id_table = mxs_gpio_ids ,
2010-12-18 16:39:31 +03:00
} ;
2011-01-24 14:57:46 +03:00
2011-06-06 19:37:58 +04:00
static int __init mxs_gpio_init ( void )
2011-01-24 14:57:46 +03:00
{
2011-06-06 19:37:58 +04:00
return platform_driver_register ( & mxs_gpio_driver ) ;
2011-01-24 14:57:46 +03:00
}
2011-06-06 19:37:58 +04:00
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 " ) ;