2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2011-03-30 11:27:56 +04:00
/*
*
2016-12-20 21:57:55 +03:00
* Copyright ( C ) 2012 John Crispin < john @ phrozen . org >
2011-03-30 11:27:56 +04:00
*/
# include <linux/slab.h>
# include <linux/init.h>
2012-05-17 00:22:47 +04:00
# include <linux/module.h>
2011-03-30 11:27:56 +04:00
# include <linux/types.h>
2012-05-17 00:22:47 +04:00
# include <linux/of_platform.h>
2011-03-30 11:27:56 +04:00
# include <linux/mutex.h>
2018-06-27 12:37:23 +03:00
# include <linux/gpio/driver.h>
2012-05-17 00:22:47 +04:00
# include <linux/io.h>
# include <linux/clk.h>
# include <linux/err.h>
2011-03-30 11:27:56 +04:00
2012-05-17 00:22:47 +04:00
/*
* The Serial To Parallel ( STP ) is found on MIPS based Lantiq socs . It is a
* peripheral controller used to drive external shift register cascades . At most
* 3 groups of 8 bits can be driven . The hardware is able to allow the DSL modem
* to drive the 2 LSBs of the cascade automatically .
*/
/* control register 0 */
# define XWAY_STP_CON0 0x00
/* control register 1 */
# define XWAY_STP_CON1 0x04
/* data register 0 */
# define XWAY_STP_CPU0 0x08
/* data register 1 */
# define XWAY_STP_CPU1 0x0C
/* access register */
# define XWAY_STP_AR 0x10
/* software or hardware update select bit */
# define XWAY_STP_CON_SWU BIT(31)
/* automatic update rates */
# define XWAY_STP_2HZ 0
# define XWAY_STP_4HZ BIT(23)
# define XWAY_STP_8HZ BIT(24)
# define XWAY_STP_10HZ (BIT(24) | BIT(23))
2020-08-14 22:48:47 +03:00
# define XWAY_STP_SPEED_MASK (BIT(23) | BIT(24) | BIT(25) | BIT(26) | BIT(27))
# define XWAY_STP_FPIS_VALUE BIT(21)
# define XWAY_STP_FPIS_MASK (BIT(20) | BIT(21))
2012-05-17 00:22:47 +04:00
/* clock source for automatic update */
# define XWAY_STP_UPD_FPI BIT(31)
# define XWAY_STP_UPD_MASK (BIT(31) | BIT(30))
/* let the adsl core drive the 2 LSBs */
# define XWAY_STP_ADSL_SHIFT 24
# define XWAY_STP_ADSL_MASK 0x3
/* 2 groups of 3 bits can be driven by the phys */
2015-05-25 23:39:50 +03:00
# define XWAY_STP_PHY_MASK 0x7
2012-05-17 00:22:47 +04:00
# define XWAY_STP_PHY1_SHIFT 27
2020-08-14 22:48:47 +03:00
# define XWAY_STP_PHY2_SHIFT 3
# define XWAY_STP_PHY3_SHIFT 6
# define XWAY_STP_PHY4_SHIFT 15
2012-05-17 00:22:47 +04:00
/* STP has 3 groups of 8 bits */
# define XWAY_STP_GROUP0 BIT(0)
# define XWAY_STP_GROUP1 BIT(1)
# define XWAY_STP_GROUP2 BIT(2)
# define XWAY_STP_GROUP_MASK (0x7)
/* Edge configuration bits */
# define XWAY_STP_FALLING BIT(26)
# define XWAY_STP_EDGE_MASK BIT(26)
# define xway_stp_r32(m, reg) __raw_readl(m + reg)
# define xway_stp_w32(m, val, reg) __raw_writel(val, m + reg)
# define xway_stp_w32_mask(m, clear, set, reg) \
2019-07-03 01:32:47 +03:00
xway_stp_w32 ( m , ( xway_stp_r32 ( m , reg ) & ~ ( clear ) ) | ( set ) , reg )
2012-05-17 00:22:47 +04:00
struct xway_stp {
struct gpio_chip gc ;
void __iomem * virt ;
u32 edge ; /* rising or falling edge triggered shift register */
2012-07-11 18:33:43 +04:00
u32 shadow ; /* shadow the shift registers state */
2012-05-17 00:22:47 +04:00
u8 groups ; /* we can drive 1-3 groups of 8bit each */
u8 dsl ; /* the 2 LSBs can be driven by the dsl core */
u8 phy1 ; /* 3 bits can be driven by phy1 */
u8 phy2 ; /* 3 bits can be driven by phy2 */
2020-08-14 22:48:47 +03:00
u8 phy3 ; /* 3 bits can be driven by phy3 */
u8 phy4 ; /* 3 bits can be driven by phy4 */
2012-05-17 00:22:47 +04:00
u8 reserved ; /* mask out the hw driven bits in gpio_request */
} ;
2018-06-28 22:57:40 +03:00
/**
* xway_stp_get ( ) - gpio_chip - > get - get gpios .
* @ gc : Pointer to gpio_chip device structure .
* @ gpio : GPIO signal number .
*
* Gets the shadow value .
*/
static int xway_stp_get ( struct gpio_chip * gc , unsigned int gpio )
{
struct xway_stp * chip = gpiochip_get_data ( gc ) ;
return ( xway_stp_r32 ( chip - > virt , XWAY_STP_CPU0 ) & BIT ( gpio ) ) ;
}
2012-05-17 00:22:47 +04:00
/**
* xway_stp_set ( ) - gpio_chip - > set - set gpios .
* @ gc : Pointer to gpio_chip device structure .
* @ gpio : GPIO signal number .
* @ val : Value to be written to specified signal .
*
* Set the shadow value and call ltq_ebu_apply .
*/
static void xway_stp_set ( struct gpio_chip * gc , unsigned gpio , int val )
2011-03-30 11:27:56 +04:00
{
2015-12-07 16:34:33 +03:00
struct xway_stp * chip = gpiochip_get_data ( gc ) ;
2012-05-17 00:22:47 +04:00
if ( val )
chip - > shadow | = BIT ( gpio ) ;
2011-03-30 11:27:56 +04:00
else
2012-05-17 00:22:47 +04:00
chip - > shadow & = ~ BIT ( gpio ) ;
xway_stp_w32 ( chip - > virt , chip - > shadow , XWAY_STP_CPU0 ) ;
2020-08-14 22:48:47 +03:00
if ( ! chip - > reserved )
xway_stp_w32_mask ( chip - > virt , 0 , XWAY_STP_CON_SWU , XWAY_STP_CON0 ) ;
2011-03-30 11:27:56 +04:00
}
2012-05-17 00:22:47 +04:00
/**
* xway_stp_dir_out ( ) - gpio_chip - > dir_out - set gpio direction .
* @ gc : Pointer to gpio_chip device structure .
* @ gpio : GPIO signal number .
* @ val : Value to be written to specified signal .
*
* Same as xway_stp_set , always returns 0.
*/
static int xway_stp_dir_out ( struct gpio_chip * gc , unsigned gpio , int val )
2011-03-30 11:27:56 +04:00
{
2012-05-17 00:22:47 +04:00
xway_stp_set ( gc , gpio , val ) ;
2011-03-30 11:27:56 +04:00
return 0 ;
}
2012-05-17 00:22:47 +04:00
/**
* xway_stp_request ( ) - gpio_chip - > request
* @ gc : Pointer to gpio_chip device structure .
* @ gpio : GPIO signal number .
*
* We mask out the HW driven pins
*/
static int xway_stp_request ( struct gpio_chip * gc , unsigned gpio )
{
2015-12-07 16:34:33 +03:00
struct xway_stp * chip = gpiochip_get_data ( gc ) ;
2012-05-17 00:22:47 +04:00
if ( ( gpio < 8 ) & & ( chip - > reserved & BIT ( gpio ) ) ) {
2015-11-04 11:56:26 +03:00
dev_err ( gc - > parent , " GPIO %d is driven by hardware \n " , gpio ) ;
2012-05-17 00:22:47 +04:00
return - ENODEV ;
}
2011-03-30 11:27:56 +04:00
2012-05-17 00:22:47 +04:00
return 0 ;
}
/**
* xway_stp_hw_init ( ) - Configure the STP unit and enable the clock gate
2019-07-03 01:32:45 +03:00
* @ chip : Pointer to the xway_stp chip structure
2012-05-17 00:22:47 +04:00
*/
2019-07-03 01:32:45 +03:00
static void xway_stp_hw_init ( struct xway_stp * chip )
2011-03-30 11:27:56 +04:00
{
/* sane defaults */
2012-05-17 00:22:47 +04:00
xway_stp_w32 ( chip - > virt , 0 , XWAY_STP_AR ) ;
xway_stp_w32 ( chip - > virt , 0 , XWAY_STP_CPU0 ) ;
xway_stp_w32 ( chip - > virt , 0 , XWAY_STP_CPU1 ) ;
xway_stp_w32 ( chip - > virt , XWAY_STP_CON_SWU , XWAY_STP_CON0 ) ;
xway_stp_w32 ( chip - > virt , 0 , XWAY_STP_CON1 ) ;
2011-03-30 11:27:56 +04:00
2012-05-17 00:22:47 +04:00
/* apply edge trigger settings for the shift register */
xway_stp_w32_mask ( chip - > virt , XWAY_STP_EDGE_MASK ,
chip - > edge , XWAY_STP_CON0 ) ;
2011-03-30 11:27:56 +04:00
2012-05-17 00:22:47 +04:00
/* apply led group settings */
xway_stp_w32_mask ( chip - > virt , XWAY_STP_GROUP_MASK ,
chip - > groups , XWAY_STP_CON1 ) ;
2011-03-30 11:27:56 +04:00
2012-05-17 00:22:47 +04:00
/* tell the hardware which pins are controlled by the dsl modem */
xway_stp_w32_mask ( chip - > virt ,
XWAY_STP_ADSL_MASK < < XWAY_STP_ADSL_SHIFT ,
chip - > dsl < < XWAY_STP_ADSL_SHIFT ,
XWAY_STP_CON0 ) ;
2011-03-30 11:27:56 +04:00
2012-05-17 00:22:47 +04:00
/* tell the hardware which pins are controlled by the phys */
xway_stp_w32_mask ( chip - > virt ,
XWAY_STP_PHY_MASK < < XWAY_STP_PHY1_SHIFT ,
chip - > phy1 < < XWAY_STP_PHY1_SHIFT ,
XWAY_STP_CON0 ) ;
xway_stp_w32_mask ( chip - > virt ,
XWAY_STP_PHY_MASK < < XWAY_STP_PHY2_SHIFT ,
chip - > phy2 < < XWAY_STP_PHY2_SHIFT ,
XWAY_STP_CON1 ) ;
2011-03-30 11:27:56 +04:00
2020-08-14 22:48:47 +03:00
if ( of_machine_is_compatible ( " lantiq,grx390 " )
| | of_machine_is_compatible ( " lantiq,ar10 " ) ) {
xway_stp_w32_mask ( chip - > virt ,
XWAY_STP_PHY_MASK < < XWAY_STP_PHY3_SHIFT ,
chip - > phy3 < < XWAY_STP_PHY3_SHIFT ,
XWAY_STP_CON1 ) ;
}
if ( of_machine_is_compatible ( " lantiq,grx390 " ) ) {
xway_stp_w32_mask ( chip - > virt ,
XWAY_STP_PHY_MASK < < XWAY_STP_PHY4_SHIFT ,
chip - > phy4 < < XWAY_STP_PHY4_SHIFT ,
XWAY_STP_CON1 ) ;
}
2012-05-17 00:22:47 +04:00
/* mask out the hw driven bits in gpio_request */
2020-08-14 22:48:47 +03:00
chip - > reserved = ( chip - > phy4 < < 11 ) | ( chip - > phy3 < < 8 ) | ( chip - > phy2 < < 5 )
| ( chip - > phy1 < < 2 ) | chip - > dsl ;
2012-05-17 00:22:47 +04:00
/*
* if we have pins that are driven by hw , we need to tell the stp what
* clock to use as a timer .
2011-03-30 11:27:56 +04:00
*/
2020-08-14 22:48:47 +03:00
if ( chip - > reserved ) {
2012-05-17 00:22:47 +04:00
xway_stp_w32_mask ( chip - > virt , XWAY_STP_UPD_MASK ,
XWAY_STP_UPD_FPI , XWAY_STP_CON1 ) ;
2020-08-14 22:48:47 +03:00
xway_stp_w32_mask ( chip - > virt , XWAY_STP_SPEED_MASK ,
XWAY_STP_10HZ , XWAY_STP_CON1 ) ;
xway_stp_w32_mask ( chip - > virt , XWAY_STP_FPIS_MASK ,
XWAY_STP_FPIS_VALUE , XWAY_STP_CON1 ) ;
}
2011-03-30 11:27:56 +04:00
}
2012-11-19 22:22:34 +04:00
static int xway_stp_probe ( struct platform_device * pdev )
2011-03-30 11:27:56 +04:00
{
2015-05-27 00:12:00 +03:00
u32 shadow , groups , dsl , phy ;
2012-05-17 00:22:47 +04:00
struct xway_stp * chip ;
struct clk * clk ;
2011-03-30 11:27:56 +04:00
int ret = 0 ;
2012-05-17 00:22:47 +04:00
chip = devm_kzalloc ( & pdev - > dev , sizeof ( * chip ) , GFP_KERNEL ) ;
if ( ! chip )
return - ENOMEM ;
2019-03-11 21:55:10 +03:00
chip - > virt = devm_platform_ioremap_resource ( pdev , 0 ) ;
2013-01-21 14:09:01 +04:00
if ( IS_ERR ( chip - > virt ) )
return PTR_ERR ( chip - > virt ) ;
2013-03-20 16:16:01 +04:00
2015-11-04 11:56:26 +03:00
chip - > gc . parent = & pdev - > dev ;
2012-05-17 00:22:47 +04:00
chip - > gc . label = " stp-xway " ;
chip - > gc . direction_output = xway_stp_dir_out ;
2018-06-28 22:57:40 +03:00
chip - > gc . get = xway_stp_get ;
2012-05-17 00:22:47 +04:00
chip - > gc . set = xway_stp_set ;
chip - > gc . request = xway_stp_request ;
chip - > gc . base = - 1 ;
chip - > gc . owner = THIS_MODULE ;
/* store the shadow value if one was passed by the devicetree */
2015-05-27 00:12:00 +03:00
if ( ! of_property_read_u32 ( pdev - > dev . of_node , " lantiq,shadow " , & shadow ) )
chip - > shadow = shadow ;
2012-05-17 00:22:47 +04:00
/* find out which gpio groups should be enabled */
2015-05-27 00:12:00 +03:00
if ( ! of_property_read_u32 ( pdev - > dev . of_node , " lantiq,groups " , & groups ) )
chip - > groups = groups & XWAY_STP_GROUP_MASK ;
2012-05-17 00:22:47 +04:00
else
chip - > groups = XWAY_STP_GROUP0 ;
chip - > gc . ngpio = fls ( chip - > groups ) * 8 ;
/* find out which gpios are controlled by the dsl core */
2015-05-27 00:12:00 +03:00
if ( ! of_property_read_u32 ( pdev - > dev . of_node , " lantiq,dsl " , & dsl ) )
chip - > dsl = dsl & XWAY_STP_ADSL_MASK ;
2012-05-17 00:22:47 +04:00
/* find out which gpios are controlled by the phys */
if ( of_machine_is_compatible ( " lantiq,ar9 " ) | |
of_machine_is_compatible ( " lantiq,gr9 " ) | |
2020-08-14 22:48:47 +03:00
of_machine_is_compatible ( " lantiq,vr9 " ) | |
of_machine_is_compatible ( " lantiq,ar10 " ) | |
of_machine_is_compatible ( " lantiq,grx390 " ) ) {
2015-05-27 00:12:00 +03:00
if ( ! of_property_read_u32 ( pdev - > dev . of_node , " lantiq,phy1 " , & phy ) )
chip - > phy1 = phy & XWAY_STP_PHY_MASK ;
if ( ! of_property_read_u32 ( pdev - > dev . of_node , " lantiq,phy2 " , & phy ) )
chip - > phy2 = phy & XWAY_STP_PHY_MASK ;
2020-08-14 22:48:47 +03:00
}
if ( of_machine_is_compatible ( " lantiq,ar10 " ) | |
of_machine_is_compatible ( " lantiq,grx390 " ) ) {
if ( ! of_property_read_u32 ( pdev - > dev . of_node , " lantiq,phy3 " , & phy ) )
chip - > phy3 = phy & XWAY_STP_PHY_MASK ;
}
if ( of_machine_is_compatible ( " lantiq,grx390 " ) ) {
if ( ! of_property_read_u32 ( pdev - > dev . of_node , " lantiq,phy4 " , & phy ) )
chip - > phy4 = phy & XWAY_STP_PHY_MASK ;
2012-05-17 00:22:47 +04:00
}
/* check which edge trigger we should use, default to a falling edge */
if ( ! of_find_property ( pdev - > dev . of_node , " lantiq,rising " , NULL ) )
chip - > edge = XWAY_STP_FALLING ;
2019-07-03 01:32:46 +03:00
clk = devm_clk_get ( & pdev - > dev , NULL ) ;
2012-05-17 00:22:47 +04:00
if ( IS_ERR ( clk ) ) {
dev_err ( & pdev - > dev , " Failed to get clock \n " ) ;
return PTR_ERR ( clk ) ;
}
2019-07-03 01:32:46 +03:00
ret = clk_prepare_enable ( clk ) ;
if ( ret )
return ret ;
2012-05-17 00:22:47 +04:00
2019-07-03 01:32:45 +03:00
xway_stp_hw_init ( chip ) ;
2011-03-30 11:27:56 +04:00
2019-07-03 01:32:45 +03:00
ret = devm_gpiochip_add_data ( & pdev - > dev , & chip - > gc , chip ) ;
2019-07-03 01:32:46 +03:00
if ( ret ) {
clk_disable_unprepare ( clk ) ;
2019-07-03 01:32:45 +03:00
return ret ;
2019-07-03 01:32:46 +03:00
}
2011-03-30 11:27:56 +04:00
2019-07-03 01:32:45 +03:00
dev_info ( & pdev - > dev , " Init done \n " ) ;
return 0 ;
2011-03-30 11:27:56 +04:00
}
2012-05-17 00:22:47 +04:00
static const struct of_device_id xway_stp_match [ ] = {
{ . compatible = " lantiq,gpio-stp-xway " } ,
{ } ,
} ;
MODULE_DEVICE_TABLE ( of , xway_stp_match ) ;
static struct platform_driver xway_stp_driver = {
. probe = xway_stp_probe ,
2011-03-30 11:27:56 +04:00
. driver = {
2012-05-17 00:22:47 +04:00
. name = " gpio-stp-xway " ,
. of_match_table = xway_stp_match ,
2011-03-30 11:27:56 +04:00
} ,
} ;
2014-09-30 11:11:15 +04:00
static int __init xway_stp_init ( void )
2011-03-30 11:27:56 +04:00
{
2012-05-17 00:22:47 +04:00
return platform_driver_register ( & xway_stp_driver ) ;
2011-03-30 11:27:56 +04:00
}
2012-05-17 00:22:47 +04:00
subsys_initcall ( xway_stp_init ) ;