2019-05-27 08:55:21 +02:00
// SPDX-License-Identifier: GPL-2.0-only
2015-04-23 10:35:40 +02:00
/*
* Copyright ( c ) 2014 MediaTek Inc .
*/
# include <linux/mfd/syscon.h>
# include <linux/module.h>
# include <linux/of.h>
# include <linux/platform_device.h>
# include <linux/regmap.h>
# include <linux/slab.h>
2022-05-23 17:33:28 +08:00
# include "reset.h"
2015-04-23 10:35:40 +02:00
2022-05-23 17:33:33 +08:00
static inline struct mtk_clk_rst_data * to_mtk_clk_rst_data ( struct reset_controller_dev * rcdev )
{
return container_of ( rcdev , struct mtk_clk_rst_data , rcdev ) ;
}
2022-05-23 17:33:31 +08:00
static int mtk_reset_update ( struct reset_controller_dev * rcdev ,
unsigned long id , bool deassert )
2019-07-26 15:01:35 +08:00
{
2022-05-23 17:33:33 +08:00
struct mtk_clk_rst_data * data = to_mtk_clk_rst_data ( rcdev ) ;
2022-05-23 17:33:31 +08:00
unsigned int val = deassert ? 0 : ~ 0 ;
2019-07-26 15:01:35 +08:00
2022-05-23 17:33:30 +08:00
return regmap_update_bits ( data - > regmap ,
2022-05-23 17:33:34 +08:00
data - > desc - > rst_bank_ofs [ id / RST_NR_PER_BANK ] ,
BIT ( id % RST_NR_PER_BANK ) , val ) ;
2022-05-23 17:33:31 +08:00
}
static int mtk_reset_assert ( struct reset_controller_dev * rcdev ,
unsigned long id )
{
return mtk_reset_update ( rcdev , id , false ) ;
2019-07-26 15:01:35 +08:00
}
2022-05-23 17:33:30 +08:00
static int mtk_reset_deassert ( struct reset_controller_dev * rcdev ,
unsigned long id )
2019-07-26 15:01:35 +08:00
{
2022-05-23 17:33:31 +08:00
return mtk_reset_update ( rcdev , id , true ) ;
2019-07-26 15:01:35 +08:00
}
2022-05-23 17:33:30 +08:00
static int mtk_reset ( struct reset_controller_dev * rcdev , unsigned long id )
2015-04-23 10:35:40 +02:00
{
2022-05-23 17:33:30 +08:00
int ret ;
ret = mtk_reset_assert ( rcdev , id ) ;
if ( ret )
return ret ;
2015-04-23 10:35:40 +02:00
2022-05-23 17:33:30 +08:00
return mtk_reset_deassert ( rcdev , id ) ;
2015-04-23 10:35:40 +02:00
}
2022-05-23 17:33:31 +08:00
static int mtk_reset_update_set_clr ( struct reset_controller_dev * rcdev ,
unsigned long id , bool deassert )
2015-04-23 10:35:40 +02:00
{
2022-05-23 17:33:33 +08:00
struct mtk_clk_rst_data * data = to_mtk_clk_rst_data ( rcdev ) ;
2022-05-23 17:33:31 +08:00
unsigned int deassert_ofs = deassert ? 0x4 : 0 ;
2015-04-23 10:35:40 +02:00
2022-05-23 17:33:30 +08:00
return regmap_write ( data - > regmap ,
2022-05-23 17:33:34 +08:00
data - > desc - > rst_bank_ofs [ id / RST_NR_PER_BANK ] +
deassert_ofs ,
BIT ( id % RST_NR_PER_BANK ) ) ;
2015-04-23 10:35:40 +02:00
}
2022-05-23 17:33:31 +08:00
static int mtk_reset_assert_set_clr ( struct reset_controller_dev * rcdev ,
unsigned long id )
{
return mtk_reset_update_set_clr ( rcdev , id , false ) ;
}
2022-05-23 17:33:30 +08:00
static int mtk_reset_deassert_set_clr ( struct reset_controller_dev * rcdev ,
unsigned long id )
2015-04-23 10:35:40 +02:00
{
2022-05-23 17:33:31 +08:00
return mtk_reset_update_set_clr ( rcdev , id , true ) ;
2015-04-23 10:35:40 +02:00
}
2019-07-26 15:01:35 +08:00
static int mtk_reset_set_clr ( struct reset_controller_dev * rcdev ,
2022-05-23 17:33:30 +08:00
unsigned long id )
2019-07-26 15:01:35 +08:00
{
int ret ;
ret = mtk_reset_assert_set_clr ( rcdev , id ) ;
if ( ret )
return ret ;
return mtk_reset_deassert_set_clr ( rcdev , id ) ;
}
2016-02-25 10:45:06 +01:00
static const struct reset_control_ops mtk_reset_ops = {
2015-04-23 10:35:40 +02:00
. assert = mtk_reset_assert ,
. deassert = mtk_reset_deassert ,
. reset = mtk_reset ,
} ;
2019-07-26 15:01:35 +08:00
static const struct reset_control_ops mtk_reset_ops_set_clr = {
. assert = mtk_reset_assert_set_clr ,
. deassert = mtk_reset_deassert_set_clr ,
. reset = mtk_reset_set_clr ,
} ;
clk: mediatek: reset: Support inuput argument index mode
There is a large number of mediatek infra reset bits, but we do not use
all of them. In addition, the proper input argement of reset controller
soulde be index.
Therefore, to be compatible with previous drivers and usage, we add
description variables to store the ids which can mapping to index.
To use this mode, we need to put the id in rst_idx_map to map from
index to ids. For example, if we want to input index 1 (this index
is used to set bank 1 bit 14) for svs, we need to declare the reset
controller like this:
In drivers:
static u16 rst_ofs[] = {
0x120, 0x130, 0x140, 0x150, 0x730,
};
static u16 rst_idx_map[] = {
0 * 32 + 0,
1 * 32 + 14,
....
};
static const struct mtk_clk_rst_desc clk_rst_desc = {
.version = MTK_RST_SET_CLR,
.rst_bank_ofs = rst_ofs,
.rst_bank_nr = ARRAY_SIZE(rst_ofs),
.rst_idx_map = rst_idx_map,
.rst_idx_map_nr = ARRAY_SIZE(rst_idx_map),
};
In dts:
svs: {
...
resets = <&infra 1>;
...
};
Signed-off-by: Rex-BC Chen <rex-bc.chen@mediatek.com>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Reviewed-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
Tested-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
Link: https://lore.kernel.org/r/20220523093346.28493-9-rex-bc.chen@mediatek.com
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
2022-05-23 17:33:35 +08:00
static int reset_xlate ( struct reset_controller_dev * rcdev ,
const struct of_phandle_args * reset_spec )
{
struct mtk_clk_rst_data * data = to_mtk_clk_rst_data ( rcdev ) ;
if ( reset_spec - > args [ 0 ] > = rcdev - > nr_resets | |
reset_spec - > args [ 0 ] > = data - > desc - > rst_idx_map_nr )
return - EINVAL ;
return data - > desc - > rst_idx_map [ reset_spec - > args [ 0 ] ] ;
}
2022-05-23 17:33:36 +08:00
int mtk_register_reset_controller ( struct device_node * np ,
const struct mtk_clk_rst_desc * desc )
2015-04-23 10:35:40 +02:00
{
struct regmap * regmap ;
2022-05-23 17:33:32 +08:00
const struct reset_control_ops * rcops = NULL ;
2022-05-23 17:33:33 +08:00
struct mtk_clk_rst_data * data ;
int ret ;
if ( ! desc ) {
pr_err ( " mtk clock reset desc is NULL \n " ) ;
2022-05-23 17:33:36 +08:00
return - EINVAL ;
2022-05-23 17:33:33 +08:00
}
2022-05-23 17:33:32 +08:00
2022-05-23 17:33:33 +08:00
switch ( desc - > version ) {
2022-05-23 17:33:32 +08:00
case MTK_RST_SIMPLE :
rcops = & mtk_reset_ops ;
break ;
case MTK_RST_SET_CLR :
rcops = & mtk_reset_ops_set_clr ;
break ;
default :
2022-05-23 17:33:33 +08:00
pr_err ( " Unknown reset version %d \n " , desc - > version ) ;
2022-05-23 17:33:36 +08:00
return - EINVAL ;
2022-05-23 17:33:32 +08:00
}
2015-04-23 10:35:40 +02:00
2021-07-26 18:57:03 +08:00
regmap = device_node_to_regmap ( np ) ;
2015-04-23 10:35:40 +02:00
if ( IS_ERR ( regmap ) ) {
2022-02-08 20:40:04 +08:00
pr_err ( " Cannot find regmap for %pOF: %pe \n " , np , regmap ) ;
2022-05-23 17:33:36 +08:00
return - EINVAL ;
2015-04-23 10:35:40 +02:00
}
data = kzalloc ( sizeof ( * data ) , GFP_KERNEL ) ;
if ( ! data )
2022-05-23 17:33:36 +08:00
return - ENOMEM ;
2015-04-23 10:35:40 +02:00
2022-05-23 17:33:33 +08:00
data - > desc = desc ;
2015-04-23 10:35:40 +02:00
data - > regmap = regmap ;
data - > rcdev . owner = THIS_MODULE ;
2022-05-23 17:33:32 +08:00
data - > rcdev . ops = rcops ;
2015-04-23 10:35:40 +02:00
data - > rcdev . of_node = np ;
clk: mediatek: reset: Support inuput argument index mode
There is a large number of mediatek infra reset bits, but we do not use
all of them. In addition, the proper input argement of reset controller
soulde be index.
Therefore, to be compatible with previous drivers and usage, we add
description variables to store the ids which can mapping to index.
To use this mode, we need to put the id in rst_idx_map to map from
index to ids. For example, if we want to input index 1 (this index
is used to set bank 1 bit 14) for svs, we need to declare the reset
controller like this:
In drivers:
static u16 rst_ofs[] = {
0x120, 0x130, 0x140, 0x150, 0x730,
};
static u16 rst_idx_map[] = {
0 * 32 + 0,
1 * 32 + 14,
....
};
static const struct mtk_clk_rst_desc clk_rst_desc = {
.version = MTK_RST_SET_CLR,
.rst_bank_ofs = rst_ofs,
.rst_bank_nr = ARRAY_SIZE(rst_ofs),
.rst_idx_map = rst_idx_map,
.rst_idx_map_nr = ARRAY_SIZE(rst_idx_map),
};
In dts:
svs: {
...
resets = <&infra 1>;
...
};
Signed-off-by: Rex-BC Chen <rex-bc.chen@mediatek.com>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Reviewed-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
Tested-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
Link: https://lore.kernel.org/r/20220523093346.28493-9-rex-bc.chen@mediatek.com
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
2022-05-23 17:33:35 +08:00
if ( data - > desc - > rst_idx_map_nr > 0 ) {
data - > rcdev . of_reset_n_cells = 1 ;
data - > rcdev . nr_resets = desc - > rst_idx_map_nr ;
data - > rcdev . of_xlate = reset_xlate ;
} else {
data - > rcdev . nr_resets = desc - > rst_bank_nr * RST_NR_PER_BANK ;
}
2015-04-23 10:35:40 +02:00
ret = reset_controller_register ( & data - > rcdev ) ;
if ( ret ) {
pr_err ( " could not register reset controller: %d \n " , ret ) ;
kfree ( data ) ;
2022-05-23 17:33:36 +08:00
return ret ;
2015-04-23 10:35:40 +02:00
}
2022-05-23 17:33:36 +08:00
return 0 ;
2015-04-23 10:35:40 +02:00
}
2019-07-26 15:01:35 +08:00
2022-05-23 17:33:37 +08:00
int mtk_register_reset_controller_with_dev ( struct device * dev ,
const struct mtk_clk_rst_desc * desc )
{
struct device_node * np = dev - > of_node ;
struct regmap * regmap ;
const struct reset_control_ops * rcops = NULL ;
struct mtk_clk_rst_data * data ;
int ret ;
if ( ! desc ) {
dev_err ( dev , " mtk clock reset desc is NULL \n " ) ;
return - EINVAL ;
}
switch ( desc - > version ) {
case MTK_RST_SIMPLE :
rcops = & mtk_reset_ops ;
break ;
case MTK_RST_SET_CLR :
rcops = & mtk_reset_ops_set_clr ;
break ;
default :
dev_err ( dev , " Unknown reset version %d \n " , desc - > version ) ;
return - EINVAL ;
}
regmap = device_node_to_regmap ( np ) ;
if ( IS_ERR ( regmap ) ) {
dev_err ( dev , " Cannot find regmap %pe \n " , regmap ) ;
return - EINVAL ;
}
data = devm_kzalloc ( dev , sizeof ( * data ) , GFP_KERNEL ) ;
if ( ! data )
return - ENOMEM ;
data - > desc = desc ;
data - > regmap = regmap ;
data - > rcdev . owner = THIS_MODULE ;
data - > rcdev . ops = rcops ;
data - > rcdev . of_node = np ;
data - > rcdev . dev = dev ;
if ( data - > desc - > rst_idx_map_nr > 0 ) {
data - > rcdev . of_reset_n_cells = 1 ;
data - > rcdev . nr_resets = desc - > rst_idx_map_nr ;
data - > rcdev . of_xlate = reset_xlate ;
} else {
data - > rcdev . nr_resets = desc - > rst_bank_nr * RST_NR_PER_BANK ;
}
ret = devm_reset_controller_register ( dev , & data - > rcdev ) ;
if ( ret ) {
dev_err ( dev , " could not register reset controller: %d \n " , ret ) ;
return ret ;
}
return 0 ;
}
2022-09-21 11:14:53 +02:00
EXPORT_SYMBOL_GPL ( mtk_register_reset_controller_with_dev ) ;
2022-05-23 17:33:37 +08:00
2021-09-02 06:25:25 +08:00
MODULE_LICENSE ( " GPL " ) ;