2018-05-22 05:32:55 +03:00
// SPDX-License-Identifier: GPL-2.0+
//
2022-06-17 13:38:09 +03:00
// MXS GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
2018-05-22 05:32:55 +03:00
// Copyright 2008 Juergen Beisert, kernel@pengutronix.de
//
// Based on code from Freescale,
// Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
2010-12-18 16:39:31 +03:00
2013-01-21 14:09:01 +04:00
# include <linux/err.h>
2010-12-18 16:39:31 +03:00
# include <linux/init.h>
# include <linux/interrupt.h>
# include <linux/io.h>
# include <linux/irq.h>
2012-08-20 12:43:32 +04:00
# include <linux/irqdomain.h>
2012-05-04 10:29:22 +04:00
# include <linux/of.h>
# include <linux/of_address.h>
2011-06-06 19:37:58 +04:00
# include <linux/platform_device.h>
# include <linux/slab.h>
2015-12-04 16:02:58 +03:00
# include <linux/gpio/driver.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)
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 ;
2012-08-20 12:43:32 +04:00
struct irq_domain * domain ;
2015-12-04 16:02:58 +03:00
struct gpio_chip gc ;
2017-08-09 15:25:07 +03:00
struct device * dev ;
2012-05-03 19:32:52 +04:00
enum mxs_gpio_id devid ;
2013-01-29 12:16:33 +04:00
u32 both_edges ;
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 ;
}
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
{
2013-01-29 12:16:33 +04:00
u32 val ;
2012-08-20 12:43:32 +04:00
u32 pin_mask = 1 < < d - > hwirq ;
2011-06-07 18:00:54 +04:00
struct irq_chip_generic * gc = irq_data_get_irq_chip_data ( d ) ;
2016-10-21 16:11:38 +03:00
struct irq_chip_type * ct = irq_data_get_chip_type ( d ) ;
2011-06-07 18:00:54 +04:00
struct mxs_gpio_port * port = gc - > private ;
2010-12-18 16:39:31 +03:00
void __iomem * pin_addr ;
int edge ;
2016-10-21 16:11:38 +03:00
if ( ! ( ct - > type & type ) )
if ( irq_setup_alt_chip ( d , type ) )
return - EINVAL ;
2013-01-29 12:16:33 +04:00
port - > both_edges & = ~ pin_mask ;
2010-12-18 16:39:31 +03:00
switch ( type ) {
2013-01-29 12:16:33 +04:00
case IRQ_TYPE_EDGE_BOTH :
2018-12-18 11:47:57 +03:00
val = readl ( port - > base + PINCTRL_DIN ( port ) ) & pin_mask ;
2013-01-29 12:16:33 +04:00
if ( val )
edge = GPIO_INT_FALL_EDGE ;
else
edge = GPIO_INT_RISE_EDGE ;
port - > both_edges | = pin_mask ;
break ;
2010-12-18 16:39:31 +03:00
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 ) ;
2016-10-21 16:11:38 +03:00
if ( edge & GPIO_INT_LEV_MASK ) {
2011-06-06 19:37:58 +04:00
writel ( pin_mask , pin_addr + MXS_SET ) ;
2016-10-21 16:11:38 +03:00
writel ( pin_mask , port - > base + PINCTRL_IRQEN ( port ) + MXS_SET ) ;
} else {
2011-06-06 19:37:58 +04:00
writel ( pin_mask , pin_addr + MXS_CLR ) ;
2016-10-21 16:11:38 +03:00
writel ( pin_mask , port - > base + PINCTRL_PIN2IRQ ( port ) + MXS_SET ) ;
}
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
2018-07-24 19:29:28 +03:00
writel ( pin_mask , port - > base + PINCTRL_IRQSTAT ( port ) + MXS_CLR ) ;
2010-12-18 16:39:31 +03:00
return 0 ;
}
2013-01-29 12:16:33 +04:00
static void mxs_flip_edge ( struct mxs_gpio_port * port , u32 gpio )
{
u32 bit , val , edge ;
void __iomem * pin_addr ;
bit = 1 < < gpio ;
pin_addr = port - > base + PINCTRL_IRQPOL ( port ) ;
val = readl ( pin_addr ) ;
edge = val & bit ;
if ( edge )
writel ( bit , pin_addr + MXS_CLR ) ;
else
writel ( bit , pin_addr + MXS_SET ) ;
}
2010-12-18 16:39:31 +03:00
/* MXS has one interrupt *per* gpio port */
2015-09-14 11:42:37 +03:00
static void mxs_gpio_irq_handler ( struct irq_desc * desc )
2010-12-18 16:39:31 +03:00
{
u32 irq_stat ;
2015-06-04 07:13:15 +03:00
struct mxs_gpio_port * port = irq_desc_get_handler_data ( desc ) ;
2010-12-18 16:39:31 +03:00
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 ;
2013-01-29 12:16:33 +04:00
if ( port - > both_edges & ( 1 < < irqoffset ) )
mxs_flip_edge ( port , irqoffset ) ;
2021-05-04 19:42:18 +03:00
generic_handle_domain_irq ( port - > domain , irqoffset ) ;
2010-12-18 16:39:31 +03:00
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 ;
}
2016-12-16 12:08:14 +03:00
static int mxs_gpio_init_gc ( struct mxs_gpio_port * port , int irq_base )
2011-06-07 18:00:54 +04:00
{
struct irq_chip_generic * gc ;
struct irq_chip_type * ct ;
2017-08-09 15:25:07 +03:00
int rv ;
2011-06-07 18:00:54 +04:00
2017-08-09 15:25:07 +03:00
gc = devm_irq_alloc_generic_chip ( port - > dev , " gpio-mxs " , 2 , irq_base ,
port - > base , handle_level_irq ) ;
2015-08-23 16:11:53 +03:00
if ( ! gc )
return - ENOMEM ;
2011-06-07 18:00:54 +04:00
gc - > private = port ;
2016-10-21 16:11:38 +03:00
ct = & gc - > chip_types [ 0 ] ;
ct - > type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW ;
ct - > chip . irq_ack = irq_gc_ack_set_bit ;
ct - > chip . irq_mask = irq_gc_mask_disable_reg ;
ct - > chip . irq_unmask = irq_gc_unmask_enable_reg ;
ct - > chip . irq_set_type = mxs_gpio_set_irq_type ;
ct - > chip . irq_set_wake = mxs_gpio_set_wake_irq ;
ct - > chip . flags = IRQCHIP_SET_TYPE_MASKED ;
ct - > regs . ack = PINCTRL_IRQSTAT ( port ) + MXS_CLR ;
ct - > regs . enable = PINCTRL_PIN2IRQ ( port ) + MXS_SET ;
ct - > regs . disable = PINCTRL_PIN2IRQ ( port ) + MXS_CLR ;
ct = & gc - > chip_types [ 1 ] ;
ct - > type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING ;
2011-07-19 17:16:56 +04:00
ct - > chip . irq_ack = irq_gc_ack_set_bit ;
2016-10-21 16:11:37 +03:00
ct - > chip . irq_mask = irq_gc_mask_disable_reg ;
ct - > chip . irq_unmask = irq_gc_unmask_enable_reg ;
2011-06-07 18:00:54 +04:00
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 ;
2016-10-21 16:11:38 +03:00
ct - > chip . flags = IRQCHIP_SET_TYPE_MASKED ;
2012-05-03 19:32:52 +04:00
ct - > regs . ack = PINCTRL_IRQSTAT ( port ) + MXS_CLR ;
2016-10-21 16:11:37 +03:00
ct - > regs . enable = PINCTRL_IRQEN ( port ) + MXS_SET ;
ct - > regs . disable = PINCTRL_IRQEN ( port ) + MXS_CLR ;
2016-10-21 16:11:38 +03:00
ct - > handler = handle_level_irq ;
2011-06-07 18:00:54 +04:00
2017-08-09 15:25:07 +03:00
rv = devm_irq_setup_generic_chip ( port - > dev , gc , IRQ_MSK ( 32 ) ,
IRQ_GC_INIT_NESTED_LOCK ,
IRQ_NOREQUEST , 0 ) ;
2015-08-23 16:11:53 +03:00
2017-08-09 15:25:07 +03:00
return rv ;
2011-06-07 18:00:54 +04:00
}
2010-12-18 16:39:31 +03:00
2021-06-24 13:15:17 +03:00
static int mxs_gpio_to_irq ( struct gpio_chip * gc , unsigned int offset )
2010-12-18 16:39:31 +03:00
{
2015-12-04 16:02:58 +03:00
struct mxs_gpio_port * port = gpiochip_get_data ( gc ) ;
2010-12-18 16:39:31 +03:00
2012-08-20 12:43:32 +04:00
return irq_find_mapping ( port - > domain , offset ) ;
2010-12-18 16:39:31 +03:00
}
2021-06-24 13:15:17 +03:00
static int mxs_gpio_get_direction ( struct gpio_chip * gc , unsigned int offset )
2014-11-19 11:55:22 +03:00
{
2015-12-04 16:02:58 +03:00
struct mxs_gpio_port * port = gpiochip_get_data ( gc ) ;
2014-11-19 11:55:22 +03:00
u32 mask = 1 < < offset ;
u32 dir ;
dir = readl ( port - > base + PINCTRL_DOE ( port ) ) ;
2019-11-06 11:54:12 +03:00
if ( dir & mask )
return GPIO_LINE_DIRECTION_OUT ;
return GPIO_LINE_DIRECTION_IN ;
2014-11-19 11:55:22 +03:00
}
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 ) ;
2012-11-19 22:22:34 +04:00
static int mxs_gpio_probe ( struct platform_device * pdev )
2010-12-18 16:39:31 +03:00
{
2012-05-04 10:29:22 +04:00
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 ;
2012-08-20 12:43:32 +04:00
int irq_base ;
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 ;
2013-11-05 23:21:22 +04:00
port - > id = of_alias_get_id ( np , " gpio " ) ;
if ( port - > id < 0 )
return port - > id ;
2023-08-10 12:59:49 +03:00
port - > devid = ( uintptr_t ) of_device_get_match_data ( & pdev - > dev ) ;
2017-08-09 15:25:07 +03:00
port - > dev = & pdev - > dev ;
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 ) {
2013-11-05 23:21:22 +04:00
parent = of_get_parent ( np ) ;
base = of_iomap ( parent , 0 ) ;
of_node_put ( parent ) ;
if ( ! base )
return - EADDRNOTAVAIL ;
2011-06-06 19:37:58 +04:00
}
port - > base = base ;
2010-12-18 16:39:31 +03:00
2016-10-21 16:11:38 +03:00
/* initially disable the interrupts */
writel ( 0 , port - > base + PINCTRL_PIN2IRQ ( port ) ) ;
2012-05-03 19:32:52 +04:00
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
2017-03-04 19:23:39 +03:00
irq_base = devm_irq_alloc_descs ( & pdev - > dev , - 1 , 0 , 32 , numa_node_id ( ) ) ;
2016-10-05 12:38:36 +03:00
if ( irq_base < 0 ) {
err = irq_base ;
goto out_iounmap ;
}
2012-08-20 12:43:32 +04:00
port - > domain = irq_domain_add_legacy ( np , 32 , irq_base , 0 ,
& irq_domain_simple_ops , NULL ) ;
if ( ! port - > domain ) {
err = - ENODEV ;
2017-03-04 19:23:39 +03:00
goto out_iounmap ;
2012-08-20 12:43:32 +04:00
}
2011-06-07 18:00:54 +04:00
/* gpio-mxs can be a generic irq chip */
2015-08-23 16:11:53 +03:00
err = mxs_gpio_init_gc ( port , irq_base ) ;
if ( err < 0 )
goto out_irqdomain_remove ;
2010-12-18 16:39:31 +03:00
2011-06-06 19:37:58 +04:00
/* setup one handler for each entry */
2015-06-17 01:06:45 +03:00
irq_set_chained_handler_and_data ( port - > irq , mxs_gpio_irq_handler ,
port ) ;
2010-12-18 16:39:31 +03:00
2015-12-04 16:02:58 +03:00
err = bgpio_init ( & port - > gc , & pdev - > dev , 4 ,
2012-05-03 19:32:52 +04:00
port - > base + PINCTRL_DIN ( port ) ,
2013-04-29 18:07:18 +04:00
port - > base + PINCTRL_DOUT ( port ) + MXS_SET ,
port - > base + PINCTRL_DOUT ( port ) + MXS_CLR ,
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 )
2015-12-04 16:02:58 +03:00
goto out_irqdomain_remove ;
2010-12-18 16:39:31 +03:00
2015-12-04 16:02:58 +03:00
port - > gc . to_irq = mxs_gpio_to_irq ;
port - > gc . get_direction = mxs_gpio_get_direction ;
port - > gc . base = port - > id * 32 ;
2011-06-06 18:31:29 +04:00
2015-12-04 16:02:58 +03:00
err = gpiochip_add_data ( & port - > gc , port ) ;
2012-08-20 12:43:32 +04:00
if ( err )
2015-12-04 16:02:58 +03:00
goto out_irqdomain_remove ;
2011-06-06 18:31:29 +04:00
2011-06-06 19:37:58 +04:00
return 0 ;
2012-08-20 12:43:32 +04:00
2015-08-23 16:11:53 +03:00
out_irqdomain_remove :
irq_domain_remove ( port - > domain ) ;
2016-10-05 12:38:36 +03:00
out_iounmap :
iounmap ( port - > base ) ;
2012-08-20 12:43:32 +04:00
return err ;
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 " ,
2012-05-04 10:29:22 +04:00
. of_match_table = mxs_gpio_dt_ids ,
2017-08-09 15:25:01 +03:00
. suppress_bind_attrs = true ,
2011-06-06 19:37:58 +04:00
} ,
. probe = mxs_gpio_probe ,
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 " ) ;