2014-03-11 21:55:14 +04:00
/*
* SYSCON GPIO driver
*
* Copyright ( C ) 2014 Alexander Shiyan < shc_work @ mail . ru >
*
* 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 .
*/
# include <linux/err.h>
2018-06-28 09:35:46 +03:00
# include <linux/gpio/driver.h>
2014-03-11 21:55:14 +04:00
# include <linux/module.h>
# include <linux/of.h>
# include <linux/of_device.h>
# include <linux/platform_device.h>
# include <linux/regmap.h>
# include <linux/mfd/syscon.h>
# define GPIO_SYSCON_FEAT_IN BIT(0)
# define GPIO_SYSCON_FEAT_OUT BIT(1)
# define GPIO_SYSCON_FEAT_DIR BIT(2)
/* SYSCON driver is designed to use 32-bit wide registers */
# define SYSCON_REG_SIZE (4)
# define SYSCON_REG_BITS (SYSCON_REG_SIZE * 8)
/**
* struct syscon_gpio_data - Configuration for the device .
* compatible : SYSCON driver compatible string .
* flags : Set of GPIO_SYSCON_FEAT_ flags :
* GPIO_SYSCON_FEAT_IN : GPIOs supports input ,
* GPIO_SYSCON_FEAT_OUT : GPIOs supports output ,
* GPIO_SYSCON_FEAT_DIR : GPIOs supports switch direction .
* bit_count : Number of bits used as GPIOs .
* dat_bit_offset : Offset ( in bits ) to the first GPIO bit .
* dir_bit_offset : Optional offset ( in bits ) to the first bit to switch
* GPIO direction ( Used with GPIO_SYSCON_FEAT_DIR flag ) .
2014-09-03 21:05:32 +04:00
* set : HW specific callback to assigns output value
* for signal " offset "
2014-03-11 21:55:14 +04:00
*/
struct syscon_gpio_data {
const char * compatible ;
unsigned int flags ;
unsigned int bit_count ;
unsigned int dat_bit_offset ;
unsigned int dir_bit_offset ;
2014-09-03 21:05:32 +04:00
void ( * set ) ( struct gpio_chip * chip ,
unsigned offset , int value ) ;
2014-03-11 21:55:14 +04:00
} ;
struct syscon_gpio_priv {
struct gpio_chip chip ;
struct regmap * syscon ;
const struct syscon_gpio_data * data ;
2014-09-03 21:05:33 +04:00
u32 dreg_offset ;
u32 dir_reg_offset ;
2014-03-11 21:55:14 +04:00
} ;
static int syscon_gpio_get ( struct gpio_chip * chip , unsigned offset )
{
2015-12-07 16:38:36 +03:00
struct syscon_gpio_priv * priv = gpiochip_get_data ( chip ) ;
2014-09-03 21:05:33 +04:00
unsigned int val , offs ;
2014-03-11 21:55:14 +04:00
int ret ;
2014-09-03 21:05:33 +04:00
offs = priv - > dreg_offset + priv - > data - > dat_bit_offset + offset ;
2014-03-11 21:55:14 +04:00
ret = regmap_read ( priv - > syscon ,
( offs / SYSCON_REG_BITS ) * SYSCON_REG_SIZE , & val ) ;
if ( ret )
return ret ;
return ! ! ( val & BIT ( offs % SYSCON_REG_BITS ) ) ;
}
static void syscon_gpio_set ( struct gpio_chip * chip , unsigned offset , int val )
{
2015-12-07 16:38:36 +03:00
struct syscon_gpio_priv * priv = gpiochip_get_data ( chip ) ;
2014-09-03 21:05:33 +04:00
unsigned int offs ;
offs = priv - > dreg_offset + priv - > data - > dat_bit_offset + offset ;
2014-03-11 21:55:14 +04:00
regmap_update_bits ( priv - > syscon ,
( offs / SYSCON_REG_BITS ) * SYSCON_REG_SIZE ,
BIT ( offs % SYSCON_REG_BITS ) ,
val ? BIT ( offs % SYSCON_REG_BITS ) : 0 ) ;
}
static int syscon_gpio_dir_in ( struct gpio_chip * chip , unsigned offset )
{
2015-12-07 16:38:36 +03:00
struct syscon_gpio_priv * priv = gpiochip_get_data ( chip ) ;
2014-03-11 21:55:14 +04:00
if ( priv - > data - > flags & GPIO_SYSCON_FEAT_DIR ) {
2014-09-03 21:05:33 +04:00
unsigned int offs ;
offs = priv - > dir_reg_offset +
priv - > data - > dir_bit_offset + offset ;
2014-03-11 21:55:14 +04:00
regmap_update_bits ( priv - > syscon ,
( offs / SYSCON_REG_BITS ) * SYSCON_REG_SIZE ,
BIT ( offs % SYSCON_REG_BITS ) , 0 ) ;
}
return 0 ;
}
static int syscon_gpio_dir_out ( struct gpio_chip * chip , unsigned offset , int val )
{
2015-12-07 16:38:36 +03:00
struct syscon_gpio_priv * priv = gpiochip_get_data ( chip ) ;
2014-03-11 21:55:14 +04:00
if ( priv - > data - > flags & GPIO_SYSCON_FEAT_DIR ) {
2014-09-03 21:05:33 +04:00
unsigned int offs ;
offs = priv - > dir_reg_offset +
priv - > data - > dir_bit_offset + offset ;
2014-03-11 21:55:14 +04:00
regmap_update_bits ( priv - > syscon ,
( offs / SYSCON_REG_BITS ) * SYSCON_REG_SIZE ,
BIT ( offs % SYSCON_REG_BITS ) ,
BIT ( offs % SYSCON_REG_BITS ) ) ;
}
2018-10-04 01:52:52 +03:00
chip - > set ( chip , offset , val ) ;
2014-03-11 21:55:14 +04:00
return 0 ;
}
static const struct syscon_gpio_data clps711x_mctrl_gpio = {
/* ARM CLPS711X SYSFLG1 Bits 8-10 */
2016-06-04 10:10:00 +03:00
. compatible = " cirrus,ep7209-syscon1 " ,
2014-03-11 21:55:14 +04:00
. flags = GPIO_SYSCON_FEAT_IN ,
. bit_count = 3 ,
. dat_bit_offset = 0x40 * 8 + 8 ,
} ;
2018-07-31 08:59:19 +03:00
static void rockchip_gpio_set ( struct gpio_chip * chip , unsigned int offset ,
int val )
{
struct syscon_gpio_priv * priv = gpiochip_get_data ( chip ) ;
unsigned int offs ;
u8 bit ;
u32 data ;
int ret ;
offs = priv - > dreg_offset + priv - > data - > dat_bit_offset + offset ;
bit = offs % SYSCON_REG_BITS ;
data = ( val ? BIT ( bit ) : 0 ) | BIT ( bit + 16 ) ;
ret = regmap_write ( priv - > syscon ,
( offs / SYSCON_REG_BITS ) * SYSCON_REG_SIZE ,
data ) ;
if ( ret < 0 )
dev_err ( chip - > parent , " gpio write failed ret(%d) \n " , ret ) ;
}
static const struct syscon_gpio_data rockchip_rk3328_gpio_mute = {
/* RK3328 GPIO_MUTE is an output only pin at GRF_SOC_CON10[1] */
. flags = GPIO_SYSCON_FEAT_OUT ,
. bit_count = 1 ,
. dat_bit_offset = 0x0428 * 8 + 1 ,
. set = rockchip_gpio_set ,
} ;
2014-09-03 21:05:34 +04:00
# define KEYSTONE_LOCK_BIT BIT(0)
static void keystone_gpio_set ( struct gpio_chip * chip , unsigned offset , int val )
{
2015-12-07 16:38:36 +03:00
struct syscon_gpio_priv * priv = gpiochip_get_data ( chip ) ;
2014-09-03 21:05:34 +04:00
unsigned int offs ;
int ret ;
offs = priv - > dreg_offset + priv - > data - > dat_bit_offset + offset ;
if ( ! val )
return ;
ret = regmap_update_bits (
priv - > syscon ,
( offs / SYSCON_REG_BITS ) * SYSCON_REG_SIZE ,
BIT ( offs % SYSCON_REG_BITS ) | KEYSTONE_LOCK_BIT ,
BIT ( offs % SYSCON_REG_BITS ) | KEYSTONE_LOCK_BIT ) ;
if ( ret < 0 )
2015-11-04 11:56:26 +03:00
dev_err ( chip - > parent , " gpio write failed ret(%d) \n " , ret ) ;
2014-09-03 21:05:34 +04:00
}
static const struct syscon_gpio_data keystone_dsp_gpio = {
/* ARM Keystone 2 */
. compatible = NULL ,
. flags = GPIO_SYSCON_FEAT_OUT ,
. bit_count = 28 ,
. dat_bit_offset = 4 ,
. set = keystone_gpio_set ,
} ;
2014-03-11 21:55:14 +04:00
static const struct of_device_id syscon_gpio_ids [ ] = {
{
2016-06-04 10:10:00 +03:00
. compatible = " cirrus,ep7209-mctrl-gpio " ,
2014-03-11 21:55:14 +04:00
. data = & clps711x_mctrl_gpio ,
} ,
2014-09-03 21:05:34 +04:00
{
. compatible = " ti,keystone-dsp-gpio " ,
. data = & keystone_dsp_gpio ,
} ,
2018-07-31 08:59:19 +03:00
{
. compatible = " rockchip,rk3328-grf-gpio " ,
. data = & rockchip_rk3328_gpio_mute ,
} ,
2014-03-11 21:55:14 +04:00
{ }
} ;
MODULE_DEVICE_TABLE ( of , syscon_gpio_ids ) ;
static int syscon_gpio_probe ( struct platform_device * pdev )
{
struct device * dev = & pdev - > dev ;
struct syscon_gpio_priv * priv ;
2014-09-03 21:05:33 +04:00
struct device_node * np = dev - > of_node ;
int ret ;
2014-03-11 21:55:14 +04:00
priv = devm_kzalloc ( dev , sizeof ( * priv ) , GFP_KERNEL ) ;
if ( ! priv )
return - ENOMEM ;
2018-04-30 10:38:16 +03:00
priv - > data = of_device_get_match_data ( dev ) ;
2014-03-11 21:55:14 +04:00
2014-09-03 21:05:33 +04:00
if ( priv - > data - > compatible ) {
priv - > syscon = syscon_regmap_lookup_by_compatible (
priv - > data - > compatible ) ;
if ( IS_ERR ( priv - > syscon ) )
return PTR_ERR ( priv - > syscon ) ;
} else {
priv - > syscon =
syscon_regmap_lookup_by_phandle ( np , " gpio,syscon-dev " ) ;
2018-05-18 06:52:04 +03:00
if ( IS_ERR ( priv - > syscon ) & & np - > parent )
priv - > syscon = syscon_node_to_regmap ( np - > parent ) ;
2014-09-03 21:05:33 +04:00
if ( IS_ERR ( priv - > syscon ) )
return PTR_ERR ( priv - > syscon ) ;
ret = of_property_read_u32_index ( np , " gpio,syscon-dev " , 1 ,
& priv - > dreg_offset ) ;
if ( ret )
dev_err ( dev , " can't read the data register offset! \n " ) ;
priv - > dreg_offset < < = 3 ;
ret = of_property_read_u32_index ( np , " gpio,syscon-dev " , 2 ,
& priv - > dir_reg_offset ) ;
if ( ret )
2015-03-24 21:42:42 +03:00
dev_dbg ( dev , " can't read the dir register offset! \n " ) ;
2014-09-03 21:05:33 +04:00
priv - > dir_reg_offset < < = 3 ;
}
2014-03-11 21:55:14 +04:00
2015-11-04 11:56:26 +03:00
priv - > chip . parent = dev ;
2014-03-11 21:55:14 +04:00
priv - > chip . owner = THIS_MODULE ;
priv - > chip . label = dev_name ( dev ) ;
priv - > chip . base = - 1 ;
priv - > chip . ngpio = priv - > data - > bit_count ;
priv - > chip . get = syscon_gpio_get ;
if ( priv - > data - > flags & GPIO_SYSCON_FEAT_IN )
priv - > chip . direction_input = syscon_gpio_dir_in ;
if ( priv - > data - > flags & GPIO_SYSCON_FEAT_OUT ) {
2014-09-03 21:05:32 +04:00
priv - > chip . set = priv - > data - > set ? : syscon_gpio_set ;
2014-03-11 21:55:14 +04:00
priv - > chip . direction_output = syscon_gpio_dir_out ;
}
platform_set_drvdata ( pdev , priv ) ;
2016-02-22 15:13:28 +03:00
return devm_gpiochip_add_data ( & pdev - > dev , & priv - > chip , priv ) ;
2014-03-11 21:55:14 +04:00
}
static struct platform_driver syscon_gpio_driver = {
. driver = {
. name = " gpio-syscon " ,
. of_match_table = syscon_gpio_ids ,
} ,
. probe = syscon_gpio_probe ,
} ;
module_platform_driver ( syscon_gpio_driver ) ;
MODULE_AUTHOR ( " Alexander Shiyan <shc_work@mail.ru> " ) ;
MODULE_DESCRIPTION ( " SYSCON GPIO driver " ) ;
MODULE_LICENSE ( " GPL " ) ;